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

     1  package commands_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/heroku/color"
    12  	"github.com/sclevine/spec"
    13  	"github.com/sclevine/spec/report"
    14  	"github.com/spf13/cobra"
    15  
    16  	"github.com/buildpacks/pack/internal/commands"
    17  	"github.com/buildpacks/pack/internal/config"
    18  	"github.com/buildpacks/pack/internal/style"
    19  	"github.com/buildpacks/pack/pkg/logging"
    20  	h "github.com/buildpacks/pack/testhelpers"
    21  )
    22  
    23  func TestConfigRegistryMirrors(t *testing.T) {
    24  	color.Disable(true)
    25  	defer color.Disable(false)
    26  	spec.Run(t, "ConfigRunImageMirrorsCommand", testConfigRegistryMirrorsCommand, spec.Random(), spec.Report(report.Terminal{}))
    27  }
    28  
    29  func testConfigRegistryMirrorsCommand(t *testing.T, when spec.G, it spec.S) {
    30  	var (
    31  		cmd          *cobra.Command
    32  		logger       logging.Logger
    33  		outBuf       bytes.Buffer
    34  		tempPackHome string
    35  		configPath   string
    36  		registry1    = "index.docker.io"
    37  		registry2    = "us.gcr.io"
    38  		testMirror1  = "10.0.0.1"
    39  		testMirror2  = "10.0.0.2"
    40  		testCfg      = config.Config{
    41  			RegistryMirrors: map[string]string{
    42  				registry1: testMirror1,
    43  				registry2: testMirror2,
    44  			},
    45  		}
    46  	)
    47  
    48  	it.Before(func() {
    49  		var err error
    50  		logger = logging.NewLogWithWriters(&outBuf, &outBuf)
    51  		tempPackHome, err = os.MkdirTemp("", "pack-home")
    52  		h.AssertNil(t, err)
    53  		configPath = filepath.Join(tempPackHome, "config.toml")
    54  
    55  		cmd = commands.ConfigRegistryMirrors(logger, testCfg, configPath)
    56  		cmd.SetOut(logging.GetWriterForLevel(logger, logging.InfoLevel))
    57  	})
    58  
    59  	it.After(func() {
    60  		h.AssertNil(t, os.RemoveAll(tempPackHome))
    61  	})
    62  
    63  	when("-h", func() {
    64  		it("prints available commands", func() {
    65  			cmd.SetArgs([]string{"-h"})
    66  			h.AssertNil(t, cmd.Execute())
    67  			output := outBuf.String()
    68  			h.AssertContains(t, output, "Usage:")
    69  			for _, command := range []string{"add", "remove", "list"} {
    70  				h.AssertContains(t, output, command)
    71  			}
    72  		})
    73  	})
    74  
    75  	when("no arguments", func() {
    76  		it("lists registry mirrors", func() {
    77  			cmd.SetArgs([]string{})
    78  			h.AssertNil(t, cmd.Execute())
    79  			output := outBuf.String()
    80  			h.AssertContains(t, strings.TrimSpace(output), `Registry Mirrors:`)
    81  			h.AssertContains(t, strings.TrimSpace(output), `index.docker.io: '10.0.0.1'`)
    82  			h.AssertContains(t, strings.TrimSpace(output), `us.gcr.io: '10.0.0.2'`)
    83  		})
    84  	})
    85  
    86  	when("add", func() {
    87  		when("no registry is specified", func() {
    88  			it("fails to run", func() {
    89  				cmd.SetArgs([]string{"add"})
    90  				err := cmd.Execute()
    91  				h.AssertError(t, err, "accepts 1 arg")
    92  			})
    93  		})
    94  
    95  		when("config path doesn't exist", func() {
    96  			it("fails to run", func() {
    97  				fakePath := filepath.Join(tempPackHome, "not-exist.toml")
    98  				h.AssertNil(t, os.WriteFile(fakePath, []byte("something"), 0001))
    99  				cmd = commands.ConfigRegistryMirrors(logger, config.Config{}, fakePath)
   100  				cmd.SetArgs([]string{"add", registry1, "-m", testMirror1})
   101  
   102  				err := cmd.Execute()
   103  				h.AssertError(t, err, "failed to write to")
   104  			})
   105  		})
   106  
   107  		when("mirrors are provided", func() {
   108  			it("adds them as mirrors to the config", func() {
   109  				cmd.SetArgs([]string{"add", "asia.gcr.io", "-m", "10.0.0.3"})
   110  				h.AssertNil(t, cmd.Execute())
   111  				cfg, err := config.Read(configPath)
   112  				h.AssertNil(t, err)
   113  				h.AssertEq(t, cfg, config.Config{
   114  					RegistryMirrors: map[string]string{
   115  						registry1:     testMirror1,
   116  						registry2:     testMirror2,
   117  						"asia.gcr.io": "10.0.0.3",
   118  					},
   119  				})
   120  			})
   121  
   122  			it("replaces pre-existing mirrors in the config", func() {
   123  				cmd.SetArgs([]string{"add", registry1, "-m", "10.0.0.3"})
   124  				h.AssertNil(t, cmd.Execute())
   125  				cfg, err := config.Read(configPath)
   126  				h.AssertNil(t, err)
   127  				h.AssertEq(t, cfg, config.Config{
   128  					RegistryMirrors: map[string]string{
   129  						registry1: "10.0.0.3",
   130  						registry2: testMirror2,
   131  					},
   132  				})
   133  			})
   134  		})
   135  
   136  		when("no mirrors are provided", func() {
   137  			it("preserves old mirrors, and prints helpful message", func() {
   138  				cmd.SetArgs([]string{"add", registry1})
   139  				h.AssertNil(t, cmd.Execute())
   140  				h.AssertContains(t, outBuf.String(), "A registry mirror was not provided")
   141  			})
   142  		})
   143  	})
   144  
   145  	when("remove", func() {
   146  		when("no registry is specified", func() {
   147  			it("fails to run", func() {
   148  				cmd.SetArgs([]string{"remove"})
   149  				err := cmd.Execute()
   150  				h.AssertError(t, err, "accepts 1 arg")
   151  			})
   152  		})
   153  
   154  		when("registry provided isn't present", func() {
   155  			it("prints a clear message", func() {
   156  				fakeImage := "not-set-image"
   157  				cmd.SetArgs([]string{"remove", fakeImage})
   158  				h.AssertNil(t, cmd.Execute())
   159  				output := outBuf.String()
   160  				h.AssertContains(t, output, fmt.Sprintf("No registry mirror has been set for %s", style.Symbol(fakeImage)))
   161  			})
   162  		})
   163  
   164  		when("config path doesn't exist", func() {
   165  			it("fails to run", func() {
   166  				fakePath := filepath.Join(tempPackHome, "not-exist.toml")
   167  				h.AssertNil(t, os.WriteFile(fakePath, []byte("something"), 0001))
   168  				cmd = commands.ConfigRegistryMirrors(logger, testCfg, fakePath)
   169  				cmd.SetArgs([]string{"remove", registry1})
   170  
   171  				err := cmd.Execute()
   172  				h.AssertError(t, err, "failed to write to")
   173  			})
   174  		})
   175  
   176  		when("registry is provided", func() {
   177  			it("removes the given registry", func() {
   178  				cmd.SetArgs([]string{"remove", registry1})
   179  				h.AssertNil(t, cmd.Execute())
   180  				cfg, err := config.Read(configPath)
   181  				h.AssertNil(t, err)
   182  				h.AssertEq(t, cfg.RegistryMirrors, map[string]string{
   183  					registry2: testMirror2,
   184  				})
   185  			})
   186  		})
   187  	})
   188  
   189  	when("list", func() {
   190  		when("mirrors were previously set", func() {
   191  			it("lists registry mirrors", func() {
   192  				cmd.SetArgs([]string{"list"})
   193  				h.AssertNil(t, cmd.Execute())
   194  				output := outBuf.String()
   195  				h.AssertContains(t, output, registry1)
   196  				h.AssertContains(t, output, testMirror1)
   197  				h.AssertContains(t, output, registry2)
   198  				h.AssertContains(t, output, testMirror2)
   199  			})
   200  		})
   201  
   202  		when("no registry mirrors were set", func() {
   203  			it("prints a clear message", func() {
   204  				cmd = commands.ConfigRegistryMirrors(logger, config.Config{}, configPath)
   205  				cmd.SetArgs([]string{"list"})
   206  				h.AssertNil(t, cmd.Execute())
   207  				output := outBuf.String()
   208  				h.AssertNotContains(t, output, registry1)
   209  				h.AssertNotContains(t, output, testMirror1)
   210  				h.AssertNotContains(t, output, registry2)
   211  				h.AssertNotContains(t, output, testMirror2)
   212  
   213  				h.AssertContains(t, output, "No registry mirrors have been set")
   214  			})
   215  		})
   216  	})
   217  }