1/*
2Copyright 2019 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package resource
18
19import (
20	"path/filepath"
21	"testing"
22
23	openapi_v2 "github.com/googleapis/gnostic/openapiv2"
24	"k8s.io/apimachinery/pkg/runtime/schema"
25	openapitesting "k8s.io/kube-openapi/pkg/util/proto/testing"
26)
27
28func TestSupportsDryRun(t *testing.T) {
29	doc, err := fakeSchema.OpenAPISchema()
30	if err != nil {
31		t.Fatalf("Failed to get OpenAPI Schema: %v", err)
32	}
33
34	tests := []struct {
35		gvk      schema.GroupVersionKind
36		success  bool
37		supports bool
38	}{
39		{
40			gvk: schema.GroupVersionKind{
41				Group:   "",
42				Version: "v1",
43				Kind:    "Pod",
44			},
45			success:  true,
46			supports: true,
47		},
48		{
49			gvk: schema.GroupVersionKind{
50				Group:   "",
51				Version: "v1",
52				Kind:    "UnknownKind",
53			},
54			success:  false,
55			supports: false,
56		},
57		{
58			gvk: schema.GroupVersionKind{
59				Group:   "",
60				Version: "v1",
61				Kind:    "NodeProxyOptions",
62			},
63			success:  true,
64			supports: false,
65		},
66	}
67
68	for _, test := range tests {
69		supports, err := supportsDryRun(doc, test.gvk)
70		if supports != test.supports || ((err == nil) != test.success) {
71			errStr := "nil"
72			if test.success == false {
73				errStr = "err"
74			}
75			t.Errorf("SupportsDryRun(doc, %v) = (%v, %v), expected (%v, %v)",
76				test.gvk,
77				supports, err,
78				test.supports, errStr,
79			)
80		}
81	}
82}
83
84var fakeSchema = openapitesting.Fake{Path: filepath.Join("..", "..", "artifacts", "openapi", "swagger.json")}
85
86func TestDryRunVerifier(t *testing.T) {
87	dryRunVerifier := DryRunVerifier{
88		finder: NewCRDFinder(func() ([]schema.GroupKind, error) {
89			return []schema.GroupKind{
90				{
91					Group: "crd.com",
92					Kind:  "MyCRD",
93				},
94				{
95					Group: "crd.com",
96					Kind:  "MyNewCRD",
97				},
98			}, nil
99		}),
100		openAPIGetter: &fakeSchema,
101	}
102
103	err := dryRunVerifier.HasSupport(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "NodeProxyOptions"})
104	if err == nil {
105		t.Fatalf("NodeProxyOptions doesn't support dry-run, yet no error found")
106	}
107
108	err = dryRunVerifier.HasSupport(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"})
109	if err != nil {
110		t.Fatalf("Pod should support dry-run: %v", err)
111	}
112
113	err = dryRunVerifier.HasSupport(schema.GroupVersionKind{Group: "crd.com", Version: "v1", Kind: "MyCRD"})
114	if err != nil {
115		t.Fatalf("MyCRD should support dry-run: %v", err)
116	}
117
118	err = dryRunVerifier.HasSupport(schema.GroupVersionKind{Group: "crd.com", Version: "v1", Kind: "Random"})
119	if err == nil {
120		t.Fatalf("Random doesn't support dry-run, yet no error found")
121	}
122}
123
124type EmptyOpenAPI struct{}
125
126func (EmptyOpenAPI) OpenAPISchema() (*openapi_v2.Document, error) {
127	return &openapi_v2.Document{}, nil
128}
129
130func TestDryRunVerifierNoOpenAPI(t *testing.T) {
131	dryRunVerifier := DryRunVerifier{
132		finder: NewCRDFinder(func() ([]schema.GroupKind, error) {
133			return []schema.GroupKind{
134				{
135					Group: "crd.com",
136					Kind:  "MyCRD",
137				},
138				{
139					Group: "crd.com",
140					Kind:  "MyNewCRD",
141				},
142			}, nil
143		}),
144		openAPIGetter: EmptyOpenAPI{},
145	}
146
147	err := dryRunVerifier.HasSupport(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"})
148	if err == nil {
149		t.Fatalf("Pod doesn't support dry-run, yet no error found")
150	}
151
152	err = dryRunVerifier.HasSupport(schema.GroupVersionKind{Group: "crd.com", Version: "v1", Kind: "MyCRD"})
153	if err == nil {
154		t.Fatalf("MyCRD doesn't support dry-run, yet no error found")
155	}
156}
157