@@ -8,10 +8,13 @@ import (
88 "encoding/json"
99 "path/filepath"
1010
11+ "github.com/go-logr/logr"
1112 . "github.com/onsi/ginkgo/v2"
1213 . "github.com/onsi/gomega"
1314 "github.com/spf13/afero"
15+ "sigs.k8s.io/controller-runtime/pkg/log/zap"
1416
17+ configv1alpha1 "github.com/gardener/gardener-landscape-kit/pkg/apis/config/v1alpha1"
1518 "github.com/gardener/gardener-landscape-kit/pkg/utils/componentvector"
1619 "github.com/gardener/gardener-landscape-kit/pkg/utils/version"
1720)
@@ -234,45 +237,115 @@ var _ = Describe("Version Metadata", func() {
234237 })
235238
236239 Describe ("#CheckGLKComponentVersion" , func () {
237- It ("should pass when versions match exactly" , func () {
238- // Get the current tool version
239- currentVersion := version .Get ().GitVersion
240+ var log logr.Logger
240241
241- baseYAML := []byte (`
242- components:
243- - name: github.com/gardener/gardener-landscape-kit
244- sourceRepository: https://github.com/gardener/gardener-landscape-kit
245- version: ` + currentVersion + `
246- ` )
247- cv , err := componentvector .NewWithOverride (baseYAML )
248- Expect (err ).NotTo (HaveOccurred ())
249-
250- err = version .CheckGLKComponentVersion (cv )
251- Expect (err ).NotTo (HaveOccurred ())
242+ BeforeEach (func () {
243+ log = zap .New (zap .WriteTo (GinkgoWriter ))
252244 })
253245
254- It ("should fail when tool version is different from component version" , func () {
255- currentVersion := version .Get ().GitVersion
256- differentVersion := "v0.99.99-test"
246+ type testCase struct {
247+ componentVersion string
248+ checkMode * configv1alpha1.VersionCheckMode
249+ expectError bool
250+ errorContains []string
251+ }
257252
258- baseYAML := []byte (`
253+ DescribeTable ("version checking behavior" ,
254+ func (tc testCase ) {
255+ baseYAML := []byte (`
259256components:
260257 - name: github.com/gardener/gardener-landscape-kit
261258 sourceRepository: https://github.com/gardener/gardener-landscape-kit
262- version: ` + differentVersion + `
259+ version: ` + tc . componentVersion + `
263260` )
264- cv , err := componentvector .NewWithOverride (baseYAML )
265- Expect (err ).NotTo (HaveOccurred ())
266-
267- err = version .CheckGLKComponentVersion (cv )
268- Expect (err ).To (MatchError (And (
269- ContainSubstring ("version mismatch" ),
270- ContainSubstring (currentVersion ),
271- ContainSubstring (differentVersion ),
272- )))
273- })
274-
275- It ("should fail when GLK component is not found" , func () {
261+ cv , err := componentvector .NewWithOverride (baseYAML )
262+ Expect (err ).NotTo (HaveOccurred ())
263+
264+ var config * configv1alpha1.LandscapeKitConfiguration
265+ if tc .checkMode != nil {
266+ config = & configv1alpha1.LandscapeKitConfiguration {
267+ VersionConfig : & configv1alpha1.VersionConfiguration {
268+ CheckMode : tc .checkMode ,
269+ },
270+ }
271+ }
272+
273+ err = version .CheckGLKComponentVersion (cv , config , log )
274+
275+ if tc .expectError {
276+ Expect (err ).To (HaveOccurred ())
277+ for _ , substr := range tc .errorContains {
278+ Expect (err .Error ()).To (ContainSubstring (substr ))
279+ }
280+ } else {
281+ Expect (err ).NotTo (HaveOccurred ())
282+ }
283+ },
284+ Entry ("should pass when versions match with nil config (default strict)" ,
285+ testCase {
286+ componentVersion : version .Get ().GitVersion ,
287+ checkMode : nil ,
288+ expectError : false ,
289+ }),
290+ Entry ("should pass when versions match in strict mode" ,
291+ testCase {
292+ componentVersion : version .Get ().GitVersion ,
293+ checkMode : ptr (configv1alpha1 .VersionCheckModeStrict ),
294+ expectError : false ,
295+ }),
296+ Entry ("should pass when versions match in warning mode" ,
297+ testCase {
298+ componentVersion : version .Get ().GitVersion ,
299+ checkMode : ptr (configv1alpha1 .VersionCheckModeWarning ),
300+ expectError : false ,
301+ }),
302+ Entry ("should fail when versions differ in strict mode" ,
303+ testCase {
304+ componentVersion : "v0.99.99-test" ,
305+ checkMode : ptr (configv1alpha1 .VersionCheckModeStrict ),
306+ expectError : true ,
307+ errorContains : []string {"version mismatch" , version .Get ().GitVersion , "v0.99.99-test" },
308+ }),
309+ Entry ("should not fail when versions differ in warning mode" ,
310+ testCase {
311+ componentVersion : "v0.99.99-test" ,
312+ checkMode : ptr (configv1alpha1 .VersionCheckModeWarning ),
313+ expectError : false ,
314+ }),
315+ Entry ("should use exact string matching - v0.2.0-dev vs v0.2.0 in strict mode" ,
316+ func () testCase {
317+ currentVersion := version .Get ().GitVersion
318+ var differentButRelated string
319+ if currentVersion == "v0.2.0-dev" {
320+ differentButRelated = "v0.2.0"
321+ } else {
322+ differentButRelated = currentVersion + "-modified"
323+ }
324+ return testCase {
325+ componentVersion : differentButRelated ,
326+ checkMode : ptr (configv1alpha1 .VersionCheckModeStrict ),
327+ expectError : true ,
328+ errorContains : []string {"version mismatch" },
329+ }
330+ }()),
331+ Entry ("should use exact string matching - v0.2.0-dev vs v0.2.0 in warning mode" ,
332+ func () testCase {
333+ currentVersion := version .Get ().GitVersion
334+ var differentButRelated string
335+ if currentVersion == "v0.2.0-dev" {
336+ differentButRelated = "v0.2.0"
337+ } else {
338+ differentButRelated = currentVersion + "-modified"
339+ }
340+ return testCase {
341+ componentVersion : differentButRelated ,
342+ checkMode : ptr (configv1alpha1 .VersionCheckModeWarning ),
343+ expectError : false ,
344+ }
345+ }()),
346+ )
347+
348+ It ("should fail when GLK component is not found in both modes" , func () {
276349 baseYAML := []byte (`
277350components:
278351 - name: github.com/gardener/other-component
@@ -282,33 +355,32 @@ components:
282355 cv , err := componentvector .NewWithOverride (baseYAML )
283356 Expect (err ).NotTo (HaveOccurred ())
284357
285- err = version .CheckGLKComponentVersion (cv )
286- Expect (err ).To (MatchError (ContainSubstring ("gardener-landscape-kit component not found" )))
287- })
358+ // Test strict mode
359+ strictMode := configv1alpha1 .VersionCheckModeStrict
360+ strictConfig := & configv1alpha1.LandscapeKitConfiguration {
361+ VersionConfig : & configv1alpha1.VersionConfiguration {
362+ CheckMode : & strictMode ,
363+ },
364+ }
288365
289- It ( "should use exact string matching (not semantic versioning)" , func () {
290- currentVersion := version . Get (). GitVersion
366+ err = version . CheckGLKComponentVersion ( cv , strictConfig , log )
367+ Expect ( err ). To ( MatchError ( ContainSubstring ( "gardener-landscape-kit component not found" )))
291368
292- // If current is v0.2.0-dev, test with v0.2.0 (different string, semantically related)
293- var differentButRelated string
294- if currentVersion == "v0.2.0-dev" {
295- differentButRelated = "v0.2.0"
296- } else {
297- differentButRelated = currentVersion + "-modified"
369+ // Test warning mode
370+ warningMode := configv1alpha1 . VersionCheckModeWarning
371+ warningConfig := & configv1alpha1. LandscapeKitConfiguration {
372+ VersionConfig : & configv1alpha1. VersionConfiguration {
373+ CheckMode : & warningMode ,
374+ },
298375 }
299376
300- baseYAML := []byte (`
301- components:
302- - name: github.com/gardener/gardener-landscape-kit
303- sourceRepository: https://github.com/gardener/gardener-landscape-kit
304- version: ` + differentButRelated + `
305- ` )
306- cv , err := componentvector .NewWithOverride (baseYAML )
307- Expect (err ).NotTo (HaveOccurred ())
308-
309- err = version .CheckGLKComponentVersion (cv )
310- Expect (err ).To (HaveOccurred ())
311- Expect (err .Error ()).To (ContainSubstring ("version mismatch" ))
377+ err = version .CheckGLKComponentVersion (cv , warningConfig , log )
378+ Expect (err ).To (MatchError (ContainSubstring ("gardener-landscape-kit component not found" )))
312379 })
313380 })
314381})
382+
383+ // ptr is a helper function to create a pointer to a value
384+ func ptr [T any ](v T ) * T {
385+ return & v
386+ }
0 commit comments