github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/kubernetes/utils_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package kubernetes 5 6 import ( 7 "errors" 8 "testing" 9 10 "github.com/SAP/jenkins-library/pkg/mock" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestRunUtils(t *testing.T) { 15 t.Run("Get container info", func(t *testing.T) { 16 testTable := []struct { 17 chartYamlFile string 18 dataChartYaml string 19 expectedChartName string 20 expectedPackageVersion string 21 expectedError error 22 setFileReadError bool 23 }{ 24 { 25 chartYamlFile: "path/to/Chart.yaml", 26 dataChartYaml: "name: nginx-testChart\nversion: 1.3.5", 27 expectedChartName: "nginx-testChart", 28 expectedPackageVersion: "1.3.5", 29 expectedError: nil, 30 setFileReadError: false, 31 }, 32 { 33 chartYamlFile: "path/to/Chart.yaml", 34 dataChartYaml: "name: nginx-testChart\nversion: 1.3.5", 35 expectedChartName: "nginx-testChart", 36 expectedPackageVersion: "1.3.5", 37 expectedError: errors.New("file couldn't read"), 38 setFileReadError: true, 39 }, 40 { 41 chartYamlFile: "path/to/Chart.yaml", 42 dataChartYaml: "version: 1.3.5", 43 expectedChartName: "nginx-testChart", 44 expectedPackageVersion: "1.3.5", 45 expectedError: errors.New("name not found in chart yaml file (or wrong type)"), 46 setFileReadError: false, 47 }, 48 { 49 chartYamlFile: "path/to/Chart.yaml", 50 dataChartYaml: "name: nginx-testChart", 51 expectedChartName: "nginx-testChart", 52 expectedPackageVersion: "1.3.5", 53 expectedError: errors.New("version not found in chart yaml file (or wrong type)"), 54 setFileReadError: false, 55 }, 56 { 57 chartYamlFile: "path/to/Chart.yaml", 58 dataChartYaml: "name=nginx-testChart", 59 expectedChartName: "nginx-testChart", 60 expectedPackageVersion: "1.3.5", 61 expectedError: errors.New("failed unmarshal"), 62 setFileReadError: false, 63 }, 64 } 65 66 for _, testCase := range testTable { 67 utils := helmMockUtilsBundle{ 68 ExecMockRunner: &mock.ExecMockRunner{}, 69 FilesMock: &mock.FilesMock{}, 70 HttpClientMock: &mock.HttpClientMock{ 71 FileUploads: map[string]string{}, 72 }, 73 } 74 utils.AddFile(testCase.chartYamlFile, []byte(testCase.dataChartYaml)) 75 if testCase.setFileReadError { 76 utils.FileReadErrors = map[string]error{testCase.chartYamlFile: testCase.expectedError} 77 } 78 nameChart, packageVersion, err := GetChartInfo(testCase.chartYamlFile, utils) 79 if testCase.expectedError != nil { 80 assert.Error(t, err) 81 assert.Contains(t, err.Error(), testCase.expectedError.Error()) 82 } else { 83 assert.NoError(t, err) 84 assert.Equal(t, testCase.expectedChartName, nameChart) 85 assert.Equal(t, testCase.expectedPackageVersion, packageVersion) 86 } 87 88 } 89 }) 90 }