File: validation_test.go

package info (click to toggle)
golang-k8s-sigs-kustomize-kyaml 0.20.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,180 kB
  • sloc: makefile: 220; sh: 68
file content (155 lines) | stat: -rw-r--r-- 4,535 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2022 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package framework

import (
	"sort"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"sigs.k8s.io/kustomize/kyaml/resid"
)

var demoFunctionDefinition = `
apiVersion: config.kubernetes.io/v1alpha1
kind: KRMFunctionDefinition
metadata:
  name: demos.example.io
spec:
  group: example.io
  names:
    kind: Demo
  versions:
  - name: v1alpha1
    schema:
      openAPIV3Schema:
        properties:
          apiVersion:
            type: string
          color:
            type: string
          kind:
            type: string
          metadata:
            type: object
        required:
        - color
        type: object
  - name: v1alpha2
    schema:
      openAPIV3Schema:
        properties:
          apiVersion:
            type: string
          flavor:
            type: string
          kind:
            type: string
          metadata:
            type: object
        required:
        - flavor
        type: object
`

var demoCRD = `
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: demos.example.io
spec:
  group: example.io
  names:
    kind: Demo
    listKind: DemoList
    plural: demos
    singular: demo
  scope: Namespaced
  versions:
  - name: v1alpha1
    schema:
      openAPIV3Schema:
        properties:
          apiVersion:
            description: 'APIVersion defines the versioned schema of this representation
              of an object. Servers should convert recognized schemas to the latest
              internal value, and may reject unrecognized values. More info: 
			  https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
            type: string
          color:
            type: string
          kind:
            description: 'Kind is a string value representing the REST resource this
              object represents. Servers may infer this from the endpoint the client
              submits requests to. Cannot be updated. In CamelCase. More info: 
			  https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
            type: string
          metadata:
            type: object
        required:
        - color
        type: object
    served: true
    storage: true
`

func TestSchemaFromFunctionDefinition(t *testing.T) {
	tests := []struct {
		name      string
		gvk       resid.Gvk
		data      string
		wantProps []string
		wantErr   string
	}{
		{
			name:      "demo KRMFunctionDefinition extract v1alpha1",
			gvk:       resid.NewGvk("example.io", "v1alpha1", "Demo"),
			data:      demoFunctionDefinition,
			wantProps: []string{"apiVersion", "kind", "metadata", "color"},
		}, {
			name:      "demo KRMFunctionDefinition extract v1alpha2",
			gvk:       resid.NewGvk("example.io", "v1alpha2", "Demo"),
			data:      demoFunctionDefinition,
			wantProps: []string{"apiVersion", "kind", "metadata", "flavor"},
		}, {
			name:      "works with CustomResourceDefinition",
			gvk:       resid.NewGvk("example.io", "v1alpha1", "Demo"),
			data:      demoCRD,
			wantProps: []string{"apiVersion", "kind", "metadata", "color"},
		}, {
			name:    "group mismatch",
			gvk:     resid.NewGvk("example.com", "v1alpha2", "Demo"),
			data:    demoFunctionDefinition,
			wantErr: "KRMFunctionDefinition does not define Demo.v1alpha2.example.com (defines: [Demo.v1alpha1.example.io Demo.v1alpha2.example.io])",
		}, {
			name:    "version mismatch",
			gvk:     resid.NewGvk("example.io", "v1alpha3", "Demo"),
			data:    demoFunctionDefinition,
			wantErr: "KRMFunctionDefinition does not define Demo.v1alpha3.example.io (defines: [Demo.v1alpha1.example.io Demo.v1alpha2.example.io])",
		}, {
			name:    "kind mismatch",
			gvk:     resid.NewGvk("example.io", "v1alpha2", "Demonstration"),
			data:    demoFunctionDefinition,
			wantErr: "KRMFunctionDefinition does not define Demonstration.v1alpha2.example.io (defines: [Demo.v1alpha1.example.io Demo.v1alpha2.example.io])",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := SchemaFromFunctionDefinition(tt.gvk, tt.data)
			if tt.wantErr != "" {
				require.EqualError(t, err, tt.wantErr)
			} else {
				require.NoError(t, err)
				var gotProps []string
				for prop := range got.Properties {
					gotProps = append(gotProps, prop)
				}
				sort.Strings(tt.wantProps)
				sort.Strings(gotProps)
				assert.Equal(t, gotProps, tt.wantProps)
			}
		})
	}
}