Skip to content

Commit

Permalink
refactor: redirect integration tests to use monaco.runWithFs instead …
Browse files Browse the repository at this point in the history
…of manually composing and executing cobra command
  • Loading branch information
jskelin committed Jul 26, 2024
1 parent 0ba4328 commit bf613e7
Show file tree
Hide file tree
Showing 31 changed files with 191 additions and 431 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ package account

import (
"context"
"net/http"
"slices"
"testing"

"github.com/dynatrace/dynatrace-configuration-as-code-core/api/clients/accounts"
accountmanagement "github.com/dynatrace/dynatrace-configuration-as-code-core/gen/account_management"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/cmd/monaco/runner"
Expand All @@ -28,9 +32,6 @@ import (
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/persistence/account/writer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"slices"
"testing"
)

func TestDeployAndDelete_AllResources(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions cmd/monaco/integrationtest/account/deploy-download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
func TestIdempotenceOfDeployment(t *testing.T) {

deploy := func(project string, fs afero.Fs) *account.Resources {
err := monaco.RunWithFsf(fs, "monaco account deploy --project %s --verbose", project)
err := monaco.RunWithFSf(fs, "monaco account deploy --project %s --verbose", project)

require.NoError(t, err)

Expand All @@ -45,7 +45,7 @@ func TestIdempotenceOfDeployment(t *testing.T) {
return r
}
download := func(project string, fs afero.Fs) *account.Resources {
err := monaco.RunWithFsf(fs, "monaco account download --project %s --output-folder output --verbose", project)
err := monaco.RunWithFSf(fs, "monaco account download --project %s --output-folder output --verbose", project)
require.NoError(t, err)

r, err := loader.Load(fs, fmt.Sprintf("%s/%s/%s", "output", project, "test-account"))
Expand Down Expand Up @@ -93,6 +93,6 @@ func TestIdempotenceOfDeployment(t *testing.T) {
assert.Equal(t, deploy1st.Groups[g.ID], deploy2nd.Groups[g.ID])
}

err := monaco.RunWithFsf(baseFs, "monaco account delete --manifest manifest.yaml --file delete.yaml")
err := monaco.RunWithFSf(baseFs, "monaco account delete --manifest manifest.yaml --file delete.yaml")
require.NoError(t, err)
}
5 changes: 3 additions & 2 deletions cmd/monaco/integrationtest/account/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ package account
import (
"bytes"
"fmt"
"strings"
"testing"

"github.com/dynatrace/dynatrace-configuration-as-code/v2/cmd/monaco/runner"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/files"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"strings"
"testing"
)

func createMZone(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/monaco/integrationtest/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ func CleanupIntegrationTest(t *testing.T, fs afero.Fs, manifestPath string, envi
absManifestPath, err := filepath.Abs(manifestPath)
require.NoError(t, err)

err = monaco.RunWithFsf(fs, "monaco generate deletefile %s --file %s --exclude-types builtin:networkzones %s", absManifestPath, deleteFile, env)
err = monaco.RunWithFSf(fs, "monaco generate deletefile %s --file %s --exclude-types builtin:networkzones %s", absManifestPath, deleteFile, env)
require.NoError(t, err)
if df, err := filepath.Abs(deleteFile); err == nil {
if b, err := afero.ReadFile(fs, df); err == nil {
fmt.Println(string(b[:]))
}
}

err = monaco.RunWithFsf(fs, "monaco delete --manifest %s --file %s %s", manifestPath, deleteFile, env)
err = monaco.RunWithFSf(fs, "monaco delete --manifest %s --file %s %s", manifestPath, deleteFile, env)
if err != nil {
t.Log(err)
t.Log("Failed to cleanup all test configurations, manual/nightly cleanup needed.")
Expand Down
4 changes: 2 additions & 2 deletions cmd/monaco/integrationtest/utils/monaco/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ func Run(command string) error {
}

func Runf(command string, args ...any) error {
return RunWithFsf(newFs(), command, args...)
return RunWithFSf(newFs(), command, args...)
}

func newFs() afero.Fs { return afero.NewCopyOnWriteFs(afero.NewOsFs(), afero.NewMemMapFs()) }

func RunWithFsf(fs afero.Fs, command string, args ...any) error {
func RunWithFSf(fs afero.Fs, command string, args ...any) error {
return RunWithFs(fs, fmt.Sprintf(command, args...))
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/monaco/integrationtest/v1/all_configs_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ func TestIntegrationAllConfigs(t *testing.T) {

RunLegacyIntegrationWithCleanup(t, allConfigsFolder, allConfigsEnvironmentsFile, "AllConfigs", func(fs afero.Fs, manifest string) {
// This causes a POST for all configs:
err := monaco.RunWithFsf(fs, "monaco deploy %s --verbose", manifest)
err := monaco.RunWithFSf(fs, "monaco deploy %s --verbose", manifest)
assert.NoError(t, err)

// This causes a PUT for all configs:
err = monaco.RunWithFsf(fs, "monaco deploy %s --verbose", manifest)
err = monaco.RunWithFSf(fs, "monaco deploy %s --verbose", manifest)
assert.NoError(t, err)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
package v1

import (
"github.com/dynatrace/dynatrace-configuration-as-code/v2/cmd/monaco/runner"
"path/filepath"
"testing"

"github.com/dynatrace/dynatrace-configuration-as-code/v2/cmd/monaco/integrationtest/utils/monaco"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/coordinate"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"

"path/filepath"
"testing"
)

// tests all configs for a single environment
Expand All @@ -35,14 +35,7 @@ func TestIntegrationContinueDeploymentOnError(t *testing.T) {
allConfigsEnvironmentsFile := filepath.Join(allConfigsFolder, "environments.yaml")

RunLegacyIntegrationWithCleanup(t, allConfigsFolder, allConfigsEnvironmentsFile, "AllConfigs", func(fs afero.Fs, manifest string) {
cmd := runner.BuildCmd(fs)
cmd.SetArgs([]string{
"deploy",
"--verbose",
manifest,
"--continue-on-error",
})
err := cmd.Execute()
err := monaco.RunWithFSf(fs, "monaco deploy %s --verbose --continue-on-error", manifest)
// deployment should fail
assert.Error(t, err, "deployment should fail")

Expand Down
13 changes: 2 additions & 11 deletions cmd/monaco/integrationtest/v1/integration_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"time"

"github.com/dynatrace/dynatrace-configuration-as-code/v2/cmd/monaco/integrationtest"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/cmd/monaco/runner"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/cmd/monaco/integrationtest/utils/monaco"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/log"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/testutils"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/api"
Expand Down Expand Up @@ -194,16 +194,7 @@ func runLegacyIntegration(t *testing.T, configFolder, envFile, suffixTest string
manifestPath := path.Join(targetDir, "manifest.yaml")

t.Log("Converting monaco-v1 to monaco-v2")
cmd := runner.BuildCmd(fs)
cmd.SetArgs([]string{
"convert",
"--verbose",
envFile,
configFolder,
"-o",
targetDir,
})
err = cmd.Execute()
err = monaco.RunWithFSf(fs, "monaco convert %s %s --output-folder=%s --verbose", envFile, configFolder, targetDir)
assert.NoError(t, err, "Conversion should had happened without errors")

exists, err := afero.Exists(fs, manifestPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var multiProjectEnvironmentsFile = filepath.Join(multiProjectFolder, "environmen
// Tests all environments with all projects
func TestIntegrationMultiProject(t *testing.T) {
RunLegacyIntegrationWithCleanup(t, multiProjectFolder, multiProjectEnvironmentsFile, "MultiProject", func(fs afero.Fs, manifest string) {
err := monaco.RunWithFsf(fs, "monaco deploy %s --verbose", manifest)
err := monaco.RunWithFSf(fs, "monaco deploy %s --verbose", manifest)
assert.NoError(t, err)

integrationtest.AssertAllConfigsAvailability(t, fs, manifest, []string{}, "", true)
Expand All @@ -46,15 +46,15 @@ func TestIntegrationMultiProject(t *testing.T) {
// Tests a dry run (validation)
func TestIntegrationValidationMultiProject(t *testing.T) {
RunLegacyIntegrationWithoutCleanup(t, multiProjectFolder, multiProjectEnvironmentsFile, "validMultiProj", func(fs afero.Fs, manifest string) {
err := monaco.RunWithFsf(fs, "monaco deploy %s --verbose --dry-run", manifest)
err := monaco.RunWithFSf(fs, "monaco deploy %s --verbose --dry-run", manifest)
assert.NoError(t, err)
})
}

// Tests a dry run (validation)
func TestIntegrationValidationMultiProjectWithoutEndingSlashInPath(t *testing.T) {
RunLegacyIntegrationWithoutCleanup(t, multiProjectFolderWithoutSlash, multiProjectEnvironmentsFile, "validMultiProj", func(fs afero.Fs, manifest string) {
err := monaco.RunWithFsf(fs, "monaco deploy %s --verbose --dry-run", manifest)
err := monaco.RunWithFSf(fs, "monaco deploy %s --verbose --dry-run", manifest)
assert.NoError(t, err)
})

Expand All @@ -63,7 +63,7 @@ func TestIntegrationValidationMultiProjectWithoutEndingSlashInPath(t *testing.T)
// tests a single project with dependencies
func TestIntegrationMultiProjectSingleProject(t *testing.T) {
RunLegacyIntegrationWithCleanup(t, multiProjectFolder, multiProjectEnvironmentsFile, "MultiProjectSingleProject", func(fs afero.Fs, manifestFile string) {
err := monaco.RunWithFsf(fs, "monaco deploy %s --project=star-trek --verbose", manifestFile)
err := monaco.RunWithFSf(fs, "monaco deploy %s --project=star-trek --verbose", manifestFile)
assert.NoError(t, err)

t.Log("Asserting available configs")
Expand Down
10 changes: 5 additions & 5 deletions cmd/monaco/integrationtest/v1/multi_tenant_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var environmentsFile = filepath.Join(folder, "environments.yaml")
// Tests all environments with all projects
func TestIntegrationMultiEnvironment(t *testing.T) {
RunLegacyIntegrationWithCleanup(t, folder, environmentsFile, "MultiEnvironment", func(fs afero.Fs, manifest string) {
err := monaco.RunWithFsf(fs, "monaco deploy --verbose %s", manifest)
err := monaco.RunWithFSf(fs, "monaco deploy --verbose %s", manifest)
assert.NoError(t, err)

integrationtest.AssertAllConfigsAvailability(t, fs, manifest, []string{}, "", true)
Expand All @@ -44,15 +44,15 @@ func TestIntegrationMultiEnvironment(t *testing.T) {
// Tests a dry run (validation)
func TestIntegrationValidationMultiEnvironment(t *testing.T) {
RunLegacyIntegrationWithoutCleanup(t, folder, environmentsFile, "validationMultiEnv", func(fs afero.Fs, manifest string) {
err := monaco.RunWithFsf(fs, "monaco deploy %s --verbose --dry-run", manifest)
err := monaco.RunWithFSf(fs, "monaco deploy %s --verbose --dry-run", manifest)
assert.NoError(t, err)
})
}

// tests a single project
func TestIntegrationMultiEnvironmentSingleProject(t *testing.T) {
RunLegacyIntegrationWithCleanup(t, folder, environmentsFile, "MultiEnvironmentSingleProject", func(fs afero.Fs, manifestFile string) {
err := monaco.RunWithFsf(fs, "monaco deploy %s --verbose --project=cinema-infrastructure", manifestFile)
err := monaco.RunWithFSf(fs, "monaco deploy %s --verbose --project=cinema-infrastructure", manifestFile)
assert.NoError(t, err)

integrationtest.AssertAllConfigsAvailability(t, fs, manifestFile, []string{"cinema-infrastructure"}, "", true)
Expand All @@ -62,7 +62,7 @@ func TestIntegrationMultiEnvironmentSingleProject(t *testing.T) {
// Tests a single project with dependency
func TestIntegrationMultiEnvironmentSingleProjectWithDependency(t *testing.T) {
RunLegacyIntegrationWithCleanup(t, folder, environmentsFile, "MultiEnvironmentSingleProjectWithDependency", func(fs afero.Fs, manifestFile string) {
err := monaco.RunWithFsf(fs, "monaco deploy %s --project=star-trek --verbose", manifestFile)
err := monaco.RunWithFSf(fs, "monaco deploy %s --project=star-trek --verbose", manifestFile)
assert.NoError(t, err)

integrationtest.AssertAllConfigsAvailability(t, fs, manifestFile, []string{"star-trek"}, "", true)
Expand All @@ -72,7 +72,7 @@ func TestIntegrationMultiEnvironmentSingleProjectWithDependency(t *testing.T) {
// tests a single environment
func TestIntegrationMultiEnvironmentSingleEnvironment(t *testing.T) {
RunLegacyIntegrationWithCleanup(t, folder, environmentsFile, "MultiEnvironmentSingleEnvironment", func(fs afero.Fs, manifestFile string) {
err := monaco.RunWithFsf(fs, "monaco deploy %s --environment=environment2 --verbose", manifestFile)
err := monaco.RunWithFSf(fs, "monaco deploy %s --environment=environment2 --verbose", manifestFile)
assert.NoError(t, err)

integrationtest.AssertAllConfigsAvailability(t, fs, manifestFile, []string{"star-trek"}, "environment2", true)
Expand Down
38 changes: 6 additions & 32 deletions cmd/monaco/integrationtest/v1/skip_deployment_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
package v1

import (
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"

"strings"
"testing"

"github.com/dynatrace/dynatrace-configuration-as-code/v2/cmd/monaco/integrationtest/utils/monaco"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/cmd/monaco/runner"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
)

var skipDeploymentFolder = AbsOrPanicFromSlash("test-resources/skip-deployment-project/")
Expand All @@ -35,15 +35,7 @@ func TestValidationSkipDeployment(t *testing.T) {
t.Setenv("TEST_TOKEN", "mock test token")

RunLegacyIntegrationWithoutCleanup(t, skipDeploymentFolder, skipDeploymentEnvironmentsFile, t.Name(), func(fs afero.Fs, manifest string) {
cmd := runner.BuildCmd(fs)
cmd.SetArgs([]string{
"deploy",
"--verbose",
"--dry-run",
manifest,
"--project", "projectA",
})
err := cmd.Execute()
err := monaco.RunWithFSf(fs, "monaco deploy %s --project=projectA --dry-run --verbose", manifest)
assert.NoError(t, err)
})

Expand Down Expand Up @@ -75,16 +67,7 @@ func TestValidationSkipDeploymentWithOverridingDependency(t *testing.T) {
t.Setenv("TEST_TOKEN", "mock test token")

RunLegacyIntegrationWithoutCleanup(t, skipDeploymentFolder, skipDeploymentEnvironmentsFile, t.Name(), func(fs afero.Fs, manifest string) {
cmd := runner.BuildCmd(fs)
cmd.SetArgs([]string{
"deploy",
"--verbose",
manifest,
"--dry-run",
"--project", "projectC",
})
err := cmd.Execute()

err := monaco.RunWithFSf(fs, "monaco deploy %s --project=projectC --dry-run --verbose", manifest)
assert.NoError(t, err)
})
}
Expand All @@ -93,16 +76,7 @@ func TestValidationSkipDeploymentWithOverridingFlagValue(t *testing.T) {
t.Setenv("TEST_TOKEN", "mock test token")

RunLegacyIntegrationWithoutCleanup(t, skipDeploymentFolder, skipDeploymentEnvironmentsFile, t.Name(), func(fs afero.Fs, manifest string) {
cmd := runner.BuildCmd(fs)
cmd.SetArgs([]string{
"deploy",
"--verbose",
manifest,
"--dry-run",
"--project", "projectE",
})
err := cmd.Execute()

err := monaco.RunWithFSf(fs, "monaco deploy %s --project=projectE --dry-run --verbose", manifest)
assert.NoError(t, err)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"testing"

"github.com/dynatrace/dynatrace-configuration-as-code/v2/cmd/monaco/integrationtest"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/cmd/monaco/runner"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/cmd/monaco/integrationtest/utils/monaco"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
)
Expand All @@ -34,14 +34,7 @@ func TestSpecialCharactersAreCorrectlyEscapedWhereNeeded(t *testing.T) {
specialCharEnvironmentsFile := filepath.Join(specialCharConfigFolder, "environments.yaml")

RunLegacyIntegrationWithCleanup(t, specialCharConfigFolder, specialCharEnvironmentsFile, "SpecialCharacterInConfig", func(fs afero.Fs, manifest string) {

cmd := runner.BuildCmd(fs)
cmd.SetArgs([]string{
"deploy",
"--verbose",
manifest,
})
err := cmd.Execute()
err := monaco.RunWithFSf(fs, "monaco deploy %s --verbose", manifest)
assert.NoError(t, err)

integrationtest.AssertAllConfigsAvailability(t, fs, manifest, []string{}, "", true)
Expand Down
23 changes: 4 additions & 19 deletions cmd/monaco/integrationtest/v2/all_configs_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ package v2
import (
"testing"

"github.com/dynatrace/dynatrace-configuration-as-code/v2/cmd/monaco/runner"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/cmd/monaco/integrationtest/utils/monaco"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/featureflags"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/testutils"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -51,20 +50,12 @@ func runAllConfigsTest(t *testing.T, specificEnvironment string) {
}

RunIntegrationWithCleanupGivenEnvs(t, configFolder, manifest, specificEnvironment, "AllConfigs", envVars, func(fs afero.Fs, _ TestContext) {

// This causes a POST for all configs:

cmd := runner.BuildCmd(fs)
cmd.SetArgs([]string{"deploy", "--verbose", manifest, "--environment", specificEnvironment})
err := cmd.Execute()

err := monaco.RunWithFSf(fs, "monaco deploy %s --environment=%s --verbose", manifest, specificEnvironment)
assert.NoError(t, err)

// This causes a PUT for all configs:

cmd = runner.BuildCmd(fs)
cmd.SetArgs([]string{"deploy", "--verbose", manifest, "--environment", specificEnvironment})
err = cmd.Execute()
err = monaco.RunWithFSf(fs, "monaco deploy %s --environment=%s --verbose", manifest, specificEnvironment)
assert.NoError(t, err)

})
Expand All @@ -76,12 +67,6 @@ func TestIntegrationValidationAllConfigs(t *testing.T) {
t.Setenv("UNIQUE_TEST_SUFFIX", "can-be-nonunique-for-validation")
t.Setenv(featureflags.Temporary[featureflags.OpenPipeline].EnvName(), "true")

configFolder := "test-resources/integration-all-configs/"
manifest := configFolder + "manifest.yaml"

cmd := runner.BuildCmd(testutils.CreateTestFileSystem())
cmd.SetArgs([]string{"deploy", "--verbose", "--dry-run", manifest})
err := cmd.Execute()

err := monaco.Runf("monaco deploy %s --dry-run --verbose", "test-resources/integration-all-configs/manifest.yaml")
assert.NoError(t, err)
}
Loading

0 comments on commit bf613e7

Please sign in to comment.