You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	* Add helm dependency command * Change name of flag for package command Co-authored-by: “Vitalii <“vitalii.sidorov@sap.com”> Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
		
			
				
	
	
		
			330 lines
		
	
	
		
			7.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			330 lines
		
	
	
		
			7.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package cmd
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"testing"
 | |
| 
 | |
| 	"github.com/SAP/jenkins-library/pkg/kubernetes/mocks"
 | |
| 	"github.com/pkg/errors"
 | |
| 	"github.com/stretchr/testify/assert"
 | |
| )
 | |
| 
 | |
| func TestRunHelmUpgrade(t *testing.T) {
 | |
| 	t.Parallel()
 | |
| 
 | |
| 	testTable := []struct {
 | |
| 		config         helmExecuteOptions
 | |
| 		methodError    error
 | |
| 		expectedErrStr string
 | |
| 	}{
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "upgrade",
 | |
| 			},
 | |
| 			methodError: nil,
 | |
| 		},
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "upgrade",
 | |
| 			},
 | |
| 			methodError:    errors.New("some error"),
 | |
| 			expectedErrStr: "failed to execute upgrade: some error",
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	for i, testCase := range testTable {
 | |
| 		t.Run(fmt.Sprint("case ", i), func(t *testing.T) {
 | |
| 			helmExecute := &mocks.HelmExecutor{}
 | |
| 			helmExecute.On("RunHelmUpgrade").Return(testCase.methodError)
 | |
| 
 | |
| 			err := runHelmExecute(testCase.config, helmExecute)
 | |
| 			if err != nil {
 | |
| 				assert.Equal(t, testCase.expectedErrStr, err.Error())
 | |
| 			}
 | |
| 		})
 | |
| 
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func TestRunHelmLint(t *testing.T) {
 | |
| 	t.Parallel()
 | |
| 
 | |
| 	testTable := []struct {
 | |
| 		config         helmExecuteOptions
 | |
| 		expectedConfig []string
 | |
| 		methodError    error
 | |
| 		expectedErrStr string
 | |
| 	}{
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "lint",
 | |
| 			},
 | |
| 			methodError: nil,
 | |
| 		},
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "lint",
 | |
| 			},
 | |
| 			methodError:    errors.New("some error"),
 | |
| 			expectedErrStr: "failed to execute helm lint: some error",
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	for i, testCase := range testTable {
 | |
| 		t.Run(fmt.Sprint("case ", i), func(t *testing.T) {
 | |
| 			helmExecute := &mocks.HelmExecutor{}
 | |
| 			helmExecute.On("RunHelmLint").Return(testCase.methodError)
 | |
| 
 | |
| 			err := runHelmExecute(testCase.config, helmExecute)
 | |
| 			if err != nil {
 | |
| 				assert.Equal(t, testCase.expectedErrStr, err.Error())
 | |
| 			}
 | |
| 		})
 | |
| 
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func TestRunHelmInstall(t *testing.T) {
 | |
| 	t.Parallel()
 | |
| 
 | |
| 	testTable := []struct {
 | |
| 		config         helmExecuteOptions
 | |
| 		expectedConfig []string
 | |
| 		methodError    error
 | |
| 		expectedErrStr string
 | |
| 	}{
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "install",
 | |
| 			},
 | |
| 			methodError: nil,
 | |
| 		},
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "install",
 | |
| 			},
 | |
| 			methodError:    errors.New("some error"),
 | |
| 			expectedErrStr: "failed to execute helm install: some error",
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	for i, testCase := range testTable {
 | |
| 		t.Run(fmt.Sprint("case ", i), func(t *testing.T) {
 | |
| 			helmExecute := &mocks.HelmExecutor{}
 | |
| 			helmExecute.On("RunHelmInstall").Return(testCase.methodError)
 | |
| 
 | |
| 			err := runHelmExecute(testCase.config, helmExecute)
 | |
| 			if err != nil {
 | |
| 				assert.Equal(t, testCase.expectedErrStr, err.Error())
 | |
| 			}
 | |
| 		})
 | |
| 
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func TestRunHelmTest(t *testing.T) {
 | |
| 	t.Parallel()
 | |
| 
 | |
| 	testTable := []struct {
 | |
| 		config         helmExecuteOptions
 | |
| 		methodError    error
 | |
| 		expectedErrStr string
 | |
| 	}{
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "test",
 | |
| 			},
 | |
| 			methodError: nil,
 | |
| 		},
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "test",
 | |
| 			},
 | |
| 			methodError:    errors.New("some error"),
 | |
| 			expectedErrStr: "failed to execute helm test: some error",
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	for i, testCase := range testTable {
 | |
| 		t.Run(fmt.Sprint("case ", i), func(t *testing.T) {
 | |
| 			helmExecute := &mocks.HelmExecutor{}
 | |
| 			helmExecute.On("RunHelmTest").Return(testCase.methodError)
 | |
| 
 | |
| 			err := runHelmExecute(testCase.config, helmExecute)
 | |
| 			if err != nil {
 | |
| 				assert.Equal(t, testCase.expectedErrStr, err.Error())
 | |
| 			}
 | |
| 		})
 | |
| 
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func TestRunHelmUninstall(t *testing.T) {
 | |
| 	t.Parallel()
 | |
| 
 | |
| 	testTable := []struct {
 | |
| 		config         helmExecuteOptions
 | |
| 		methodError    error
 | |
| 		expectedErrStr string
 | |
| 	}{
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "uninstall",
 | |
| 			},
 | |
| 			methodError: nil,
 | |
| 		},
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "uninstall",
 | |
| 			},
 | |
| 			methodError:    errors.New("some error"),
 | |
| 			expectedErrStr: "failed to execute helm uninstall: some error",
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	for i, testCase := range testTable {
 | |
| 		t.Run(fmt.Sprint("case ", i), func(t *testing.T) {
 | |
| 			helmExecute := &mocks.HelmExecutor{}
 | |
| 			helmExecute.On("RunHelmUninstall").Return(testCase.methodError)
 | |
| 
 | |
| 			err := runHelmExecute(testCase.config, helmExecute)
 | |
| 			if err != nil {
 | |
| 				assert.Equal(t, testCase.expectedErrStr, err.Error())
 | |
| 			}
 | |
| 		})
 | |
| 
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func TestRunHelmDependency(t *testing.T) {
 | |
| 	t.Parallel()
 | |
| 
 | |
| 	testTable := []struct {
 | |
| 		config         helmExecuteOptions
 | |
| 		methodError    error
 | |
| 		expectedErrStr string
 | |
| 	}{
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "dependency",
 | |
| 			},
 | |
| 			methodError: nil,
 | |
| 		},
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "dependency",
 | |
| 			},
 | |
| 			methodError:    errors.New("some error"),
 | |
| 			expectedErrStr: "failed to execute helm dependency: some error",
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	for i, testCase := range testTable {
 | |
| 		t.Run(fmt.Sprint("case ", i), func(t *testing.T) {
 | |
| 			helmExecute := &mocks.HelmExecutor{}
 | |
| 			helmExecute.On("RunHelmDependency").Return(testCase.methodError)
 | |
| 
 | |
| 			err := runHelmExecute(testCase.config, helmExecute)
 | |
| 			if err != nil {
 | |
| 				assert.Equal(t, testCase.expectedErrStr, err.Error())
 | |
| 			}
 | |
| 		})
 | |
| 
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func TestRunHelmPush(t *testing.T) {
 | |
| 	t.Parallel()
 | |
| 
 | |
| 	testTable := []struct {
 | |
| 		config         helmExecuteOptions
 | |
| 		methodError    error
 | |
| 		expectedErrStr string
 | |
| 	}{
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "publish",
 | |
| 			},
 | |
| 			methodError: nil,
 | |
| 		},
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "publish",
 | |
| 			},
 | |
| 			methodError:    errors.New("some error"),
 | |
| 			expectedErrStr: "failed to execute helm publish: some error",
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	for i, testCase := range testTable {
 | |
| 		t.Run(fmt.Sprint("case ", i), func(t *testing.T) {
 | |
| 			helmExecute := &mocks.HelmExecutor{}
 | |
| 			helmExecute.On("RunHelmPublish").Return(testCase.methodError)
 | |
| 
 | |
| 			err := runHelmExecute(testCase.config, helmExecute)
 | |
| 			if err != nil {
 | |
| 				assert.Equal(t, testCase.expectedErrStr, err.Error())
 | |
| 			}
 | |
| 		})
 | |
| 
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func TestRunHelmDefaultCommand(t *testing.T) {
 | |
| 	t.Parallel()
 | |
| 
 | |
| 	testTable := []struct {
 | |
| 		config             helmExecuteOptions
 | |
| 		methodLintError    error
 | |
| 		methodPackageError error
 | |
| 		methodPublishError error
 | |
| 		expectedErrStr     string
 | |
| 	}{
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "",
 | |
| 			},
 | |
| 			methodLintError:    nil,
 | |
| 			methodPackageError: nil,
 | |
| 			methodPublishError: nil,
 | |
| 		},
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "",
 | |
| 			},
 | |
| 			methodLintError: errors.New("some error"),
 | |
| 			expectedErrStr:  "failed to execute helm lint: some error",
 | |
| 		},
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "",
 | |
| 			},
 | |
| 			methodPackageError: errors.New("some error"),
 | |
| 			expectedErrStr:     "failed to execute helm dependency: some error",
 | |
| 		},
 | |
| 		{
 | |
| 			config: helmExecuteOptions{
 | |
| 				HelmCommand: "",
 | |
| 			},
 | |
| 			methodPublishError: errors.New("some error"),
 | |
| 			expectedErrStr:     "failed to execute helm publish: some error",
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	for i, testCase := range testTable {
 | |
| 		t.Run(fmt.Sprint("case ", i), func(t *testing.T) {
 | |
| 			helmExecute := &mocks.HelmExecutor{}
 | |
| 			helmExecute.On("RunHelmLint").Return(testCase.methodLintError)
 | |
| 			helmExecute.On("RunHelmDependency").Return(testCase.methodPackageError)
 | |
| 			helmExecute.On("RunHelmPublish").Return(testCase.methodPublishError)
 | |
| 
 | |
| 			err := runHelmExecute(testCase.config, helmExecute)
 | |
| 			if err != nil {
 | |
| 				assert.Equal(t, testCase.expectedErrStr, err.Error())
 | |
| 			}
 | |
| 
 | |
| 		})
 | |
| 	}
 | |
| 
 | |
| }
 |