Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions pkg/validation/internal/operatorhub.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package internal

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/mail"
"net/url"
"os"
"path/filepath"
"strings"

"github.com/blang/semver"
Expand Down Expand Up @@ -148,12 +152,57 @@ func validateHubCSVSpec(csv v1alpha1.ClusterServiceVersion) []error {
if categories, ok := csv.ObjectMeta.Annotations["categories"]; ok {
categorySlice := strings.Split(categories, ",")

for _, category := range categorySlice {
if _, ok := validCategories[category]; !ok {
errs = append(errs, fmt.Errorf("csv.Metadata.Annotations.Categories %s is not a valid category", category))
// use custom categories for validation if provided
customCategoriesPath := os.Getenv("OPERATOR_BUNDLE_CATEGORIES")
if customCategoriesPath != "" {
customCategories, err := extractCategories(customCategoriesPath)
if err != nil {
errs = append(errs, fmt.Errorf("could not extract custom categories from categories %#v: %s", customCategories, err))
return errs
}
for _, category := range categorySlice {
if _, ok := customCategories[category]; !ok {
errs = append(errs, fmt.Errorf("csv.Metadata.Annotations.Categories %s is not a valid custom category", category))
}
}
} else {
// use default categories
for _, category := range categorySlice {
if _, ok := validCategories[category]; !ok {
errs = append(errs, fmt.Errorf("csv.Metadata.Annotations.Categories %s is not a valid category", category))
}
}
}
}

return errs
}

type categories struct {
Contents []string `json:"categories"`
}

// extractCategories reads a custom categories file and returns the contents in a map[string]struct{}
func extractCategories(path string) (map[string]struct{}, error) {
path, err := filepath.Abs(path)
if err != nil {
return nil, fmt.Errorf("finding category file: %w", err)
}

data, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("reading category file: %w", err)
}

cat := categories{}
err = json.Unmarshal(data, &cat)
if err != nil {
return nil, fmt.Errorf("unmarshaling category file: %w", err)
}

customCategories := make(map[string]struct{})
for _, c := range cat.Contents {
customCategories[c] = struct{}{}
}
return customCategories, nil
}
71 changes: 71 additions & 0 deletions pkg/validation/internal/operatorhub_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"os"
"testing"

"github.com/operator-framework/api/pkg/manifests"
Expand Down Expand Up @@ -58,3 +59,73 @@ func TestValidateBundleOperatorHub(t *testing.T) {
}
}
}

func TestCustomCategories(t *testing.T) {
var table = []struct {
description string
directory string
hasError bool
errStrings []string
custom bool
}{
{
description: "valid bundle custom categories",
directory: "./testdata/valid_bundle_custom_categories",
hasError: false,
custom: true,
},
{
description: "valid bundle standard categories",
directory: "./testdata/valid_bundle",
hasError: false,
custom: false,
},
}

for _, tt := range table {
t.Logf("%s", tt.description)
if tt.custom {
os.Setenv("OPERATOR_BUNDLE_CATEGORIES", "./testdata/categories.json")
} else {
os.Setenv("OPERATOR_BUNDLE_CATEGORIES", "")
}

// Validate the bundle object
bundle, err := manifests.GetBundleFromDir(tt.directory)
require.NoError(t, err)

results := OperatorHubValidator.Validate(bundle)

if len(results) > 0 {
require.Equal(t, results[0].HasError(), tt.hasError)
if results[0].HasError() {
require.Equal(t, len(tt.errStrings), len(results[0].Errors))
for _, err := range results[0].Errors {
errString := err.Error()
require.Contains(t, tt.errStrings, errString)
}
}
}
}
}

func TestExtractCategories(t *testing.T) {
path := "./testdata/categories.json"
categories, err := extractCategories(path)
if err != nil {
t.Fatalf("extracting categories.json: %s", err)
}

expected := map[string]struct{}{
"Cloud Pak": {},
"Registry": {},
"MyCoolThing": {},
"This/Or & That": {},
}

for key := range categories {
if _, ok := expected[key]; !ok {
t.Fatalf("did not find key %s", key)
}
}
}
8 changes: 8 additions & 0 deletions pkg/validation/internal/testdata/categories.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"categories":[
"Cloud Pak",
"Registry",
"MyCoolThing",
"This/Or & That"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: etcdbackups.etcd.database.coreos.com
spec:
group: etcd.database.coreos.com
names:
kind: EtcdBackup
listKind: EtcdBackupList
plural: etcdbackups
singular: etcdbackup
scope: Namespaced
version: v1beta2
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: etcdclusters.etcd.database.coreos.com
spec:
group: etcd.database.coreos.com
names:
kind: EtcdCluster
listKind: EtcdClusterList
plural: etcdclusters
shortNames:
- etcdclus
- etcd
singular: etcdcluster
scope: Namespaced
version: v1beta2
Loading