1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package leasable
16
17 import (
18 "testing"
19
20 "github.com/nasa9084/go-openapi"
21 )
22
23 func TestDCLSchemaSupportsLeasing(t *testing.T) {
24 tests := []struct {
25 name string
26 schema *openapi.Schema
27 expected bool
28 }{
29 {
30 name: "no labels field",
31 schema: &openapi.Schema{
32 Type: "object",
33 Properties: map[string]*openapi.Schema{
34 "intKey": &openapi.Schema{
35 Type: "integer",
36 },
37 },
38 },
39 expected: false,
40 },
41 {
42 name: "immutable labels field",
43 schema: &openapi.Schema{
44 Type: "object",
45 Properties: map[string]*openapi.Schema{
46 "labels": &openapi.Schema{
47 Type: "string",
48 Extension: map[string]interface{}{
49 "x-kubernetes-immutable": true,
50 },
51 },
52 },
53 Extension: map[string]interface{}{
54 "x-dcl-labels": "labels",
55 },
56 },
57 expected: false,
58 },
59 {
60 name: "mutable labels field",
61 schema: &openapi.Schema{
62 Type: "object",
63 Properties: map[string]*openapi.Schema{
64 "labels": &openapi.Schema{
65 Type: "string",
66 },
67 },
68 Extension: map[string]interface{}{
69 "x-dcl-labels": "labels",
70 },
71 },
72 expected: true,
73 },
74 {
75 name: "immutable nested labels field",
76 schema: &openapi.Schema{
77 Type: "object",
78 Properties: map[string]*openapi.Schema{
79 "setting": &openapi.Schema{
80 Type: "object",
81 Properties: map[string]*openapi.Schema{
82 "labels": &openapi.Schema{
83 Type: "string",
84 Extension: map[string]interface{}{
85 "x-kubernetes-immutable": true,
86 },
87 },
88 },
89 },
90 },
91 Extension: map[string]interface{}{
92 "x-dcl-labels": "setting.labels",
93 },
94 },
95 expected: false,
96 },
97 {
98 name: "mutable nested labels field",
99 schema: &openapi.Schema{
100 Type: "object",
101 Properties: map[string]*openapi.Schema{
102 "setting": &openapi.Schema{
103 Type: "object",
104 Properties: map[string]*openapi.Schema{
105 "labels": &openapi.Schema{
106 Type: "string",
107 },
108 },
109 },
110 },
111 Extension: map[string]interface{}{
112 "x-dcl-labels": "setting.labels",
113 },
114 },
115 expected: true,
116 },
117 }
118
119 for _, tc := range tests {
120 tc := tc
121 t.Run(tc.name, func(t *testing.T) {
122 t.Parallel()
123 res, err := DCLSchemaSupportsLeasing(tc.schema)
124 if err != nil {
125 t.Fatalf("unexpcted error: %v", err)
126 }
127 if res != tc.expected {
128 t.Fatalf("expect to get %v, but got %v", tc.expected, res)
129 }
130 })
131 }
132 }
133
View as plain text