refactor test

Signed-off-by: Austin Abro <AustinAbro321@gmail.com>
pull/13604/head
Austin Abro 9 months ago
parent 807cc925f5
commit a6e5466942
No known key found for this signature in database
GPG Key ID: 92EB5159E403F9D6

@ -18,6 +18,7 @@ package kube // import "helm.sh/helm/v3/pkg/kube"
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"time" "time"
@ -82,15 +83,16 @@ func (w *kstatusWaiter) Wait(resourceList ResourceList, timeout time.Duration) e
// Only check parent context error, otherwise we would error when desired status is achieved. // Only check parent context error, otherwise we would error when desired status is achieved.
if ctx.Err() != nil { if ctx.Err() != nil {
var err error errs := []error{}
for _, id := range resources { for _, id := range resources {
rs := statusCollector.ResourceStatuses[id] rs := statusCollector.ResourceStatuses[id]
if rs.Status == status.CurrentStatus { if rs.Status == status.CurrentStatus {
continue continue
} }
err = fmt.Errorf("%s: %s not ready, status: %s", rs.Identifier.Name, rs.Identifier.GroupKind.Kind, rs.Status) errs = append(errs, fmt.Errorf("%s: %s not ready, status: %s", rs.Identifier.Name, rs.Identifier.GroupKind.Kind, rs.Status))
} }
return fmt.Errorf("not all resources ready: %w: %w", ctx.Err(), err) errs = append(errs, ctx.Err())
return errors.Join(errs...)
} }
return nil return nil
} }

@ -21,6 +21,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/meta"
@ -101,7 +102,7 @@ func TestKWaitJob(t *testing.T) {
{ {
name: "Job is not complete", name: "Job is not complete",
objYamls: []string{jobNoStatus}, objYamls: []string{jobNoStatus},
expectErrs: []error{errors.New("not all resources ready: context deadline exceeded: test: Job not ready, status: InProgress")}, expectErrs: []error{errors.New("test: Job not ready, status: InProgress"), errors.New("context deadline exceeded")},
}, },
{ {
name: "Pod is ready", name: "Pod is ready",
@ -109,10 +110,9 @@ func TestKWaitJob(t *testing.T) {
expectErrs: nil, expectErrs: nil,
}, },
{ {
name: "one of the pods never becomes ready", name: "one of the pods never becomes ready",
objYamls: []string{podNoStatus, podCurrent}, objYamls: []string{podNoStatus, podCurrent},
// TODO, make this better expectErrs: []error{errors.New("in-progress-pod: Pod not ready, status: InProgress"), errors.New("context deadline exceeded")},
expectErrs: []error{errors.New("not all resources ready: context deadline exceeded: in-progress-pod: Pod not ready, status: InProgress")},
}, },
} }
@ -134,12 +134,12 @@ func TestKWaitJob(t *testing.T) {
for _, podYaml := range tt.objYamls { for _, podYaml := range tt.objYamls {
m := make(map[string]interface{}) m := make(map[string]interface{})
err := yaml.Unmarshal([]byte(podYaml), &m) err := yaml.Unmarshal([]byte(podYaml), &m)
require.NoError(t, err) assert.NoError(t, err)
resource := &unstructured.Unstructured{Object: m} resource := &unstructured.Unstructured{Object: m}
objs = append(objs, resource) objs = append(objs, resource)
gvr := getGVR(t, fakeMapper, resource) gvr := getGVR(t, fakeMapper, resource)
err = fakeClient.Tracker().Create(gvr, resource, resource.GetNamespace()) err = fakeClient.Tracker().Create(gvr, resource, resource.GetNamespace())
require.NoError(t, err) assert.NoError(t, err)
} }
c.Waiter = &kstatusWaiter{ c.Waiter = &kstatusWaiter{
sw: statusWatcher, sw: statusWatcher,
@ -149,19 +149,16 @@ func TestKWaitJob(t *testing.T) {
resourceList := ResourceList{} resourceList := ResourceList{}
for _, obj := range objs { for _, obj := range objs {
list, err := c.Build(objBody(obj), false) list, err := c.Build(objBody(obj), false)
if err != nil { assert.NoError(t, err)
t.Fatal(err)
}
resourceList = append(resourceList, list...) resourceList = append(resourceList, list...)
} }
err := c.Wait(resourceList, time.Second*3) err := c.Wait(resourceList, time.Second*3)
if tt.expectErrs != nil { if tt.expectErrs != nil {
//TODO remove require assert.EqualError(t, err, errors.Join(tt.expectErrs...).Error())
require.EqualError(t, err, errors.Join(tt.expectErrs...).Error())
return return
} }
require.NoError(t, err) assert.NoError(t, err)
}) })
} }
} }

Loading…
Cancel
Save