github.com/oam-dev/kubevela@v1.9.11/cmd/core/app/server_test.go (about) 1 /* 2 Copyright 2021 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package app 18 19 import ( 20 "os" 21 "testing" 22 "time" 23 24 . "github.com/onsi/ginkgo/v2" 25 . "github.com/onsi/gomega" 26 ) 27 28 var ( 29 testdir = "testdir" 30 testTimeout = 2 * time.Second 31 testInterval = 1 * time.Second 32 ) 33 34 func TestGinkgo(t *testing.T) { 35 RegisterFailHandler(Fail) 36 RunSpecs(t, "test main") 37 } 38 39 var _ = Describe("test waitSecretVolume", func() { 40 BeforeEach(func() { 41 err := os.MkdirAll(testdir, 0755) 42 Expect(err).NotTo(HaveOccurred()) 43 }) 44 AfterEach(func() { 45 os.RemoveAll(testdir) 46 }) 47 48 When("dir not exist or empty", func() { 49 It("return timeout error", func() { 50 err := waitWebhookSecretVolume(testdir, testTimeout, testInterval) 51 Expect(err).To(HaveOccurred()) 52 By("remove dir") 53 os.RemoveAll(testdir) 54 err = waitWebhookSecretVolume(testdir, testTimeout, testInterval) 55 Expect(err).To(HaveOccurred()) 56 }) 57 }) 58 59 When("dir contains empty file", func() { 60 It("return timeout error", func() { 61 By("add empty file") 62 _, err := os.Create(testdir + "/emptyFile") 63 Expect(err).NotTo(HaveOccurred()) 64 err = waitWebhookSecretVolume(testdir, testTimeout, testInterval) 65 Expect(err).To(HaveOccurred()) 66 }) 67 }) 68 69 When("files in dir are not empty", func() { 70 It("return nil", func() { 71 By("add non-empty file") 72 _, err := os.Create(testdir + "/file") 73 Expect(err).NotTo(HaveOccurred()) 74 err = os.WriteFile(testdir+"/file", []byte("test"), os.ModeAppend) 75 Expect(err).NotTo(HaveOccurred()) 76 err = waitWebhookSecretVolume(testdir, testTimeout, testInterval) 77 Expect(err).NotTo(HaveOccurred()) 78 }) 79 }) 80 81 })