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