1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/kubernetes/utils_test.go

91 lines
2.8 KiB
Go
Raw Normal View History

//go:build unit
// +build unit
package kubernetes
import (
"errors"
"testing"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
)
func TestRunUtils(t *testing.T) {
t.Run("Get container info", func(t *testing.T) {
testTable := []struct {
chartYamlFile string
dataChartYaml string
expectedChartName string
expectedPackageVersion string
expectedError error
setFileReadError bool
}{
{
chartYamlFile: "path/to/Chart.yaml",
dataChartYaml: "name: nginx-testChart\nversion: 1.3.5",
expectedChartName: "nginx-testChart",
expectedPackageVersion: "1.3.5",
expectedError: nil,
setFileReadError: false,
},
{
chartYamlFile: "path/to/Chart.yaml",
dataChartYaml: "name: nginx-testChart\nversion: 1.3.5",
expectedChartName: "nginx-testChart",
expectedPackageVersion: "1.3.5",
expectedError: errors.New("file couldn't read"),
setFileReadError: true,
},
{
chartYamlFile: "path/to/Chart.yaml",
dataChartYaml: "version: 1.3.5",
expectedChartName: "nginx-testChart",
expectedPackageVersion: "1.3.5",
expectedError: errors.New("name not found in chart yaml file (or wrong type)"),
setFileReadError: false,
},
{
chartYamlFile: "path/to/Chart.yaml",
dataChartYaml: "name: nginx-testChart",
expectedChartName: "nginx-testChart",
expectedPackageVersion: "1.3.5",
expectedError: errors.New("version not found in chart yaml file (or wrong type)"),
setFileReadError: false,
},
{
chartYamlFile: "path/to/Chart.yaml",
dataChartYaml: "name=nginx-testChart",
expectedChartName: "nginx-testChart",
expectedPackageVersion: "1.3.5",
expectedError: errors.New("failed unmarshal"),
setFileReadError: false,
},
}
for _, testCase := range testTable {
utils := helmMockUtilsBundle{
ExecMockRunner: &mock.ExecMockRunner{},
FilesMock: &mock.FilesMock{},
HttpClientMock: &mock.HttpClientMock{
FileUploads: map[string]string{},
},
}
utils.AddFile(testCase.chartYamlFile, []byte(testCase.dataChartYaml))
if testCase.setFileReadError {
utils.FileReadErrors = map[string]error{testCase.chartYamlFile: testCase.expectedError}
}
nameChart, packageVersion, err := GetChartInfo(testCase.chartYamlFile, utils)
if testCase.expectedError != nil {
assert.Error(t, err)
assert.Contains(t, err.Error(), testCase.expectedError.Error())
} else {
assert.NoError(t, err)
assert.Equal(t, testCase.expectedChartName, nameChart)
assert.Equal(t, testCase.expectedPackageVersion, packageVersion)
}
}
})
}