istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/pkg/translate/translate_common_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 translate
    16  
    17  import (
    18  	"testing"
    19  
    20  	"istio.io/api/operator/v1alpha1"
    21  	"istio.io/istio/operator/pkg/name"
    22  	"istio.io/istio/operator/pkg/util"
    23  )
    24  
    25  func TestGetEnabledComponents(t *testing.T) {
    26  	tests := []struct {
    27  		desc     string
    28  		yamlStr  string
    29  		want     []string
    30  		wantErrs bool
    31  	}{
    32  		{
    33  			desc:    "nil success",
    34  			yamlStr: "",
    35  			want:    nil,
    36  		},
    37  		{
    38  			desc: "all components disabled",
    39  			yamlStr: `
    40  components:
    41    pilot:
    42      enabled: false
    43    ingressGateways:
    44    - enabled: false`,
    45  			want: nil,
    46  		},
    47  		{
    48  			desc: "only pilot component enabled",
    49  			yamlStr: `
    50  components:
    51    pilot:
    52      enabled: true
    53    ingressGateways:
    54    - enabled: false`,
    55  			want: []string{string(name.PilotComponentName)},
    56  		},
    57  		{
    58  			desc: "only gateway component enabled",
    59  			yamlStr: `
    60  components:
    61    pilot:
    62      enabled: false
    63    ingressGateways:
    64    - enabled: true`,
    65  			want: []string{string(name.IngressComponentName)},
    66  		},
    67  		{
    68  			desc: "all components enabled",
    69  			yamlStr: `
    70  components:
    71    pilot:
    72      enabled: true
    73    ingressGateways:
    74    - enabled: true`,
    75  			want: []string{string(name.PilotComponentName), string(name.IngressComponentName)},
    76  		},
    77  	}
    78  
    79  	for _, tt := range tests {
    80  		t.Run(tt.desc, func(t *testing.T) {
    81  			iopSpec := &v1alpha1.IstioOperatorSpec{}
    82  			err := util.UnmarshalWithJSONPB(tt.yamlStr, iopSpec, false)
    83  			if err != nil {
    84  				t.Fatalf("unmarshalWithJSONPB(%s): got error %s", tt.desc, err)
    85  			}
    86  
    87  			enabledComponents, err := GetEnabledComponents(iopSpec)
    88  			if err != nil {
    89  				t.Errorf("GetEnabledComponents(%s)(%v): got error: %v", tt.desc, tt.yamlStr, err)
    90  			}
    91  			if !testEquality(enabledComponents, tt.want) {
    92  				t.Errorf("GetEnabledComponents(%s)(%v): got: %v, want: %v", tt.desc, tt.yamlStr, enabledComponents, tt.want)
    93  			}
    94  		})
    95  	}
    96  }
    97  
    98  func testEquality(a, b []string) bool {
    99  	if (a == nil) != (b == nil) {
   100  		return false
   101  	}
   102  
   103  	if len(a) != len(b) {
   104  		return false
   105  	}
   106  
   107  	for i := range a {
   108  		if a[i] != b[i] {
   109  			return false
   110  		}
   111  	}
   112  
   113  	return true
   114  }