github.com/jaylevin/jenkins-library@v1.230.4/pkg/versioning/helm_test.go (about) 1 package versioning 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/ghodss/yaml" 8 "github.com/stretchr/testify/assert" 9 "helm.sh/helm/v3/pkg/chart" 10 ) 11 12 func TestHelmChartInit(t *testing.T) { 13 t.Run("success case", func(t *testing.T) { 14 chartMetadata := chart.Metadata{Version: "1.2.3"} 15 content, err := yaml.Marshal(chartMetadata) 16 assert.NoError(t, err) 17 18 fileUtils := newVersioningMockUtils() 19 fileUtils.AddFile("testchart/Chart.yaml", content) 20 21 helmChart := HelmChart{ 22 utils: fileUtils, 23 } 24 25 err = helmChart.init() 26 27 assert.NoError(t, err) 28 assert.Equal(t, "1.2.3", helmChart.metadata.Version) 29 }) 30 31 t.Run("success case - with chart path", func(t *testing.T) { 32 chartMetadata := chart.Metadata{Version: "1.2.3"} 33 content, err := yaml.Marshal(chartMetadata) 34 assert.NoError(t, err) 35 36 fileUtils := newVersioningMockUtils() 37 fileUtils.AddFile("chart1/Chart.yaml", []byte("")) 38 fileUtils.AddFile("chart2/Chart.yaml", content) 39 fileUtils.AddFile("chart3/Chart.yaml", []byte("")) 40 41 helmChart := HelmChart{ 42 path: "chart2/Chart.yaml", 43 utils: fileUtils, 44 } 45 46 err = helmChart.init() 47 48 assert.NoError(t, err) 49 assert.Equal(t, "1.2.3", helmChart.metadata.Version) 50 }) 51 52 t.Run("error case - init failed with missing utils", func(t *testing.T) { 53 helmChart := HelmChart{ 54 path: "chart2/Chart.yaml", 55 } 56 57 err := helmChart.init() 58 assert.EqualError(t, err, "no file utils provided") 59 }) 60 61 t.Run("error case - init failed with missing chart", func(t *testing.T) { 62 fileUtils := newVersioningMockUtils() 63 64 helmChart := HelmChart{ 65 utils: fileUtils, 66 } 67 68 err := helmChart.init() 69 assert.EqualError(t, err, "failed to find a helm chart file") 70 }) 71 72 t.Run("error case - failed reading file", func(t *testing.T) { 73 fileUtils := newVersioningMockUtils() 74 fileUtils.FileReadErrors = map[string]error{"testchart/Chart.yaml": fmt.Errorf("read error")} 75 76 helmChart := HelmChart{ 77 utils: fileUtils, 78 path: "testchart/Chart.yaml", 79 } 80 81 err := helmChart.init() 82 assert.EqualError(t, err, "failed to read file 'testchart/Chart.yaml': read error") 83 }) 84 85 t.Run("error case - chart invalid", func(t *testing.T) { 86 fileUtils := newVersioningMockUtils() 87 fileUtils.AddFile("testchart/Chart.yaml", []byte("{")) 88 89 helmChart := HelmChart{ 90 utils: fileUtils, 91 path: "testchart/Chart.yaml", 92 } 93 94 err := helmChart.init() 95 assert.Contains(t, fmt.Sprint(err), "helm chart content invalid 'testchart/Chart.yaml'") 96 }) 97 } 98 99 func TestHelmChartVersioningScheme(t *testing.T) { 100 helmChart := HelmChart{} 101 assert.Equal(t, "semver2", helmChart.VersioningScheme()) 102 } 103 104 func TestHelmChartGetVersion(t *testing.T) { 105 t.Run("success case", func(t *testing.T) { 106 chartMetadata := chart.Metadata{Version: "1.2.3"} 107 content, err := yaml.Marshal(chartMetadata) 108 assert.NoError(t, err) 109 110 fileUtils := newVersioningMockUtils() 111 fileUtils.AddFile("testchart/Chart.yaml", content) 112 113 helmChart := HelmChart{ 114 utils: fileUtils, 115 } 116 117 version, err := helmChart.GetVersion() 118 assert.NoError(t, err) 119 assert.Equal(t, "1.2.3", version) 120 }) 121 122 t.Run("error case - init failed", func(t *testing.T) { 123 fileUtils := newVersioningMockUtils() 124 125 helmChart := HelmChart{ 126 utils: fileUtils, 127 } 128 129 _, err := helmChart.GetVersion() 130 assert.Contains(t, fmt.Sprint(err), "failed to init helm chart versioning:") 131 }) 132 } 133 134 func TestHelmChartSetVersion(t *testing.T) { 135 t.Run("success case", func(t *testing.T) { 136 fileUtils := newVersioningMockUtils() 137 138 helmChart := HelmChart{ 139 utils: fileUtils, 140 path: "testchart/Chart.yaml", 141 metadata: chart.Metadata{Version: "1.2.3"}, 142 } 143 144 err := helmChart.SetVersion("1.2.4") 145 assert.NoError(t, err) 146 assert.Equal(t, "1.2.4", helmChart.metadata.Version) 147 148 fileContent, err := fileUtils.FileRead("testchart/Chart.yaml") 149 assert.NoError(t, err) 150 assert.Contains(t, string(fileContent), "version: 1.2.4") 151 }) 152 153 t.Run("success case - update app version", func(t *testing.T) { 154 fileUtils := newVersioningMockUtils() 155 156 helmChart := HelmChart{ 157 utils: fileUtils, 158 path: "testchart/Chart.yaml", 159 metadata: chart.Metadata{Version: "1.2.3"}, 160 updateAppVersion: true, 161 } 162 163 err := helmChart.SetVersion("1.2.4") 164 assert.NoError(t, err) 165 assert.Equal(t, "1.2.4", helmChart.metadata.AppVersion) 166 }) 167 168 t.Run("error case - init failed with missing chart", func(t *testing.T) { 169 fileUtils := newVersioningMockUtils() 170 171 helmChart := HelmChart{ 172 utils: fileUtils, 173 } 174 175 err := helmChart.SetVersion("1.2.4") 176 assert.Contains(t, fmt.Sprint(err), "failed to init helm chart versioning:") 177 }) 178 179 t.Run("error case - failed to write chart", func(t *testing.T) { 180 fileUtils := newVersioningMockUtils() 181 fileUtils.FileWriteError = fmt.Errorf("write error") 182 183 helmChart := HelmChart{ 184 path: "testchart/Chart.yaml", 185 utils: fileUtils, 186 metadata: chart.Metadata{Version: "1.2.3"}, 187 } 188 189 err := helmChart.SetVersion("1.2.4") 190 assert.EqualError(t, err, "failed to write file 'testchart/Chart.yaml': write error") 191 }) 192 } 193 194 func TestHelmChartGetCoordinates(t *testing.T) { 195 t.Run("success case", func(t *testing.T) { 196 fileUtils := newVersioningMockUtils() 197 helmChart := HelmChart{ 198 utils: fileUtils, 199 path: "testchart/Chart.yaml", 200 metadata: chart.Metadata{Version: "1.2.3", Name: "myChart", Home: "myHome"}, 201 } 202 203 coordinates, err := helmChart.GetCoordinates() 204 assert.NoError(t, err) 205 assert.Equal(t, Coordinates{GroupID: "myHome", ArtifactID: "myChart", Version: "1.2.3"}, coordinates) 206 }) 207 208 t.Run("error case - version retrieval failed", func(t *testing.T) { 209 fileUtils := newVersioningMockUtils() 210 211 helmChart := HelmChart{ 212 utils: fileUtils, 213 } 214 215 _, err := helmChart.GetCoordinates() 216 assert.Contains(t, fmt.Sprint(err), "failed to init helm chart versioning:") 217 }) 218 }