mirror of
https://github.com/helm/helm.git
synced 2026-04-03 16:15:46 -04:00
Merge pull request #6363 from jlegrone/refactor-test-command
Remove run test subcommand and --cleanup flag, print test status
This commit is contained in:
commit
0227ade173
28 changed files with 58 additions and 97 deletions
|
|
@ -18,28 +18,43 @@ package main
|
|||
|
||||
import (
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"helm.sh/helm/v3/cmd/helm/require"
|
||||
"helm.sh/helm/v3/pkg/action"
|
||||
"helm.sh/helm/v3/pkg/cli/output"
|
||||
)
|
||||
|
||||
const releaseTestHelp = `
|
||||
The test command consists of multiple subcommands around running tests on a release.
|
||||
|
||||
Example usage:
|
||||
$ helm test run [RELEASE]
|
||||
The test command runs the tests for a release.
|
||||
|
||||
The argument this command takes is the name of a deployed release.
|
||||
The tests to be run are defined in the chart that was installed.
|
||||
`
|
||||
|
||||
func newReleaseTestCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
|
||||
client := action.NewReleaseTesting(cfg)
|
||||
var outfmt output.Format
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "test",
|
||||
Short: "test a release or cleanup test artifacts",
|
||||
Use: "test [RELEASE]",
|
||||
Short: "run tests for a release",
|
||||
Long: releaseTestHelp,
|
||||
Args: require.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
rel, err := client.Run(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return outfmt.Write(out, &statusPrinter{rel, settings.Debug})
|
||||
},
|
||||
}
|
||||
cmd.AddCommand(
|
||||
newReleaseTestRunCmd(cfg, out),
|
||||
)
|
||||
|
||||
f := cmd.Flags()
|
||||
f.DurationVar(&client.Timeout, "timeout", 300*time.Second, "time to wait for any individual Kubernetes operation (like Jobs for hooks)")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,53 +0,0 @@
|
|||
/*
|
||||
Copyright The Helm Authors.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"helm.sh/helm/v3/cmd/helm/require"
|
||||
"helm.sh/helm/v3/pkg/action"
|
||||
)
|
||||
|
||||
const releaseTestRunHelp = `
|
||||
The test command runs the tests for a release.
|
||||
|
||||
The argument this command takes is the name of a deployed release.
|
||||
The tests to be run are defined in the chart that was installed.
|
||||
`
|
||||
|
||||
func newReleaseTestRunCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
|
||||
client := action.NewReleaseTesting(cfg)
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "run [RELEASE]",
|
||||
Short: "run tests for a release",
|
||||
Long: releaseTestRunHelp,
|
||||
Args: require.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return client.Run(args[0])
|
||||
},
|
||||
}
|
||||
|
||||
f := cmd.Flags()
|
||||
f.DurationVar(&client.Timeout, "timeout", 300*time.Second, "time to wait for any individual Kubernetes operation (like Jobs for hooks)")
|
||||
f.BoolVar(&client.Cleanup, "cleanup", false, "delete test pods upon completion")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
@ -139,7 +139,7 @@ __helm_custom_func()
|
|||
{
|
||||
__helm_debug "${FUNCNAME[0]}: last_command is $last_command"
|
||||
case ${last_command} in
|
||||
helm_uninstall | helm_history | helm_status | helm_test_run |\
|
||||
helm_uninstall | helm_history | helm_status | helm_test |\
|
||||
helm_upgrade | helm_rollback | helm_get_*)
|
||||
__helm_list_releases
|
||||
return
|
||||
|
|
|
|||
|
|
@ -97,13 +97,15 @@ func (s statusPrinter) WriteTable(out io.Writer) error {
|
|||
fmt.Fprintf(out, "REVISION: %d\n", s.release.Version)
|
||||
|
||||
executions := executionsByHookEvent(s.release)
|
||||
if tests, ok := executions[release.HookTest]; ok {
|
||||
if tests, ok := executions[release.HookTest]; !ok || len(tests) == 0 {
|
||||
fmt.Fprintln(out, "TEST SUITE: None")
|
||||
} else {
|
||||
for _, h := range tests {
|
||||
// Don't print anything if hook has not been initiated
|
||||
if h.LastRun.StartedAt.IsZero() {
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(out, "TEST SUITE: %s\n%s\n%s\n%s\n\n",
|
||||
fmt.Fprintf(out, "TEST SUITE: %s\n%s\n%s\n%s\n",
|
||||
h.Name,
|
||||
fmt.Sprintf("Last Started: %s", h.LastRun.StartedAt.Format(time.ANSIC)),
|
||||
fmt.Sprintf("Last Completed: %s", h.LastRun.CompletedAt.Format(time.ANSIC)),
|
||||
|
|
|
|||
1
cmd/helm/testdata/output/get-release.txt
vendored
1
cmd/helm/testdata/output/get-release.txt
vendored
|
|
@ -3,6 +3,7 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 1
|
||||
TEST SUITE: None
|
||||
USER-SUPPLIED VALUES:
|
||||
name: value
|
||||
|
||||
|
|
|
|||
|
|
@ -3,3 +3,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 1
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
|
|
@ -3,3 +3,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 1
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
|
|
@ -3,3 +3,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 1
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
|
|
@ -3,3 +3,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 1
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
|
|
@ -3,3 +3,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 1
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
|
|
@ -3,3 +3,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 1
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
|
|
@ -3,3 +3,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 1
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
|
|
@ -3,3 +3,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 1
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
|
|
@ -3,3 +3,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 1
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
1
cmd/helm/testdata/output/install.txt
vendored
1
cmd/helm/testdata/output/install.txt
vendored
|
|
@ -3,3 +3,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 1
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
1
cmd/helm/testdata/output/schema.txt
vendored
1
cmd/helm/testdata/output/schema.txt
vendored
|
|
@ -3,3 +3,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 1
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
|
|
@ -3,5 +3,6 @@ LAST DEPLOYED: Sat Jan 16 00:00:00 2016
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 0
|
||||
TEST SUITE: None
|
||||
NOTES:
|
||||
release notes
|
||||
|
|
|
|||
|
|
@ -7,9 +7,7 @@ TEST SUITE: passing-test
|
|||
Last Started: Mon Jan 2 15:04:05 2006
|
||||
Last Completed: Mon Jan 2 15:04:07 2006
|
||||
Phase: Succeeded
|
||||
|
||||
TEST SUITE: failing-test
|
||||
Last Started: Mon Jan 2 15:10:05 2006
|
||||
Last Completed: Mon Jan 2 15:10:07 2006
|
||||
Phase: Failed
|
||||
|
||||
|
|
|
|||
1
cmd/helm/testdata/output/status.txt
vendored
1
cmd/helm/testdata/output/status.txt
vendored
|
|
@ -3,3 +3,4 @@ LAST DEPLOYED: Sat Jan 16 00:00:00 2016
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 0
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
|
|
@ -3,3 +3,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 1
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
|
|
@ -4,3 +4,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 2
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
|
|
@ -4,3 +4,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 2
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
|
|
@ -4,3 +4,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 5
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
|
|
@ -4,3 +4,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 6
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
|
|
@ -4,3 +4,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 4
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
|
|
@ -4,3 +4,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 3
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
1
cmd/helm/testdata/output/upgrade.txt
vendored
1
cmd/helm/testdata/output/upgrade.txt
vendored
|
|
@ -4,3 +4,4 @@ LAST DEPLOYED: Fri Sep 2 22:04:05 1977
|
|||
NAMESPACE: default
|
||||
STATUS: deployed
|
||||
REVISION: 3
|
||||
TEST SUITE: None
|
||||
|
|
|
|||
|
|
@ -17,9 +17,6 @@ limitations under the License.
|
|||
package action
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
|
@ -31,10 +28,8 @@ import (
|
|||
//
|
||||
// It provides the implementation of 'helm test'.
|
||||
type ReleaseTesting struct {
|
||||
cfg *Configuration
|
||||
|
||||
cfg *Configuration
|
||||
Timeout time.Duration
|
||||
Cleanup bool
|
||||
}
|
||||
|
||||
// NewReleaseTesting creates a new ReleaseTesting object with the given configuration.
|
||||
|
|
@ -45,43 +40,25 @@ func NewReleaseTesting(cfg *Configuration) *ReleaseTesting {
|
|||
}
|
||||
|
||||
// Run executes 'helm test' against the given release.
|
||||
func (r *ReleaseTesting) Run(name string) error {
|
||||
func (r *ReleaseTesting) Run(name string) (*release.Release, error) {
|
||||
if err := r.cfg.KubeClient.IsReachable(); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := validateReleaseName(name); err != nil {
|
||||
return errors.Errorf("releaseTest: Release name is invalid: %s", name)
|
||||
return nil, errors.Errorf("releaseTest: Release name is invalid: %s", name)
|
||||
}
|
||||
|
||||
// finds the non-deleted release with the given name
|
||||
rel, err := r.cfg.Releases.Last(name)
|
||||
if err != nil {
|
||||
return err
|
||||
return rel, err
|
||||
}
|
||||
|
||||
if err := r.cfg.execHook(rel, release.HookTest, r.Timeout); err != nil {
|
||||
r.cfg.Releases.Update(rel)
|
||||
return err
|
||||
return rel, err
|
||||
}
|
||||
|
||||
if r.Cleanup {
|
||||
var manifestsToDelete strings.Builder
|
||||
for _, h := range rel.Hooks {
|
||||
for _, e := range h.Events {
|
||||
if e == release.HookTest {
|
||||
fmt.Fprintf(&manifestsToDelete, "\n---\n%s", h.Manifest)
|
||||
}
|
||||
}
|
||||
}
|
||||
hooks, err := r.cfg.KubeClient.Build(bytes.NewBufferString(manifestsToDelete.String()), false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to build test hooks: %v", err)
|
||||
}
|
||||
if _, errs := r.cfg.KubeClient.Delete(hooks); errs != nil {
|
||||
return fmt.Errorf("unable to delete test hooks: %v", joinErrors(errs))
|
||||
}
|
||||
}
|
||||
|
||||
return r.cfg.Releases.Update(rel)
|
||||
return rel, r.cfg.Releases.Update(rel)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue