istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/ztunnelconfig/ztunnelconfig_test.go (about) 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 ztunnelconfig 16 17 import ( 18 "bytes" 19 "fmt" 20 "net/http" 21 "strings" 22 "testing" 23 24 "github.com/spf13/cobra" 25 "k8s.io/cli-runtime/pkg/resource" 26 "k8s.io/client-go/rest/fake" 27 cmdtesting "k8s.io/kubectl/pkg/cmd/testing" 28 cmdutil "k8s.io/kubectl/pkg/cmd/util" 29 30 "istio.io/istio/istioctl/pkg/cli" 31 "istio.io/istio/pkg/kube" 32 ) 33 34 type execTestCase struct { 35 execClientConfig map[string][]byte 36 args []string 37 38 // Typically use one of the three 39 expectedOutput string // Expected constant output 40 expectedString string // String output is expected to contain 41 42 wantException bool 43 } 44 45 func TestProxyConfig(t *testing.T) { 46 loggingConfig := map[string][]byte{ 47 "ztunnel-9v7nw": []byte("current log level is debug"), 48 } 49 cases := []execTestCase{ 50 { 51 args: []string{}, 52 expectedString: "A group of commands used to update or retrieve Ztunnel", 53 }, 54 { // logger name invalid when namespacing is used improperly 55 execClientConfig: loggingConfig, 56 args: strings.Split("log ztunnel-9v7nw --level ztunnel:::pool:debug", " "), 57 expectedString: "unrecognized logging level: pool:debug", 58 wantException: true, 59 }, 60 { // logger name valid and logging level valid 61 execClientConfig: loggingConfig, 62 args: strings.Split("log ztunnel-9v7nw --level ztunnel::pool:debug", " "), 63 expectedString: "", 64 wantException: false, 65 }, 66 { // set ztunnel logging level 67 execClientConfig: loggingConfig, 68 args: strings.Split("log ztunnel-9v7nw --level debug", " "), 69 expectedString: "current log level is debug", 70 wantException: false, 71 }, 72 } 73 74 for i, c := range cases { 75 t.Run(fmt.Sprintf("case %d %s", i, strings.Join(c.args, " ")), func(t *testing.T) { 76 verifyExecTestOutput(t, ZtunnelConfig(cli.NewFakeContext(&cli.NewFakeContextOption{ 77 Results: c.execClientConfig, 78 Namespace: "default", 79 })), c) 80 }) 81 } 82 } 83 84 func verifyExecTestOutput(t *testing.T, cmd *cobra.Command, c execTestCase) { 85 t.Helper() 86 87 var out bytes.Buffer 88 cmd.SetArgs(c.args) 89 cmd.SilenceUsage = true 90 cmd.SetOut(&out) 91 cmd.SetErr(&out) 92 93 fErr := cmd.Execute() 94 output := out.String() 95 96 if c.expectedOutput != "" && c.expectedOutput != output { 97 t.Fatalf("Unexpected output for 'istioctl %s'\n got: %q\nwant: %q", strings.Join(c.args, " "), output, c.expectedOutput) 98 } 99 100 if c.expectedString != "" && !strings.Contains(output, c.expectedString) { 101 t.Fatalf("Output didn't match for '%s %s'\n got %v\nwant: %v", cmd.Name(), strings.Join(c.args, " "), output, c.expectedString) 102 } 103 104 if c.wantException { 105 if fErr == nil { 106 t.Fatalf("Wanted an exception for 'istioctl %s', didn't get one, output was %q", 107 strings.Join(c.args, " "), output) 108 } 109 } else { 110 if fErr != nil { 111 t.Fatalf("Unwanted exception for 'istioctl %s': %v", strings.Join(c.args, " "), fErr) 112 } 113 } 114 } 115 116 func init() { 117 cli.MakeKubeFactory = func(k kube.CLIClient) cmdutil.Factory { 118 tf := cmdtesting.NewTestFactory() 119 _, _, codec := cmdtesting.NewExternalScheme() 120 tf.UnstructuredClient = &fake.RESTClient{ 121 NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer, 122 Resp: &http.Response{ 123 StatusCode: http.StatusOK, 124 Header: cmdtesting.DefaultHeader(), 125 Body: cmdtesting.ObjBody(codec, 126 cmdtesting.NewInternalType("", "", "foo")), 127 }, 128 } 129 return tf 130 } 131 }