github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/internal/commands/remove_registry_test.go (about)

     1  package commands_test
     2  
     3  import (
     4  	"bytes"
     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/buildpacks/pack/internal/commands"
    14  	"github.com/buildpacks/pack/internal/config"
    15  	"github.com/buildpacks/pack/pkg/logging"
    16  	h "github.com/buildpacks/pack/testhelpers"
    17  )
    18  
    19  func TestRemoveRegistry(t *testing.T) {
    20  	color.Disable(true)
    21  	defer color.Disable(false)
    22  
    23  	spec.Run(t, "RemoveRegistryCommand", testRemoveRegistryCommand, spec.Parallel(), spec.Report(report.Terminal{}))
    24  }
    25  
    26  func testRemoveRegistryCommand(t *testing.T, when spec.G, it spec.S) {
    27  	when("#RemoveRegistry", func() {
    28  		var (
    29  			outBuf     bytes.Buffer
    30  			logger     = logging.NewLogWithWriters(&outBuf, &outBuf)
    31  			tmpDir     string
    32  			configFile string
    33  			cfg        config.Config
    34  			assert     = h.NewAssertionManager(t)
    35  		)
    36  
    37  		it.Before(func() {
    38  			var err error
    39  			tmpDir, err = os.MkdirTemp("", "pack-home-*")
    40  			assert.Nil(err)
    41  
    42  			cfg = config.Config{
    43  				DefaultRegistryName: "buildpack-registry",
    44  				Registries: []config.Registry{
    45  					{
    46  						Name: "buildpack-registry",
    47  						URL:  "https://github.com/buildpacks/registry-index",
    48  						Type: "github",
    49  					},
    50  					{
    51  						Name: "elbandito-registry",
    52  						URL:  "https://github.com/elbandito/registry-index",
    53  						Type: "github",
    54  					},
    55  				},
    56  			}
    57  
    58  			configFile = filepath.Join(tmpDir, "config.toml")
    59  			err = config.Write(cfg, configFile)
    60  			assert.Nil(err)
    61  		})
    62  
    63  		it.After(func() {
    64  			_ = os.RemoveAll(tmpDir)
    65  		})
    66  
    67  		it("should remove the registry", func() {
    68  			command := commands.RemoveRegistry(logger, cfg, configFile)
    69  			command.SetArgs([]string{"elbandito-registry"})
    70  			assert.Succeeds(command.Execute())
    71  
    72  			newCfg, err := config.Read(configFile)
    73  			assert.Nil(err)
    74  
    75  			assert.Equal(newCfg, config.Config{
    76  				DefaultRegistryName: "buildpack-registry",
    77  				Registries: []config.Registry{
    78  					{
    79  						Name: "buildpack-registry",
    80  						URL:  "https://github.com/buildpacks/registry-index",
    81  						Type: "github",
    82  					},
    83  				},
    84  			})
    85  			assert.Contains(outBuf.String(), "been deprecated, please use 'pack config registries remove' instead")
    86  		})
    87  
    88  		it("should remove the registry and matching default registry name", func() {
    89  			command := commands.RemoveRegistry(logger, cfg, configFile)
    90  			command.SetArgs([]string{"buildpack-registry"})
    91  			assert.Succeeds(command.Execute())
    92  
    93  			newCfg, err := config.Read(configFile)
    94  			assert.Nil(err)
    95  
    96  			assert.Equal(newCfg, config.Config{
    97  				DefaultRegistryName: config.OfficialRegistryName,
    98  				Registries: []config.Registry{
    99  					{
   100  						Name: "elbandito-registry",
   101  						URL:  "https://github.com/elbandito/registry-index",
   102  						Type: "github",
   103  					},
   104  				},
   105  			})
   106  		})
   107  
   108  		it("should return error when registry does NOT already exist", func() {
   109  			command := commands.RemoveRegistry(logger, cfg, configFile)
   110  			command.SetArgs([]string{"missing-registry"})
   111  			assert.Error(command.Execute())
   112  
   113  			output := outBuf.String()
   114  			h.AssertContains(t, output, "registry 'missing-registry' does not exist")
   115  		})
   116  
   117  		it("should throw error when registry name is official", func() {
   118  			command := commands.RemoveRegistry(logger, config.Config{}, configFile)
   119  			command.SetArgs([]string{"official"})
   120  			assert.Error(command.Execute())
   121  
   122  			output := outBuf.String()
   123  			h.AssertContains(t, output, "'official' is a reserved registry name, please provide a different registry")
   124  		})
   125  	})
   126  }