/*
Copyright 2021 The KubeVela Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
	"os"
	"testing"
	"time"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var (
	testdir      = "testdir"
	testTimeout  = 2 * time.Second
	testInterval = 1 * time.Second
)

func TestGinkgo(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "test main")
}

var _ = Describe("test waitSecretVolume", func() {
	BeforeEach(func() {
		err := os.MkdirAll(testdir, 0755)
		Expect(err).NotTo(HaveOccurred())
	})
	AfterEach(func() {
		os.RemoveAll(testdir)
	})

	When("dir not exist or empty", func() {
		It("return timeout error", func() {
			err := waitWebhookSecretVolume(testdir, testTimeout, testInterval)
			Expect(err).To(HaveOccurred())
			By("remove dir")
			os.RemoveAll(testdir)
			err = waitWebhookSecretVolume(testdir, testTimeout, testInterval)
			Expect(err).To(HaveOccurred())
		})
	})

	When("dir contains empty file", func() {
		It("return timeout error", func() {
			By("add empty file")
			_, err := os.Create(testdir + "/emptyFile")
			Expect(err).NotTo(HaveOccurred())
			err = waitWebhookSecretVolume(testdir, testTimeout, testInterval)
			Expect(err).To(HaveOccurred())
		})
	})

	When("files in dir are not empty", func() {
		It("return nil", func() {
			By("add non-empty file")
			_, err := os.Create(testdir + "/file")
			Expect(err).NotTo(HaveOccurred())
			err = os.WriteFile(testdir+"/file", []byte("test"), os.ModeAppend)
			Expect(err).NotTo(HaveOccurred())
			err = waitWebhookSecretVolume(testdir, testTimeout, testInterval)
			Expect(err).NotTo(HaveOccurred())
		})
	})

})
