istio

Форк
0
/
metadata_test.go 
109 строк · 2.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 matcher
16

17
import (
18
	"testing"
19

20
	matcher "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
21
	"github.com/google/go-cmp/cmp"
22
	"google.golang.org/protobuf/testing/protocmp"
23
)
24

25
func TestMetadataStringMatcher(t *testing.T) {
26
	m := &matcher.StringMatcher{
27
		MatchPattern: &matcher.StringMatcher_Exact{
28
			Exact: "exact",
29
		},
30
	}
31
	actual := MetadataStringMatcher("istio_authn", "key", m)
32
	expect := &matcher.MetadataMatcher{
33
		Filter: "istio_authn",
34
		Path: []*matcher.MetadataMatcher_PathSegment{
35
			{
36
				Segment: &matcher.MetadataMatcher_PathSegment_Key{
37
					Key: "key",
38
				},
39
			},
40
		},
41
		Value: &matcher.ValueMatcher{
42
			MatchPattern: &matcher.ValueMatcher_StringMatch{
43
				StringMatch: m,
44
			},
45
		},
46
	}
47

48
	if !cmp.Equal(actual, expect, protocmp.Transform()) {
49
		t.Errorf("want %s, got %s", expect.String(), actual.String())
50
	}
51
}
52

53
func TestMetadataListMatcher(t *testing.T) {
54
	getWant := func(regex string) *matcher.MetadataMatcher {
55
		return &matcher.MetadataMatcher{
56
			Filter: "istio_authn",
57
			Path: []*matcher.MetadataMatcher_PathSegment{
58
				{
59
					Segment: &matcher.MetadataMatcher_PathSegment_Key{
60
						Key: "key1",
61
					},
62
				},
63
				{
64
					Segment: &matcher.MetadataMatcher_PathSegment_Key{
65
						Key: "key2",
66
					},
67
				},
68
			},
69
			Value: &matcher.ValueMatcher{
70
				MatchPattern: &matcher.ValueMatcher_ListMatch{
71
					ListMatch: &matcher.ListMatcher{
72
						MatchPattern: &matcher.ListMatcher_OneOf{
73
							OneOf: &matcher.ValueMatcher{
74
								MatchPattern: &matcher.ValueMatcher_StringMatch{
75
									StringMatch: &matcher.StringMatcher{
76
										MatchPattern: &matcher.StringMatcher_SafeRegex{
77
											SafeRegex: &matcher.RegexMatcher{
78
												Regex: regex,
79
											},
80
										},
81
									},
82
								},
83
							},
84
						},
85
					},
86
				},
87
			},
88
		}
89
	}
90

91
	testCases := []struct {
92
		name string
93
		want string
94
	}{
95
		{
96
			name: "wildcard",
97
			want: ".+",
98
		},
99
	}
100
	for _, tc := range testCases {
101
		t.Run(tc.name, func(t *testing.T) {
102
			want := getWant(tc.want)
103
			actual := MetadataListMatcher("istio_authn", []string{"key1", "key2"}, StringMatcher("*"), false)
104
			if !cmp.Equal(want, actual, protocmp.Transform()) {
105
				t.Errorf("want %s, but got %s", want.String(), actual.String())
106
			}
107
		})
108
	}
109
}
110

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

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

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

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