github.com/latiif/helm@v2.15.0+incompatible/cmd/helm/release_testing.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  
    23  	"github.com/spf13/cobra"
    24  
    25  	"k8s.io/helm/pkg/helm"
    26  	"k8s.io/helm/pkg/proto/hapi/release"
    27  )
    28  
    29  const releaseTestDesc = `
    30  The test command runs the tests for a release.
    31  
    32  The argument this command takes is the name of a deployed release.
    33  The tests to be run are defined in the chart that was installed.
    34  `
    35  
    36  type releaseTestCmd struct {
    37  	name        string
    38  	out         io.Writer
    39  	client      helm.Interface
    40  	timeout     int64
    41  	cleanup     bool
    42  	parallel    bool
    43  	maxParallel uint32
    44  	logs        bool
    45  }
    46  
    47  func newReleaseTestCmd(c helm.Interface, out io.Writer) *cobra.Command {
    48  	rlsTest := &releaseTestCmd{
    49  		out:    out,
    50  		client: c,
    51  	}
    52  
    53  	cmd := &cobra.Command{
    54  		Use:     "test [RELEASE]",
    55  		Short:   "Test a release",
    56  		Long:    releaseTestDesc,
    57  		PreRunE: func(_ *cobra.Command, _ []string) error { return setupConnection() },
    58  		RunE: func(cmd *cobra.Command, args []string) error {
    59  			if err := checkArgsLength(len(args), "release name"); err != nil {
    60  				return err
    61  			}
    62  
    63  			rlsTest.name = args[0]
    64  			rlsTest.client = ensureHelmClient(rlsTest.client)
    65  			return rlsTest.run()
    66  		},
    67  	}
    68  
    69  	f := cmd.Flags()
    70  	settings.AddFlagsTLS(f)
    71  	f.Int64Var(&rlsTest.timeout, "timeout", 300, "Time in seconds to wait for any individual Kubernetes operation (like Jobs for hooks)")
    72  	f.BoolVar(&rlsTest.cleanup, "cleanup", false, "Delete test pods upon completion")
    73  	f.BoolVar(&rlsTest.parallel, "parallel", false, "Run test pods in parallel")
    74  	f.Uint32Var(&rlsTest.maxParallel, "max", 20, "Maximum number of test pods to run in parallel")
    75  	f.BoolVar(&rlsTest.logs, "logs", false, "Dump the logs from test pods (this runs after all tests are complete, but before any cleanup")
    76  
    77  	// set defaults from environment
    78  	settings.InitTLS(f)
    79  
    80  	return cmd
    81  }
    82  
    83  func (t *releaseTestCmd) run() (err error) {
    84  	c, errc := t.client.RunReleaseTest(
    85  		t.name,
    86  		helm.ReleaseTestTimeout(t.timeout),
    87  		helm.ReleaseTestCleanup(t.cleanup),
    88  		helm.ReleaseTestParallel(t.parallel),
    89  		helm.ReleaseTestMaxParallel(t.maxParallel),
    90  		helm.ReleaseTestLogs(t.logs),
    91  	)
    92  	testErr := &testErr{}
    93  
    94  	for {
    95  		select {
    96  		case err := <-errc:
    97  			if prettyError(err) == nil && testErr.failed > 0 {
    98  				return testErr.Error()
    99  			}
   100  			return prettyError(err)
   101  		case res, ok := <-c:
   102  			if !ok {
   103  				break
   104  			}
   105  
   106  			if res.Status == release.TestRun_FAILURE {
   107  				testErr.failed++
   108  			}
   109  
   110  			fmt.Fprintf(t.out, res.Msg+"\n")
   111  
   112  		}
   113  	}
   114  
   115  }
   116  
   117  type testErr struct {
   118  	failed int
   119  }
   120  
   121  func (err *testErr) Error() error {
   122  	return fmt.Errorf("%v test(s) failed", err.failed)
   123  }