forked from operator-framework/operator-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use a type to protect migration interactions
Signed-off-by: Jordan Keister <jordan@nimblewidget.com>
- Loading branch information
Showing
14 changed files
with
410 additions
and
299 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,132 @@ | ||
package migrations | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/operator-framework/api/pkg/operators/v1alpha1" | ||
"github.com/operator-framework/operator-registry/alpha/declcfg" | ||
"github.com/operator-framework/operator-registry/alpha/property" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMigrations(t *testing.T) { | ||
noneMigration, err := NewMigrations(NoMigrations) | ||
require.NoError(t, err) | ||
csvMigration, err := NewMigrations("bundle-object-to-csv-metadata") | ||
require.NoError(t, err) | ||
allMigrations, err := NewMigrations(AllMigrations) | ||
require.NoError(t, err) | ||
|
||
var evaluators map[MigrationToken]func(*declcfg.DeclarativeConfig) error = map[MigrationToken]func(*declcfg.DeclarativeConfig) error{ | ||
MigrationToken(NoMigrations): func(d *declcfg.DeclarativeConfig) error { | ||
if diff := cmp.Diff(*d, unmigratedCatalogFBC); diff != "" { | ||
return fmt.Errorf("'none' migrator is not expected to change the config\n%s", diff) | ||
} | ||
return nil | ||
}, | ||
MigrationToken("bundle-object-to-csv-metadata"): func(d *declcfg.DeclarativeConfig) error { | ||
if diff := cmp.Diff(*d, csvMetadataCatalogFBC); diff == "" { | ||
return fmt.Errorf("unexpected result of migration\n%s", diff) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
migrators *Migrations | ||
}{ | ||
{ | ||
name: "NoMigrations", | ||
migrators: noneMigration, | ||
}, | ||
{ | ||
name: "BundleObjectToCSVMetadata", | ||
migrators: csvMigration, | ||
}, | ||
{ | ||
name: "MigrationSequence", | ||
migrators: allMigrations, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
var config declcfg.DeclarativeConfig = unmigratedCatalogFBC | ||
|
||
for _, m := range test.migrators.Migrations { | ||
err := m.Migrate(&config) | ||
require.NoError(t, err) | ||
err = evaluators[m.Token()](&config) | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func mustBuildCSVMetadata(r io.Reader) property.Property { | ||
var csv v1alpha1.ClusterServiceVersion | ||
if err := json.NewDecoder(r).Decode(&csv); err != nil { | ||
panic(err) | ||
} | ||
return property.MustBuildCSVMetadata(csv) | ||
} | ||
|
||
var fooRawCsv = []byte(`{"apiVersion": "operators.coreos.com/v1alpha1", "kind": "ClusterServiceVersion", "metadata": {"name": "foo.v0.1.0"}, "spec": {"displayName": "Foo Operator", "customresourcedefinitions": {"owned": [{"group": "test.foo", "version": "v1", "kind": "Foo", "name": "foos.test.foo"}]}, "version": "0.1.0", "relatedImages": [{"name": "operator", "image": "test.registry/foo-operator/foo:v0.1.0"}]}}`) | ||
|
||
var fooRawCrd = []byte(`--- | ||
apiVersion: apiextensions.k8s.io/v1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
name: foos.test.foo | ||
spec: | ||
group: test.foo | ||
names: | ||
kind: Foo | ||
plural: foos | ||
versions: | ||
- name: v1`, | ||
) | ||
|
||
var unmigratedCatalogFBC = declcfg.DeclarativeConfig{ | ||
Bundles: []declcfg.Bundle{ | ||
{ | ||
Schema: "olm.bundle", | ||
Name: "foo.v0.1.0", | ||
Package: "foo", | ||
Properties: []property.Property{ | ||
property.MustBuildGVK("test.foo", "v1", "Foo"), | ||
property.MustBuildGVKRequired("test.bar", "v1alpha1", "Bar"), | ||
property.MustBuildPackage("foo", "0.1.0"), | ||
property.MustBuildPackageRequired("bar", "<0.1.0"), | ||
property.MustBuildBundleObject(fooRawCrd), | ||
property.MustBuildBundleObject(fooRawCsv), | ||
}, | ||
Objects: []string{string(fooRawCsv), string(fooRawCrd)}, | ||
CsvJSON: string(fooRawCsv), | ||
}, | ||
}, | ||
} | ||
var csvMetadataCatalogFBC = declcfg.DeclarativeConfig{ | ||
Bundles: []declcfg.Bundle{ | ||
{ | ||
Schema: "olm.bundle", | ||
Name: "foo.v0.2.0", | ||
Package: "foo", | ||
Properties: []property.Property{ | ||
property.MustBuildGVK("test.foo", "v1", "Foo"), | ||
property.MustBuildGVKRequired("test.bar", "v1alpha1", "Bar"), | ||
property.MustBuildPackage("foo", "0.1.0"), | ||
property.MustBuildPackageRequired("bar", "<0.1.0"), | ||
mustBuildCSVMetadata(bytes.NewReader(fooRawCsv)), | ||
}, | ||
Objects: []string{string(fooRawCsv), string(fooRawCrd)}, | ||
CsvJSON: string(fooRawCsv), | ||
}, | ||
}, | ||
} |
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.