github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/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  	"regexp"
    23  	"strings"
    24  	"time"
    25  
    26  	"github.com/spf13/cobra"
    27  
    28  	"github.com/stefanmcshane/helm/cmd/helm/require"
    29  	"github.com/stefanmcshane/helm/pkg/action"
    30  	"github.com/stefanmcshane/helm/pkg/cli/output"
    31  )
    32  
    33  const releaseTestHelp = `
    34  The test command runs the tests for a release.
    35  
    36  The argument this command takes is the name of a deployed release.
    37  The tests to be run are defined in the chart that was installed.
    38  `
    39  
    40  func newReleaseTestCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
    41  	client := action.NewReleaseTesting(cfg)
    42  	var outfmt = output.Table
    43  	var outputLogs bool
    44  	var filter []string
    45  
    46  	cmd := &cobra.Command{
    47  		Use:   "test [RELEASE]",
    48  		Short: "run tests for a release",
    49  		Long:  releaseTestHelp,
    50  		Args:  require.ExactArgs(1),
    51  		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    52  			if len(args) != 0 {
    53  				return nil, cobra.ShellCompDirectiveNoFileComp
    54  			}
    55  			return compListReleases(toComplete, args, cfg)
    56  		},
    57  		RunE: func(cmd *cobra.Command, args []string) error {
    58  			client.Namespace = settings.Namespace()
    59  			notName := regexp.MustCompile(`^!\s?name=`)
    60  			for _, f := range filter {
    61  				if strings.HasPrefix(f, "name=") {
    62  					client.Filters["name"] = append(client.Filters["name"], strings.TrimPrefix(f, "name="))
    63  				} else if notName.MatchString(f) {
    64  					client.Filters["!name"] = append(client.Filters["!name"], notName.ReplaceAllLiteralString(f, ""))
    65  				}
    66  			}
    67  			rel, runErr := client.Run(args[0])
    68  			// We only return an error if we weren't even able to get the
    69  			// release, otherwise we keep going so we can print status and logs
    70  			// if requested
    71  			if runErr != nil && rel == nil {
    72  				return runErr
    73  			}
    74  
    75  			if err := outfmt.Write(out, &statusPrinter{rel, settings.Debug, false}); err != nil {
    76  				return err
    77  			}
    78  
    79  			if outputLogs {
    80  				// Print a newline to stdout to separate the output
    81  				fmt.Fprintln(out)
    82  				if err := client.GetPodLogs(out, rel); err != nil {
    83  					return err
    84  				}
    85  			}
    86  
    87  			return runErr
    88  		},
    89  	}
    90  
    91  	f := cmd.Flags()
    92  	f.DurationVar(&client.Timeout, "timeout", 300*time.Second, "time to wait for any individual Kubernetes operation (like Jobs for hooks)")
    93  	f.BoolVar(&outputLogs, "logs", false, "dump the logs from test pods (this runs after all tests are complete, but before any cleanup)")
    94  	f.StringSliceVar(&filter, "filter", []string{}, "specify tests by attribute (currently \"name\") using attribute=value syntax or '!attribute=value' to exclude a test (can specify multiple or separate values with commas: name=test1,name=test2)")
    95  
    96  	return cmd
    97  }