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