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 <moser.sts@gmail.com>
pull/9180/head
Stephane Moser 3 years ago
parent 2164e3f26c
commit 7bfb2a3602

@ -26,7 +26,7 @@ import (
"regexp" "regexp"
"strings" "strings"
"testing" "testing"
gotime "time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -36,7 +36,7 @@ import (
kubefake "helm.sh/helm/v3/pkg/kube/fake" kubefake "helm.sh/helm/v3/pkg/kube/fake"
"helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/storage/driver" "helm.sh/helm/v3/pkg/storage/driver"
"helm.sh/helm/v3/pkg/time" helmtime "helm.sh/helm/v3/pkg/time"
) )
type nameTemplateTestCase struct { type nameTemplateTestCase struct {
@ -370,7 +370,7 @@ func TestInstallRelease_Wait_Interrupted(t *testing.T) {
instAction := installAction(t) instAction := installAction(t)
instAction.ReleaseName = "interrupted-release" instAction.ReleaseName = "interrupted-release"
failer := instAction.cfg.KubeClient.(*kubefake.FailingKubeClient) failer := instAction.cfg.KubeClient.(*kubefake.FailingKubeClient)
failer.PrintingKubeClient.WaitDuration = 10 * gotime.Second failer.WaitDuration = 10 * time.Second
instAction.cfg.KubeClient = failer instAction.cfg.KubeClient = failer
instAction.Wait = true instAction.Wait = true
vals := map[string]interface{}{} vals := map[string]interface{}{}
@ -407,7 +407,7 @@ func TestInstallRelease_Wait_Interrupted(t *testing.T) {
fmt.Printf("%s\n", slurp) fmt.Printf("%s\n", slurp)
}() }()
gotime.Sleep(2 * gotime.Second) time.Sleep(2 * time.Second)
p, _ := os.FindProcess(cmd.Process.Pid) p, _ := os.FindProcess(cmd.Process.Pid)
if err := p.Signal(os.Interrupt); err != nil { if err := p.Signal(os.Interrupt); err != nil {
@ -483,7 +483,7 @@ func TestInstallRelease_Atomic_Interrupted(t *testing.T) {
instAction := installAction(t) instAction := installAction(t)
instAction.ReleaseName = "interrupted-release" instAction.ReleaseName = "interrupted-release"
failer := instAction.cfg.KubeClient.(*kubefake.FailingKubeClient) failer := instAction.cfg.KubeClient.(*kubefake.FailingKubeClient)
failer.PrintingKubeClient.WaitDuration = 10 * gotime.Second failer.WaitDuration = 10 * time.Second
instAction.cfg.KubeClient = failer instAction.cfg.KubeClient = failer
instAction.Atomic = true instAction.Atomic = true
vals := map[string]interface{}{} vals := map[string]interface{}{}
@ -527,7 +527,7 @@ func TestInstallRelease_Atomic_Interrupted(t *testing.T) {
fmt.Printf("%s\n", slurp) fmt.Printf("%s\n", slurp)
}() }()
gotime.Sleep(2 * gotime.Second) time.Sleep(2 * time.Second)
p, _ := os.FindProcess(cmd.Process.Pid) p, _ := os.FindProcess(cmd.Process.Pid)
if err := p.Signal(os.Interrupt); err != nil { if err := p.Signal(os.Interrupt); err != nil {
@ -743,32 +743,32 @@ func TestNameAndChartGenerateName(t *testing.T) {
{ {
"local filepath", "local filepath",
"./chart", "./chart",
fmt.Sprintf("chart-%d", time.Now().Unix()), fmt.Sprintf("chart-%d", helmtime.Now().Unix()),
}, },
{ {
"dot filepath", "dot filepath",
".", ".",
fmt.Sprintf("chart-%d", time.Now().Unix()), fmt.Sprintf("chart-%d", helmtime.Now().Unix()),
}, },
{ {
"empty filepath", "empty filepath",
"", "",
fmt.Sprintf("chart-%d", time.Now().Unix()), fmt.Sprintf("chart-%d", helmtime.Now().Unix()),
}, },
{ {
"packaged chart", "packaged chart",
"chart.tgz", "chart.tgz",
fmt.Sprintf("chart-%d", time.Now().Unix()), fmt.Sprintf("chart-%d", helmtime.Now().Unix()),
}, },
{ {
"packaged chart with .tar.gz extension", "packaged chart with .tar.gz extension",
"chart.tar.gz", "chart.tar.gz",
fmt.Sprintf("chart-%d", time.Now().Unix()), fmt.Sprintf("chart-%d", helmtime.Now().Unix()),
}, },
{ {
"packaged chart with local extension", "packaged chart with local extension",
"./chart.tgz", "./chart.tgz",
fmt.Sprintf("chart-%d", time.Now().Unix()), fmt.Sprintf("chart-%d", helmtime.Now().Unix()),
}, },
} }

@ -314,7 +314,7 @@ func TestUpgradeRelease_Interrupted_Wait(t *testing.T) {
upAction.cfg.Releases.Create(rel) upAction.cfg.Releases.Create(rel)
failer := upAction.cfg.KubeClient.(*kubefake.FailingKubeClient) failer := upAction.cfg.KubeClient.(*kubefake.FailingKubeClient)
failer.PrintingKubeClient.WaitDuration = 10 * time.Second failer.WaitDuration = 10 * time.Second
upAction.cfg.KubeClient = failer upAction.cfg.KubeClient = failer
upAction.Wait = true upAction.Wait = true
vals := map[string]interface{}{} vals := map[string]interface{}{}
@ -377,7 +377,7 @@ func TestUpgradeRelease_Interrupted_Atomic(t *testing.T) {
upAction.cfg.Releases.Create(rel) upAction.cfg.Releases.Create(rel)
failer := upAction.cfg.KubeClient.(*kubefake.FailingKubeClient) failer := upAction.cfg.KubeClient.(*kubefake.FailingKubeClient)
failer.PrintingKubeClient.WaitDuration = 5 * time.Second failer.WaitDuration = 5 * time.Second
upAction.cfg.KubeClient = failer upAction.cfg.KubeClient = failer
upAction.Atomic = true upAction.Atomic = true
vals := map[string]interface{}{} vals := map[string]interface{}{}

@ -51,14 +51,12 @@ func (f *FailingKubeClient) Create(resources kube.ResourceList) (*kube.Result, e
return f.PrintingKubeClient.Create(resources) 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 { func (f *FailingKubeClient) Wait(resources kube.ResourceList, d time.Duration) error {
if f.WaitDuration != 0 { if f.WaitDuration != 0 {
d = f.WaitDuration time.Sleep(f.WaitDuration)
} }
if f.WaitError != nil { if f.WaitError != nil {
time.Sleep(d)
return f.WaitError return f.WaitError
} }
return f.PrintingKubeClient.Wait(resources, d) return f.PrintingKubeClient.Wait(resources, d)

@ -30,8 +30,7 @@ import (
// PrintingKubeClient implements KubeClient, but simply prints the reader to // PrintingKubeClient implements KubeClient, but simply prints the reader to
// the given output. // the given output.
type PrintingKubeClient struct { type PrintingKubeClient struct {
Out io.Writer Out io.Writer
WaitDuration time.Duration
} }
// IsReachable checks if the cluster is reachable // 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 { 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)) _, err := io.Copy(p.Out, bufferize(resources))
return err return err
} }

Loading…
Cancel
Save