Skip to content

Commit

Permalink
Consolidate testdata so we don't have a bunch of similar bundles
Browse files Browse the repository at this point in the history
mybuns = a test bundle that contains as many features / test cases we can squeeze into a bundle, without making a bunch of paramters required so that it's easy to run. Long term I want to remove dependencies from this bundle though and switch to using a new set of bundles just for dependencies.

myenv = a metabundle that is comprised of its dependencies. Has a deep dependency graph.
myinfra = a bundle that creates infrastructure and generates outputs that parent bundles would want to consume.
myapp = a bundle that deploys an application and requires a db connection string
mysqldb = a leaf bundle that creates a db and outputs a connection string. I am not reusing mydb because that would impact too many tests that rely on mybuns (which depend on mydb), and it's also useful to regression test depsv1.

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
  • Loading branch information
carolynvs committed Feb 16, 2023
1 parent efad6f2 commit aca2818
Show file tree
Hide file tree
Showing 31 changed files with 939 additions and 327 deletions.
105 changes: 50 additions & 55 deletions pkg/cnab/config-adapter/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"get.porter.sh/porter/pkg/cnab"
depsv1ext "get.porter.sh/porter/pkg/cnab/extensions/dependencies/v1"
depsv2ext "get.porter.sh/porter/pkg/cnab/extensions/dependencies/v2"
"get.porter.sh/porter/pkg/config"
"get.porter.sh/porter/pkg/experimental"
"get.porter.sh/porter/pkg/manifest"
Expand Down Expand Up @@ -87,7 +86,7 @@ func TestManifestConverter_ToBundle(t *testing.T) {
t.Parallel()

c := config.NewTestConfig(t)
c.TestContext.AddTestFile("testdata/porter.yaml", config.Name)
c.TestContext.AddTestFileFromRoot("tests/testdata/mybuns/porter.yaml", config.Name)

ctx := context.Background()
m, err := manifest.LoadManifestFrom(ctx, c.Config, config.Name)
Expand All @@ -99,9 +98,9 @@ func TestManifestConverter_ToBundle(t *testing.T) {
require.NoError(t, err, "ToBundle failed")

assert.Equal(t, cnab.BundleSchemaVersion(), bun.SchemaVersion)
assert.Equal(t, "porter-hello", bun.Name)
assert.Equal(t, "0.1.0", bun.Version)
assert.Equal(t, "An example Porter configuration", bun.Description)
assert.Equal(t, "mybuns", bun.Name)
assert.Equal(t, "0.1.2", bun.Version)
assert.Equal(t, "A very thorough test bundle", bun.Description)

stamp, err := LoadStamp(bun)
require.NoError(t, err, "could not load porter's stamp")
Expand All @@ -116,14 +115,14 @@ func TestManifestConverter_ToBundle(t *testing.T) {
assert.True(t, bun.HasDependenciesV1(), "DependenciesV1 was not populated")
assert.Contains(t, bun.RequiredExtensions, "io.cnab.dependencies")

assert.Len(t, bun.Outputs, 1, "expected one output for the bundle state")
assert.NotEmpty(t, bun.Outputs, "expected multiple outputs generated")
}

func TestManifestConverter_generateBundleCredentials(t *testing.T) {
t.Parallel()

c := config.NewTestConfig(t)
c.TestContext.AddTestFile("testdata/porter.yaml", config.Name)
c.TestContext.AddTestFileFromRoot("tests/testdata/mybuns/porter.yaml", config.Name)

ctx := context.Background()
m, err := manifest.LoadManifestFrom(ctx, c.Config, config.Name)
Expand All @@ -136,14 +135,14 @@ func TestManifestConverter_generateBundleCredentials(t *testing.T) {

assert.Contains(t, bun.Credentials, "username", "credential 'username' was not populated")
username := bun.Credentials["username"]
assert.Equal(t, "Name of the database user", username.Description, "credential.Description was not populated")
assert.Equal(t, "The name you want on the audit log", username.Description, "credential.Description was not populated")
assert.False(t, username.Required, "credential.Required was not populated correctly")
assert.Equal(t, "ROOT_USERNAME", username.EnvironmentVariable, "credential.EnvironmentVariable was not populated")

assert.Contains(t, bun.Credentials, "password", "credential 'password' was not populated")
password := bun.Credentials["password"]
assert.True(t, password.Required, "credential.Required was not populated correctly")
assert.Equal(t, []string{"uninstall"}, password.ApplyTo, "credential.ApplyTo was not populated")
assert.Equal(t, []string{"boom"}, password.ApplyTo, "credential.ApplyTo was not populated")
assert.Equal(t, "/tmp/password", password.Path, "credential.Path was not populated")
}

Expand Down Expand Up @@ -597,9 +596,8 @@ func TestManifestConverter_generateDependenciesv1(t *testing.T) {

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

c := config.NewTestConfig(t)
c.TestContext.AddTestFile("testdata/porter.yaml", config.Name)
c.TestContext.AddTestFile("testdata/porter-with-deps.yaml", config.Name)

ctx := context.Background()
m, err := manifest.LoadManifestFrom(ctx, c.Config, config.Name)
Expand Down Expand Up @@ -634,28 +632,28 @@ func TestManifestConverter_generateDependenciesv2(t *testing.T) {

testcases := []struct {
name string
wantDep depsv2ext.Dependency
wantDep depsv1ext.Dependency
}{
{"no-version", depsv2ext.Dependency{
{"no-version", depsv1ext.Dependency{
Name: "mysql",
Bundle: "getporter/azure-mysql:5.7",
Parameters: map[string]depsv2ext.DependencySource{
"database": {Value: "wordpress"},
"collation": {Parameter: "db_collation"},
},
Credentials: map[string]depsv2ext.DependencySource{
"user": {Credential: "username"},
},
}},
{"no-ranges, uses prerelease", depsv2ext.Dependency{
Name: "ad",
Bundle: "getporter/azure-active-directory",
Version: "1.0.0-0",
{"no-ranges, uses prerelease", depsv1ext.Dependency{
Name: "ad",
Bundle: "getporter/azure-active-directory",
Version: &depsv1ext.DependencyVersion{
AllowPrereleases: true,
Ranges: []string{"1.0.0-0"},
},
}},
{"with-ranges", depsv2ext.Dependency{
Name: "storage",
Bundle: "getporter/azure-blob-storage",
Version: "1.x - 2,2.1 - 3.x",
{"with-ranges", depsv1ext.Dependency{
Name: "storage",
Bundle: "getporter/azure-blob-storage",
Version: &depsv1ext.DependencyVersion{
Ranges: []string{
"1.x - 2,2.1 - 3.x",
},
},
}},
}

Expand All @@ -664,10 +662,8 @@ func TestManifestConverter_generateDependenciesv2(t *testing.T) {

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

c := config.NewTestConfig(t)
c.SetExperimentalFlags(experimental.FlagDependenciesV2)
c.TestContext.AddTestFile("testdata/porter.yaml", config.Name)
c.TestContext.AddTestFile("testdata/porter-with-deps.yaml", config.Name)

ctx := context.Background()
m, err := manifest.LoadManifestFrom(ctx, c.Config, config.Name)
Expand All @@ -677,12 +673,13 @@ func TestManifestConverter_generateDependenciesv2(t *testing.T) {

depsExt, depsExtKey, err := a.generateDependencies()
require.NoError(t, err)
require.Equal(t, cnab.DependenciesV2ExtensionKey, depsExtKey, "expected the v2 dependencies extension key")
require.IsType(t, &depsv2ext.Dependencies{}, depsExt, "expected a v2 dependencies extension section")
deps := depsExt.(*depsv2ext.Dependencies)
require.Equal(t, cnab.DependenciesV1ExtensionKey, depsExtKey, "expected the v1 dependencies extension key")
require.IsType(t, &depsv1ext.Dependencies{}, depsExt, "expected a v1 dependencies extension section")
deps := depsExt.(*depsv1ext.Dependencies)
require.Len(t, deps.Requires, 3, "incorrect number of dependencies were generated")
require.Equal(t, []string{"mysql", "ad", "storage"}, deps.Sequence, "incorrect sequence was generated")

var dep *depsv2ext.Dependency
var dep *depsv1ext.Dependency
for _, d := range deps.Requires {
if d.Bundle == tc.wantDep.Bundle {
dep = &d
Expand All @@ -700,7 +697,7 @@ func TestManifestConverter_generateParameterSources(t *testing.T) {
t.Parallel()

c := config.NewTestConfig(t)
c.TestContext.AddTestFile("testdata/porter-with-templating.yaml", config.Name)
c.TestContext.AddTestFileFromRoot("tests/testdata/mybuns/porter.yaml", config.Name)

ctx := context.Background()
m, err := manifest.LoadManifestFrom(ctx, c.Config, config.Name)
Expand All @@ -717,8 +714,7 @@ func TestManifestConverter_generateParameterSources(t *testing.T) {
want.SetParameterFromOutput("porter-msg-output", "msg")
want.SetParameterFromOutput("tfstate", "tfstate")
want.SetParameterFromOutput("porter-state", "porter-state")
want.SetParameterFromDependencyOutput("porter-mysql-mysql-password-dep-output", "mysql", "mysql-password")
want.SetParameterFromDependencyOutput("root-password", "mysql", "mysql-root-password")
want.SetParameterFromDependencyOutput("mysql-connstr", "db", "connstr")

assert.Equal(t, want, sources)
}
Expand All @@ -727,7 +723,7 @@ func TestNewManifestConverter_generateOutputWiringParameter(t *testing.T) {
t.Parallel()

c := config.NewTestConfig(t)
c.TestContext.AddTestFile("testdata/porter-with-templating.yaml", config.Name)
c.TestContext.AddTestFileFromRoot("tests/testdata/mybuns/porter.yaml", config.Name)

ctx := context.Background()
m, err := manifest.LoadManifestFrom(ctx, c.Config, config.Name)
Expand Down Expand Up @@ -783,7 +779,7 @@ func TestNewManifestConverter_generateDependencyOutputWiringParameter(t *testing
t.Parallel()

c := config.NewTestConfig(t)
c.TestContext.AddTestFile("testdata/porter-with-templating.yaml", config.Name)
c.TestContext.AddTestFileFromRoot("tests/testdata/mybuns/porter.yaml", config.Name)

ctx := context.Background()
m, err := manifest.LoadManifestFrom(ctx, c.Config, config.Name)
Expand All @@ -808,7 +804,7 @@ func TestManifestConverter_generateRequiredExtensions_ParameterSources(t *testin
t.Parallel()

c := config.NewTestConfig(t)
c.TestContext.AddTestFile("testdata/porter-with-templating.yaml", config.Name)
c.TestContext.AddTestFileFromRoot("tests/testdata/mybuns/porter.yaml", config.Name)

ctx := context.Background()
m, err := manifest.LoadManifestFrom(ctx, c.Config, config.Name)
Expand All @@ -825,7 +821,7 @@ func TestManifestConverter_generateRequiredExtensions(t *testing.T) {
t.Parallel()

c := config.NewTestConfig(t)
c.TestContext.AddTestFile("testdata/porter-with-required-extensions.yaml", config.Name)
c.TestContext.AddTestFileFromRoot("tests/testdata/mybuns/porter.yaml", config.Name)

ctx := context.Background()
m, err := manifest.LoadManifestFrom(ctx, c.Config, config.Name)
Expand All @@ -836,7 +832,7 @@ func TestManifestConverter_generateRequiredExtensions(t *testing.T) {
bun, err := a.ToBundle(ctx)
require.NoError(t, err, "ToBundle failed")

expected := []string{"sh.porter.file-parameters", "io.cnab.parameter-sources", "requiredExtension1", "requiredExtension2"}
expected := []string{"sh.porter.file-parameters", "io.cnab.dependencies", "io.cnab.parameter-sources", "io.cnab.docker"}
assert.Equal(t, expected, bun.RequiredExtensions)
}

Expand Down Expand Up @@ -864,7 +860,7 @@ func TestManifestConverter_GenerateCustomActionDefinitions(t *testing.T) {
t.Parallel()

c := config.NewTestConfig(t)
c.TestContext.AddTestFile("testdata/porter-with-custom-action.yaml", config.Name)
c.TestContext.AddTestFileFromRoot("tests/testdata/mybuns/porter.yaml", config.Name)

ctx := context.Background()
m, err := manifest.LoadManifestFrom(ctx, c.Config, config.Name)
Expand All @@ -873,19 +869,18 @@ func TestManifestConverter_GenerateCustomActionDefinitions(t *testing.T) {
a := NewManifestConverter(c.Config, m, nil, nil)

defs := a.generateCustomActionDefinitions()
require.Len(t, defs, 2, "expected 2 custom action definitions to be generated")
require.Len(t, defs, 3, "expected 3 custom action definitions to be generated")

require.Contains(t, defs, "status")
statusDef := defs["status"]
assert.Equal(t, "Prints out status of world", statusDef.Description)
assert.True(t, statusDef.Stateless, "expected the status custom action to be stateless")
assert.Equal(t, "Print the installation status", statusDef.Description)
assert.False(t, statusDef.Stateless, "expected the status custom action to not be stateless")
assert.False(t, statusDef.Modifies, "expected the status custom action to not modify resources")

require.Contains(t, defs, "zombies")
zombieDef := defs["zombies"]
assert.Equal(t, "zombies", zombieDef.Description)
assert.False(t, zombieDef.Stateless, "expected the zombies custom action to default to not stateless")
assert.True(t, zombieDef.Modifies, "expected the zombies custom action to default to modifying resources")
require.Contains(t, defs, "boom")
boomDef := defs["boom"]
assert.False(t, boomDef.Stateless, "expected the dry-run custom action to default to not stateless")
assert.True(t, boomDef.Modifies, "expected the dry-run custom action to default to modifying resources")
}

func TestManifestConverter_generateDefaultAction(t *testing.T) {
Expand Down Expand Up @@ -939,7 +934,7 @@ func TestManifestConverter_generateCustomMetadata(t *testing.T) {
t.Parallel()

c := config.NewTestConfig(t)
c.TestContext.AddTestFile("./testdata/porter-with-custom-metadata.yaml", config.Name)
c.TestContext.AddTestFileFromRoot("tests/testdata/mybuns/porter.yaml", config.Name)

ctx := context.Background()
m, err := manifest.LoadManifestFrom(ctx, c.Config, config.Name)
Expand All @@ -949,7 +944,7 @@ func TestManifestConverter_generateCustomMetadata(t *testing.T) {

bun, err := a.ToBundle(ctx)
require.NoError(t, err, "ToBundle failed")
assert.Len(t, bun.Custom, 4)
assert.Len(t, bun.Custom, 6)

f, err := os.CreateTemp("", "")
require.NoError(t, err, "Failed to create bundle file")
Expand All @@ -974,7 +969,7 @@ func TestManifestConverter_generatedMaintainers(t *testing.T) {
}

c := config.NewTestConfig(t)
c.TestContext.AddTestFile("./testdata/porter-with-maintainers.yaml", config.Name)
c.TestContext.AddTestFileFromRoot("tests/testdata/mybuns/porter.yaml", config.Name)

ctx := context.Background()
m, err := manifest.LoadManifestFrom(ctx, c.Config, config.Name)
Expand Down
Loading

0 comments on commit aca2818

Please sign in to comment.