-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Joe Lanford <joe.lanford@gmail.com>
- Loading branch information
1 parent
b6a731b
commit 29c9db1
Showing
13 changed files
with
508 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"bytes" | ||
"embed" | ||
"errors" | ||
"io" | ||
"testing" | ||
"testing/iotest" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLoadFilterConfiguration(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
openFile func() (io.Reader, error) | ||
assertion func(*testing.T, *FilterConfiguration, error) | ||
}{ | ||
{ | ||
name: "ReadFailure", | ||
openFile: func() (io.Reader, error) { return iotest.ErrReader(errors.New("read failure")), nil }, | ||
assertion: func(t *testing.T, cfg *FilterConfiguration, err error) { | ||
assert.Nil(t, cfg) | ||
require.Error(t, err) | ||
assert.ErrorContains(t, err, "read failure") | ||
}, | ||
}, | ||
{ | ||
name: "ParseFailure", | ||
openFile: func() (io.Reader, error) { return bytes.NewReader([]byte(`{`)), nil }, | ||
assertion: func(t *testing.T, cfg *FilterConfiguration, err error) { | ||
assert.Nil(t, cfg) | ||
require.Error(t, err) | ||
assert.ErrorContains(t, err, "yaml: line 1: did not find expected node content") | ||
}, | ||
}, | ||
{ | ||
name: "WrongAPIVersion", | ||
openFile: func() (io.Reader, error) { return testdataFS.Open("testdata/invalid_apiversion.yaml") }, | ||
assertion: func(t *testing.T, cfg *FilterConfiguration, err error) { | ||
assert.Nil(t, cfg) | ||
require.Error(t, err) | ||
assert.ErrorContains(t, err, "unexpected API version") | ||
}, | ||
}, | ||
{ | ||
name: "WrongKind", | ||
openFile: func() (io.Reader, error) { return testdataFS.Open("testdata/invalid_kind.yaml") }, | ||
assertion: func(t *testing.T, cfg *FilterConfiguration, err error) { | ||
assert.Nil(t, cfg) | ||
require.Error(t, err) | ||
assert.ErrorContains(t, err, "unexpected kind") | ||
}, | ||
}, | ||
{ | ||
name: "NoPackages", | ||
openFile: func() (io.Reader, error) { return testdataFS.Open("testdata/invalid_nopackages.yaml") }, | ||
assertion: func(t *testing.T, cfg *FilterConfiguration, err error) { | ||
assert.Nil(t, cfg) | ||
require.Error(t, err) | ||
assert.ErrorContains(t, err, "at least one package must be specified") | ||
}, | ||
}, | ||
{ | ||
name: "MissingPackageName", | ||
openFile: func() (io.Reader, error) { return testdataFS.Open("testdata/invalid_missingpackagename.yaml") }, | ||
assertion: func(t *testing.T, cfg *FilterConfiguration, err error) { | ||
assert.Nil(t, cfg) | ||
require.Error(t, err) | ||
assert.ErrorContains(t, err, `package "" at index [1] is invalid: name must be specified`) | ||
}, | ||
}, | ||
{ | ||
name: "MissingChannelName", | ||
openFile: func() (io.Reader, error) { return testdataFS.Open("testdata/invalid_missingchannelname.yaml") }, | ||
assertion: func(t *testing.T, cfg *FilterConfiguration, err error) { | ||
assert.Nil(t, cfg) | ||
require.Error(t, err) | ||
assert.ErrorContains(t, err, `package "bar" at index [1] is invalid: channel "" at index [1] is invalid: name must be specified`) | ||
}, | ||
}, | ||
{ | ||
name: "MultipleErrors", | ||
openFile: func() (io.Reader, error) { return testdataFS.Open("testdata/invalid_multipleerrors.yaml") }, | ||
assertion: func(t *testing.T, cfg *FilterConfiguration, err error) { | ||
assert.Nil(t, cfg) | ||
require.Error(t, err) | ||
assert.ErrorContains(t, err, `unexpected API version`) | ||
assert.ErrorContains(t, err, "unexpected kind") | ||
assert.ErrorContains(t, err, `package "" at index [2] is invalid: name must be specified`) | ||
assert.ErrorContains(t, err, `package "" at index [2] is invalid: channel "" at index [0] is invalid: name must be specified`) | ||
assert.ErrorContains(t, err, `package "" at index [2] is invalid: channel "" at index [1] is invalid: name must be specified`) | ||
assert.ErrorContains(t, err, `package "" at index [3] is invalid: name must be specified`) | ||
assert.ErrorContains(t, err, `package "" at index [3] is invalid: channel "" at index [0] is invalid: name must be specified`) | ||
assert.ErrorContains(t, err, `package "" at index [3] is invalid: channel "" at index [1] is invalid: name must be specified`) | ||
assert.ErrorContains(t, err, `package "baz" at index [4] is invalid: channel "" at index [0] is invalid: name must be specified`) | ||
}, | ||
}, | ||
{ | ||
name: "Valid", | ||
openFile: func() (io.Reader, error) { return testdataFS.Open("testdata/valid.yaml") }, | ||
assertion: func(t *testing.T, cfg *FilterConfiguration, err error) { | ||
require.NoError(t, err) | ||
require.NotNil(t, cfg) | ||
assert.Equal(t, "olm.operatorframework.io/v1alpha1", cfg.APIVersion) | ||
assert.Equal(t, "FilterConfiguration", cfg.Kind) | ||
assert.Len(t, cfg.Packages, 2) | ||
assert.Equal(t, "foo", cfg.Packages[0].Name) | ||
assert.Len(t, cfg.Packages[0].Channels, 0) | ||
assert.Equal(t, "", cfg.Packages[0].DefaultChannel) | ||
assert.Equal(t, "bar", cfg.Packages[1].Name) | ||
assert.Len(t, cfg.Packages[1].Channels, 2) | ||
assert.Equal(t, "bar-channel1", cfg.Packages[1].DefaultChannel) | ||
assert.Equal(t, "bar-channel1", cfg.Packages[1].Channels[0].Name) | ||
assert.Equal(t, ">=1.0.0 <2.0.0", cfg.Packages[1].Channels[0].VersionRange) | ||
assert.Equal(t, "bar-channel2", cfg.Packages[1].Channels[1].Name) | ||
assert.Equal(t, ">=2.0.0 <3.0.0", cfg.Packages[1].Channels[1].VersionRange) | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
f, err := tt.openFile() | ||
if err != nil { | ||
tt.assertion(t, nil, err) | ||
return | ||
} | ||
cfg, err := LoadFilterConfiguration(f) | ||
tt.assertion(t, cfg, err) | ||
}) | ||
} | ||
} | ||
|
||
//go:embed testdata/* | ||
var testdataFS embed.FS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
alpha/declcfg/filter/config/v1alpha1/testdata/invalid_apiversion.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
apiVersion: olmfiltering.operatorframework.io/v1alpha1 | ||
kind: FilterConfiguration | ||
packages: | ||
- name: "foo" | ||
- name: "bar" | ||
channels: | ||
- name: "bar-channel1" | ||
versionRange: ">=1.0.0 <2.0.0" | ||
- name: "bar-channel2" | ||
versionRange: ">=2.0.0 <3.0.0" |
10 changes: 10 additions & 0 deletions
10
alpha/declcfg/filter/config/v1alpha1/testdata/invalid_kind.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
apiVersion: olm.operatorframework.io/v1alpha1 | ||
kind: CatalogFilterConfiguration | ||
packages: | ||
- name: "foo" | ||
- name: "bar" | ||
channels: | ||
- name: "bar-channel1" | ||
versionRange: ">=1.0.0 <2.0.0" | ||
- name: "bar-channel2" | ||
versionRange: ">=2.0.0 <3.0.0" |
10 changes: 10 additions & 0 deletions
10
alpha/declcfg/filter/config/v1alpha1/testdata/invalid_missingchannelname.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
apiVersion: olm.operatorframework.io/v1alpha1 | ||
kind: FilterConfiguration | ||
packages: | ||
- name: "foo" | ||
- name: "bar" | ||
channels: | ||
- name: "bar-channel1" | ||
versionRange: ">=1.0.0 <2.0.0" | ||
- name: "" | ||
versionRange: ">=2.0.0 <3.0.0" |
5 changes: 5 additions & 0 deletions
5
alpha/declcfg/filter/config/v1alpha1/testdata/invalid_missingpackagename.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
apiVersion: olm.operatorframework.io/v1alpha1 | ||
kind: FilterConfiguration | ||
packages: | ||
- name: "foo" | ||
- name: "" |
24 changes: 24 additions & 0 deletions
24
alpha/declcfg/filter/config/v1alpha1/testdata/invalid_multipleerrors.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
apiVersion: olmfiltering.operatorframework.io/v1alpha1 | ||
kind: CatalogFilterConfiguration | ||
packages: | ||
- name: "foo" | ||
- name: "bar" | ||
channels: | ||
- name: "bar-channel1" | ||
versionRange: ">=1.0.0 <2.0.0" | ||
- name: "bar-channel2" | ||
versionRange: ">=2.0.0 <3.0.0" | ||
- name: "" | ||
channels: | ||
- name: "" | ||
- name: "" | ||
- name: "" | ||
channels: | ||
- name: "" | ||
- name: "" | ||
- name: "baz" | ||
channels: | ||
- name: "" | ||
versionRange: ">=1.0.0 <2.0.0" | ||
- name: "baz-channel2" | ||
versionRange: ">=2.0.0 <3.0.0" |
2 changes: 2 additions & 0 deletions
2
alpha/declcfg/filter/config/v1alpha1/testdata/invalid_nopackages.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
apiVersion: olm.operatorframework.io/v1alpha1 | ||
kind: FilterConfiguration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
apiVersion: olm.operatorframework.io/v1alpha1 | ||
kind: FilterConfiguration | ||
packages: | ||
- name: "foo" | ||
- name: "bar" | ||
defaultChannel: "bar-channel1" | ||
channels: | ||
- name: "bar-channel1" | ||
versionRange: ">=1.0.0 <2.0.0" | ||
- name: "bar-channel2" | ||
versionRange: ">=2.0.0 <3.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.