istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/kubeinject/kubeinject_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  package kubeinject
    15  
    16  import (
    17  	"fmt"
    18  	"regexp"
    19  	"strings"
    20  	"testing"
    21  
    22  	"istio.io/istio/istioctl/pkg/cli"
    23  	"istio.io/istio/istioctl/pkg/util/testutil"
    24  )
    25  
    26  func TestKubeInject(t *testing.T) {
    27  	cases := []testutil.TestCase{
    28  		{ // case 0
    29  			Args:           []string{},
    30  			ExpectedRegexp: regexp.MustCompile(`filename not specified \(see --filename or -f\)`),
    31  			WantException:  true,
    32  		},
    33  		{ // case 1
    34  			Args:           strings.Split("-f missing.yaml", " "),
    35  			ExpectedRegexp: regexp.MustCompile(`open missing.yaml: no such file or directory`),
    36  			WantException:  true,
    37  		},
    38  		{ // case 2
    39  			Args: strings.Split(
    40  				"--meshConfigFile testdata/mesh-config.yaml"+
    41  					" --injectConfigFile testdata/inject-config.yaml -f testdata/deployment/hello.yaml"+
    42  					" --valuesFile testdata/inject-values.yaml",
    43  				" "),
    44  			GoldenFilename: "testdata/deployment/hello.yaml.injected",
    45  		},
    46  		{ // case 3
    47  			Args: strings.Split(
    48  				"--meshConfigFile testdata/mesh-config.yaml"+
    49  					" --injectConfigFile testdata/inject-config-inline.yaml -f testdata/deployment/hello.yaml"+
    50  					" --valuesFile testdata/inject-values.yaml",
    51  				" "),
    52  			GoldenFilename: "testdata/deployment/hello.yaml.injected",
    53  		},
    54  		{ // case 4 with only iop files
    55  			Args: strings.Split(
    56  				"--operatorFileName testdata/istio-operator.yaml"+
    57  					" --injectConfigFile testdata/inject-config-iop.yaml -f testdata/deployment/hello.yaml",
    58  				" "),
    59  			GoldenFilename: "testdata/deployment/hello.yaml.iop.injected",
    60  		},
    61  		{ // case 5 with only iop files
    62  			Args: strings.Split(
    63  				"--operatorFileName testdata/istio-operator.yaml"+
    64  					" --injectConfigFile testdata/inject-config-inline-iop.yaml -f testdata/deployment/hello.yaml",
    65  				" "),
    66  			GoldenFilename: "testdata/deployment/hello.yaml.iop.injected",
    67  		},
    68  		{ // case 6 with iops and values override
    69  			Args: strings.Split(
    70  				"--operatorFileName testdata/istio-operator.yaml"+
    71  					" --injectConfigFile testdata/inject-config-iop.yaml -f testdata/deployment/hello.yaml"+
    72  					" -f testdata/deployment/hello.yaml"+
    73  					" --valuesFile testdata/inject-values.yaml",
    74  				" "),
    75  			GoldenFilename: "testdata/deployment/hello.yaml.iop.injected",
    76  		},
    77  		{ // case 7
    78  			Args: strings.Split(
    79  				"--meshConfigFile testdata/mesh-config.yaml"+
    80  					" --injectConfigFile testdata/inject-config.yaml -f testdata/deployment/hello-with-proxyconfig-anno.yaml"+
    81  					" --valuesFile testdata/inject-values.yaml",
    82  				" "),
    83  			GoldenFilename: "testdata/deployment/hello-with-proxyconfig-anno.yaml.injected",
    84  		},
    85  	}
    86  
    87  	kubeInject := InjectCommand(cli.NewFakeContext(nil))
    88  	for i, c := range cases {
    89  		t.Run(fmt.Sprintf("case %d %s", i, strings.Join(c.Args, " ")), func(t *testing.T) {
    90  			testutil.VerifyOutput(t, kubeInject, c)
    91  			cleanUpKubeInjectTestEnv()
    92  		})
    93  	}
    94  }
    95  
    96  func cleanUpKubeInjectTestEnv() {
    97  	meshConfigFile = ""
    98  	injectConfigFile = ""
    99  	valuesFile = ""
   100  	inFilename = ""
   101  	iopFilename = ""
   102  }