istio
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 util16
17import (18"errors"19"testing"20)
21
22func TestParseValue(t *testing.T) {23tests := []struct {24desc string25in string26want any
27}{28{29desc: "empty",30in: "",31want: "",32},33{34desc: "true",35in: "true",36want: true,37},38{39desc: "false",40in: "false",41want: false,42},43{44desc: "numeric-one",45in: "1",46want: 1,47},48{49desc: "numeric-zero",50in: "0",51want: 0,52},53{54desc: "numeric-large",55in: "12345678",56want: 12345678,57},58{59desc: "numeric-negative",60in: "-12345678",61want: -12345678,62},63{64desc: "float",65in: "1.23456",66want: 1.23456,67},68{69desc: "float-zero",70in: "0.00",71want: 0.00,72},73{74desc: "float-negative",75in: "-6.54321",76want: -6.54321,77},78{79desc: "string",80in: "foobar",81want: "foobar",82},83{84desc: "string-number-prefix",85in: "123foobar",86want: "123foobar",87},88}89for _, tt := range tests {90t.Run(tt.desc, func(t *testing.T) {91if got, want := ParseValue(tt.in), tt.want; !(got == want) {92t.Errorf("%s: got:%v, want:%v", tt.desc, got, want)93}94})95}96}
97
98func TestConsolidateLog(t *testing.T) {99tests := []struct {100desc string101in string102want string103}{104{105desc: "empty",106in: "",107want: "",108},109{110desc: "2 errors once",111in: "err1\nerr2\n",112want: "err1 (repeated 1 times)\nerr2 (repeated 1 times)\n",113},114{115desc: "3 errors multiple times",116in: "err1\nerr2\nerr3\nerr1\nerr2\nerr3\nerr3\nerr3\n",117want: "err1 (repeated 2 times)\nerr2 (repeated 2 times)\nerr3 (repeated 4 times)\n",118},119}120for _, tt := range tests {121t.Run(tt.desc, func(t *testing.T) {122if got, want := ConsolidateLog(tt.in), tt.want; !(got == want) {123t.Errorf("%s: got:%s, want:%s", tt.desc, got, want)124}125})126}127}
128
129func TestStringBoolMapToSlice(t *testing.T) {130tests := []struct {131desc string132in map[string]bool133want []string134}{135{136desc: "empty",137in: make(map[string]bool),138want: make([]string, 0),139},140{141desc: "",142in: map[string]bool{143"yo": true,144"yolo": false,145"test1": true,146"water bottle": false,147"baseball hat": true,148},149want: []string{150"yo",151"test1",152"baseball hat",153},154},155}156for _, tt := range tests {157t.Run(tt.desc, func(t *testing.T) {158if got, want := StringBoolMapToSlice(tt.in), tt.want; !(sameStringSlice(got, want)) {159t.Errorf("%s: got:%s, want: %s", tt.desc, got, want)160}161})162}163}
164
165// Helper function to check if values in 2 slices are the same,
166// no correspondence to order
167func sameStringSlice(x, y []string) bool {168if len(x) != len(y) {169return false170}171diff := make(map[string]int, len(x))172for _, _x := range x {173diff[_x]++174}175for _, _y := range y {176if _, ok := diff[_y]; !ok {177return false178}179diff[_y]--180if diff[_y] == 0 {181delete(diff, _y)182}183}184return len(diff) == 0185}
186
187func TestRenderTemplate(t *testing.T) {188type tmplValue struct {189Name string190Proxy string191}192tests := []struct {193desc string194template string195in tmplValue
196want string197err error198}{199{200desc: "valid-template",201template: "{{.Name}} uses {{.Proxy}} as sidecar",202in: tmplValue{203Name: "istio",204Proxy: "envoy",205},206want: "istio uses envoy as sidecar",207err: nil,208},209{210desc: "empty-template",211template: "",212in: tmplValue{213Name: "istio",214Proxy: "envoy",215},216want: "",217err: nil,218},219{220desc: "template with no template strings",221template: "this template is without handlebar expressions",222in: tmplValue{223Name: "istio",224Proxy: "envoy",225},226want: "this template is without handlebar expressions",227err: nil,228},229{230desc: "template with missing variable",231template: "{{ .Name }} has replaced its control plane with {{ .Istiod }} component",232in: tmplValue{233Name: "istio",234Proxy: "envoy",235},236want: "",237err: errors.New(""),238},239}240for _, tt := range tests {241t.Run(tt.desc, func(t *testing.T) {242got, err := RenderTemplate(tt.template, tt.in)243if got != tt.want {244t.Errorf("%s: got :%v, wanted output: %v", tt.desc, got, tt.want)245}246
247if (err == nil && tt.err != nil) || (err != nil && tt.err == nil) {248t.Errorf("%s: got error :%v, wanted error: %v", tt.desc, err, tt.err)249}250})251}252}
253