| 1 |
/* |
| 2 |
Copyright 2022 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 openapiconv |
| 18 |
|
| 19 |
import ( |
| 20 |
"encoding/json" |
| 21 |
"log" |
| 22 |
"reflect" |
| 23 |
"testing" |
| 24 |
|
| 25 |
builderv2 "k8s.io/kube-openapi/pkg/builder" |
| 26 |
builderv3 "k8s.io/kube-openapi/pkg/builder3" |
| 27 |
"k8s.io/kube-openapi/pkg/openapiconv" |
| 28 |
"k8s.io/kube-openapi/pkg/validation/spec" |
| 29 |
"k8s.io/kube-openapi/test/integration/pkg/generated" |
| 30 |
"k8s.io/kube-openapi/test/integration/testutil" |
| 31 |
) |
| 32 |
|
| 33 |
func TestConvertGolden(t *testing.T) { |
| 34 |
// Generate the definition names from the map keys returned |
| 35 |
// from GetOpenAPIDefinitions. Anonymous function returning empty |
| 36 |
// Ref is not used. |
| 37 |
var defNames []string |
| 38 |
for name := range generated.GetOpenAPIDefinitions(func(name string) spec.Ref { |
| 39 |
return spec.Ref{} |
| 40 |
}) { |
| 41 |
defNames = append(defNames, name) |
| 42 |
} |
| 43 |
|
| 44 |
// Create a minimal builder config, then call the builder with the definition names. |
| 45 |
config := testutil.CreateOpenAPIBuilderConfig() |
| 46 |
config3 := testutil.CreateOpenAPIV3BuilderConfig() |
| 47 |
config.GetDefinitions = generated.GetOpenAPIDefinitions |
| 48 |
config3.GetDefinitions = generated.GetOpenAPIDefinitions |
| 49 |
// Build the Paths using a simple WebService for the final spec |
| 50 |
openapiv2, serr := builderv2.BuildOpenAPISpec(testutil.CreateWebServices(false), config) |
| 51 |
if serr != nil { |
| 52 |
log.Fatalf("ERROR: %s", serr.Error()) |
| 53 |
} |
| 54 |
|
| 55 |
openAPIV2JSONBeforeConversion, err := json.Marshal(openapiv2) |
| 56 |
if err != nil { |
| 57 |
t.Fatal(err) |
| 58 |
} |
| 59 |
openapiv3, serr := builderv3.BuildOpenAPISpec(testutil.CreateWebServices(false), config3) |
| 60 |
if serr != nil { |
| 61 |
log.Fatalf("ERROR: %s", serr.Error()) |
| 62 |
} |
| 63 |
|
| 64 |
convertedOpenAPIV3 := openapiconv.ConvertV2ToV3(openapiv2) |
| 65 |
if err != nil { |
| 66 |
t.Fatal(err) |
| 67 |
} |
| 68 |
openAPIV2JSONAfterConversion, err := json.Marshal(openapiv2) |
| 69 |
if !reflect.DeepEqual(openAPIV2JSONBeforeConversion, openAPIV2JSONAfterConversion) { |
| 70 |
t.Errorf("Expected OpenAPI V2 to be untouched before and after conversion") |
| 71 |
} |
| 72 |
|
| 73 |
if !reflect.DeepEqual(openapiv3, convertedOpenAPIV3) { |
| 74 |
t.Errorf("Expected converted OpenAPI to be equal, %v, %v", openapiv3, convertedOpenAPIV3) |
| 75 |
} |
| 76 |
} |