package cmd import ( "bytes" "fmt" "io" "io/ioutil" "net/http" "os" "testing" piperhttp "github.com/SAP/jenkins-library/pkg/http" "github.com/SAP/jenkins-library/pkg/mock" "github.com/SAP/jenkins-library/pkg/telemetry" "github.com/stretchr/testify/assert" ) type kanikoMockClient struct { httpMethod string httpStatusCode int urlsCalled []string requestBody io.Reader responseBody string errorMessage string } func (c *kanikoMockClient) SetOptions(opts piperhttp.ClientOptions) {} func (c *kanikoMockClient) SendRequest(method, url string, body io.Reader, header http.Header, cookies []*http.Cookie) (*http.Response, error) { c.httpMethod = method c.urlsCalled = append(c.urlsCalled, url) c.requestBody = body if len(c.errorMessage) > 0 { return nil, fmt.Errorf(c.errorMessage) } return &http.Response{StatusCode: c.httpStatusCode, Body: ioutil.NopCloser(bytes.NewReader([]byte(c.responseBody)))}, nil } type kanikoFileMock struct { *mock.FilesMock fileReadContent map[string]string fileReadErr map[string]error fileWriteContent map[string]string fileWriteErr map[string]error } func (f *kanikoFileMock) FileRead(path string) ([]byte, error) { if f.fileReadErr[path] != nil { return []byte{}, f.fileReadErr[path] } return []byte(f.fileReadContent[path]), nil } func (f *kanikoFileMock) FileWrite(path string, content []byte, perm os.FileMode) error { if f.fileWriteErr[path] != nil { return f.fileWriteErr[path] } f.fileWriteContent[path] = string(content) return nil } func TestRunKanikoExecute(t *testing.T) { commonPipelineEnvironment := kanikoExecuteCommonPipelineEnvironment{} t.Run("success case", func(t *testing.T) { config := &kanikoExecuteOptions{ BuildOptions: []string{"--skip-tls-verify-pull"}, ContainerImage: "myImage:tag", ContainerPreparationCommand: "rm -f /kaniko/.docker/config.json", CustomTLSCertificateLinks: []string{"https://test.url/cert.crt"}, DockerfilePath: "Dockerfile", DockerConfigJSON: "path/to/docker/config.json", } runner := &mock.ExecMockRunner{} certClient := &kanikoMockClient{ responseBody: "testCert", } fileUtils := &kanikoFileMock{ fileReadContent: map[string]string{"path/to/docker/config.json": `{"auths":{"custom":"test"}}`}, fileWriteContent: map[string]string{}, } err := runKanikoExecute(config, &telemetry.CustomData{}, &commonPipelineEnvironment, runner, certClient, fileUtils) assert.NoError(t, err) assert.Equal(t, "rm", runner.Calls[0].Exec) assert.Equal(t, []string{"-f", "/kaniko/.docker/config.json"}, runner.Calls[0].Params) assert.Equal(t, config.CustomTLSCertificateLinks, certClient.urlsCalled) assert.Equal(t, `{"auths":{"custom":"test"}}`, fileUtils.fileWriteContent["/kaniko/.docker/config.json"]) assert.Equal(t, "/kaniko/executor", runner.Calls[1].Exec) cwd, _ := os.Getwd() assert.Equal(t, []string{"--dockerfile", "Dockerfile", "--context", cwd, "--skip-tls-verify-pull", "--destination", "myImage:tag"}, runner.Calls[1].Params) }) t.Run("success case - image params", func(t *testing.T) { config := &kanikoExecuteOptions{ BuildOptions: []string{"--skip-tls-verify-pull"}, ContainerImageName: "myImage", ContainerImageTag: "1.2.3-a+x", ContainerRegistryURL: "https://my.registry.com:50000", ContainerPreparationCommand: "rm -f /kaniko/.docker/config.json", CustomTLSCertificateLinks: []string{"https://test.url/cert.crt"}, DockerfilePath: "Dockerfile", DockerConfigJSON: "path/to/docker/config.json", } runner := &mock.ExecMockRunner{} certClient := &kanikoMockClient{ responseBody: "testCert", } fileUtils := &kanikoFileMock{ fileReadContent: map[string]string{"path/to/docker/config.json": `{"auths":{"custom":"test"}}`}, fileWriteContent: map[string]string{}, } err := runKanikoExecute(config, &telemetry.CustomData{}, &commonPipelineEnvironment, runner, certClient, fileUtils) assert.NoError(t, err) assert.Equal(t, "rm", runner.Calls[0].Exec) assert.Equal(t, []string{"-f", "/kaniko/.docker/config.json"}, runner.Calls[0].Params) assert.Equal(t, config.CustomTLSCertificateLinks, certClient.urlsCalled) assert.Equal(t, `{"auths":{"custom":"test"}}`, fileUtils.fileWriteContent["/kaniko/.docker/config.json"]) assert.Equal(t, "/kaniko/executor", runner.Calls[1].Exec) cwd, _ := os.Getwd() assert.Equal(t, []string{"--dockerfile", "Dockerfile", "--context", cwd, "--skip-tls-verify-pull", "--destination", "my.registry.com:50000/myImage:1.2.3-a-x"}, runner.Calls[1].Params) }) t.Run("no error case - when cert update skipped", func(t *testing.T) { config := &kanikoExecuteOptions{ BuildOptions: []string{"--skip-tls-verify-pull"}, ContainerImageName: "myImage", ContainerImageTag: "1.2.3-a+x", ContainerRegistryURL: "https://my.registry.com:50000", ContainerPreparationCommand: "rm -f /kaniko/.docker/config.json", CustomTLSCertificateLinks: []string{}, DockerfilePath: "Dockerfile", DockerConfigJSON: "path/to/docker/config.json", } runner := &mock.ExecMockRunner{} certClient := &kanikoMockClient{} fileUtils := &kanikoFileMock{ fileWriteContent: map[string]string{}, fileReadErr: map[string]error{"/kaniko/ssl/certs/ca-certificates.crt": fmt.Errorf("read error")}, } err := runKanikoExecute(config, &telemetry.CustomData{}, &commonPipelineEnvironment, runner, certClient, fileUtils) assert.NoErrorf(t, err, "failed to update certificates: failed to load file '/kaniko/ssl/certs/ca-certificates.crt': read error") }) t.Run("success case - no push, no docker config.json", func(t *testing.T) { config := &kanikoExecuteOptions{ ContainerBuildOptions: "--skip-tls-verify-pull", ContainerPreparationCommand: "rm -f /kaniko/.docker/config.json", CustomTLSCertificateLinks: []string{"https://test.url/cert.crt"}, DockerfilePath: "Dockerfile", } runner := &mock.ExecMockRunner{} certClient := &kanikoMockClient{ responseBody: "testCert", } fileUtils := &kanikoFileMock{ fileWriteContent: map[string]string{}, } err := runKanikoExecute(config, &telemetry.CustomData{}, &commonPipelineEnvironment, runner, certClient, fileUtils) assert.NoError(t, err) assert.Equal(t, `{"auths":{}}`, fileUtils.fileWriteContent["/kaniko/.docker/config.json"]) cwd, _ := os.Getwd() assert.Equal(t, []string{"--dockerfile", "Dockerfile", "--context", cwd, "--skip-tls-verify-pull", "--no-push"}, runner.Calls[1].Params) }) t.Run("success case - backward compatibility", func(t *testing.T) { config := &kanikoExecuteOptions{ ContainerBuildOptions: "--skip-tls-verify-pull", ContainerImage: "myImage:tag", ContainerPreparationCommand: "rm -f /kaniko/.docker/config.json", CustomTLSCertificateLinks: []string{"https://test.url/cert.crt"}, DockerfilePath: "Dockerfile", DockerConfigJSON: "path/to/docker/config.json", } runner := &mock.ExecMockRunner{} certClient := &kanikoMockClient{ responseBody: "testCert", } fileUtils := &kanikoFileMock{ fileReadContent: map[string]string{"path/to/docker/config.json": `{"auths":{"custom":"test"}}`}, fileWriteContent: map[string]string{}, } err := runKanikoExecute(config, &telemetry.CustomData{}, &commonPipelineEnvironment, runner, certClient, fileUtils) assert.NoError(t, err) cwd, _ := os.Getwd() assert.Equal(t, []string{"--dockerfile", "Dockerfile", "--context", cwd, "--skip-tls-verify-pull", "--destination", "myImage:tag"}, runner.Calls[1].Params) }) t.Run("error case - Kaniko init failed", func(t *testing.T) { config := &kanikoExecuteOptions{ ContainerPreparationCommand: "rm -f /kaniko/.docker/config.json", } runner := &mock.ExecMockRunner{ ShouldFailOnCommand: map[string]error{"rm": fmt.Errorf("rm failed")}, } certClient := &kanikoMockClient{} fileUtils := &kanikoFileMock{} err := runKanikoExecute(config, &telemetry.CustomData{}, &commonPipelineEnvironment, runner, certClient, fileUtils) assert.EqualError(t, err, "failed to initialize Kaniko container: rm failed") }) t.Run("error case - Kaniko execution failed", func(t *testing.T) { config := &kanikoExecuteOptions{} runner := &mock.ExecMockRunner{ ShouldFailOnCommand: map[string]error{"/kaniko/executor": fmt.Errorf("kaniko run failed")}, } certClient := &kanikoMockClient{} fileUtils := &kanikoFileMock{ fileWriteContent: map[string]string{}, } err := runKanikoExecute(config, &telemetry.CustomData{}, &commonPipelineEnvironment, runner, certClient, fileUtils) assert.EqualError(t, err, "execution of '/kaniko/executor' failed: kaniko run failed") }) t.Run("error case - cert update failed", func(t *testing.T) { config := &kanikoExecuteOptions{ BuildOptions: []string{"--skip-tls-verify-pull"}, ContainerImageName: "myImage", ContainerImageTag: "1.2.3-a+x", ContainerRegistryURL: "https://my.registry.com:50000", ContainerPreparationCommand: "rm -f /kaniko/.docker/config.json", CustomTLSCertificateLinks: []string{"https://test.url/cert.crt"}, DockerfilePath: "Dockerfile", DockerConfigJSON: "path/to/docker/config.json", } runner := &mock.ExecMockRunner{} certClient := &kanikoMockClient{} fileUtils := &kanikoFileMock{ fileWriteContent: map[string]string{}, fileReadErr: map[string]error{"/kaniko/ssl/certs/ca-certificates.crt": fmt.Errorf("read error")}, } err := runKanikoExecute(config, &telemetry.CustomData{}, &commonPipelineEnvironment, runner, certClient, fileUtils) assert.EqualError(t, err, "failed to update certificates: failed to load file '/kaniko/ssl/certs/ca-certificates.crt': read error") }) t.Run("error case - dockerconfig read failed", func(t *testing.T) { config := &kanikoExecuteOptions{ DockerConfigJSON: "path/to/docker/config.json", } runner := &mock.ExecMockRunner{} certClient := &kanikoMockClient{} fileUtils := &kanikoFileMock{ fileWriteContent: map[string]string{}, fileReadErr: map[string]error{"path/to/docker/config.json": fmt.Errorf("read error")}, } err := runKanikoExecute(config, &telemetry.CustomData{}, &commonPipelineEnvironment, runner, certClient, fileUtils) assert.EqualError(t, err, "failed to read file 'path/to/docker/config.json': read error") }) t.Run("error case - dockerconfig write failed", func(t *testing.T) { config := &kanikoExecuteOptions{ DockerConfigJSON: "path/to/docker/config.json", } runner := &mock.ExecMockRunner{} certClient := &kanikoMockClient{} fileUtils := &kanikoFileMock{ fileWriteContent: map[string]string{}, fileWriteErr: map[string]error{"/kaniko/.docker/config.json": fmt.Errorf("write error")}, } err := runKanikoExecute(config, &telemetry.CustomData{}, &commonPipelineEnvironment, runner, certClient, fileUtils) assert.EqualError(t, err, "failed to write file '/kaniko/.docker/config.json': write error") }) }