From 7bfb2a3602953b052db85dcb222157a32db9547a Mon Sep 17 00:00:00 2001 From: Stephane Moser Date: Thu, 15 Jul 2021 10:26:49 +0100 Subject: [PATCH] Improve tests Rename the package time Redesgin the logic to make a FakeKubeClient wait for a ammount time. Remove unneed logic in the PrintingKubeClient Signed-off-by: Stephane Moser --- pkg/action/install_test.go | 24 ++++++++++++------------ pkg/action/upgrade_test.go | 4 ++-- pkg/kube/fake/fake.go | 6 ++---- pkg/kube/fake/printer.go | 7 +------ 4 files changed, 17 insertions(+), 24 deletions(-) diff --git a/pkg/action/install_test.go b/pkg/action/install_test.go index 786f7270e..46fac72f3 100644 --- a/pkg/action/install_test.go +++ b/pkg/action/install_test.go @@ -26,7 +26,7 @@ import ( "regexp" "strings" "testing" - gotime "time" + "time" "github.com/stretchr/testify/assert" @@ -36,7 +36,7 @@ import ( kubefake "helm.sh/helm/v3/pkg/kube/fake" "helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/storage/driver" - "helm.sh/helm/v3/pkg/time" + helmtime "helm.sh/helm/v3/pkg/time" ) type nameTemplateTestCase struct { @@ -370,7 +370,7 @@ func TestInstallRelease_Wait_Interrupted(t *testing.T) { instAction := installAction(t) instAction.ReleaseName = "interrupted-release" failer := instAction.cfg.KubeClient.(*kubefake.FailingKubeClient) - failer.PrintingKubeClient.WaitDuration = 10 * gotime.Second + failer.WaitDuration = 10 * time.Second instAction.cfg.KubeClient = failer instAction.Wait = true vals := map[string]interface{}{} @@ -407,7 +407,7 @@ func TestInstallRelease_Wait_Interrupted(t *testing.T) { fmt.Printf("%s\n", slurp) }() - gotime.Sleep(2 * gotime.Second) + time.Sleep(2 * time.Second) p, _ := os.FindProcess(cmd.Process.Pid) if err := p.Signal(os.Interrupt); err != nil { @@ -483,7 +483,7 @@ func TestInstallRelease_Atomic_Interrupted(t *testing.T) { instAction := installAction(t) instAction.ReleaseName = "interrupted-release" failer := instAction.cfg.KubeClient.(*kubefake.FailingKubeClient) - failer.PrintingKubeClient.WaitDuration = 10 * gotime.Second + failer.WaitDuration = 10 * time.Second instAction.cfg.KubeClient = failer instAction.Atomic = true vals := map[string]interface{}{} @@ -527,7 +527,7 @@ func TestInstallRelease_Atomic_Interrupted(t *testing.T) { fmt.Printf("%s\n", slurp) }() - gotime.Sleep(2 * gotime.Second) + time.Sleep(2 * time.Second) p, _ := os.FindProcess(cmd.Process.Pid) if err := p.Signal(os.Interrupt); err != nil { @@ -743,32 +743,32 @@ func TestNameAndChartGenerateName(t *testing.T) { { "local filepath", "./chart", - fmt.Sprintf("chart-%d", time.Now().Unix()), + fmt.Sprintf("chart-%d", helmtime.Now().Unix()), }, { "dot filepath", ".", - fmt.Sprintf("chart-%d", time.Now().Unix()), + fmt.Sprintf("chart-%d", helmtime.Now().Unix()), }, { "empty filepath", "", - fmt.Sprintf("chart-%d", time.Now().Unix()), + fmt.Sprintf("chart-%d", helmtime.Now().Unix()), }, { "packaged chart", "chart.tgz", - fmt.Sprintf("chart-%d", time.Now().Unix()), + fmt.Sprintf("chart-%d", helmtime.Now().Unix()), }, { "packaged chart with .tar.gz extension", "chart.tar.gz", - fmt.Sprintf("chart-%d", time.Now().Unix()), + fmt.Sprintf("chart-%d", helmtime.Now().Unix()), }, { "packaged chart with local extension", "./chart.tgz", - fmt.Sprintf("chart-%d", time.Now().Unix()), + fmt.Sprintf("chart-%d", helmtime.Now().Unix()), }, } diff --git a/pkg/action/upgrade_test.go b/pkg/action/upgrade_test.go index 04af94e71..2aad26573 100644 --- a/pkg/action/upgrade_test.go +++ b/pkg/action/upgrade_test.go @@ -314,7 +314,7 @@ func TestUpgradeRelease_Interrupted_Wait(t *testing.T) { upAction.cfg.Releases.Create(rel) failer := upAction.cfg.KubeClient.(*kubefake.FailingKubeClient) - failer.PrintingKubeClient.WaitDuration = 10 * time.Second + failer.WaitDuration = 10 * time.Second upAction.cfg.KubeClient = failer upAction.Wait = true vals := map[string]interface{}{} @@ -377,7 +377,7 @@ func TestUpgradeRelease_Interrupted_Atomic(t *testing.T) { upAction.cfg.Releases.Create(rel) failer := upAction.cfg.KubeClient.(*kubefake.FailingKubeClient) - failer.PrintingKubeClient.WaitDuration = 5 * time.Second + failer.WaitDuration = 5 * time.Second upAction.cfg.KubeClient = failer upAction.Atomic = true vals := map[string]interface{}{} diff --git a/pkg/kube/fake/fake.go b/pkg/kube/fake/fake.go index a80961566..1aa390511 100644 --- a/pkg/kube/fake/fake.go +++ b/pkg/kube/fake/fake.go @@ -51,14 +51,12 @@ func (f *FailingKubeClient) Create(resources kube.ResourceList) (*kube.Result, e return f.PrintingKubeClient.Create(resources) } -// Wait returns the configured error if set or prints +// Waits the amount of time defined on f.WaitDuration, then returns the configured error if set or prints. func (f *FailingKubeClient) Wait(resources kube.ResourceList, d time.Duration) error { if f.WaitDuration != 0 { - d = f.WaitDuration + time.Sleep(f.WaitDuration) } - if f.WaitError != nil { - time.Sleep(d) return f.WaitError } return f.PrintingKubeClient.Wait(resources, d) diff --git a/pkg/kube/fake/printer.go b/pkg/kube/fake/printer.go index d08c71444..bdaae6be3 100644 --- a/pkg/kube/fake/printer.go +++ b/pkg/kube/fake/printer.go @@ -30,8 +30,7 @@ import ( // PrintingKubeClient implements KubeClient, but simply prints the reader to // the given output. type PrintingKubeClient struct { - Out io.Writer - WaitDuration time.Duration + Out io.Writer } // IsReachable checks if the cluster is reachable @@ -49,10 +48,6 @@ func (p *PrintingKubeClient) Create(resources kube.ResourceList) (*kube.Result, } func (p *PrintingKubeClient) Wait(resources kube.ResourceList, d time.Duration) error { - if p.WaitDuration != 0 { - time.Sleep(p.WaitDuration) - } - _, err := io.Copy(p.Out, bufferize(resources)) return err }