1 package validate
2
3 import (
4 _ "embed"
5 "os"
6 "testing"
7
8 "maps"
9
10 "github.com/google/go-containerregistry/pkg/name"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13
14 wh "edge-infra.dev/pkg/f8n/warehouse"
15 "edge-infra.dev/pkg/f8n/warehouse/oci"
16 "edge-infra.dev/test/fixtures"
17 )
18
19 var path *fixtures.Path
20
21 var testWhRefs = []string{
22 "cert-manager:latest",
23 "shoot:latest",
24 }
25
26 func TestMain(m *testing.M) {
27 var err error
28 path, err = fixtures.Layout()
29 if err != nil {
30 panic(err)
31 }
32
33 os.Exit(m.Run())
34 }
35
36 func getArtifact(reference string) (oci.Artifact, error) {
37 ref, err := name.ParseReference(reference)
38 if err != nil {
39 return nil, err
40 }
41
42 return path.Get(ref)
43 }
44
45 func TestValidateWarehouse(t *testing.T) {
46 for _, ref := range testWhRefs {
47 a, err := getArtifact(ref)
48 require.NoError(t, err)
49
50 assert.NoError(t, Warehouse(a))
51 }
52 }
53
54 func TestWarehouseAnnotations(t *testing.T) {
55 a, err := getArtifact("cert-manager:latest")
56 require.NoError(t, err)
57
58 annos, err := oci.Annotations(a)
59 require.NoError(t, err)
60
61
62 assert.NoError(t, Annotations(annos, whAnnotationValidators, nil))
63
64
65 annosWithoutName := maps.Clone(annos)
66 delete(annosWithoutName, wh.AnnotationName)
67 err = Annotations(annosWithoutName, whAnnotationValidators, nil)
68 assert.ErrorContains(t, err, wh.AnnotationName)
69 assert.ErrorContains(t, err, errMissingAnnotation.Error())
70
71
72 annosWithEmptyKind := maps.Clone(annos)
73 annosWithEmptyKind[wh.AnnotationKind] = ""
74 err = Annotations(annosWithEmptyKind, whAnnotationValidators, nil)
75 assert.ErrorContains(t, err, wh.AnnotationKind)
76 assert.ErrorContains(t, err, errEmptyAnnotation.Error())
77
78
79 annosWithInvalidProviders := maps.Clone(annos)
80 annosWithInvalidProviders[wh.AnnotationClusterProviders] = "gke generic sds"
81 err = Annotations(annosWithInvalidProviders, whAnnotationValidators, nil)
82 assert.ErrorContains(t, err, wh.AnnotationClusterProviders)
83 assert.ErrorContains(t, err, errInvalidListAnnotation.Error())
84
85
86 annosWithInvalidLayerType := maps.Clone(annos)
87 annosWithInvalidLayerType[wh.AnnotationLayerType] = "invalid"
88 err = Annotations(annosWithInvalidLayerType, whLayerAnnotationValidators, nil)
89 assert.ErrorContains(t, err, wh.AnnotationLayerType)
90 assert.ErrorContains(t, err, "invalid warehouse layer type")
91
92
93 annosWithoutRefName := maps.Clone(annos)
94 annosWithoutRefName[wh.AnnotationRefName] = ""
95 err = Annotations(annosWithoutRefName, whManifestAnnotationValidators, nil)
96 assert.ErrorContains(t, err, wh.AnnotationRefName)
97 assert.ErrorContains(t, err, errEmptyAnnotation.Error())
98 }
99
View as plain text