github.com/cloudfoundry-incubator/stembuild@v0.0.0-20211223202937-5b61d62226c6/package_stemcell/config/output_config_test.go (about) 1 package config_test 2 3 import ( 4 "os" 5 "path/filepath" 6 7 . "github.com/cloudfoundry-incubator/stembuild/package_stemcell/config" 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 ) 11 12 var _ = Describe("OutputConfig", func() { 13 Describe("os", func() { 14 Context("no os specified", func() { 15 It("should be invalid", func() { 16 valid := IsValidOS("") 17 Expect(valid).To(BeFalse()) 18 }) 19 }) 20 Context("a supported os is specified", func() { 21 It("2016 should be valid", func() { 22 valid := IsValidOS("2016") 23 Expect(valid).To(BeTrue()) 24 }) 25 It("2012R2 should be valid", func() { 26 valid := IsValidOS("2012R2") 27 Expect(valid).To(BeTrue()) 28 }) 29 It("1803 should be valid", func() { 30 valid := IsValidOS("1803") 31 Expect(valid).To(BeTrue()) 32 }) 33 It("2019 should be valid", func() { 34 valid := IsValidOS("2019") 35 Expect(valid).To(BeTrue()) 36 }) 37 38 }) 39 Context("something other than a supported os is specified", func() { 40 It("should be invalid", func() { 41 valid := IsValidOS("bad-thing") 42 Expect(valid).To(BeFalse()) 43 }) 44 It("1709 should be invalid", func() { 45 valid := IsValidOS("1709") 46 Expect(valid).To(BeFalse()) 47 }) 48 }) 49 50 }) 51 Describe("stemcell version", func() { 52 Context("no stemcell version specified", func() { 53 It("should be invalid", func() { 54 valid := IsValidStemcellVersion("") 55 Expect(valid).To(BeFalse()) 56 }) 57 }) 58 Context("stemcell version specified is valid pattern", func() { 59 It("should be valid", func() { 60 valids := []string{"4.4", "4.4-build.1", "4.4.4", "4.4.4-build.4"} 61 for _, version := range valids { 62 valid := IsValidStemcellVersion(version) 63 Expect(valid).To(BeTrue()) 64 } 65 }) 66 }) 67 Context("stemcell version specified is invalid pattern", func() { 68 It("should be invalid", func() { 69 valid := IsValidStemcellVersion("4.4.4.4") 70 Expect(valid).To(BeFalse()) 71 }) 72 }) 73 }) 74 75 Describe("validateOutputDir", func() { 76 Context("no dir given", func() { 77 It("should be an error", func() { 78 err := ValidateOrCreateOutputDir("") 79 Expect(err).To(HaveOccurred()) 80 }) 81 }) 82 83 Context("valid relative directory that does not exist", func() { 84 It("should create the directory and not fail", func() { 85 err := os.RemoveAll(filepath.Join(".", "tmp")) 86 Expect(err).ToNot(HaveOccurred()) 87 err = ValidateOrCreateOutputDir(filepath.Join(".", "tmp")) 88 Expect(err).ToNot(HaveOccurred()) 89 _, err = os.Stat(filepath.Join(filepath.Join(".", "tmp"))) 90 Expect(err).ToNot(HaveOccurred()) 91 }) 92 AfterEach(func() { 93 err := os.RemoveAll(filepath.Join(".", "tmp")) 94 Expect(err).ToNot(HaveOccurred()) 95 }) 96 }) 97 98 Context("valid directory that already exists", func() { 99 It("should not fail", func() { 100 err := os.Mkdir(filepath.Join(".", "tmp"), 0700) 101 Expect(err).ToNot(HaveOccurred()) 102 err = ValidateOrCreateOutputDir(filepath.Join(".", "tmp")) 103 Expect(err).ToNot(HaveOccurred()) 104 }) 105 AfterEach(func() { 106 err := os.RemoveAll(filepath.Join(".", "tmp")) 107 Expect(err).ToNot(HaveOccurred()) 108 }) 109 }) 110 111 Context("valid absolute directory", func() { 112 It("should not fail", func() { 113 cwd, err := os.Getwd() 114 Expect(err).ToNot(HaveOccurred()) 115 err = ValidateOrCreateOutputDir(cwd) 116 Expect(err).ToNot(HaveOccurred()) 117 }) 118 }) 119 120 Context("invalid directory because intermediate directories do not exist", func() { 121 It("should be an error", func() { 122 err := os.RemoveAll(filepath.Join(".", "tmp")) 123 Expect(err).ToNot(HaveOccurred()) 124 err = ValidateOrCreateOutputDir(filepath.Join(".", "tmp", "subtmp")) 125 Expect(err).To(HaveOccurred()) 126 }) 127 }) 128 }) 129 })