istio
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
15package config16
17import (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
28func TestUnmarshalKubeCaptureConfig(t *testing.T) {29config := `30kubeConfigPath: a/b/c
31context: d
32istioNamespace: e1
33dryRun: true
34fullSecrets: true
35commandTimeout: 5m
36include:
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
39exclude:
40- ns7,ns8/d7,d8/p7,p8/l7=lv7,l8=lv8/a7=av7,a8=av8/c7,c8
41startTime: 2002-10-02T10:00:00-05:00
42endTime: 2002-10-02T10:00:00-05:00
43since: 1m
44criticalErrors:
45- e1
46- e2
47ignoredErrors:
48- e3
49- e4
50`
51wantTime, err := time.Parse(time.RFC3339, "2002-10-02T10:00:00-05:00")52if err != nil {53t.Fatal(err)54}55want := &BugReportConfig{56KubeConfigPath: "a/b/c",57Context: "d",58IstioNamespace: "e1",59DryRun: true,60FullSecrets: true,61CommandTimeout: Duration(5 * time.Minute),62Include: []*SelectionSpec{63{64Namespaces: []string{"ns1", "ns2"},65Deployments: []string{"d1", "d2"},66Pods: []string{"p1", "p2"},67Containers: []string{"c1", "c2"},68Labels: map[string]string{69"l1": "lv1",70"l2": "lv2",71},72Annotations: map[string]string{73"a1": "av1",74"a2": "av2",75},76},77{78Namespaces: []string{"ns4", "ns5"},79Deployments: []string{"d4", "d5"},80Pods: []string{"p4", "p5"},81Containers: []string{"c4", "c5"},82Labels: map[string]string{83"l4": "lv4",84"l5": "lv5",85},86Annotations: map[string]string{87"a4": "av4",88"a5": "av5",89},90},91},92Exclude: []*SelectionSpec{93{94Namespaces: []string{"ns7", "ns8"},95Deployments: []string{"d7", "d8"},96Pods: []string{"p7", "p8"},97Containers: []string{"c7", "c8"},98Labels: map[string]string{99"l7": "lv7",100"l8": "lv8",101},102Annotations: map[string]string{103"a7": "av7",104"a8": "av8",105},106},107},108StartTime: wantTime,109EndTime: wantTime,110Since: Duration(time.Minute),111CriticalErrors: []string{"e1", "e2"},112IgnoredErrors: []string{"e3", "e4"},113}114
115got := &BugReportConfig{}116if err := yaml.Unmarshal([]byte(config), got); err != nil {117t.Fatal(err)118}119
120if !reflect.DeepEqual(got, want) {121t.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
125func TestUnmarshalSelectionSpec(t *testing.T) {126include := "ns1,ns2/d1,d2/p1,p2/l1=lv1,l2=lv2/a1=av1,a2=av2/c1,c2"127want := &SelectionSpec{128Namespaces: []string{"ns1", "ns2"},129Deployments: []string{"d1", "d2"},130Pods: []string{"p1", "p2"},131Labels: map[string]string{132"l1": "lv1",133"l2": "lv2",134},135Annotations: map[string]string{136"a1": "av1",137"a2": "av2",138},139Containers: []string{"c1", "c2"},140}141
142got := &SelectionSpec{}143if err := got.UnmarshalJSON([]byte(include)); err != nil {144t.Fatal(err)145}146
147if !reflect.DeepEqual(got, want) {148t.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
152func TestMarshalSelectionSpec(t *testing.T) {153spec := &SelectionSpec{154Namespaces: []string{"ns1", "ns2"},155Deployments: []string{"d1", "d2"},156Pods: []string{"p1", "p2"},157Labels: map[string]string{158"l1": "lv1",159"l2": "lv2",160},161Annotations: map[string]string{162"a1": "av1",163},164Containers: []string{"c1", "c2"},165}166// there are 2 possible results as map iteration order is nondeterministic167want1 := []byte(`"ns1,ns2/d1,d2/p1,p2/l1=lv1,l2=lv2/a1=av1/c1,c2"`)168want2 := []byte(`"ns1,ns2/d1,d2/p1,p2/l2=lv2,l1=lv1/a1=av1/c1,c2"`)169
170got, err := spec.MarshalJSON()171if err != nil {172t.Fatal(err)173}174
175if !bytes.Equal(got, want1) && !bytes.Equal(got, want2) {176t.Errorf("got:\n%s\nwant:\n%s or %s\n\n", got, want1, want2)177}178}
179