Skip to content

Commit efda050

Browse files
committed
Add bundle validation test cases
Signed-off-by: Vu Dinh <[email protected]>
1 parent 80fd2f1 commit efda050

13 files changed

+1063
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13+
14+
### macOS template
15+
# General
16+
*.DS_Store
17+
.AppleDouble
18+
.LSOverride
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package internal
2+
3+
import (
4+
"io/ioutil"
5+
"path/filepath"
6+
"strings"
7+
"testing"
8+
9+
"github.com/operator-framework/api/pkg/validation/errors"
10+
"github.com/operator-framework/operator-registry/pkg/registry"
11+
"github.com/stretchr/testify/require"
12+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
13+
14+
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
15+
)
16+
17+
func TestValidateBundle(t *testing.T) {
18+
var table = []struct {
19+
description string
20+
directory string
21+
hasError bool
22+
errString string
23+
}{
24+
{
25+
description: "registryv1 bundle/valid bundle",
26+
directory: "./testdata/valid_bundle",
27+
hasError: false,
28+
},
29+
{
30+
description: "registryv1 bundle/valid bundle",
31+
directory: "./testdata/invalid_bundle",
32+
hasError: true,
33+
errString: `owned CRD "etcdclusters.etcd.database.coreos.com" not found in bundle`,
34+
},
35+
{
36+
description: "registryv1 bundle/valid bundle",
37+
directory: "./testdata/invalid_bundle_2",
38+
hasError: true,
39+
errString: `owned CRD "etcdclusters.etcd.database.coreos.com" is present in bundle "test" but not defined in CSV`,
40+
},
41+
}
42+
43+
for _, tt := range table {
44+
results := []errors.ManifestResult{}
45+
unstObjs := []*unstructured.Unstructured{}
46+
47+
// Read all files in manifests directory
48+
items, err := ioutil.ReadDir(tt.directory)
49+
require.NoError(t, err, "Unable to read directory: %s", tt.description)
50+
51+
for _, item := range items {
52+
fileWithPath := filepath.Join(tt.directory, item.Name())
53+
data, err := ioutil.ReadFile(fileWithPath)
54+
require.NoError(t, err, "Unable to read file: %s", fileWithPath)
55+
56+
dec := k8syaml.NewYAMLOrJSONDecoder(strings.NewReader(string(data)), 30)
57+
k8sFile := &unstructured.Unstructured{}
58+
err = dec.Decode(k8sFile)
59+
require.NoError(t, err, "Unable to decode file: %s", fileWithPath)
60+
61+
unstObjs = append(unstObjs, k8sFile)
62+
}
63+
64+
// Validate the bundle object
65+
bundle := registry.NewBundle("test", "", "", unstObjs...)
66+
results = BundleValidator.Validate(bundle)
67+
68+
if len(results) > 0 {
69+
require.Equal(t, results[0].HasError(), tt.hasError)
70+
if results[0].HasError() {
71+
require.Contains(t, results[0].Errors[0].Error(), tt.errString)
72+
}
73+
}
74+
}
75+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: etcdbackups.etcd.database.coreos.com
5+
spec:
6+
group: etcd.database.coreos.com
7+
names:
8+
kind: EtcdBackup
9+
listKind: EtcdBackupList
10+
plural: etcdbackups
11+
singular: etcdbackup
12+
scope: Namespaced
13+
version: v1beta2

pkg/validation/internal/testdata/invalid_bundle/etcdoperator.v0.9.4.clusterserviceversion.yaml

Lines changed: 309 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: etcdrestores.etcd.database.coreos.com
5+
spec:
6+
group: etcd.database.coreos.com
7+
names:
8+
kind: EtcdRestore
9+
listKind: EtcdRestoreList
10+
plural: etcdrestores
11+
singular: etcdrestore
12+
scope: Namespaced
13+
version: v1beta2
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: etcdbackups.etcd.database.coreos.com
5+
spec:
6+
group: etcd.database.coreos.com
7+
names:
8+
kind: EtcdBackup
9+
listKind: EtcdBackupList
10+
plural: etcdbackups
11+
singular: etcdbackup
12+
scope: Namespaced
13+
version: v1beta2
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: etcdclusters.etcd.database.coreos.com
5+
spec:
6+
group: etcd.database.coreos.com
7+
names:
8+
kind: EtcdCluster
9+
listKind: EtcdClusterList
10+
plural: etcdclusters
11+
shortNames:
12+
- etcdclus
13+
- etcd
14+
singular: etcdcluster
15+
scope: Namespaced
16+
version: v1beta2

pkg/validation/internal/testdata/invalid_bundle_2/etcdoperator.v0.9.4.clusterserviceversion.yaml

Lines changed: 254 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: etcdrestores.etcd.database.coreos.com
5+
spec:
6+
group: etcd.database.coreos.com
7+
names:
8+
kind: EtcdRestore
9+
listKind: EtcdRestoreList
10+
plural: etcdrestores
11+
singular: etcdrestore
12+
scope: Namespaced
13+
version: v1beta2
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: etcdbackups.etcd.database.coreos.com
5+
spec:
6+
group: etcd.database.coreos.com
7+
names:
8+
kind: EtcdBackup
9+
listKind: EtcdBackupList
10+
plural: etcdbackups
11+
singular: etcdbackup
12+
scope: Namespaced
13+
version: v1beta2

0 commit comments

Comments
 (0)