Skip to content

Commit 1f0b4f2

Browse files
perdasilvaPer Goncalves da Silva
and
Per Goncalves da Silva
authored
add package name validation function (#1901)
Signed-off-by: Per Goncalves da Silva <[email protected]> Co-authored-by: Per Goncalves da Silva <[email protected]>
1 parent 115a122 commit 1f0b4f2

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

internal/operator-controller/applier/helm_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ func (mag *mockActionGetter) Reconcile(rel *release.Release) error {
101101
var (
102102
// required for unmockable call to convert.RegistryV1ToHelmChart
103103
validFS = fstest.MapFS{
104-
"metadata/annotations.yaml": &fstest.MapFile{Data: []byte("{}")},
104+
"metadata/annotations.yaml": &fstest.MapFile{Data: []byte(`annotations:
105+
operators.operatorframework.io.bundle.package.v1: my-package`)},
105106
"manifests": &fstest.MapFile{Data: []byte(`apiVersion: operators.operatorframework.io/v1alpha1
106107
kind: ClusterServiceVersion
107108
metadata:

internal/operator-controller/rukpak/convert/validator.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var RegistryV1BundleValidator = BundleValidator{
2525
CheckDeploymentSpecUniqueness,
2626
CheckCRDResourceUniqueness,
2727
CheckOwnedCRDExistence,
28+
CheckPackageNameNotEmpty,
2829
}
2930

3031
// CheckDeploymentSpecUniqueness checks that each strategy deployment spec in the csv has a unique name.
@@ -39,8 +40,7 @@ func CheckDeploymentSpecUniqueness(rv1 *RegistryV1) []error {
3940
deploymentNameSet.Insert(dep.Name)
4041
}
4142

42-
//nolint:prealloc
43-
var errs []error
43+
errs := make([]error, 0, len(duplicateDeploymentNames))
4444
for _, d := range slices.Sorted(slices.Values(duplicateDeploymentNames.UnsortedList())) {
4545
errs = append(errs, fmt.Errorf("cluster service version contains duplicate strategy deployment spec '%s'", d))
4646
}
@@ -54,15 +54,14 @@ func CheckOwnedCRDExistence(rv1 *RegistryV1) []error {
5454
crdsNames.Insert(crd.Name)
5555
}
5656

57-
//nolint:prealloc
58-
var errs []error
5957
missingCRDNames := sets.Set[string]{}
6058
for _, crd := range rv1.CSV.Spec.CustomResourceDefinitions.Owned {
6159
if !crdsNames.Has(crd.Name) {
6260
missingCRDNames.Insert(crd.Name)
6361
}
6462
}
6563

64+
errs := make([]error, 0, len(missingCRDNames))
6665
for _, crdName := range slices.Sorted(slices.Values(missingCRDNames.UnsortedList())) {
6766
errs = append(errs, fmt.Errorf("cluster service definition references owned custom resource definition '%s' not found in bundle", crdName))
6867
}
@@ -80,10 +79,17 @@ func CheckCRDResourceUniqueness(rv1 *RegistryV1) []error {
8079
crdsNames.Insert(crd.Name)
8180
}
8281

83-
//nolint:prealloc
84-
var errs []error
82+
errs := make([]error, 0, len(duplicateCRDNames))
8583
for _, crdName := range slices.Sorted(slices.Values(duplicateCRDNames.UnsortedList())) {
8684
errs = append(errs, fmt.Errorf("bundle contains duplicate custom resource definition '%s'", crdName))
8785
}
8886
return errs
8987
}
88+
89+
// CheckPackageNameNotEmpty checks that PackageName is not empty
90+
func CheckPackageNameNotEmpty(rv1 *RegistryV1) []error {
91+
if rv1.PackageName == "" {
92+
return []error{errors.New("package name is empty")}
93+
}
94+
return nil
95+
}

internal/operator-controller/rukpak/convert/validator_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func Test_BundleValidatorHasAllValidationFns(t *testing.T) {
1919
convert.CheckDeploymentSpecUniqueness,
2020
convert.CheckCRDResourceUniqueness,
2121
convert.CheckOwnedCRDExistence,
22+
convert.CheckPackageNameNotEmpty,
2223
}
2324
actualValidationFns := convert.RegistryV1BundleValidator
2425

@@ -60,6 +61,7 @@ func Test_CheckDeploymentSpecUniqueness(t *testing.T) {
6061
),
6162
),
6263
},
64+
expectedErrs: []error{},
6365
}, {
6466
name: "rejects bundles with duplicate deployment strategy spec names",
6567
bundle: &convert.RegistryV1{
@@ -114,6 +116,7 @@ func Test_CRDResourceUniqueness(t *testing.T) {
114116
{ObjectMeta: metav1.ObjectMeta{Name: "b.crd.something"}},
115117
},
116118
},
119+
expectedErrs: []error{},
117120
}, {
118121
name: "rejects bundles with duplicate custom resource definition resources",
119122
bundle: &convert.RegistryV1{CRDs: []apiextensionsv1.CustomResourceDefinition{
@@ -164,6 +167,7 @@ func Test_CheckOwnedCRDExistence(t *testing.T) {
164167
),
165168
),
166169
},
170+
expectedErrs: []error{},
167171
}, {
168172
name: "rejects bundles with missing owned custom resource definition resources",
169173
bundle: &convert.RegistryV1{
@@ -201,6 +205,32 @@ func Test_CheckOwnedCRDExistence(t *testing.T) {
201205
}
202206
}
203207

208+
func Test_CheckPackageNameNotEmpty(t *testing.T) {
209+
for _, tc := range []struct {
210+
name string
211+
bundle *convert.RegistryV1
212+
expectedErrs []error
213+
}{
214+
{
215+
name: "accepts bundles with non-empty package name",
216+
bundle: &convert.RegistryV1{
217+
PackageName: "not-empty",
218+
},
219+
}, {
220+
name: "rejects bundles with empty package name",
221+
bundle: &convert.RegistryV1{},
222+
expectedErrs: []error{
223+
errors.New("package name is empty"),
224+
},
225+
},
226+
} {
227+
t.Run(tc.name, func(t *testing.T) {
228+
errs := convert.CheckPackageNameNotEmpty(tc.bundle)
229+
require.Equal(t, tc.expectedErrs, errs)
230+
})
231+
}
232+
}
233+
204234
type csvOption func(version *v1alpha1.ClusterServiceVersion)
205235

206236
func withStrategyDeploymentSpecs(strategyDeploymentSpecs ...v1alpha1.StrategyDeploymentSpec) csvOption {

0 commit comments

Comments
 (0)