github.com/codefresh-io/kcfi@v0.0.0-20230301195427-c1578715cc46/cmd/kcfi/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  	"time"
    23  
    24  	"github.com/spf13/cobra"
    25  
    26  	"github.com/codefresh-io/kcfi/pkg/helm-internal/completion"
    27  	"helm.sh/helm/v3/cmd/helm/require"
    28  	"helm.sh/helm/v3/pkg/action"
    29  	"helm.sh/helm/v3/pkg/cli/output"
    30  )
    31  
    32  const releaseTestHelp = `
    33  The test command runs the tests for a release.
    34  
    35  The argument this command takes is the name of a deployed release.
    36  The tests to be run are defined in the chart that was installed.
    37  `
    38  
    39  func newReleaseTestCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
    40  	client := action.NewReleaseTesting(cfg)
    41  	var outfmt = output.Table
    42  	var outputLogs bool
    43  
    44  	cmd := &cobra.Command{
    45  		Use:   "test [RELEASE]",
    46  		Short: "run tests for a release",
    47  		Long:  releaseTestHelp,
    48  		Args:  require.ExactArgs(1),
    49  		RunE: func(cmd *cobra.Command, args []string) error {
    50  			client.Namespace = settings.Namespace()
    51  			rel, runErr := client.Run(args[0])
    52  			// We only return an error if we weren't even able to get the
    53  			// release, otherwise we keep going so we can print status and logs
    54  			// if requested
    55  			if runErr != nil && rel == nil {
    56  				return runErr
    57  			}
    58  
    59  			if err := outfmt.Write(out, &statusPrinter{rel, settings.Debug}); err != nil {
    60  				return err
    61  			}
    62  
    63  			if outputLogs {
    64  				// Print a newline to stdout to separate the output
    65  				fmt.Fprintln(out)
    66  				if err := client.GetPodLogs(out, rel); err != nil {
    67  					return err
    68  				}
    69  			}
    70  
    71  			return runErr
    72  		},
    73  	}
    74  
    75  	// Function providing dynamic auto-completion
    76  	completion.RegisterValidArgsFunc(cmd, func(cmd *cobra.Command, args []string, toComplete string) ([]string, completion.BashCompDirective) {
    77  		if len(args) != 0 {
    78  			return nil, completion.BashCompDirectiveNoFileComp
    79  		}
    80  		return compListReleases(toComplete, cfg)
    81  	})
    82  
    83  	f := cmd.Flags()
    84  	f.DurationVar(&client.Timeout, "timeout", 300*time.Second, "time to wait for any individual Kubernetes operation (like Jobs for hooks)")
    85  	f.BoolVar(&outputLogs, "logs", false, "dump the logs from test pods (this runs after all tests are complete, but before any cleanup)")
    86  
    87  	return cmd
    88  }