github.com/quay/claircore@v1.5.28/gobin/exe_test.go (about) 1 package gobin 2 3 import ( 4 "context" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/Masterminds/semver" 10 "github.com/google/go-cmp/cmp" 11 "github.com/quay/zlog" 12 13 "github.com/quay/claircore" 14 ) 15 16 func TestBin(t *testing.T) { 17 ms, err := filepath.Glob("testdata/bin/*") 18 if err != nil { 19 t.Fatal(err) 20 } 21 for _, n := range ms { 22 name := filepath.Base(n) 23 t.Run(name, func(t *testing.T) { 24 ctx := zlog.Test(context.Background(), t) 25 f, err := os.Open(n) 26 if err != nil { 27 t.Fatal(err) 28 } 29 defer f.Close() 30 var out []*claircore.Package 31 if err := toPackages(ctx, &out, name, f); err != nil { 32 t.Fatal(err) 33 } 34 for _, pkg := range out { 35 t.Logf("%s@%s / %v", pkg.Name, pkg.Version, pkg.NormalizedVersion.String()) 36 } 37 }) 38 } 39 } 40 41 var versionTestcases = []struct { 42 name string 43 versionIn string 44 want claircore.Version 45 skipLegacy bool 46 }{ 47 { 48 name: "unchanged", 49 versionIn: "1.2.30", 50 want: claircore.Version{ 51 Kind: "semver", 52 V: [...]int32{0, 1, 2, 30, 0, 0, 0, 0, 0, 0}, 53 }, 54 }, 55 { 56 name: "with leading v", 57 versionIn: "v1.2.30", 58 want: claircore.Version{ 59 Kind: "semver", 60 V: [...]int32{0, 1, 2, 30, 0, 0, 0, 0, 0, 0}, 61 }, 62 }, 63 { 64 name: "with large version segment", 65 versionIn: "v1.2.2023071210521689159162", 66 want: claircore.Version{ 67 Kind: "semver", 68 V: [...]int32{0, 1, 2, 202307121, 0, 0, 0, 0, 0, 0}, 69 }, 70 skipLegacy: true, 71 }, 72 { 73 name: "typical", 74 versionIn: "v0.0.0-20200804184101-5ec99f83aff1", 75 want: claircore.Version{ 76 Kind: "semver", 77 V: [...]int32{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 78 }, 79 }, 80 { 81 name: "missing patch", 82 versionIn: `1.18`, 83 want: claircore.Version{ 84 Kind: "semver", 85 V: [...]int32{0, 1, 18, 0, 0, 0, 0, 0, 0, 0}, 86 }, 87 }, 88 { 89 name: "incompatible", 90 versionIn: "v24.0.7+incompatible", 91 want: claircore.Version{ 92 Kind: "semver", 93 V: [...]int32{0, 24, 0, 7, 0, 0, 0, 0, 0, 0}, 94 }, 95 }, 96 } 97 98 func TestParseVersion(t *testing.T) { 99 // Run testcases on the legacy and new parsing logic 100 // to test symmetry. 101 102 for _, tt := range versionTestcases { 103 t.Run(tt.name, func(t *testing.T) { 104 if !tt.skipLegacy { 105 // legacy 106 ver, err := semver.NewVersion(tt.versionIn) 107 if err != nil { 108 t.Fatal("error creating new semver:", err) 109 } 110 gotLegacy := claircore.FromSemver(ver) 111 if !cmp.Equal(tt.want, gotLegacy) { 112 t.Error("unexpected legacy parsing", cmp.Diff(tt.want, gotLegacy)) 113 } 114 } 115 116 // new 117 got, err := ParseVersion(tt.versionIn) 118 if err != nil { 119 t.Fatal("got error parsing version:", err) 120 } 121 if !cmp.Equal(tt.want, got) { 122 t.Error(cmp.Diff(tt.want, got)) 123 } 124 }) 125 } 126 }