2023-05-03 18:02:11 +02:00
|
|
|
//go:build unit
|
|
|
|
// +build unit
|
|
|
|
|
2022-02-10 11:25:03 +02:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2022-03-17 18:13:34 +02:00
|
|
|
"errors"
|
2022-02-10 11:25:03 +02:00
|
|
|
"testing"
|
|
|
|
|
2022-06-29 10:03:54 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
2022-02-10 11:25:03 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRunUtils(t *testing.T) {
|
|
|
|
t.Run("Get container info", func(t *testing.T) {
|
|
|
|
testTable := []struct {
|
2022-03-17 18:13:34 +02:00
|
|
|
chartYamlFile string
|
|
|
|
dataChartYaml string
|
|
|
|
expectedChartName string
|
|
|
|
expectedPackageVersion string
|
|
|
|
expectedError error
|
|
|
|
setFileReadError bool
|
2022-02-10 11:25:03 +02:00
|
|
|
}{
|
|
|
|
{
|
2022-03-17 18:13:34 +02:00
|
|
|
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,
|
2022-02-10 11:25:03 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testTable {
|
2022-06-29 10:03:54 +02:00
|
|
|
utils := helmMockUtilsBundle{
|
|
|
|
ExecMockRunner: &mock.ExecMockRunner{},
|
|
|
|
FilesMock: &mock.FilesMock{},
|
|
|
|
HttpClientMock: &mock.HttpClientMock{
|
|
|
|
FileUploads: map[string]string{},
|
|
|
|
},
|
|
|
|
}
|
2022-03-17 18:13:34 +02:00
|
|
|
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)
|
|
|
|
}
|
2022-02-10 11:25:03 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|