Add test for Upgrade Release Interrupted

Implement timer in the fake.go and printer.go to simulate the wait period
Add test Upgrade Release when it is interruped with SIGINT

Signed-off-by: Stephane Moser <moser.sts@gmail.com>
pull/9180/head
Stephane Moser 4 years ago
parent 3434053d38
commit 2fa339b25d

@ -18,7 +18,11 @@ package action
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"testing"
gotime "time"
"helm.sh/helm/v3/pkg/chart"
@ -296,3 +300,66 @@ func TestUpgradeRelease_Pending(t *testing.T) {
_, err := upAction.Run(rel.Name, buildChart(), vals)
req.Contains(err.Error(), "progress", err)
}
func TestUpgradeRelease_Interrupted_Wait(t *testing.T) {
if os.Getenv("HANDLE_SIGINT") == "1" {
t.Run("Execute TestUpgradeRelease_Interrupted_Wait", func(t *testing.T) {
is := assert.New(t)
req := require.New(t)
upAction := upgradeAction(t)
rel := releaseStub()
rel.Name = "interrupted-release"
rel.Info.Status = release.StatusDeployed
upAction.cfg.Releases.Create(rel)
failer := upAction.cfg.KubeClient.(*kubefake.FailingKubeClient)
failer.PrintingKubeClient.WaitDuration = 10 * gotime.Second
upAction.cfg.KubeClient = failer
upAction.Wait = true
vals := map[string]interface{}{}
res, err := upAction.Run(rel.Name, buildChart(), vals)
req.Error(err)
is.Contains(res.Info.Description, "Upgrade \"interrupted-release\" failed: SIGTERM or SIGINT received, release failed")
is.Equal(res.Info.Status, release.StatusFailed)
})
return
}
t.Run("Setup TestUpgradeRelease_Interrupted_Wait", func(t *testing.T) {
cmd := exec.Command(os.Args[0], "-test.run=TestUpgradeRelease_Interrupted_Wait")
cmd.Env = append(os.Environ(), "HANDLE_SIGINT=1")
stdout, err := cmd.StdoutPipe()
if err != nil {
t.Fatal(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
t.Fatal(err)
}
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
go func() {
slurp, _ := ioutil.ReadAll(stdout)
fmt.Printf("%s\n", slurp)
}()
go func() {
slurp, _ := ioutil.ReadAll(stderr)
fmt.Printf("%s\n", slurp)
}()
gotime.Sleep(2 * gotime.Second)
p, _ := os.FindProcess(cmd.Process.Pid)
if err := p.Signal(os.Interrupt); err != nil {
t.Fatal(err)
}
if err := cmd.Wait(); err != nil {
t.FailNow()
}
})
}

@ -40,6 +40,7 @@ type FailingKubeClient struct {
BuildError error
BuildUnstructuredError error
WaitAndGetCompletedPodPhaseError error
WaitDuration time.Duration
}
// Create returns the configured error if set or prints
@ -52,7 +53,12 @@ func (f *FailingKubeClient) Create(resources kube.ResourceList) (*kube.Result, e
// Wait 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
}
if f.WaitError != nil {
time.Sleep(d)
return f.WaitError
}
return f.PrintingKubeClient.Wait(resources, d)

@ -31,6 +31,7 @@ import (
// the given output.
type PrintingKubeClient struct {
Out io.Writer
WaitDuration time.Duration
}
// IsReachable checks if the cluster is reachable
@ -47,7 +48,11 @@ func (p *PrintingKubeClient) Create(resources kube.ResourceList) (*kube.Result,
return &kube.Result{Created: resources}, nil
}
func (p *PrintingKubeClient) Wait(resources kube.ResourceList, _ 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))
return err
}

Loading…
Cancel
Save