File: convert_test.go

package info (click to toggle)
golang-k8s-kube-openapi 0.0~git20241212.2c72e55-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,396 kB
  • sloc: sh: 50; makefile: 5
file content (81 lines) | stat: -rw-r--r-- 2,009 bytes parent folder | download
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
/*
Copyright 2022 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package openapiconv

import (
	"encoding/json"
	"os"
	"path/filepath"
	"reflect"
	"testing"

	"k8s.io/kube-openapi/pkg/spec3"
	"k8s.io/kube-openapi/pkg/util/jsontesting"
	"k8s.io/kube-openapi/pkg/validation/spec"
)

func TestConvert(t *testing.T) {

	tcs := []struct {
		groupVersion string
	}{{
		"batch.v1",
	}, {
		"api.v1",
	}, {
		"apiextensions.k8s.io.v1",
	}}

	for _, tc := range tcs {

		spec2JSON, err := os.ReadFile(filepath.Join("testdata_generated_from_k8s/v2_" + tc.groupVersion + ".json"))
		if err != nil {
			t.Fatal(err)
		}
		var swaggerSpec spec.Swagger
		err = json.Unmarshal(spec2JSON, &swaggerSpec)
		if err != nil {
			t.Fatal(err)
		}

		openAPIV2JSONBeforeConversion, err := swaggerSpec.MarshalJSON()
		if err != nil {
			t.Fatal(err)
		}

		convertedV3Spec := ConvertV2ToV3(&swaggerSpec)

		openAPIV2JSONAfterConversion, err := swaggerSpec.MarshalJSON()
		if err != nil {
			t.Fatal(err)
		}
		if err := jsontesting.JsonCompare(openAPIV2JSONBeforeConversion, openAPIV2JSONAfterConversion); err != nil {
			t.Errorf("Expected OpenAPI V2 to be untouched before and after conversion: %v", err)
		}

		spec3JSON, err := os.ReadFile(filepath.Join("testdata_generated_from_k8s/v3_" + tc.groupVersion + ".json"))
		if err != nil {
			t.Fatal(err)
		}

		var V3Spec spec3.OpenAPI
		json.Unmarshal(spec3JSON, &V3Spec)
		if !reflect.DeepEqual(V3Spec, *convertedV3Spec) {
			t.Error("Expected specs to be equal")
		}
	}
}