github.com/YousefHaggyHeroku/pack@v1.5.5/internal/config/config_test.go (about) 1 package config_test 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/heroku/color" 10 "github.com/sclevine/spec" 11 "github.com/sclevine/spec/report" 12 13 "github.com/YousefHaggyHeroku/pack/internal/config" 14 h "github.com/YousefHaggyHeroku/pack/testhelpers" 15 ) 16 17 func TestConfig(t *testing.T) { 18 color.Disable(true) 19 defer color.Disable(false) 20 spec.Run(t, "config", testConfig, spec.Parallel(), spec.Report(report.Terminal{})) 21 } 22 23 func testConfig(t *testing.T, when spec.G, it spec.S) { 24 var ( 25 tmpDir string 26 configPath string 27 ) 28 29 it.Before(func() { 30 var err error 31 tmpDir, err = ioutil.TempDir("", "pack.config.test.") 32 h.AssertNil(t, err) 33 configPath = filepath.Join(tmpDir, "config.toml") 34 }) 35 36 it.After(func() { 37 err := os.RemoveAll(tmpDir) 38 h.AssertNil(t, err) 39 }) 40 41 when("#Read", func() { 42 when("no config on disk", func() { 43 it("returns an empty config", func() { 44 subject, err := config.Read(configPath) 45 h.AssertNil(t, err) 46 h.AssertEq(t, subject.DefaultBuilder, "") 47 h.AssertEq(t, len(subject.RunImages), 0) 48 h.AssertEq(t, subject.Experimental, false) 49 }) 50 }) 51 }) 52 53 when("#Write", func() { 54 when("no config on disk", func() { 55 it("writes config to disk", func() { 56 h.AssertNil(t, config.Write(config.Config{ 57 DefaultBuilder: "some/builder", 58 RunImages: []config.RunImage{ 59 { 60 Image: "some/run", 61 Mirrors: []string{"example.com/some/run", "example.com/some/mirror"}, 62 }, 63 { 64 Image: "other/run", 65 Mirrors: []string{"example.com/other/run", "example.com/other/mirror"}, 66 }, 67 }, 68 TrustedBuilders: []config.TrustedBuilder{ 69 {Name: "some-trusted-builder"}, 70 }, 71 }, configPath)) 72 b, err := ioutil.ReadFile(configPath) 73 h.AssertNil(t, err) 74 h.AssertContains(t, string(b), `default-builder-image = "some/builder"`) 75 h.AssertContains(t, string(b), `[[run-images]] 76 image = "some/run" 77 mirrors = ["example.com/some/run", "example.com/some/mirror"]`) 78 79 h.AssertContains(t, string(b), `[[run-images]] 80 image = "other/run" 81 mirrors = ["example.com/other/run", "example.com/other/mirror"]`) 82 83 h.AssertContains(t, string(b), `[[trusted-builders]] 84 name = "some-trusted-builder"`) 85 }) 86 }) 87 88 when("config on disk", func() { 89 it.Before(func() { 90 h.AssertNil(t, ioutil.WriteFile(configPath, []byte("some-old-contents"), 0777)) 91 }) 92 93 it("replaces the file", func() { 94 h.AssertNil(t, config.Write(config.Config{ 95 DefaultBuilder: "some/builder", 96 }, configPath)) 97 b, err := ioutil.ReadFile(configPath) 98 h.AssertNil(t, err) 99 h.AssertContains(t, string(b), `default-builder-image = "some/builder"`) 100 h.AssertNotContains(t, string(b), "some-old-contents") 101 }) 102 }) 103 104 when("directories are missing", func() { 105 it("creates the directories", func() { 106 missingDirConfigPath := filepath.Join(tmpDir, "not", "yet", "created", "config.toml") 107 h.AssertNil(t, config.Write(config.Config{ 108 DefaultBuilder: "some/builder", 109 }, missingDirConfigPath)) 110 111 b, err := ioutil.ReadFile(missingDirConfigPath) 112 h.AssertNil(t, err) 113 h.AssertContains(t, string(b), `default-builder-image = "some/builder"`) 114 }) 115 }) 116 }) 117 118 when("#MkdirAll", func() { 119 when("the directory doesn't exist yet", func() { 120 it("creates the directory", func() { 121 path := filepath.Join(tmpDir, "a-new-dir") 122 err := config.MkdirAll(path) 123 h.AssertNil(t, err) 124 fi, err := os.Stat(path) 125 h.AssertNil(t, err) 126 h.AssertEq(t, fi.Mode().IsDir(), true) 127 }) 128 }) 129 130 when("the directory already exists", func() { 131 it("doesn't error", func() { 132 err := config.MkdirAll(tmpDir) 133 h.AssertNil(t, err) 134 fi, err := os.Stat(tmpDir) 135 h.AssertNil(t, err) 136 h.AssertEq(t, fi.Mode().IsDir(), true) 137 }) 138 }) 139 }) 140 141 when("#SetRunImageMirrors", func() { 142 when("run image exists in config", func() { 143 it("replaces the mirrors", func() { 144 cfg := config.SetRunImageMirrors( 145 config.Config{ 146 RunImages: []config.RunImage{ 147 { 148 Image: "some/run-image", 149 Mirrors: []string{"old/mirror", "other/mirror"}, 150 }, 151 }, 152 }, 153 "some/run-image", 154 []string{"some-other/run"}, 155 ) 156 157 h.AssertEq(t, len(cfg.RunImages), 1) 158 h.AssertEq(t, cfg.RunImages[0].Image, "some/run-image") 159 h.AssertSliceContainsOnly(t, cfg.RunImages[0].Mirrors, "some-other/run") 160 }) 161 }) 162 163 when("run image does not exist in config", func() { 164 it("adds the run image", func() { 165 cfg := config.SetRunImageMirrors( 166 config.Config{}, 167 "some/run-image", 168 []string{"some-other/run"}, 169 ) 170 171 h.AssertEq(t, len(cfg.RunImages), 1) 172 h.AssertEq(t, cfg.RunImages[0].Image, "some/run-image") 173 h.AssertSliceContainsOnly(t, cfg.RunImages[0].Mirrors, "some-other/run") 174 }) 175 }) 176 }) 177 178 when("#GetRegistry", func() { 179 it("should return a default registry", func() { 180 cfg := config.Config{} 181 182 registry, err := config.GetRegistry(cfg, "") 183 184 h.AssertNil(t, err) 185 h.AssertEq(t, registry, config.Registry{ 186 Name: "official", 187 Type: "github", 188 URL: "https://github.com/buildpacks/registry-index", 189 }) 190 }) 191 192 it("should return the corresponding registry", func() { 193 cfg := config.Config{ 194 Registries: []config.Registry{ 195 { 196 Name: "registry", 197 Type: "github", 198 URL: "https://github.com/registry/buildpack-registry", 199 }, 200 }, 201 } 202 203 registry, err := config.GetRegistry(cfg, "registry") 204 205 h.AssertNil(t, err) 206 h.AssertEq(t, registry, config.Registry{ 207 Name: "registry", 208 Type: "github", 209 URL: "https://github.com/registry/buildpack-registry", 210 }) 211 }) 212 213 it("should return the first matched registry", func() { 214 cfg := config.Config{ 215 Registries: []config.Registry{ 216 { 217 Name: "duplicate registry", 218 Type: "github", 219 URL: "https://github.com/duplicate1/buildpack-registry", 220 }, 221 { 222 Name: "duplicate registry", 223 Type: "github", 224 URL: "https://github.com/duplicate2/buildpack-registry", 225 }, 226 }, 227 } 228 229 registry, err := config.GetRegistry(cfg, "duplicate registry") 230 231 h.AssertNil(t, err) 232 h.AssertEq(t, registry, config.Registry{ 233 Name: "duplicate registry", 234 Type: "github", 235 URL: "https://github.com/duplicate1/buildpack-registry", 236 }) 237 }) 238 239 it("should return an error when mismatched", func() { 240 cfg := config.Config{} 241 _, err := config.GetRegistry(cfg, "missing") 242 h.AssertError(t, err, "registry 'missing' is not defined in your config file") 243 }) 244 }) 245 }