istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/config/kube/crd/conversion_test.go (about)

     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  	"encoding/json"
    19  	"testing"
    20  
    21  	gateway "sigs.k8s.io/gateway-api/apis/v1beta1"
    22  
    23  	"istio.io/api/meta/v1alpha1"
    24  	"istio.io/istio/pilot/test/mock"
    25  	"istio.io/istio/pkg/config"
    26  	"istio.io/istio/pkg/config/schema/collections"
    27  	"istio.io/istio/pkg/config/schema/gvk"
    28  	"istio.io/istio/pkg/test/util/assert"
    29  )
    30  
    31  func TestConvertIstioKind(t *testing.T) {
    32  	if _, err := ConvertObject(collections.VirtualService, &IstioKind{Spec: json.RawMessage(`{"x":1}`)}, "local"); err != nil {
    33  		t.Errorf("error for converting object: %s", err)
    34  	}
    35  }
    36  
    37  func TestConvert(t *testing.T) {
    38  	cases := []struct {
    39  		name string
    40  		cfg  config.Config
    41  	}{
    42  		{
    43  			name: "istio",
    44  			cfg: config.Config{
    45  				Meta: config.Meta{
    46  					GroupVersionKind: gvk.VirtualService,
    47  					Name:             "test",
    48  					Namespace:        "default",
    49  					Domain:           "cluster",
    50  					ResourceVersion:  "1234",
    51  					Labels:           map[string]string{"label": "value"},
    52  					Annotations:      map[string]string{"annotation": "value"},
    53  				},
    54  				Spec: mock.ExampleVirtualService,
    55  			},
    56  		},
    57  		{
    58  			name: "istio status",
    59  			cfg: config.Config{
    60  				Meta: config.Meta{
    61  					GroupVersionKind: gvk.VirtualService,
    62  					Name:             "test",
    63  					Namespace:        "default",
    64  					Domain:           "cluster",
    65  					ResourceVersion:  "1234",
    66  					Labels:           map[string]string{"label": "value"},
    67  					Annotations:      map[string]string{"annotation": "value"},
    68  				},
    69  				Spec: mock.ExampleVirtualService,
    70  				Status: &v1alpha1.IstioStatus{
    71  					Conditions: []*v1alpha1.IstioCondition{
    72  						{Type: "Health"},
    73  					},
    74  				},
    75  			},
    76  		},
    77  		{
    78  			name: "gateway",
    79  			cfg: config.Config{
    80  				Meta: config.Meta{
    81  					GroupVersionKind: gvk.HTTPRoute,
    82  					Name:             "test",
    83  					Namespace:        "default",
    84  					Domain:           "cluster",
    85  				},
    86  				Spec: &gateway.HTTPRouteSpec{
    87  					Hostnames: []gateway.Hostname{"example.com"},
    88  				},
    89  			},
    90  		},
    91  		{
    92  			name: "gateway status",
    93  			cfg: config.Config{
    94  				Meta: config.Meta{
    95  					GroupVersionKind: gvk.HTTPRoute,
    96  					Name:             "test",
    97  					Namespace:        "default",
    98  					Domain:           "cluster",
    99  				},
   100  				Spec: &gateway.HTTPRouteSpec{
   101  					Hostnames: []gateway.Hostname{"example.com"},
   102  				},
   103  				Status: &gateway.HTTPRouteStatus{},
   104  			},
   105  		},
   106  	}
   107  	for _, tt := range cases {
   108  		t.Run(tt.name, func(t *testing.T) {
   109  			obj, err := ConvertConfig(tt.cfg)
   110  			if err != nil {
   111  				t.Errorf("ConvertConfig() => unexpected error %v", err)
   112  			}
   113  			col, _ := collections.All.FindByGroupVersionAliasesKind(tt.cfg.GroupVersionKind)
   114  			got, err := ConvertObject(col, obj, "cluster")
   115  			if err != nil {
   116  				t.Errorf("ConvertObject() => unexpected error %v", err)
   117  			}
   118  			assert.Equal(t, &tt.cfg, got)
   119  		})
   120  	}
   121  }
   122  
   123  func TestParseInputs(t *testing.T) {
   124  	if varr, _, err := ParseInputs(""); len(varr) > 0 || err != nil {
   125  		t.Errorf(`ParseInput("") => got %v, %v, want nil, nil`, varr, err)
   126  	}
   127  	if _, _, err := ParseInputs("a"); err == nil {
   128  		t.Error(`ParseInput("a") => got no error`)
   129  	}
   130  	if _, others, err := ParseInputs("apiVersion: v1\nkind: Pod"); err != nil || len(others) != 1 {
   131  		t.Errorf(`ParseInput("kind: Pod") => got %v, %v`, others, err)
   132  	}
   133  	if varr, others, err := ParseInputs("---\n"); err != nil || len(varr) != 0 || len(others) != 0 {
   134  		t.Errorf(`ParseInput("---") => got %v, %v, %v`, varr, others, err)
   135  	}
   136  	if _, _, err := ParseInputs("apiVersion: networking.istio.io/v1alpha3\nkind: VirtualService\nspec:\n  destination: x"); err == nil {
   137  		t.Error("ParseInput(bad spec) => got no error")
   138  	}
   139  	if _, _, err := ParseInputs("apiVersion: networking.istio.io/v1alpha3\nkind: VirtualService\nspec:\n  destination:\n    service:"); err == nil {
   140  		t.Error("ParseInput(invalid spec) => got no error")
   141  	}
   142  
   143  	// nolint: lll
   144  	validInput := `{"apiVersion": "networking.istio.io/v1alpha3", "kind":"VirtualService", "spec":{"hosts":["foo"],"http":[{"route":[{"destination":{"host":"bar"},"weight":100}]}]}}`
   145  	varr, _, err := ParseInputs(validInput)
   146  	if err != nil || len(varr) == 0 {
   147  		t.Errorf("ParseInputs(correct input) => got %v, %v", varr, err)
   148  	}
   149  }