Use testify and add a CRD to the `goodone` test case.

Signed-off-by: Zach Burgess <zachburg@google.com>
pull/31015/head
Zach Burgess 2 months ago
parent dadaff716a
commit 357957b0d3

@ -21,6 +21,8 @@ import (
"testing"
"time"
"github.com/stretchr/testify/assert"
chartutil "helm.sh/helm/v4/pkg/chart/v2/util"
"helm.sh/helm/v4/pkg/lint/support"
)
@ -114,12 +116,9 @@ func TestBadValues(t *testing.T) {
func TestBadCrdFile(t *testing.T) {
m := RunAll(badCrdFileDir, values, namespace).Messages
if len(m) < 1 {
t.Fatalf("All didn't fail with expected errors, got %#v", m)
}
if !strings.Contains(m[0].Err.Error(), "object kind is not 'CustomResourceDefinition'") {
t.Errorf("All didn't have the error for invalid CRD: %s", m[0].Err)
}
assert.Lenf(t, m, 2, "All didn't fail with expected errors, got %#v", m)
assert.ErrorContains(t, m[0].Err, "apiVersion is not in 'apiextensions.k8s.io'")
assert.ErrorContains(t, m[1].Err, "object kind is not 'CustomResourceDefinition'")
}
func TestGoodChart(t *testing.T) {

@ -24,6 +24,7 @@ import (
"io/fs"
"os"
"path/filepath"
"strings"
"k8s.io/apimachinery/pkg/util/yaml"
@ -41,7 +42,10 @@ func Crds(linter *support.Linter) {
return
}
linter.RunLinterRule(support.WarningSev, fpath, validateCrdsDir(crdsPath))
crdsDirValid := linter.RunLinterRule(support.WarningSev, fpath, validateCrdsDir(crdsPath))
if !crdsDirValid {
return
}
// Load chart and parse CRDs
chart, err := loader.Load(linter.ChartDir)
@ -53,8 +57,9 @@ func Crds(linter *support.Linter) {
}
/* Iterate over all the CRDs to check:
- It is a YAML file and not a template
- The kind is CustomResourceDefinition
1. It is a YAML file and not a template
2. The API version is apiextensions.k8s.io
3. The kind is CustomResourceDefinition
*/
for _, crd := range chart.CRDObjects() {
fileName := crd.Name
@ -75,6 +80,7 @@ func Crds(linter *support.Linter) {
return
}
linter.RunLinterRule(support.ErrorSev, fpath, validateCrdApiVersion(yamlStruct))
linter.RunLinterRule(support.ErrorSev, fpath, validateCrdKind(yamlStruct))
}
}
@ -92,6 +98,13 @@ func validateCrdsDir(crdsPath string) error {
return nil
}
func validateCrdApiVersion(obj *K8sYamlStruct) error {
if !strings.HasPrefix(obj.APIVersion, "apiextensions.k8s.io") {
return fmt.Errorf("apiVersion is not in 'apiextensions.k8s.io'")
}
return nil
}
func validateCrdKind(obj *K8sYamlStruct) error {
if obj.Kind != "CustomResourceDefinition" {
return fmt.Errorf("object kind is not 'CustomResourceDefinition'")

@ -17,13 +17,14 @@ limitations under the License.
package rules
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"helm.sh/helm/v4/pkg/lint/support"
)
const crdsTestBaseDir = "./testdata/withcrd"
const crdsTestBaseDir = "./testdata/goodone"
const invalidCrdsDir = "./testdata/invalidcrdsdir"
func TestCrdsDir(t *testing.T) {
@ -31,9 +32,7 @@ func TestCrdsDir(t *testing.T) {
Crds(&linter)
res := linter.Messages
if len(res) > 0 {
t.Fatalf("Expected no errors, got %d, %v", len(res), res)
}
assert.Emptyf(t, res, "Expected no errors, got %v", res)
}
func TestInvalidCrdsDir(t *testing.T) {
@ -41,11 +40,6 @@ func TestInvalidCrdsDir(t *testing.T) {
Crds(&linter)
res := linter.Messages
if len(res) != 1 {
t.Fatalf("Expected one error, got %d, %v", len(res), res)
}
if !strings.Contains(res[0].Err.Error(), "not a directory") {
t.Errorf("Unexpected error: %s", res[0])
}
assert.Lenf(t, res, 1, "Expected one error, got %d, %v", len(res), res)
assert.ErrorContains(t, res[0].Err, "not a directory")
}

@ -0,0 +1,2 @@
apiVersion: bad.k8s.io/v1beta1
kind: CustomResourceDefinition

@ -1,5 +0,0 @@
apiVersion: v1
name: withcrd
description: testing chart with a CRD
version: 199.44.12345-Alpha.1+cafe009
icon: http://riverrun.io
Loading…
Cancel
Save