github.com/openshift/source-to-image@v1.4.1-0.20240516041539-bf52fc02204e/pkg/build/strategies/sti/postexecutorstep_test.go (about) 1 package sti 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 "github.com/openshift/source-to-image/pkg/docker" 9 ) 10 11 func TestStorePreviousImageStep(t *testing.T) { 12 testCases := []struct { 13 imageIDError error 14 expectedPreviousImageID string 15 expectedPreviousImageTag string 16 }{ 17 { 18 imageIDError: nil, 19 expectedPreviousImageID: "12345", 20 expectedPreviousImageTag: "0.1", 21 }, 22 { 23 imageIDError: fmt.Errorf("fail"), 24 expectedPreviousImageID: "", 25 expectedPreviousImageTag: "0.1", 26 }, 27 } 28 29 for _, testCase := range testCases { 30 31 builder := newFakeBaseSTI() 32 builder.incremental = true 33 builder.config.RemovePreviousImage = true 34 builder.config.Tag = testCase.expectedPreviousImageTag 35 36 fakeDocker := builder.docker.(*docker.FakeDocker) 37 fakeDocker.GetImageIDResult = testCase.expectedPreviousImageID 38 fakeDocker.GetImageIDError = testCase.imageIDError 39 40 step := &storePreviousImageStep{builder: builder, docker: fakeDocker} 41 42 ctx := &postExecutorStepContext{} 43 44 if err := step.execute(ctx); err != nil { 45 t.Fatalf("should exit without error, but it returned %v", err) 46 } 47 48 if fakeDocker.GetImageIDImage != testCase.expectedPreviousImageTag { 49 t.Errorf("should invoke fakeDocker.GetImageID(%q) but invoked with %q", testCase.expectedPreviousImageTag, fakeDocker.GetImageIDImage) 50 } 51 52 if ctx.previousImageID != testCase.expectedPreviousImageID { 53 t.Errorf("should set previousImageID field to %q but it's %q", testCase.expectedPreviousImageID, ctx.previousImageID) 54 } 55 } 56 } 57 58 func TestRemovePreviousImageStep(t *testing.T) { 59 testCases := []struct { 60 removeImageError error 61 expectedPreviousImageID string 62 }{ 63 { 64 removeImageError: nil, 65 expectedPreviousImageID: "", 66 }, 67 { 68 removeImageError: nil, 69 expectedPreviousImageID: "12345", 70 }, 71 { 72 removeImageError: fmt.Errorf("fail"), 73 expectedPreviousImageID: "12345", 74 }, 75 } 76 77 for _, testCase := range testCases { 78 79 builder := newFakeBaseSTI() 80 builder.incremental = true 81 builder.config.RemovePreviousImage = true 82 83 fakeDocker := builder.docker.(*docker.FakeDocker) 84 fakeDocker.RemoveImageError = testCase.removeImageError 85 86 step := &removePreviousImageStep{builder: builder, docker: fakeDocker} 87 88 ctx := &postExecutorStepContext{previousImageID: testCase.expectedPreviousImageID} 89 90 if err := step.execute(ctx); err != nil { 91 t.Fatalf("should exit without error, but it returned %v", err) 92 } 93 94 if fakeDocker.RemoveImageName != testCase.expectedPreviousImageID { 95 t.Errorf("should invoke fakeDocker.RemoveImage(%q) but invoked with %q", testCase.expectedPreviousImageID, fakeDocker.RemoveImageName) 96 } 97 } 98 } 99 100 func TestCommitImageStep(t *testing.T) { 101 102 testCases := []struct { 103 embeddedScript bool 104 destination string 105 expectedImageCmd string 106 }{ 107 { 108 embeddedScript: false, 109 destination: "/path/to/location", 110 expectedImageCmd: "/path/to/location/scripts/run", 111 }, 112 { 113 embeddedScript: true, 114 destination: "image:///usr/bin/run.sh", 115 expectedImageCmd: "/usr/bin/run.sh", 116 }, 117 } 118 119 for _, testCase := range testCases { 120 121 expectedEnv := []string{"BUILD_LOGLEVEL"} 122 expectedContainerID := "container-yyyy" 123 expectedImageID := "image-xxx" 124 expectedImageTag := "v1" 125 expectedImageUser := "jboss" 126 expectedEntrypoint := []string{"test_entrypoint"} 127 128 displayName := "MyApp" 129 description := "My Application is awesome!" 130 131 baseImageLabels := make(map[string]string) 132 baseImageLabels["vendor"] = "CentOS" 133 134 configLabels := make(map[string]string) 135 configLabels["distribution-scope"] = "private" 136 137 expectedLabels := make(map[string]string) 138 expectedLabels["io.k8s.description"] = description 139 expectedLabels["io.k8s.display-name"] = displayName 140 expectedLabels["vendor"] = "CentOS" 141 expectedLabels["distribution-scope"] = "private" 142 143 builder := newFakeBaseSTI() 144 builder.config.DisplayName = displayName 145 builder.config.Description = description 146 builder.config.Tag = expectedImageTag 147 builder.config.Labels = configLabels 148 builder.env = expectedEnv 149 150 fakeDocker := builder.docker.(*docker.FakeDocker) 151 fakeDocker.CommitContainerResult = expectedImageID 152 fakeDocker.GetImageUserResult = expectedImageUser 153 fakeDocker.GetImageEntrypointResult = expectedEntrypoint 154 fakeDocker.Labels = baseImageLabels 155 156 ctx := &postExecutorStepContext{ 157 containerID: expectedContainerID, 158 } 159 160 if testCase.embeddedScript { 161 builder.scriptsURL = make(map[string]string) 162 builder.scriptsURL["run"] = testCase.destination 163 } else { 164 ctx.destination = testCase.destination 165 } 166 167 step := &commitImageStep{builder: builder, docker: fakeDocker} 168 169 if err := step.execute(ctx); err != nil { 170 t.Fatalf("should exit without error, but it returned %v", err) 171 } 172 173 if ctx.imageID != expectedImageID { 174 t.Errorf("should set ImageID field to %q but it's %q", expectedImageID, ctx.imageID) 175 } 176 177 commitOpts := fakeDocker.CommitContainerOpts 178 179 if len(commitOpts.Command) != 1 { 180 t.Errorf("should commit container with Command: %q, but committed with %q", testCase.expectedImageCmd, commitOpts.Command) 181 182 } else if commitOpts.Command[0] != testCase.expectedImageCmd { 183 t.Errorf("should commit container with Command: %q, but committed with %q", testCase.expectedImageCmd, commitOpts.Command[0]) 184 } 185 186 if !reflect.DeepEqual(commitOpts.Env, expectedEnv) { 187 t.Errorf("should commit container with Env: %v, but committed with %v", expectedEnv, commitOpts.Env) 188 } 189 190 if commitOpts.ContainerID != expectedContainerID { 191 t.Errorf("should commit container with ContainerID: %q, but committed with %q", expectedContainerID, commitOpts.ContainerID) 192 } 193 194 if commitOpts.Repository != expectedImageTag { 195 t.Errorf("should commit container with Repository: %q, but committed with %q", expectedImageTag, commitOpts.Repository) 196 } 197 198 if commitOpts.User != expectedImageUser { 199 t.Errorf("should commit container with User: %q, but committed with %q", expectedImageUser, commitOpts.User) 200 } 201 202 if !reflect.DeepEqual(commitOpts.Entrypoint, expectedEntrypoint) { 203 t.Errorf("should commit container with Entrypoint: %q, but committed with %q", expectedEntrypoint, commitOpts.Entrypoint) 204 } 205 206 if !reflect.DeepEqual(commitOpts.Labels, expectedLabels) { 207 t.Errorf("should commit container with Labels: %v, but committed with %v", expectedLabels, commitOpts.Labels) 208 } 209 } 210 } 211 212 func TestDownloadFilesFromBuilderImageStep(t *testing.T) { 213 // FIXME 214 } 215 216 func TestStartRuntimeImageAndUploadFilesStep(t *testing.T) { 217 // FIXME 218 } 219 220 func TestReportSuccessStep(t *testing.T) { 221 builder := newFakeBaseSTI() 222 step := &reportSuccessStep{builder: builder} 223 ctx := &postExecutorStepContext{imageID: "my-app"} 224 225 if err := step.execute(ctx); err != nil { 226 t.Fatalf("should exit without error, but it returned %v", err) 227 } 228 229 if builder.result.Success != true { 230 t.Errorf("should set Success field to 'true' but it's %v", builder.result.Success) 231 } 232 233 if builder.result.ImageID != ctx.imageID { 234 t.Errorf("should set ImageID field to %q but it's %q", ctx.imageID, builder.result.ImageID) 235 } 236 }