github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/internal/commands/rebase_test.go (about) 1 package commands_test 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/heroku/color" 8 9 "github.com/buildpacks/pack/pkg/client" 10 "github.com/buildpacks/pack/pkg/image" 11 12 "github.com/golang/mock/gomock" 13 "github.com/sclevine/spec" 14 "github.com/sclevine/spec/report" 15 "github.com/spf13/cobra" 16 17 "github.com/buildpacks/pack/internal/commands" 18 "github.com/buildpacks/pack/internal/commands/testmocks" 19 "github.com/buildpacks/pack/internal/config" 20 "github.com/buildpacks/pack/pkg/logging" 21 h "github.com/buildpacks/pack/testhelpers" 22 ) 23 24 func TestRebaseCommand(t *testing.T) { 25 color.Disable(true) 26 defer color.Disable(false) 27 28 spec.Run(t, "Commands", testRebaseCommand, spec.Parallel(), spec.Report(report.Terminal{})) 29 } 30 31 func testRebaseCommand(t *testing.T, when spec.G, it spec.S) { 32 var ( 33 command *cobra.Command 34 logger logging.Logger 35 outBuf bytes.Buffer 36 mockController *gomock.Controller 37 mockClient *testmocks.MockPackClient 38 cfg config.Config 39 ) 40 41 it.Before(func() { 42 logger = logging.NewLogWithWriters(&outBuf, &outBuf) 43 cfg = config.Config{} 44 mockController = gomock.NewController(t) 45 mockClient = testmocks.NewMockPackClient(mockController) 46 47 command = commands.Rebase(logger, cfg, mockClient) 48 }) 49 50 when("#RebaseCommand", func() { 51 when("no image is provided", func() { 52 it("fails to run", func() { 53 err := command.Execute() 54 h.AssertError(t, err, "accepts 1 arg") 55 }) 56 }) 57 58 when("image name is provided", func() { 59 var ( 60 repoName string 61 opts client.RebaseOptions 62 ) 63 it.Before(func() { 64 runImage := "test/image" 65 testMirror1 := "example.com/some/run1" 66 testMirror2 := "example.com/some/run2" 67 68 cfg.RunImages = []config.RunImage{{ 69 Image: runImage, 70 Mirrors: []string{testMirror1, testMirror2}, 71 }} 72 command = commands.Rebase(logger, cfg, mockClient) 73 74 repoName = "test/repo-image" 75 opts = client.RebaseOptions{ 76 RepoName: repoName, 77 Publish: false, 78 PullPolicy: image.PullAlways, 79 RunImage: "", 80 AdditionalMirrors: map[string][]string{ 81 runImage: {testMirror1, testMirror2}, 82 }, 83 } 84 }) 85 86 it("works", func() { 87 mockClient.EXPECT(). 88 Rebase(gomock.Any(), opts). 89 Return(nil) 90 91 command.SetArgs([]string{repoName}) 92 h.AssertNil(t, command.Execute()) 93 }) 94 95 when("--pull-policy never", func() { 96 it("works", func() { 97 opts.PullPolicy = image.PullNever 98 mockClient.EXPECT(). 99 Rebase(gomock.Any(), opts). 100 Return(nil) 101 102 command.SetArgs([]string{repoName, "--pull-policy", "never"}) 103 h.AssertNil(t, command.Execute()) 104 }) 105 it("takes precedence over config policy", func() { 106 opts.PullPolicy = image.PullNever 107 mockClient.EXPECT(). 108 Rebase(gomock.Any(), opts). 109 Return(nil) 110 111 cfg.PullPolicy = "if-not-present" 112 command = commands.Rebase(logger, cfg, mockClient) 113 114 command.SetArgs([]string{repoName, "--pull-policy", "never"}) 115 h.AssertNil(t, command.Execute()) 116 }) 117 }) 118 119 when("--pull-policy unknown-policy", func() { 120 it("fails to run", func() { 121 command.SetArgs([]string{repoName, "--pull-policy", "unknown-policy"}) 122 h.AssertError(t, command.Execute(), "parsing pull policy") 123 }) 124 }) 125 when("--pull-policy not set", func() { 126 when("no policy set in config", func() { 127 it("uses the default policy", func() { 128 opts.PullPolicy = image.PullAlways 129 mockClient.EXPECT(). 130 Rebase(gomock.Any(), opts). 131 Return(nil) 132 133 command.SetArgs([]string{repoName}) 134 h.AssertNil(t, command.Execute()) 135 }) 136 }) 137 when("policy is set in config", func() { 138 it("uses set policy", func() { 139 opts.PullPolicy = image.PullIfNotPresent 140 mockClient.EXPECT(). 141 Rebase(gomock.Any(), opts). 142 Return(nil) 143 144 cfg.PullPolicy = "if-not-present" 145 command = commands.Rebase(logger, cfg, mockClient) 146 147 command.SetArgs([]string{repoName}) 148 h.AssertNil(t, command.Execute()) 149 }) 150 }) 151 when("rebase is true", func() { 152 it("passes it through", func() { 153 opts.Force = true 154 mockClient.EXPECT().Rebase(gomock.Any(), opts).Return(nil) 155 command = commands.Rebase(logger, cfg, mockClient) 156 command.SetArgs([]string{repoName, "--force"}) 157 h.AssertNil(t, command.Execute()) 158 }) 159 }) 160 }) 161 when("image name and previous image are provided", func() { 162 var expectedOpts client.RebaseOptions 163 164 it.Before(func() { 165 runImage := "test/image" 166 testMirror1 := "example.com/some/run1" 167 testMirror2 := "example.com/some/run2" 168 169 cfg.RunImages = []config.RunImage{{ 170 Image: runImage, 171 Mirrors: []string{testMirror1, testMirror2}, 172 }} 173 command = commands.Rebase(logger, cfg, mockClient) 174 175 repoName = "test/repo-image" 176 previousImage := "example.com/previous-image:tag" // Example of previous image with tag 177 opts := client.RebaseOptions{ 178 RepoName: repoName, 179 Publish: false, 180 PullPolicy: image.PullAlways, 181 RunImage: "", 182 AdditionalMirrors: map[string][]string{ 183 runImage: {testMirror1, testMirror2}, 184 }, 185 PreviousImage: previousImage, 186 } 187 expectedOpts = opts 188 }) 189 190 it("works", func() { 191 mockClient.EXPECT(). 192 Rebase(gomock.Any(), gomock.Eq(expectedOpts)). 193 Return(nil) 194 195 command.SetArgs([]string{repoName, "--previous-image", "example.com/previous-image:tag"}) 196 h.AssertNil(t, command.Execute()) 197 }) 198 }) 199 }) 200 }) 201 }