1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/helmExecute_test.go
Vitalii Sidorov 93e3801945
feat: add step for Helm execution (#3419)
* Implement helm step

* Create kubernetes package

* Refactoring helm.go

* Add package, test commands

* Add test for helm package

* Add tests for helm.go

* Add tests for helm.go

* Add tests for utils.go

* Add tests for helmExecute.go

* small fix

* Add helm lint

* small fix

* small fix

* Fix according to comments

* Fix test

* small fix

* Add helm add function

* Changes according to new comments

* Add helm push

* Add unit tests

* Add tests for helmExecute

* Add small fix

* small fix

* small fix

* Move DeployUtilsBundle from kubernetesDeploy to kubernetes package

* small fix

* small fix

* Add unit-tests

* Fix

* Update resources/metadata/helmExecute.yaml

* Update resources/metadata/helmExecute.yaml

* Add helm chart server parameterization

* small fix

* small fix

Co-authored-by: “Vitalii <“vitalii.sidorov@sap.com”>
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2022-02-10 10:25:03 +01:00

272 lines
5.7 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.HelmCommand, 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.HelmCommand, 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.HelmCommand, 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.HelmCommand, 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.HelmCommand, helmExecute)
if err != nil {
assert.Equal(t, testCase.expectedErrStr, err.Error())
}
})
}
}
func TestRunHelmPackage(t *testing.T) {
t.Parallel()
testTable := []struct {
config helmExecuteOptions
methodError error
expectedErrStr string
}{
{
config: helmExecuteOptions{
HelmCommand: "package",
},
methodError: nil,
},
{
config: helmExecuteOptions{
HelmCommand: "package",
},
methodError: errors.New("some error"),
expectedErrStr: "failed to execute helm package: some error",
},
}
for i, testCase := range testTable {
t.Run(fmt.Sprint("case ", i), func(t *testing.T) {
helmExecute := &mocks.HelmExecutor{}
helmExecute.On("RunHelmPackage").Return(testCase.methodError)
err := runHelmExecute(testCase.config.HelmCommand, 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: "push",
},
methodError: nil,
},
{
config: helmExecuteOptions{
HelmCommand: "push",
},
methodError: errors.New("some error"),
expectedErrStr: "failed to execute helm push: some error",
},
}
for i, testCase := range testTable {
t.Run(fmt.Sprint("case ", i), func(t *testing.T) {
helmExecute := &mocks.HelmExecutor{}
helmExecute.On("RunHelmPush").Return(testCase.methodError)
err := runHelmExecute(testCase.config.HelmCommand, helmExecute)
if err != nil {
assert.Equal(t, testCase.expectedErrStr, err.Error())
}
})
}
}