Mohammed Naser | 3dfc2a8 | 2024-02-20 23:51:39 -0500 | [diff] [blame] | 1 | package charts |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "os" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/stretchr/testify/assert" |
| 10 | "github.com/stretchr/testify/require" |
| 11 | "github.com/yannh/kubeconform/pkg/validator" |
| 12 | "helm.sh/helm/v3/pkg/action" |
| 13 | "helm.sh/helm/v3/pkg/chart/loader" |
| 14 | "helm.sh/helm/v3/pkg/chartutil" |
| 15 | ) |
| 16 | |
| 17 | var ( |
| 18 | KUBERNETES_VERSIONS = []string{ |
Mohammed Naser | 3dfc2a8 | 2024-02-20 23:51:39 -0500 | [diff] [blame] | 19 | "1.23.0", |
| 20 | "1.24.0", |
| 21 | "1.25.0", |
| 22 | "1.26.0", |
| 23 | "1.27.0", |
| 24 | "1.28.0", |
| 25 | } |
| 26 | ) |
| 27 | |
| 28 | func TestKubeconform(t *testing.T) { |
| 29 | t.Parallel() |
| 30 | |
| 31 | files, err := os.ReadDir("./") |
| 32 | require.NoError(t, err) |
| 33 | require.NotEmpty(t, files) |
| 34 | |
| 35 | schemas := []string{ |
| 36 | "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/{{ .NormalizedKubernetesVersion }}-standalone{{ .StrictSuffix }}/{{ .ResourceKind }}{{ .KindSuffix }}.json", |
| 37 | "https://raw.githubusercontent.com/datreeio/CRDs-catalog/main/{{.Group}}/{{.ResourceKind}}_{{.ResourceAPIVersion}}.json", |
| 38 | } |
| 39 | |
| 40 | var clients map[string]*action.Install = make(map[string]*action.Install) |
| 41 | for _, version := range KUBERNETES_VERSIONS { |
| 42 | kubeVersion, err := chartutil.ParseKubeVersion(version) |
| 43 | require.NoError(t, err) |
| 44 | |
| 45 | client := action.NewInstall(&action.Configuration{}) |
| 46 | client.ClientOnly = true |
| 47 | client.DryRun = true |
| 48 | client.ReleaseName = "kubeconform" |
| 49 | client.Namespace = "default" |
| 50 | client.IncludeCRDs = true |
| 51 | client.KubeVersion = kubeVersion |
| 52 | |
| 53 | clients[version] = client |
| 54 | } |
| 55 | |
| 56 | var validators map[string]validator.Validator = make(map[string]validator.Validator) |
| 57 | for _, version := range KUBERNETES_VERSIONS { |
| 58 | opts := validator.Opts{ |
| 59 | KubernetesVersion: version, |
| 60 | SkipKinds: map[string]struct{}{ |
vexxhost-bot | 09893a1 | 2024-10-31 10:46:10 -0400 | [diff] [blame] | 61 | "CephBlockPool": {}, |
| 62 | "CephCluster": {}, |
| 63 | "CephFilesystem": {}, |
| 64 | "CephObjectStore": {}, |
| 65 | "CephFilesystemSubVolumeGroup": {}, |
Mohammed Naser | 3dfc2a8 | 2024-02-20 23:51:39 -0500 | [diff] [blame] | 66 | "apiextensions.k8s.io/v1/CustomResourceDefinition": {}, |
| 67 | }, |
| 68 | Strict: true, |
| 69 | } |
| 70 | |
| 71 | v, err := validator.New(schemas, opts) |
| 72 | require.NoError(t, err) |
| 73 | |
| 74 | validators[version] = v |
| 75 | } |
| 76 | |
| 77 | for _, file := range files { |
| 78 | if !file.IsDir() { |
| 79 | continue |
| 80 | } |
vexxhost-bot | 09893a1 | 2024-10-31 10:46:10 -0400 | [diff] [blame] | 81 | if file.Name() == "patches" { |
| 82 | continue |
| 83 | } |
Mohammed Naser | 3dfc2a8 | 2024-02-20 23:51:39 -0500 | [diff] [blame] | 84 | |
| 85 | t.Run(file.Name(), func(t *testing.T) { |
| 86 | chart, err := loader.LoadDir(file.Name()) |
| 87 | require.NoError(t, err) |
| 88 | |
| 89 | t.Parallel() |
| 90 | |
| 91 | for _, version := range KUBERNETES_VERSIONS { |
| 92 | t.Run(version, func(t *testing.T) { |
| 93 | client := clients[version] |
| 94 | v := validators[version] |
| 95 | |
| 96 | t.Parallel() |
| 97 | |
vexxhost-bot | 09893a1 | 2024-10-31 10:46:10 -0400 | [diff] [blame] | 98 | rel, err := client.Run( |
| 99 | chart, |
| 100 | // NOTE(okozachenko1203): loki helm chart default values doesn't work. |
| 101 | map[string]interface{}{ |
| 102 | "loki": map[string]interface{}{ |
| 103 | "storage": map[string]interface{}{ |
| 104 | "bucketNames": map[string]string{ |
| 105 | "chunks": "FIXME", |
| 106 | "ruler": "FIXME", |
| 107 | "admin": "FIXME", |
| 108 | }, |
| 109 | }, |
| 110 | "useTestSchema": true, |
| 111 | }, |
| 112 | }, |
| 113 | ) |
Mohammed Naser | 3dfc2a8 | 2024-02-20 23:51:39 -0500 | [diff] [blame] | 114 | require.NoError(t, err) |
| 115 | |
| 116 | manifests := io.NopCloser(strings.NewReader(rel.Manifest)) |
| 117 | for _, res := range v.Validate(chart.Name(), manifests) { |
| 118 | require.NoError(t, res.Err) |
| 119 | assert.Empty(t, res.ValidationErrors) |
| 120 | } |
| 121 | }) |
| 122 | } |
| 123 | }) |
| 124 | } |
| 125 | } |