fix(install): check lenght and file nil, add tests

Signed-off-by: Manuel Alonso <434575+manute@users.noreply.github.com>
(cherry picked from commit 52235cc0bf)
release-4.1
Manuel Alonso 4 months ago committed by Scott Rigby
parent 10d6067265
commit fdadff59eb
No known key found for this signature in database
GPG Key ID: C7C6FBB5B91C1155

@ -181,13 +181,17 @@ func (i *Install) installCRDs(crds []chart.CRD) error {
// We do these one file at a time in the order they were read.
totalItems := []*resource.Info{}
for _, obj := range crds {
if obj.File == nil {
return fmt.Errorf("failed to install CRD %s: file is empty", obj.Name)
}
// Read in the resources
res, err := i.cfg.KubeClient.Build(bytes.NewBuffer(obj.File.Data), false)
if err != nil {
return fmt.Errorf("failed to install CRD %s: %w", obj.Name, err)
}
if res == nil {
if len(res) == 0 {
return fmt.Errorf("failed to install CRD %s: resources are empty", obj.Name)
}

@ -1208,3 +1208,42 @@ func TestInstallRelease_WaitOptionsPassedDownstream(t *testing.T) {
// Verify that WaitOptions were passed to GetWaiter
is.NotEmpty(failer.RecordedWaitOptions, "WaitOptions should be passed to GetWaiter")
}
func TestInstallCRDs_CheckNilErrors(t *testing.T) {
tests := []struct {
name string
input []chart.CRD
}{
{
name: "only one crd with file nil",
input: []chart.CRD{
{Name: "one", File: nil},
},
},
{
name: "only one crd with its file data nil",
input: []chart.CRD{
{Name: "one", File: &common.File{Name: "crds/foo.yaml", Data: nil}},
},
},
{
name: "at least a crd with its file data nil",
input: []chart.CRD{
{Name: "one", File: &common.File{Name: "crds/foo.yaml", Data: []byte("data")}},
{Name: "two", File: &common.File{Name: "crds/foo.yaml", Data: nil}},
{Name: "three", File: &common.File{Name: "crds/foo.yaml", Data: []byte("data")}},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
instAction := installAction(t)
err := instAction.installCRDs(tt.input)
if err == nil {
t.Errorf("got error expected nil")
}
})
}
}

Loading…
Cancel
Save