1
// Copyright Istio Authors
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
7
// http://www.apache.org/licenses/LICENSE-2.0
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.
20
gateway "sigs.k8s.io/gateway-api/apis/v1beta1"
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"
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)
36
func TestConvert(t *testing.T) {
45
GroupVersionKind: gvk.VirtualService,
49
ResourceVersion: "1234",
50
Labels: map[string]string{"label": "value"},
51
Annotations: map[string]string{"annotation": "value"},
53
Spec: mock.ExampleVirtualService,
60
GroupVersionKind: gvk.VirtualService,
64
ResourceVersion: "1234",
65
Labels: map[string]string{"label": "value"},
66
Annotations: map[string]string{"annotation": "value"},
68
Spec: mock.ExampleVirtualService,
69
Status: &v1alpha1.IstioStatus{
70
Conditions: []*v1alpha1.IstioCondition{
80
GroupVersionKind: gvk.HTTPRoute,
85
Spec: &gateway.HTTPRouteSpec{
86
Hostnames: []gateway.Hostname{"example.com"},
91
name: "gateway status",
94
GroupVersionKind: gvk.HTTPRoute,
99
Spec: &gateway.HTTPRouteSpec{
100
Hostnames: []gateway.Hostname{"example.com"},
102
Status: &gateway.HTTPRouteStatus{},
106
for _, tt := range cases {
107
t.Run(tt.name, func(t *testing.T) {
108
obj, err := ConvertConfig(tt.cfg)
110
t.Errorf("ConvertConfig() => unexpected error %v", err)
112
col, _ := collections.All.FindByGroupVersionAliasesKind(tt.cfg.GroupVersionKind)
113
got, err := ConvertObject(col, obj, "cluster")
115
t.Errorf("ConvertObject() => unexpected error %v", err)
117
assert.Equal(t, &tt.cfg, got)
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)
126
if _, _, err := ParseInputs("a"); err == nil {
127
t.Error(`ParseInput("a") => got no error`)
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)
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)
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")
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")
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)