istio

Форк
0
210 строк · 7.8 Кб
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 serviceentry
16

17
import (
18
	"reflect"
19
	"testing"
20

21
	"k8s.io/apimachinery/pkg/types"
22

23
	networking "istio.io/api/networking/v1alpha3"
24
	"istio.io/istio/pilot/pkg/model"
25
	"istio.io/istio/pkg/config/constants"
26
	"istio.io/istio/pkg/ptr"
27
	"istio.io/istio/pkg/test/util/assert"
28
	"istio.io/istio/pkg/util/sets"
29
)
30

31
func TestServiceInstancesStore(t *testing.T) {
32
	store := serviceInstancesStore{
33
		ip2instance:            map[string][]*model.ServiceInstance{},
34
		instances:              map[instancesKey]map[configKey][]*model.ServiceInstance{},
35
		instancesBySE:          map[types.NamespacedName]map[configKey][]*model.ServiceInstance{},
36
		instancesByHostAndPort: sets.Set[hostPort]{},
37
	}
38
	instances := []*model.ServiceInstance{
39
		makeInstance(selector, "1.1.1.1", 444, selector.Spec.(*networking.ServiceEntry).Ports[0], nil, PlainText),
40
		makeInstance(selector, "1.1.1.1", 445, selector.Spec.(*networking.ServiceEntry).Ports[1], nil, PlainText),
41
		makeInstance(dnsSelector, "1.1.1.1", 444, dnsSelector.Spec.(*networking.ServiceEntry).Ports[0], nil, PlainText),
42
	}
43
	cKey := configKey{
44
		namespace: "default",
45
		name:      "test-wle",
46
	}
47
	store.addInstances(cKey, instances)
48

49
	// 1. test getByIP
50
	gotInstances := store.getByIP("1.1.1.1")
51
	if !reflect.DeepEqual(instances, gotInstances) {
52
		t.Errorf("got unexpected instances : %v", gotInstances)
53
	}
54

55
	// 2. test getAll
56
	gotInstances = store.getAll()
57
	if !reflect.DeepEqual(instances, gotInstances) {
58
		t.Errorf("got unexpected instances : %v", gotInstances)
59
	}
60

61
	// 3. test getByKey
62
	gotInstances = store.getByKey(instancesKey{
63
		hostname:  "selector.com",
64
		namespace: "selector",
65
	})
66
	expected := []*model.ServiceInstance{
67
		makeInstance(selector, "1.1.1.1", 444, selector.Spec.(*networking.ServiceEntry).Ports[0], nil, PlainText),
68
		makeInstance(selector, "1.1.1.1", 445, selector.Spec.(*networking.ServiceEntry).Ports[1], nil, PlainText),
69
	}
70
	if !reflect.DeepEqual(gotInstances, expected) {
71
		t.Errorf("got unexpected instances : %v", gotInstances)
72
	}
73

74
	// 4. test getServiceEntryInstances
75
	expectedSeInstances := map[configKey][]*model.ServiceInstance{cKey: {
76
		makeInstance(selector, "1.1.1.1", 444, selector.Spec.(*networking.ServiceEntry).Ports[0], nil, PlainText),
77
		makeInstance(selector, "1.1.1.1", 445, selector.Spec.(*networking.ServiceEntry).Ports[1], nil, PlainText),
78
	}}
79
	key := selector.NamespacedName()
80
	store.updateServiceEntryInstances(key, expectedSeInstances)
81

82
	gotSeInstances := store.getServiceEntryInstances(key)
83
	if !reflect.DeepEqual(gotSeInstances, expectedSeInstances) {
84
		t.Errorf("got unexpected se instances : %v", gotSeInstances)
85
	}
86

87
	// 5. test deleteServiceEntryInstances
88
	store.deleteServiceEntryInstances(key, cKey)
89
	gotSeInstances = store.getServiceEntryInstances(key)
90
	if len(gotSeInstances) != 0 {
91
		t.Errorf("got unexpected instances %v", gotSeInstances)
92
	}
93

94
	// 6. test deleteAllServiceEntryInstances
95
	store.deleteAllServiceEntryInstances(key)
96
	gotSeInstances = store.getServiceEntryInstances(key)
97
	if len(gotSeInstances) != 0 {
98
		t.Errorf("got unexpected instances %v", gotSeInstances)
99
	}
100

101
	// 7. test deleteInstanceKeys
102
	store.deleteInstanceKeys(cKey, instances)
103
	gotInstances = store.getAll()
104
	if len(gotInstances) != 0 {
105
		t.Errorf("got unexpected instances %v", gotSeInstances)
106
	}
107
}
108

109
func TestServiceStore(t *testing.T) {
110
	store := serviceStore{
111
		servicesBySE: map[types.NamespacedName][]*model.Service{},
112
	}
113

114
	expectedServices := []*model.Service{
115
		makeService("*.istio.io", "httpDNSRR", constants.UnspecifiedIP, map[string]int{"http-port": 80, "http-alt-port": 8080}, true, model.DNSRoundRobinLB),
116
		makeService("*.istio.io", "httpDNSRR", constants.UnspecifiedIP, map[string]int{"http-port": 80, "http-alt-port": 8080}, true, model.DNSLB),
117
	}
118

119
	store.updateServices(httpDNSRR.NamespacedName(), expectedServices)
120
	got := store.getServices(httpDNSRR.NamespacedName())
121
	if !reflect.DeepEqual(got, expectedServices) {
122
		t.Errorf("got unexpected services %v", got)
123
	}
124

125
	got = store.getAllServices()
126
	if !reflect.DeepEqual(got, expectedServices) {
127
		t.Errorf("got unexpected services %v", got)
128
	}
129
	if !store.allocateNeeded {
130
		t.Errorf("expected allocate needed")
131
	}
132
	store.allocateNeeded = false
133
	store.deleteServices(httpDNSRR.NamespacedName())
134
	got = store.getAllServices()
135
	if got != nil {
136
		t.Errorf("got unexpected services %v", got)
137
	}
138
	if store.allocateNeeded {
139
		t.Errorf("expected no allocate needed")
140
	}
141
}
142

143
// Tests that when multiple service entries with "DNSRounbRobinLB" resolution type
144
// are created with different/same endpoints, we only consider the first service because
145
// Envoy's LogicalDNS type of cluster does not allow more than one locality LB Endpoint.
146
func TestServiceInstancesForDnsRoundRobinLB(t *testing.T) {
147
	otherNs := ptr.Of(dnsRoundRobinLBSE1.DeepCopy())
148
	otherNs.Namespace = "other"
149
	store := serviceInstancesStore{
150
		ip2instance:            map[string][]*model.ServiceInstance{},
151
		instances:              map[instancesKey]map[configKey][]*model.ServiceInstance{},
152
		instancesBySE:          map[types.NamespacedName]map[configKey][]*model.ServiceInstance{},
153
		instancesByHostAndPort: sets.Set[hostPort]{},
154
	}
155
	instances := []*model.ServiceInstance{
156
		makeInstance(dnsRoundRobinLBSE1, "1.1.1.1", 444, dnsRoundRobinLBSE1.Spec.(*networking.ServiceEntry).Ports[0], nil, PlainText),
157
		makeInstance(dnsRoundRobinLBSE1, "1.1.1.1", 445, dnsRoundRobinLBSE1.Spec.(*networking.ServiceEntry).Ports[1], nil, PlainText),
158
	}
159
	cKey := configKey{
160
		namespace: "dns",
161
		name:      "dns-round-robin-1",
162
	}
163
	// Add instance related to first Service Entry and validate they are added correctly.
164
	store.addInstances(cKey, instances)
165

166
	store.addInstances(
167
		configKey{namespace: otherNs.Namespace, name: otherNs.Name},
168
		[]*model.ServiceInstance{
169
			makeInstance(otherNs, "1.1.1.1", 444, otherNs.Spec.(*networking.ServiceEntry).Ports[0], nil, PlainText),
170
			makeInstance(otherNs, "1.1.1.1", 445, otherNs.Spec.(*networking.ServiceEntry).Ports[1], nil, PlainText),
171
		},
172
	)
173

174
	expected := []*model.ServiceInstance{
175
		makeInstance(dnsRoundRobinLBSE1, "1.1.1.1", 444, dnsRoundRobinLBSE1.Spec.(*networking.ServiceEntry).Ports[0], nil, PlainText),
176
		makeInstance(dnsRoundRobinLBSE1, "1.1.1.1", 445, dnsRoundRobinLBSE1.Spec.(*networking.ServiceEntry).Ports[1], nil, PlainText),
177
	}
178
	assert.Equal(t, store.getByKey(instancesKey{
179
		hostname:  "example.com",
180
		namespace: "dns",
181
	}), expected)
182

183
	otherNsExpected := []*model.ServiceInstance{
184
		makeInstance(otherNs, "1.1.1.1", 444, otherNs.Spec.(*networking.ServiceEntry).Ports[0], nil, PlainText),
185
		makeInstance(otherNs, "1.1.1.1", 445, otherNs.Spec.(*networking.ServiceEntry).Ports[1], nil, PlainText),
186
	}
187
	assert.Equal(t, store.getByKey(instancesKey{
188
		hostname:  "example.com",
189
		namespace: otherNs.Namespace,
190
	}), otherNsExpected)
191

192
	// Add instance related to second Service Entry and validate it is ignored.
193
	instances = []*model.ServiceInstance{
194
		makeInstance(dnsRoundRobinLBSE2, "2.2.2.2", 444, dnsRoundRobinLBSE2.Spec.(*networking.ServiceEntry).Ports[0], nil, PlainText),
195
	}
196
	cKey = configKey{
197
		namespace: "dns",
198
		name:      "dns-round-robin-2",
199
	}
200
	store.addInstances(cKey, instances)
201

202
	assert.Equal(t, store.getByKey(instancesKey{
203
		hostname:  "example.com",
204
		namespace: "dns",
205
	}), expected)
206
	assert.Equal(t, store.getByKey(instancesKey{
207
		hostname:  "example.com",
208
		namespace: otherNs.Namespace,
209
	}), otherNsExpected)
210
}
211

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.