github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/internal/commands/config_registries_default_test.go (about) 1 package commands_test 2 3 import ( 4 "bytes" 5 "fmt" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/heroku/color" 11 "github.com/sclevine/spec" 12 "github.com/sclevine/spec/report" 13 14 "github.com/buildpacks/pack/internal/commands" 15 "github.com/buildpacks/pack/internal/config" 16 "github.com/buildpacks/pack/internal/style" 17 "github.com/buildpacks/pack/pkg/logging" 18 h "github.com/buildpacks/pack/testhelpers" 19 ) 20 21 func TestConfigRegistriesDefault(t *testing.T) { 22 color.Disable(true) 23 defer color.Disable(false) 24 25 spec.Run(t, "ConfigRegistriesDefaultCommand", testConfigRegistriesDefaultCommand, spec.Parallel(), spec.Report(report.Terminal{})) 26 } 27 28 func testConfigRegistriesDefaultCommand(t *testing.T, when spec.G, it spec.S) { 29 when("#ConfigRegistriesDefault", func() { 30 var ( 31 tmpDir string 32 configFile string 33 outBuf bytes.Buffer 34 logger = logging.NewLogWithWriters(&outBuf, &outBuf) 35 assert = h.NewAssertionManager(t) 36 ) 37 38 it.Before(func() { 39 var err error 40 tmpDir, err = os.MkdirTemp("", "pack-home-*") 41 assert.Nil(err) 42 43 configFile = filepath.Join(tmpDir, "config.toml") 44 }) 45 46 it.After(func() { 47 _ = os.RemoveAll(tmpDir) 48 }) 49 50 when("list", func() { 51 it("returns official if none is set", func() { 52 command := commands.ConfigRegistriesDefault(logger, config.Config{}, configFile) 53 command.SetArgs([]string{}) 54 assert.Succeeds(command.Execute()) 55 56 assert.Contains(outBuf.String(), "official") 57 }) 58 59 it("returns the default registry if one is set", func() { 60 command := commands.ConfigRegistriesDefault(logger, config.Config{DefaultRegistryName: "some-registry"}, configFile) 61 command.SetArgs([]string{}) 62 assert.Succeeds(command.Execute()) 63 64 assert.Contains(outBuf.String(), "some-registry") 65 }) 66 }) 67 68 when("set default", func() { 69 it("should set the default registry", func() { 70 cfg := config.Config{ 71 Registries: []config.Registry{ 72 { 73 Name: "myregistry", 74 URL: "https://github.com/buildpacks/registry-index", 75 Type: "github", 76 }, 77 }, 78 } 79 command := commands.ConfigRegistriesDefault(logger, cfg, configFile) 80 command.SetArgs([]string{"myregistry"}) 81 assert.Succeeds(command.Execute()) 82 83 cfg, err := config.Read(configFile) 84 assert.Nil(err) 85 86 assert.Equal(cfg.DefaultRegistryName, "myregistry") 87 }) 88 89 it("should fail if no corresponding registry exists", func() { 90 command := commands.ConfigRegistriesDefault(logger, config.Config{}, configFile) 91 command.SetArgs([]string{"myregistry"}) 92 assert.Error(command.Execute()) 93 94 output := outBuf.String() 95 assert.Contains(output, "no registry with the name 'myregistry' exists") 96 }) 97 98 it("returns clear error if fails to write", func() { 99 assert.Nil(os.WriteFile(configFile, []byte("something"), 0001)) 100 cfg := config.Config{ 101 Registries: []config.Registry{ 102 { 103 Name: "myregistry", 104 URL: "https://github.com/buildpacks/registry-index", 105 Type: "github", 106 }, 107 }, 108 } 109 command := commands.ConfigRegistriesDefault(logger, cfg, configFile) 110 command.SetArgs([]string{"myregistry"}) 111 assert.ErrorContains(command.Execute(), "writing config to") 112 }) 113 }) 114 115 when("--unset", func() { 116 it("should unset the default registry, if set", func() { 117 command := commands.ConfigRegistriesDefault(logger, config.Config{DefaultRegistryName: "some-registry"}, configFile) 118 command.SetArgs([]string{"--unset"}) 119 assert.Nil(command.Execute()) 120 121 cfg, err := config.Read(configFile) 122 assert.Nil(err) 123 assert.Equal(cfg.DefaultRegistryName, "") 124 assert.Contains(outBuf.String(), fmt.Sprintf("Successfully unset default registry %s", style.Symbol("some-registry"))) 125 }) 126 127 it("should return an error if official registry is being unset", func() { 128 command := commands.ConfigRegistriesDefault(logger, config.Config{DefaultRegistryName: config.OfficialRegistryName}, configFile) 129 command.SetArgs([]string{"--unset"}) 130 assert.ErrorContains(command.Execute(), fmt.Sprintf("Registry %s is a protected registry", style.Symbol(config.OfficialRegistryName))) 131 }) 132 133 it("should return a clear message if no registry is set", func() { 134 command := commands.ConfigRegistriesDefault(logger, config.Config{}, configFile) 135 command.SetArgs([]string{"--unset"}) 136 assert.ErrorContains(command.Execute(), fmt.Sprintf("Registry %s is a protected registry", style.Symbol(config.OfficialRegistryName))) 137 }) 138 139 it("returns clear error if fails to write", func() { 140 assert.Nil(os.WriteFile(configFile, []byte("something"), 0001)) 141 command := commands.ConfigRegistriesDefault(logger, config.Config{DefaultRegistryName: "some-registry"}, configFile) 142 command.SetArgs([]string{"--unset"}) 143 assert.ErrorContains(command.Execute(), "writing config to") 144 }) 145 }) 146 }) 147 }