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