gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/shells/abstract_test.go (about) 1 package shells 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/mock" 8 "github.com/stretchr/testify/require" 9 10 "gitlab.com/gitlab-org/gitlab-runner/common" 11 "gitlab.com/gitlab-org/gitlab-runner/helpers/featureflags" 12 "gitlab.com/gitlab-org/gitlab-runner/helpers/tls" 13 ) 14 15 func TestWriteGitSSLConfig(t *testing.T) { 16 gitlabURL := "https://example.com:3443" 17 runnerURL := gitlabURL + "/ci/" 18 19 shell := AbstractShell{} 20 build := &common.Build{ 21 Runner: &common.RunnerConfig{ 22 RunnerCredentials: common.RunnerCredentials{ 23 URL: runnerURL, 24 }, 25 }, 26 JobResponse: common.JobResponse{ 27 TLSAuthCert: "TLS_CERT", 28 TLSAuthKey: "TLS_KEY", 29 TLSCAChain: "CA_CHAIN", 30 }, 31 } 32 33 mockWriter := new(MockShellWriter) 34 mockWriter.On("EnvVariableKey", tls.VariableCAFile).Return("VariableCAFile").Once() 35 mockWriter.On("EnvVariableKey", tls.VariableCertFile).Return("VariableCertFile").Once() 36 mockWriter.On("EnvVariableKey", tls.VariableKeyFile).Return("VariableKeyFile").Once() 37 38 mockWriter.On("Command", "git", "config", fmt.Sprintf("http.%s.%s", gitlabURL, "sslCAInfo"), "VariableCAFile").Once() 39 mockWriter.On("Command", "git", "config", fmt.Sprintf("http.%s.%s", gitlabURL, "sslCert"), "VariableCertFile").Once() 40 mockWriter.On("Command", "git", "config", fmt.Sprintf("http.%s.%s", gitlabURL, "sslKey"), "VariableKeyFile").Once() 41 42 shell.writeGitSSLConfig(mockWriter, build, nil) 43 44 mockWriter.AssertExpectations(t) 45 } 46 47 func getJobResponseWithMultipleArtifacts(t *testing.T) common.JobResponse { 48 return common.JobResponse{ 49 ID: 1000, 50 Token: "token", 51 Artifacts: common.Artifacts{ 52 common.Artifact{ 53 Paths: []string{"default"}, 54 }, 55 common.Artifact{ 56 Paths: []string{"on-success"}, 57 When: common.ArtifactWhenOnSuccess, 58 }, 59 common.Artifact{ 60 Paths: []string{"on-failure"}, 61 When: common.ArtifactWhenOnFailure, 62 }, 63 common.Artifact{ 64 Paths: []string{"always"}, 65 When: common.ArtifactWhenAlways, 66 }, 67 common.Artifact{ 68 Paths: []string{"zip-archive"}, 69 When: common.ArtifactWhenAlways, 70 Format: common.ArtifactFormatZip, 71 Type: "archive", 72 }, 73 common.Artifact{ 74 Paths: []string{"gzip-junit"}, 75 When: common.ArtifactWhenAlways, 76 Format: common.ArtifactFormatGzip, 77 Type: "junit", 78 }, 79 }, 80 } 81 } 82 83 func TestWriteWritingArtifactsOnSuccess(t *testing.T) { 84 gitlabURL := "https://example.com:3443" 85 86 shell := AbstractShell{} 87 build := &common.Build{ 88 JobResponse: getJobResponseWithMultipleArtifacts(t), 89 Runner: &common.RunnerConfig{ 90 RunnerCredentials: common.RunnerCredentials{ 91 URL: gitlabURL, 92 }, 93 }, 94 } 95 info := common.ShellScriptInfo{ 96 RunnerCommand: "gitlab-runner-helper", 97 Build: build, 98 } 99 100 mockWriter := new(MockShellWriter) 101 defer mockWriter.AssertExpectations(t) 102 mockWriter.On("Variable", mock.Anything) 103 mockWriter.On("Cd", mock.Anything) 104 mockWriter.On("IfCmd", "gitlab-runner-helper", "--version") 105 mockWriter.On("Notice", mock.Anything) 106 mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader", 107 "--url", gitlabURL, 108 "--token", "token", 109 "--id", "1000", 110 "--path", "default").Once() 111 mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader", 112 "--url", gitlabURL, 113 "--token", "token", 114 "--id", "1000", 115 "--path", "on-success").Once() 116 mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader", 117 "--url", gitlabURL, 118 "--token", "token", 119 "--id", "1000", 120 "--path", "always").Once() 121 mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader", 122 "--url", gitlabURL, 123 "--token", "token", 124 "--id", "1000", 125 "--path", "zip-archive", 126 "--artifact-format", "zip", 127 "--artifact-type", "archive").Once() 128 mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader", 129 "--url", gitlabURL, 130 "--token", "token", 131 "--id", "1000", 132 "--path", "gzip-junit", 133 "--artifact-format", "gzip", 134 "--artifact-type", "junit").Once() 135 mockWriter.On("Else") 136 mockWriter.On("Warning", mock.Anything, mock.Anything, mock.Anything) 137 mockWriter.On("EndIf") 138 139 err := shell.writeScript(mockWriter, common.BuildStageUploadOnSuccessArtifacts, info) 140 require.NoError(t, err) 141 } 142 143 func TestWriteWritingArtifactsOnFailure(t *testing.T) { 144 gitlabURL := "https://example.com:3443" 145 146 shell := AbstractShell{} 147 build := &common.Build{ 148 JobResponse: getJobResponseWithMultipleArtifacts(t), 149 Runner: &common.RunnerConfig{ 150 RunnerCredentials: common.RunnerCredentials{ 151 URL: gitlabURL, 152 }, 153 }, 154 } 155 info := common.ShellScriptInfo{ 156 RunnerCommand: "gitlab-runner-helper", 157 Build: build, 158 } 159 160 mockWriter := new(MockShellWriter) 161 defer mockWriter.AssertExpectations(t) 162 mockWriter.On("Variable", mock.Anything) 163 mockWriter.On("Cd", mock.Anything) 164 mockWriter.On("IfCmd", "gitlab-runner-helper", "--version") 165 mockWriter.On("Notice", mock.Anything) 166 mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader", 167 "--url", gitlabURL, 168 "--token", "token", 169 "--id", "1000", 170 "--path", "on-failure").Once() 171 mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader", 172 "--url", gitlabURL, 173 "--token", "token", 174 "--id", "1000", 175 "--path", "always").Once() 176 mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader", 177 "--url", gitlabURL, 178 "--token", "token", 179 "--id", "1000", 180 "--path", "zip-archive", 181 "--artifact-format", "zip", 182 "--artifact-type", "archive").Once() 183 mockWriter.On("Command", "gitlab-runner-helper", "artifacts-uploader", 184 "--url", gitlabURL, 185 "--token", "token", 186 "--id", "1000", 187 "--path", "gzip-junit", 188 "--artifact-format", "gzip", 189 "--artifact-type", "junit").Once() 190 mockWriter.On("Else") 191 mockWriter.On("Warning", mock.Anything, mock.Anything, mock.Anything) 192 mockWriter.On("EndIf") 193 194 err := shell.writeScript(mockWriter, common.BuildStageUploadOnFailureArtifacts, info) 195 require.NoError(t, err) 196 } 197 198 func TestGitCleanFlags(t *testing.T) { 199 tests := map[string]struct { 200 value string 201 legacyCleanStrategy string 202 203 expectedGitClean bool 204 expectedGitCleanFlags []interface{} 205 }{ 206 "empty clean flags": { 207 value: "", 208 legacyCleanStrategy: "false", 209 expectedGitClean: true, 210 expectedGitCleanFlags: []interface{}{"-ffdx"}, 211 }, 212 "use custom flags": { 213 value: "custom-flags", 214 legacyCleanStrategy: "false", 215 expectedGitClean: true, 216 expectedGitCleanFlags: []interface{}{"custom-flags"}, 217 }, 218 "use custom flags with multiple arguments": { 219 value: "-ffdx -e cache/", 220 legacyCleanStrategy: "false", 221 expectedGitClean: true, 222 expectedGitCleanFlags: []interface{}{"-ffdx", "-e", "cache/"}, 223 }, 224 "uses legacy strategy": { 225 value: "custom-flags", 226 legacyCleanStrategy: "true", 227 expectedGitClean: false, 228 }, 229 "disabled": { 230 value: "none", 231 legacyCleanStrategy: "false", 232 expectedGitClean: false, 233 }, 234 } 235 236 for name, test := range tests { 237 t.Run(name, func(t *testing.T) { 238 shell := AbstractShell{} 239 240 const dummySha = "01234567abcdef" 241 const dummyRef = "master" 242 243 build := &common.Build{ 244 Runner: &common.RunnerConfig{}, 245 JobResponse: common.JobResponse{ 246 GitInfo: common.GitInfo{Sha: dummySha, Ref: dummyRef}, 247 Variables: common.JobVariables{ 248 {Key: "GIT_CLEAN_FLAGS", Value: test.value}, 249 {Key: featureflags.UseLegacyGitCleanStrategy, Value: test.legacyCleanStrategy}, 250 }, 251 }, 252 } 253 254 mockWriter := new(MockShellWriter) 255 defer mockWriter.AssertExpectations(t) 256 257 mockWriter.On("Notice", "Checking out %s as %s...", dummySha[0:8], dummyRef).Once() 258 mockWriter.On("Command", "git", "checkout", "-f", "-q", dummySha).Once() 259 260 if test.expectedGitClean { 261 command := []interface{}{"git", "clean"} 262 command = append(command, test.expectedGitCleanFlags...) 263 mockWriter.On("Command", command...).Once() 264 } 265 266 shell.writeCheckoutCmd(mockWriter, build) 267 }) 268 } 269 }