istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/pkg/apis/istio/v1alpha1/deepcopy_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 v1alpha1_test
    16  
    17  import (
    18  	"os"
    19  	"testing"
    20  
    21  	"github.com/google/go-cmp/cmp"
    22  	"google.golang.org/protobuf/testing/protocmp"
    23  	"sigs.k8s.io/yaml"
    24  
    25  	v1alpha12 "istio.io/api/operator/v1alpha1"
    26  	"istio.io/istio/operator/pkg/apis/istio"
    27  	install "istio.io/istio/operator/pkg/apis/istio/v1alpha1"
    28  )
    29  
    30  // This is to verify that certain proto types handle marshal and unmarshal properly
    31  func TestIstioOperatorSpec_DeepCopy(t *testing.T) {
    32  	x := &v1alpha12.ResourceMetricSource{}
    33  	err := yaml.UnmarshalStrict([]byte("targetAverageValue: 100m"), x)
    34  	t.Log(x)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	y := &v1alpha12.MetricSpec{}
    39  	err = yaml.UnmarshalStrict([]byte(`
    40  type: Resource
    41  resource:
    42    targetAverageValue: 100m`), y)
    43  	t.Log(y)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	z := &v1alpha12.HorizontalPodAutoscalerSpec{}
    48  	err = yaml.UnmarshalStrict([]byte(`metrics:
    49  - type: Resource
    50    resource:
    51      targetAverageValue: 100m`), z)
    52  	t.Log(z)
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  	fa := &install.IstioOperator{}
    57  	err = yaml.UnmarshalStrict([]byte(`apiVersion: install.istio.io/v1alpha1
    58  kind: IstioOperator
    59  metadata:
    60    namespace: istio-system
    61    name: example-istiocontrolplane
    62  spec:
    63    profile: demo
    64    components:
    65      pilot:
    66        k8s:
    67          hpaSpec:
    68            scaleTargetRef:
    69              apiVersion: apps/v1
    70              kind: Deployment
    71              name: istiod
    72            minReplicas: 1
    73            maxReplicas: 5
    74            metrics:
    75            - type: Resource
    76              resource:
    77                name: cpu
    78                targetAverageValue: 100m
    79  `), fa)
    80  	t.Log(fa)
    81  	if err != nil {
    82  		t.Error(err)
    83  	}
    84  	f := &install.IstioOperator{}
    85  	err = yaml.UnmarshalStrict([]byte(`apiVersion: install.istio.io/v1alpha1
    86  kind: IstioOperator
    87  metadata:
    88    namespace: istio-system
    89    name: example-istiocontrolplane
    90  spec:
    91    profile: demo
    92    components:
    93      pilot:
    94        k8s:
    95          hpaSpec:
    96            scaleTargetRef:
    97              apiVersion: apps/v1
    98              kind: Deployment
    99              name: istiod
   100            minReplicas: 1
   101            maxReplicas: 5
   102            metrics:
   103            - type: Resource
   104              resource:
   105                name: cpu
   106                targetAverageValue: 100m
   107  `), f)
   108  	t.Log(f)
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	tests := []struct {
   113  		name string
   114  		iop  string
   115  	}{
   116  		{
   117  			name: "handles Kubernetes quantity types",
   118  			iop:  "testdata/quantity.yaml",
   119  		},
   120  	}
   121  	for _, tt := range tests {
   122  		t.Run(tt.name, func(t *testing.T) {
   123  			m := loadResource(t, tt.iop)
   124  			gotSpec := m.Spec.DeepCopy()
   125  			if diff := cmp.Diff(m.Spec, gotSpec, protocmp.Transform()); diff != "" {
   126  				t.Error(diff)
   127  			}
   128  		})
   129  	}
   130  }
   131  
   132  func loadResource(t *testing.T, filepath string) *install.IstioOperator {
   133  	t.Helper()
   134  	contents, err := os.ReadFile(filepath)
   135  	if err != nil {
   136  		t.Fatal(err)
   137  	}
   138  	resource, err := istio.UnmarshalIstioOperator(string(contents), false)
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  	return resource
   143  }