istio
213 строк · 5.0 Кб
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
15package controller16
17import (18"reflect"19"testing"20
21v1 "k8s.io/api/core/v1"22metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"23"k8s.io/apimachinery/pkg/types"24
25"istio.io/istio/pilot/pkg/model"26"istio.io/istio/pkg/config/labels"27)
28
29func TestHasProxyIP(t *testing.T) {30tests := []struct {31name string32addresses []v1.EndpointAddress33proxyIP string34expected bool35}{36{37"has proxy ip",38[]v1.EndpointAddress{{IP: "172.17.0.1"}, {IP: "172.17.0.2"}},39"172.17.0.1",40true,41},42{43"has no proxy ip",44[]v1.EndpointAddress{{IP: "172.17.0.1"}, {IP: "172.17.0.2"}},45"172.17.0.100",46false,47},48}49
50for _, test := range tests {51t.Run(test.name, func(t *testing.T) {52got := hasProxyIP(test.addresses, test.proxyIP)53if test.expected != got {54t.Errorf("Expected %v, but got %v", test.expected, got)55}56})57}58}
59
60func TestGetLabelValue(t *testing.T) {61tests := []struct {62name string63node *v1.Node64expectedLabelValue string65}{66{67"Chooses beta label",68&v1.Node{ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{NodeRegionLabel: "beta-region", NodeRegionLabelGA: "ga-region"}}},69"beta-region",70},71{72"Fallback no beta label defined",73&v1.Node{ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{NodeRegionLabelGA: "ga-region"}}},74"ga-region",75},76{77"Only beta label specified",78&v1.Node{ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{NodeRegionLabel: "beta-region"}}},79"beta-region",80},81{82"No label defined at all",83&v1.Node{},84"",85},86}87
88for _, test := range tests {89t.Run(test.name, func(t *testing.T) {90got := getLabelValue(test.node.ObjectMeta, NodeRegionLabel, NodeRegionLabelGA)91if test.expectedLabelValue != got {92t.Errorf("Expected %v, but got %v", test.expectedLabelValue, got)93}94})95}96}
97
98func TestPodKeyByProxy(t *testing.T) {99testCases := []struct {100name string101proxy *model.Proxy102expectedKey types.NamespacedName103}{104{105name: "invalid id: bad format",106proxy: &model.Proxy{107ID: "invalid",108Metadata: &model.NodeMetadata{109Namespace: "default",110},111},112},113{114name: "invalid id: namespace mismatch",115proxy: &model.Proxy{116ID: "pod1.ns1",117Metadata: &model.NodeMetadata{118Namespace: "default",119},120},121},122{123name: "invalid id: namespace mismatch",124proxy: &model.Proxy{125ID: "pod1.ns1",126Metadata: &model.NodeMetadata{127Namespace: "ns1",128},129},130expectedKey: types.NamespacedName{Namespace: "ns1", Name: "pod1"},131},132}133
134for _, tc := range testCases {135t.Run(tc.name, func(t *testing.T) {136key := podKeyByProxy(tc.proxy)137if key != tc.expectedKey {138t.Errorf("expected key %s != %s", tc.expectedKey, key)139}140})141}142}
143
144func TestGetNodeSelectorsForService(t *testing.T) {145testCases := []struct {146name string147svc *v1.Service148expectedLabelSelector labels.Instance149}{150{151name: "empty selector",152svc: makeFakeSvc(""),153expectedLabelSelector: nil,154},155{156name: "invalid selector",157svc: makeFakeSvc("invalid value"),158expectedLabelSelector: nil,159},160{161name: "wildcard match",162svc: makeFakeSvc("{}"),163expectedLabelSelector: labels.Instance{},164},165{166name: "specific match",167svc: makeFakeSvc(`{"kubernetes.io/hostname": "node1"}`),168expectedLabelSelector: labels.Instance{"kubernetes.io/hostname": "node1"},169},170}171
172for _, tc := range testCases {173t.Run(tc.name, func(t *testing.T) {174selector := getNodeSelectorsForService(tc.svc)175if !reflect.DeepEqual(selector, tc.expectedLabelSelector) {176t.Errorf("expected selector %v != %v", tc.expectedLabelSelector, selector)177}178})179}180}
181
182func makeFakeSvc(nodeSelector string) *v1.Service {183svc := &v1.Service{184ObjectMeta: metav1.ObjectMeta{185Name: "service",186Namespace: "ns",187},188Spec: v1.ServiceSpec{189Ports: []v1.ServicePort{{190Name: "http",191Port: 80,192}},193Selector: map[string]string{"app": "helloworld"},194ClusterIP: "9.9.9.9",195},196}197
198if nodeSelector != "" {199svc.Annotations = map[string]string{200"traffic.istio.io/nodeSelector": nodeSelector,201}202}203return svc204}
205
206func hasProxyIP(addresses []v1.EndpointAddress, proxyIP string) bool {207for _, addr := range addresses {208if addr.IP == proxyIP {209return true210}211}212return false213}
214