istio

Форк
0
/
conversion_test.go 
148 строк · 4.5 Кб
1
// Copyright Istio Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package crd
16

17
import (
18
	"testing"
19

20
	gateway "sigs.k8s.io/gateway-api/apis/v1beta1"
21

22
	"istio.io/api/meta/v1alpha1"
23
	"istio.io/istio/pilot/test/mock"
24
	"istio.io/istio/pkg/config"
25
	"istio.io/istio/pkg/config/schema/collections"
26
	"istio.io/istio/pkg/config/schema/gvk"
27
	"istio.io/istio/pkg/test/util/assert"
28
)
29

30
func TestConvertIstioKind(t *testing.T) {
31
	if _, err := ConvertObject(collections.VirtualService, &IstioKind{Spec: map[string]any{"x": 1}}, "local"); err != nil {
32
		t.Errorf("error for converting object: %s", err)
33
	}
34
}
35

36
func TestConvert(t *testing.T) {
37
	cases := []struct {
38
		name string
39
		cfg  config.Config
40
	}{
41
		{
42
			name: "istio",
43
			cfg: config.Config{
44
				Meta: config.Meta{
45
					GroupVersionKind: gvk.VirtualService,
46
					Name:             "test",
47
					Namespace:        "default",
48
					Domain:           "cluster",
49
					ResourceVersion:  "1234",
50
					Labels:           map[string]string{"label": "value"},
51
					Annotations:      map[string]string{"annotation": "value"},
52
				},
53
				Spec: mock.ExampleVirtualService,
54
			},
55
		},
56
		{
57
			name: "istio status",
58
			cfg: config.Config{
59
				Meta: config.Meta{
60
					GroupVersionKind: gvk.VirtualService,
61
					Name:             "test",
62
					Namespace:        "default",
63
					Domain:           "cluster",
64
					ResourceVersion:  "1234",
65
					Labels:           map[string]string{"label": "value"},
66
					Annotations:      map[string]string{"annotation": "value"},
67
				},
68
				Spec: mock.ExampleVirtualService,
69
				Status: &v1alpha1.IstioStatus{
70
					Conditions: []*v1alpha1.IstioCondition{
71
						{Type: "Health"},
72
					},
73
				},
74
			},
75
		},
76
		{
77
			name: "gateway",
78
			cfg: config.Config{
79
				Meta: config.Meta{
80
					GroupVersionKind: gvk.HTTPRoute,
81
					Name:             "test",
82
					Namespace:        "default",
83
					Domain:           "cluster",
84
				},
85
				Spec: &gateway.HTTPRouteSpec{
86
					Hostnames: []gateway.Hostname{"example.com"},
87
				},
88
			},
89
		},
90
		{
91
			name: "gateway status",
92
			cfg: config.Config{
93
				Meta: config.Meta{
94
					GroupVersionKind: gvk.HTTPRoute,
95
					Name:             "test",
96
					Namespace:        "default",
97
					Domain:           "cluster",
98
				},
99
				Spec: &gateway.HTTPRouteSpec{
100
					Hostnames: []gateway.Hostname{"example.com"},
101
				},
102
				Status: &gateway.HTTPRouteStatus{},
103
			},
104
		},
105
	}
106
	for _, tt := range cases {
107
		t.Run(tt.name, func(t *testing.T) {
108
			obj, err := ConvertConfig(tt.cfg)
109
			if err != nil {
110
				t.Errorf("ConvertConfig() => unexpected error %v", err)
111
			}
112
			col, _ := collections.All.FindByGroupVersionAliasesKind(tt.cfg.GroupVersionKind)
113
			got, err := ConvertObject(col, obj, "cluster")
114
			if err != nil {
115
				t.Errorf("ConvertObject() => unexpected error %v", err)
116
			}
117
			assert.Equal(t, &tt.cfg, got)
118
		})
119
	}
120
}
121

122
func TestParseInputs(t *testing.T) {
123
	if varr, _, err := ParseInputs(""); len(varr) > 0 || err != nil {
124
		t.Errorf(`ParseInput("") => got %v, %v, want nil, nil`, varr, err)
125
	}
126
	if _, _, err := ParseInputs("a"); err == nil {
127
		t.Error(`ParseInput("a") => got no error`)
128
	}
129
	if _, others, err := ParseInputs("apiVersion: v1\nkind: Pod"); err != nil || len(others) != 1 {
130
		t.Errorf(`ParseInput("kind: Pod") => got %v, %v`, others, err)
131
	}
132
	if varr, others, err := ParseInputs("---\n"); err != nil || len(varr) != 0 || len(others) != 0 {
133
		t.Errorf(`ParseInput("---") => got %v, %v, %v`, varr, others, err)
134
	}
135
	if _, _, err := ParseInputs("apiVersion: networking.istio.io/v1alpha3\nkind: VirtualService\nspec:\n  destination: x"); err == nil {
136
		t.Error("ParseInput(bad spec) => got no error")
137
	}
138
	if _, _, err := ParseInputs("apiVersion: networking.istio.io/v1alpha3\nkind: VirtualService\nspec:\n  destination:\n    service:"); err == nil {
139
		t.Error("ParseInput(invalid spec) => got no error")
140
	}
141

142
	// nolint: lll
143
	validInput := `{"apiVersion": "networking.istio.io/v1alpha3", "kind":"VirtualService", "spec":{"hosts":["foo"],"http":[{"route":[{"destination":{"host":"bar"},"weight":100}]}]}}`
144
	varr, _, err := ParseInputs(validInput)
145
	if err != nil || len(varr) == 0 {
146
		t.Errorf("ParseInputs(correct input) => got %v, %v", varr, err)
147
	}
148
}
149

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.