istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/pkg/util/k8s_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  package util
    15  
    16  import (
    17  	"testing"
    18  
    19  	v1 "k8s.io/api/core/v1"
    20  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    21  	"k8s.io/apimachinery/pkg/version"
    22  	fakediscovery "k8s.io/client-go/discovery/fake"
    23  	"sigs.k8s.io/yaml"
    24  
    25  	pkgAPI "istio.io/istio/operator/pkg/apis/istio/v1alpha1"
    26  	"istio.io/istio/pkg/kube"
    27  	"istio.io/istio/pkg/test/util/assert"
    28  )
    29  
    30  var (
    31  	o1 = `
    32  apiVersion: install.istio.io/v1alpha1
    33  kind: IstioOperator
    34  spec:
    35    values:
    36      global:
    37        pilotCertProvider: kubernetes
    38  `
    39  	o2 = `
    40  apiVersion: install.istio.io/v1alpha1
    41  kind: IstioOperator
    42  spec:
    43    values:
    44      global:
    45        pilotCertProvider: istiod
    46  `
    47  	o3 = `
    48  apiVersion: install.istio.io/v1alpha1
    49  kind: IstioOperator
    50  spec:
    51    values:
    52  `
    53  )
    54  
    55  func TestValidateIOPCAConfig(t *testing.T) {
    56  	var err error
    57  
    58  	tests := []struct {
    59  		major        string
    60  		minor        string
    61  		expErr       bool
    62  		operatorYaml string
    63  	}{
    64  		{
    65  			major:        "1",
    66  			minor:        "16",
    67  			expErr:       false,
    68  			operatorYaml: o1,
    69  		},
    70  		{
    71  			major:        "1",
    72  			minor:        "22",
    73  			expErr:       true,
    74  			operatorYaml: o1,
    75  		},
    76  		{
    77  			major:        "1",
    78  			minor:        "23",
    79  			expErr:       false,
    80  			operatorYaml: o2,
    81  		},
    82  		{
    83  			major:        "1",
    84  			minor:        "24",
    85  			expErr:       false,
    86  			operatorYaml: o3,
    87  		},
    88  	}
    89  
    90  	for i, tt := range tests {
    91  		k8sClient := kube.NewFakeClient()
    92  		k8sClient.Kube().Discovery().(*fakediscovery.FakeDiscovery).FakedServerVersion = &version.Info{
    93  			Major: tt.major,
    94  			Minor: tt.minor,
    95  		}
    96  		op := &pkgAPI.IstioOperator{}
    97  		err = yaml.Unmarshal([]byte(tt.operatorYaml), op)
    98  		if err != nil {
    99  			t.Fatalf("Failure in test case %v. Error %s", i, err)
   100  		}
   101  		err = ValidateIOPCAConfig(k8sClient, op)
   102  		if !tt.expErr && err != nil {
   103  			t.Fatalf("Failure in test case %v. Expected No Error. Got %s", i, err)
   104  		} else if tt.expErr && err == nil {
   105  			t.Fatalf("Failure in test case %v. Expected Error. Got No error", i)
   106  		}
   107  	}
   108  }
   109  
   110  func TestPrometheusPathAndPort(t *testing.T) {
   111  	cases := []struct {
   112  		pod  *v1.Pod
   113  		path string
   114  		port int
   115  	}{
   116  		{
   117  			pod: &v1.Pod{
   118  				ObjectMeta: metav1.ObjectMeta{
   119  					Name: "case-1",
   120  					Annotations: map[string]string{
   121  						"prometheus.io/path": "/metrics",
   122  						"prometheus.io/port": "15020",
   123  					},
   124  				},
   125  			},
   126  			path: "/metrics",
   127  			port: 15020,
   128  		},
   129  		{
   130  			pod: &v1.Pod{
   131  				ObjectMeta: metav1.ObjectMeta{
   132  					Name: "case-2",
   133  					Annotations: map[string]string{
   134  						"prometheus.io.path": "/metrics",
   135  						"prometheus.io.port": "15020",
   136  					},
   137  				},
   138  			},
   139  			path: "/metrics",
   140  			port: 15020,
   141  		},
   142  		{
   143  			pod: &v1.Pod{
   144  				ObjectMeta: metav1.ObjectMeta{
   145  					Name: "case-3",
   146  					Annotations: map[string]string{
   147  						"prometheus-io/path": "/metrics",
   148  						"prometheus-io/port": "15020",
   149  					},
   150  				},
   151  			},
   152  			path: "/metrics",
   153  			port: 15020,
   154  		},
   155  	}
   156  
   157  	for _, tc := range cases {
   158  		t.Run(tc.pod.Name, func(t *testing.T) {
   159  			path, port, err := PrometheusPathAndPort(tc.pod)
   160  			assert.NoError(t, err)
   161  			assert.Equal(t, tc.path, path)
   162  			assert.Equal(t, tc.port, port)
   163  		})
   164  	}
   165  }