github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/internal/commands/config_run_image_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 TestConfigRunImageMirrors(t *testing.T) { 24 color.Disable(true) 25 defer color.Disable(false) 26 spec.Run(t, "ConfigRunImageMirrorsCommand", testConfigRunImageMirrorsCommand, spec.Random(), spec.Report(report.Terminal{})) 27 } 28 29 func testConfigRunImageMirrorsCommand(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 runImage = "test/image" 37 testMirror1 = "example.com/some/run1" 38 testMirror2 = "example.com/some/run2" 39 testCfg = config.Config{ 40 Experimental: true, 41 RunImages: []config.RunImage{{ 42 Image: runImage, 43 Mirrors: []string{testMirror1, testMirror2}, 44 }}, 45 } 46 expandedCfg = config.Config{ 47 Experimental: true, 48 RunImages: append(testCfg.RunImages, config.RunImage{ 49 Image: "new-image", 50 Mirrors: []string{"some-mirror1", "some-mirror2"}, 51 }), 52 } 53 ) 54 55 it.Before(func() { 56 var err error 57 logger = logging.NewLogWithWriters(&outBuf, &outBuf) 58 tempPackHome, err = os.MkdirTemp("", "pack-home") 59 h.AssertNil(t, err) 60 configPath = filepath.Join(tempPackHome, "config.toml") 61 62 cmd = commands.ConfigRunImagesMirrors(logger, testCfg, configPath) 63 cmd.SetOut(logging.GetWriterForLevel(logger, logging.InfoLevel)) 64 }) 65 66 it.After(func() { 67 h.AssertNil(t, os.RemoveAll(tempPackHome)) 68 }) 69 70 when("-h", func() { 71 it("prints available commands", func() { 72 cmd.SetArgs([]string{"-h"}) 73 h.AssertNil(t, cmd.Execute()) 74 output := outBuf.String() 75 h.AssertContains(t, output, "Usage:") 76 for _, command := range []string{"add", "remove", "list"} { 77 h.AssertContains(t, output, command) 78 } 79 }) 80 }) 81 82 when("no arguments", func() { 83 it("lists run image mirrors", func() { 84 cmd.SetArgs([]string{}) 85 h.AssertNil(t, cmd.Execute()) 86 output := outBuf.String() 87 h.AssertEq(t, strings.TrimSpace(output), `Run Image Mirrors: 88 'test/image': 89 example.com/some/run1 90 example.com/some/run2`) 91 }) 92 }) 93 94 when("add", func() { 95 when("no run image is specified", func() { 96 it("fails to run", func() { 97 cmd.SetArgs([]string{"add"}) 98 err := cmd.Execute() 99 h.AssertError(t, err, "accepts 1 arg") 100 }) 101 }) 102 103 when("config path doesn't exist", func() { 104 it("fails to run", func() { 105 fakePath := filepath.Join(tempPackHome, "not-exist.toml") 106 h.AssertNil(t, os.WriteFile(fakePath, []byte("something"), 0001)) 107 cmd = commands.ConfigRunImagesMirrors(logger, config.Config{}, fakePath) 108 cmd.SetArgs([]string{"add", runImage, "-m", testMirror1}) 109 110 err := cmd.Execute() 111 h.AssertError(t, err, "failed to write to") 112 }) 113 }) 114 115 when("mirrors are provided", func() { 116 it("adds them as mirrors to the config", func() { 117 cmd.SetArgs([]string{"add", runImage, "-m", testMirror1, "-m", testMirror2}) 118 h.AssertNil(t, cmd.Execute()) 119 cfg, err := config.Read(configPath) 120 h.AssertNil(t, err) 121 h.AssertEq(t, cfg, testCfg) 122 // This ensures that there are no dups 123 h.AssertEq(t, len(cfg.RunImages[0].Mirrors), 2) 124 }) 125 }) 126 127 when("no mirrors are provided", func() { 128 it("preserves old mirrors, and prints helpful message", func() { 129 cmd.SetArgs([]string{"add", runImage}) 130 h.AssertNil(t, cmd.Execute()) 131 h.AssertContains(t, outBuf.String(), "No run image mirrors were provided") 132 }) 133 }) 134 }) 135 136 when("remove", func() { 137 when("no run image is specified", func() { 138 it("fails to run", func() { 139 cmd.SetArgs([]string{"remove"}) 140 err := cmd.Execute() 141 h.AssertError(t, err, "accepts 1 arg") 142 }) 143 }) 144 145 when("run image provided isn't present", func() { 146 it("prints a clear message", func() { 147 fakeImage := "not-set-image" 148 cmd.SetArgs([]string{"remove", fakeImage}) 149 h.AssertNil(t, cmd.Execute()) 150 output := outBuf.String() 151 h.AssertContains(t, output, fmt.Sprintf("No run image mirrors have been set for %s", style.Symbol(fakeImage))) 152 }) 153 }) 154 155 when("config path doesn't exist", func() { 156 it("fails to run", func() { 157 fakePath := filepath.Join(tempPackHome, "not-exist.toml") 158 h.AssertNil(t, os.WriteFile(fakePath, []byte("something"), 0001)) 159 cmd = commands.ConfigRunImagesMirrors(logger, testCfg, fakePath) 160 cmd.SetArgs([]string{"remove", runImage, "-m", testMirror1}) 161 162 err := cmd.Execute() 163 h.AssertError(t, err, "failed to write to") 164 }) 165 }) 166 167 when("mirrors are provided", func() { 168 it("removes them for the given run image", func() { 169 cmd.SetArgs([]string{"remove", runImage, "-m", testMirror2}) 170 h.AssertNil(t, cmd.Execute()) 171 cfg, err := config.Read(configPath) 172 h.AssertNil(t, err) 173 h.AssertEq(t, cfg.RunImages, []config.RunImage{{ 174 Image: runImage, 175 Mirrors: []string{testMirror1}, 176 }}) 177 }) 178 179 it("removes the image if all mirrors are removed", func() { 180 cmd.SetArgs([]string{"remove", runImage, "-m", testMirror1, "-m", testMirror2}) 181 h.AssertNil(t, cmd.Execute()) 182 cfg, err := config.Read(configPath) 183 h.AssertNil(t, err) 184 h.AssertEq(t, cfg.RunImages, []config.RunImage{}) 185 }) 186 }) 187 188 when("no mirrors are provided", func() { 189 it("removes all mirrors for the given run image", func() { 190 cmd.SetArgs([]string{"remove", runImage}) 191 h.AssertNil(t, cmd.Execute()) 192 193 cfg, err := config.Read(configPath) 194 h.AssertNil(t, err) 195 h.AssertEq(t, cfg.Experimental, testCfg.Experimental) 196 h.AssertEq(t, cfg.RunImages, []config.RunImage{}) 197 }) 198 199 it("preserves all mirrors aside from the given run image", func() { 200 cmd = commands.ConfigRunImagesMirrors(logger, expandedCfg, configPath) 201 cmd.SetArgs([]string{"remove", runImage}) 202 h.AssertNil(t, cmd.Execute()) 203 204 cfg, err := config.Read(configPath) 205 h.AssertNil(t, err) 206 h.AssertEq(t, cfg.Experimental, testCfg.Experimental) 207 h.AssertNotEq(t, cfg.RunImages, []config.RunImage{}) 208 h.AssertEq(t, cfg.RunImages, []config.RunImage{expandedCfg.RunImages[1]}) 209 }) 210 }) 211 }) 212 213 when("list", func() { 214 when("mirrors were previously set", func() { 215 it("lists run image mirrors", func() { 216 cmd.SetArgs([]string{"list"}) 217 h.AssertNil(t, cmd.Execute()) 218 output := outBuf.String() 219 h.AssertContains(t, output, runImage) 220 h.AssertContains(t, output, testMirror1) 221 h.AssertContains(t, output, testMirror2) 222 }) 223 }) 224 225 when("no run image mirrors were set", func() { 226 it("prints a clear message", func() { 227 cmd = commands.ConfigRunImagesMirrors(logger, config.Config{}, configPath) 228 cmd.SetArgs([]string{"list"}) 229 h.AssertNil(t, cmd.Execute()) 230 output := outBuf.String() 231 h.AssertNotContains(t, output, runImage) 232 h.AssertNotContains(t, output, testMirror1) 233 234 h.AssertContains(t, output, "No run image mirrors have been set") 235 }) 236 }) 237 238 when("run image provided", func() { 239 when("mirrors are set", func() { 240 it("returns image mirrors", func() { 241 cmd = commands.ConfigRunImagesMirrors(logger, expandedCfg, configPath) 242 cmd.SetArgs([]string{"list", "new-image"}) 243 h.AssertNil(t, cmd.Execute()) 244 output := outBuf.String() 245 h.AssertNotContains(t, output, runImage) 246 h.AssertNotContains(t, output, testMirror1) 247 h.AssertContains(t, output, "new-image") 248 h.AssertContains(t, output, "some-mirror1") 249 h.AssertContains(t, output, "some-mirror2") 250 }) 251 }) 252 253 when("mirrors aren't set", func() { 254 it("prints a clear message", func() { 255 fakeImage := "not-set-image" 256 cmd.SetArgs([]string{"list", fakeImage}) 257 h.AssertNil(t, cmd.Execute()) 258 output := outBuf.String() 259 h.AssertNotContains(t, output, runImage) 260 h.AssertNotContains(t, output, testMirror1) 261 h.AssertContains(t, output, fmt.Sprintf("No run image mirrors have been set for %s", style.Symbol(fakeImage))) 262 }) 263 }) 264 }) 265 }) 266 }