istio

Форк
0
/
config_test.go 
178 строк · 4.4 Кб
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 config
16

17
import (
18
	"bytes"
19
	"reflect"
20
	"testing"
21
	"time"
22

23
	"github.com/google/go-cmp/cmp"
24
	"github.com/kr/pretty"
25
	"sigs.k8s.io/yaml"
26
)
27

28
func TestUnmarshalKubeCaptureConfig(t *testing.T) {
29
	config := `
30
kubeConfigPath: a/b/c
31
context: d
32
istioNamespace: e1
33
dryRun: true
34
fullSecrets: true
35
commandTimeout: 5m
36
include:
37
  - ns1,ns2/d1,d2/p1,p2/l1=lv1,l2=lv2/a1=av1,a2=av2/c1,c2
38
  - ns4,ns5/d4,d5/p4,p5/l4=lv4,l5=lv5/a4=av4,a5=av5/c4,c5
39
exclude: 
40
  - ns7,ns8/d7,d8/p7,p8/l7=lv7,l8=lv8/a7=av7,a8=av8/c7,c8
41
startTime: 2002-10-02T10:00:00-05:00
42
endTime: 2002-10-02T10:00:00-05:00
43
since: 1m
44
criticalErrors:
45
  - e1
46
  - e2
47
ignoredErrors:
48
  - e3
49
  - e4
50
`
51
	wantTime, err := time.Parse(time.RFC3339, "2002-10-02T10:00:00-05:00")
52
	if err != nil {
53
		t.Fatal(err)
54
	}
55
	want := &BugReportConfig{
56
		KubeConfigPath: "a/b/c",
57
		Context:        "d",
58
		IstioNamespace: "e1",
59
		DryRun:         true,
60
		FullSecrets:    true,
61
		CommandTimeout: Duration(5 * time.Minute),
62
		Include: []*SelectionSpec{
63
			{
64
				Namespaces:  []string{"ns1", "ns2"},
65
				Deployments: []string{"d1", "d2"},
66
				Pods:        []string{"p1", "p2"},
67
				Containers:  []string{"c1", "c2"},
68
				Labels: map[string]string{
69
					"l1": "lv1",
70
					"l2": "lv2",
71
				},
72
				Annotations: map[string]string{
73
					"a1": "av1",
74
					"a2": "av2",
75
				},
76
			},
77
			{
78
				Namespaces:  []string{"ns4", "ns5"},
79
				Deployments: []string{"d4", "d5"},
80
				Pods:        []string{"p4", "p5"},
81
				Containers:  []string{"c4", "c5"},
82
				Labels: map[string]string{
83
					"l4": "lv4",
84
					"l5": "lv5",
85
				},
86
				Annotations: map[string]string{
87
					"a4": "av4",
88
					"a5": "av5",
89
				},
90
			},
91
		},
92
		Exclude: []*SelectionSpec{
93
			{
94
				Namespaces:  []string{"ns7", "ns8"},
95
				Deployments: []string{"d7", "d8"},
96
				Pods:        []string{"p7", "p8"},
97
				Containers:  []string{"c7", "c8"},
98
				Labels: map[string]string{
99
					"l7": "lv7",
100
					"l8": "lv8",
101
				},
102
				Annotations: map[string]string{
103
					"a7": "av7",
104
					"a8": "av8",
105
				},
106
			},
107
		},
108
		StartTime:      wantTime,
109
		EndTime:        wantTime,
110
		Since:          Duration(time.Minute),
111
		CriticalErrors: []string{"e1", "e2"},
112
		IgnoredErrors:  []string{"e3", "e4"},
113
	}
114

115
	got := &BugReportConfig{}
116
	if err := yaml.Unmarshal([]byte(config), got); err != nil {
117
		t.Fatal(err)
118
	}
119

120
	if !reflect.DeepEqual(got, want) {
121
		t.Errorf("got:\n%s\nwant:\n%s\n\ndiff (-got, +want):\n%s\n", pretty.Sprint(got), pretty.Sprint(want), cmp.Diff(got, want))
122
	}
123
}
124

125
func TestUnmarshalSelectionSpec(t *testing.T) {
126
	include := "ns1,ns2/d1,d2/p1,p2/l1=lv1,l2=lv2/a1=av1,a2=av2/c1,c2"
127
	want := &SelectionSpec{
128
		Namespaces:  []string{"ns1", "ns2"},
129
		Deployments: []string{"d1", "d2"},
130
		Pods:        []string{"p1", "p2"},
131
		Labels: map[string]string{
132
			"l1": "lv1",
133
			"l2": "lv2",
134
		},
135
		Annotations: map[string]string{
136
			"a1": "av1",
137
			"a2": "av2",
138
		},
139
		Containers: []string{"c1", "c2"},
140
	}
141

142
	got := &SelectionSpec{}
143
	if err := got.UnmarshalJSON([]byte(include)); err != nil {
144
		t.Fatal(err)
145
	}
146

147
	if !reflect.DeepEqual(got, want) {
148
		t.Errorf("got:\n%s\nwant:\n%s\n\ndiff (-got, +want):\n%s\n", pretty.Sprint(got), pretty.Sprint(want), cmp.Diff(got, want))
149
	}
150
}
151

152
func TestMarshalSelectionSpec(t *testing.T) {
153
	spec := &SelectionSpec{
154
		Namespaces:  []string{"ns1", "ns2"},
155
		Deployments: []string{"d1", "d2"},
156
		Pods:        []string{"p1", "p2"},
157
		Labels: map[string]string{
158
			"l1": "lv1",
159
			"l2": "lv2",
160
		},
161
		Annotations: map[string]string{
162
			"a1": "av1",
163
		},
164
		Containers: []string{"c1", "c2"},
165
	}
166
	// there are 2 possible results as map iteration order is nondeterministic
167
	want1 := []byte(`"ns1,ns2/d1,d2/p1,p2/l1=lv1,l2=lv2/a1=av1/c1,c2"`)
168
	want2 := []byte(`"ns1,ns2/d1,d2/p1,p2/l2=lv2,l1=lv1/a1=av1/c1,c2"`)
169

170
	got, err := spec.MarshalJSON()
171
	if err != nil {
172
		t.Fatal(err)
173
	}
174

175
	if !bytes.Equal(got, want1) && !bytes.Equal(got, want2) {
176
		t.Errorf("got:\n%s\nwant:\n%s or %s\n\n", got, want1, want2)
177
	}
178
}
179

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

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

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

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