Debian Codegraph @

pkg/util/openapi/enablement_test.go

raw
1 /*
2 Copyright 2021 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package openapi
18
19 import (
20 "fmt"
21 "strings"
22 "testing"
23
24 "k8s.io/apiserver/pkg/features"
25 utilfeature "k8s.io/apiserver/pkg/util/feature"
26 featuregatetesting "k8s.io/component-base/featuregate/testing"
27 "k8s.io/kube-openapi/pkg/common"
28 "k8s.io/kube-openapi/pkg/validation/spec"
29 )
30
31 func schema_k8sio_api_apps_v1_DeploymentCondition(ref common.ReferenceCallback) common.OpenAPIDefinition {
32 return common.OpenAPIDefinition{
33 Schema: spec.Schema{
34 SchemaProps: spec.SchemaProps{
35 Description: "DeploymentCondition describes the state of a deployment at a certain point.",
36 Type: []string{"object"},
37 Properties: map[string]spec.Schema{
38 "type": {
39 SchemaProps: spec.SchemaProps{
40 Description: "Type of deployment condition.\n\nPossible enum values:\n - `Available`: Available means the deployment is available, ie. at least the minimum available replicas required are up and running for at least minReadySeconds.\n - `Progressing`: Progressing means the deployment is progressing. Progress for a deployment is considered when a new replica set is created or adopted, and when new pods scale up or old pods scale down. Progress is not estimated for paused deployments or when progressDeadlineSeconds is not specified.\n - `ReplicaFailure`: ReplicaFailure is added in a deployment when one of its pods fails to be created or deleted.",
41 Default: "",
42 Type: []string{"string"},
43 Format: "",
44 Enum: []interface{}{"Available", "Progressing", "ReplicaFailure"}},
45 },
46 "status": {
47 SchemaProps: spec.SchemaProps{
48 Description: "Status of the condition, one of True, False, Unknown.\n\nPossible enum values:\n - `False`:\n - `True`:\n - `Unknown`:",
49 Default: "",
50 Type: []string{"string"},
51 Format: "",
52 Enum: []interface{}{"False", "True", "Unknown"}},
53 },
54 "lastUpdateTime": {
55 SchemaProps: spec.SchemaProps{
56 Description: "The last time this condition was updated.",
57 Default: map[string]interface{}{},
58 Ref: ref("k8s.io/apimachinery/pkg/apis/meta/v1.Time"),
59 },
60 },
61 "lastTransitionTime": {
62 SchemaProps: spec.SchemaProps{
63 Description: "Last time the condition transitioned from one status to another.",
64 Default: map[string]interface{}{},
65 Ref: ref("k8s.io/apimachinery/pkg/apis/meta/v1.Time"),
66 },
67 },
68 "reason": {
69 SchemaProps: spec.SchemaProps{
70 Description: "The reason for the condition's last transition.",
71 Type: []string{"string"},
72 Format: "",
73 },
74 },
75 "message": {
76 SchemaProps: spec.SchemaProps{
77 Description: "A human readable message indicating details about the transition.",
78 Type: []string{"string"},
79 Format: "",
80 },
81 },
82 },
83 Required: []string{"type", "status"},
84 },
85 },
86 Dependencies: []string{
87 "k8s.io/apimachinery/pkg/apis/meta/v1.Time"},
88 }
89 }
90
91 var getOpenAPIDefs common.GetOpenAPIDefinitions = func(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition {
92 return map[string]common.OpenAPIDefinition{
93 "k8s.io/api/apps/v1.DeploymentCondition": schema_k8sio_api_apps_v1_DeploymentCondition(ref),
94 }
95 }
96
97 func TestGetOpenAPIDefinitionsWithoutDisabledFeatures(t *testing.T) {
98 for _, tc := range []struct {
99 enabled bool
100 shouldHaveEnum bool
101 }{
102 {
103 enabled: true,
104 shouldHaveEnum: true,
105 },
106 {
107 enabled: false,
108 shouldHaveEnum: false,
109 },
110 } {
111 t.Run(fmt.Sprintf("enabled=%v", tc.enabled), func(t *testing.T) {
112 featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.OpenAPIEnums, tc.enabled)
113 defs := GetOpenAPIDefinitionsWithoutDisabledFeatures(getOpenAPIDefs)(func(path string) spec.Ref {
114 return spec.Ref{}
115 })
116 def := defs["k8s.io/api/apps/v1.DeploymentCondition"]
117 enumAppeared := false
118 for _, prop := range def.Schema.Properties {
119 if strings.Contains(prop.Description, "enum") {
120 enumAppeared = true
121 if !tc.shouldHaveEnum {
122 t.Errorf("enum appeared, description: %s", prop.Description)
123 }
124 }
125 if len(prop.Enum) != 0 {
126 enumAppeared = true
127 if !tc.shouldHaveEnum {
128 t.Errorf("enum appeared, enum: %v", prop.Enum)
129 }
130 }
131 }
132 if !enumAppeared && tc.shouldHaveEnum {
133 t.Errorf("enum did not appear")
134 }
135 })
136 }
137
138 }