Skip to content

Commit

Permalink
feat: Rework integration tests to work with new config
Browse files Browse the repository at this point in the history
Since we reworked the configuration syntax, the current configuration
file syntax won't work. In order to support different types of
integration tests, they have been moved into their own package. Also
all the command parsing logic of the main file was factored out into a
runner package. This way we can easily use it from the integration test
packages.
  • Loading branch information
Patrick Pichler committed Jul 20, 2021
1 parent 8ab9895 commit f4ceb79
Show file tree
Hide file tree
Showing 183 changed files with 481 additions and 476 deletions.
36 changes: 0 additions & 36 deletions cmd/monaco/environment_migration_e2e_test.go

This file was deleted.

39 changes: 0 additions & 39 deletions cmd/monaco/special_character_in_config_integration_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,38 @@
* limitations under the License.
*/

package main
package legacy

import (
"testing"

"github.com/dynatrace-oss/dynatrace-monitoring-as-code/cmd/monaco/v2/runner"
"github.com/spf13/afero"
"gotest.tools/assert"
)

// tests all configs for a single environment
func TestIntegrationAllConfigs(t *testing.T) {

allConfigsFolder := "test-resources/integration-all-configs/"
allConfigsEnvironmentsFile := allConfigsFolder + "environments.yaml"

RunIntegrationWithCleanup(t, allConfigsFolder, allConfigsEnvironmentsFile, "AllConfigs", func(fs afero.Fs) {
RunLegacyIntegrationWithCleanup(t, allConfigsFolder, allConfigsEnvironmentsFile, "AllConfigs", func(fs afero.Fs) {

// This causes a POST for all configs:
statusCode := RunImpl([]string{
statusCode := runner.RunImpl([]string{
"monaco",
"--environments", allConfigsEnvironmentsFile,
"deploy",
"--environments",
allConfigsEnvironmentsFile,
allConfigsFolder,
}, fs)

assert.Equal(t, statusCode, 0)

// This causes a PUT for all configs:
statusCode = RunImpl([]string{
statusCode = runner.RunImpl([]string{
"monaco",
"deploy",
"--environments", allConfigsEnvironmentsFile,
// Currently there are some APIs for which updating the config does not work. These configs are included in
// the project "only-post" (folder ./test-resources/integration-all-configs/only-post)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

package main
package legacy

import (
"regexp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

package main
package legacy

import (
"log"
Expand All @@ -25,10 +25,12 @@ import (
"strings"
"testing"

"github.com/dynatrace-oss/dynatrace-monitoring-as-code/cmd/monaco/v2/runner"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/api"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/environment"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/rest"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/util"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/util/envvars"
"github.com/spf13/afero"
"gotest.tools/assert"
)
Expand All @@ -43,6 +45,14 @@ import (

//This version runs the test against 2 simple configs (alerting profiles and management zones)
func TestRestoreConfigsSimple(t *testing.T) {
envvars.InstallFakeEnvironment(map[string]string{
"CONFIG_V1": "1",
})

defer func() {
envvars.InstallOsBased()
}()

initialConfigsFolder := "test-resources/integration-download-configs/"
envFile := initialConfigsFolder + "environments.yaml"
downloadFolder := "test-resources/download"
Expand All @@ -61,24 +71,20 @@ func TestRestoreConfigsSimple(t *testing.T) {
// testRestoreConfigs(t, initialConfigsFolder, downloadFolder, suffixTest, envFile, subsetOfConfigsToDownload)
// }
func testRestoreConfigs(t *testing.T, initialConfigsFolder string, downloadFolder string, suffixTest string, envFile string, apisToDownload string) {
os.Setenv("NEW_CLI", "1")
fs := util.CreateTestFileSystem()
err := preparation_uploadConfigs(t, fs, suffixTest, initialConfigsFolder, envFile)
if err != nil {
assert.NilError(t, err, "Error during download preparation stage")
os.Setenv("NEW_CLI", "0")
return
}
err = execution_downloadConfigs(t, fs, downloadFolder, envFile, apisToDownload, suffixTest)
if err != nil {
assert.NilError(t, err, "Error during download execution stage")
os.Setenv("NEW_CLI", "0")
return
}
cleanupEnvironmentConfigs(t, fs, envFile, suffixTest)
validation_uploadDownloadedConfigs(t, fs, downloadFolder, envFile)
cleanupEnvironmentConfigs(t, fs, envFile, suffixTest)
os.Setenv("NEW_CLI", "0")
}

func preparation_uploadConfigs(t *testing.T, fs afero.Fs, suffixTest string, configFolder string, envFile string) error {
Expand All @@ -91,7 +97,7 @@ func preparation_uploadConfigs(t *testing.T, fs afero.Fs, suffixTest string, con
return err
}
//uploads the configs
statusCode := RunImpl([]string{
statusCode := runner.RunImpl([]string{
"monaco", "deploy",
"--environments", envFile,
configFolder,
Expand Down Expand Up @@ -123,7 +129,7 @@ func execution_downloadConfigs(t *testing.T, fs afero.Fs, downloadFolder string,
downloadFolder,
}
}
statusCode := RunImpl(parameters, fs)
statusCode := runner.RunImpl(parameters, fs)
assert.Equal(t, statusCode, 0)

return nil
Expand All @@ -138,7 +144,7 @@ func validation_uploadDownloadedConfigs(t *testing.T, fs afero.Fs, downloadFolde
return nil
})

statusCode := RunImpl([]string{
statusCode := runner.RunImpl([]string{
"monaco", "deploy",
"--environments", envFile,
downloadFolder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
* limitations under the License.
*/

package main
package legacy

import (
"testing"

"github.com/dynatrace-oss/dynatrace-monitoring-as-code/cmd/monaco/v2/runner"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/api"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/environment"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/project"
Expand All @@ -35,17 +36,19 @@ func TestIntegrationContinueDeploymentOnError(t *testing.T) {
const allConfigsFolder = "test-resources/integration-configs-with-errors/"
const allConfigsEnvironmentsFile = allConfigsFolder + "environments.yaml"

RunIntegrationWithCleanup(t, allConfigsFolder, allConfigsEnvironmentsFile, "AllConfigs", func(fs afero.Fs) {
RunLegacyIntegrationWithCleanup(t, allConfigsFolder, allConfigsEnvironmentsFile, "AllConfigs", func(fs afero.Fs) {

environments, errs := environment.LoadEnvironmentList("", allConfigsEnvironmentsFile, fs)
assert.Check(t, len(errs) == 0, "didn't expect errors loading test environments")

projects, err := project.LoadProjectsToDeploy(fs, "", api.NewApis(), allConfigsFolder)
assert.NilError(t, err)

statusCode := RunImpl([]string{
statusCode := runner.RunImpl([]string{
"monaco",
"--verbose", "--continue-on-error",
"deploy",
"--verbose",
"--continue-on-error",
"--environments", allConfigsEnvironmentsFile,
allConfigsFolder,
}, fs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

package main
package legacy

import (
"fmt"
Expand All @@ -32,6 +32,7 @@ import (
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/project"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/rest"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/util"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/util/envvars"
"github.com/spf13/afero"
"gotest.tools/assert"
)
Expand Down Expand Up @@ -166,7 +167,14 @@ func cleanupIntegrationTest(t *testing.T, fs afero.Fs, envFile, suffix string) {
// <original name>_<current timestamp><defined suffix>
// e.g. my-config_1605258980000_Suffix

func RunIntegrationWithCleanup(t *testing.T, configFolder, envFile, suffixTest string, testFunc func(fs afero.Fs)) {
func RunLegacyIntegrationWithCleanup(t *testing.T, configFolder, envFile, suffixTest string, testFunc func(fs afero.Fs)) {
envvars.InstallFakeEnvironment(map[string]string{
"CONFIG_V1": "1",
})

defer func() {
envvars.InstallOsBased()
}()

suffix := getTimestamp() + suffixTest
transformers := []func(string) string{getTransformerFunc(suffix)}
Expand All @@ -176,6 +184,8 @@ func RunIntegrationWithCleanup(t *testing.T, configFolder, envFile, suffixTest s
log.Fatal("Error rewriting configs names")
return
}

defer cleanupIntegrationTest(t, fs, envFile, suffix)

testFunc(fs)
cleanupIntegrationTest(t, fs, envFile, suffix)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
* limitations under the License.
*/

package main
package legacy

import (
"testing"

"github.com/dynatrace-oss/dynatrace-monitoring-as-code/cmd/monaco/v2/runner"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/api"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/environment"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/project"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/util"
"github.com/dynatrace-oss/dynatrace-monitoring-as-code/pkg/util/envvars"
"github.com/spf13/afero"
"gotest.tools/assert"
)
Expand All @@ -36,16 +38,17 @@ const multiProjectEnvironmentsFile = multiProjectFolder + "environments.yaml"
// Tests all environments with all projects
func TestIntegrationMultiProject(t *testing.T) {

RunIntegrationWithCleanup(t, multiProjectFolder, multiProjectEnvironmentsFile, "MultiProject", func(fs afero.Fs) {
RunLegacyIntegrationWithCleanup(t, multiProjectFolder, multiProjectEnvironmentsFile, "MultiProject", func(fs afero.Fs) {

environments, errs := environment.LoadEnvironmentList("", multiProjectEnvironmentsFile, fs)
assert.Check(t, len(errs) == 0, "didn't expect errors loading test environments")

projects, err := project.LoadProjectsToDeploy(fs, "", api.NewApis(), multiProjectFolder)
assert.NilError(t, err)

statusCode := RunImpl([]string{
statusCode := runner.RunImpl([]string{
"monaco",
"deploy",
"--environments", multiProjectEnvironmentsFile,
multiProjectFolder,
}, fs)
Expand All @@ -58,9 +61,17 @@ func TestIntegrationMultiProject(t *testing.T) {

// Tests a dry run (validation)
func TestIntegrationValidationMultiProject(t *testing.T) {
envvars.InstallFakeEnvironment(map[string]string{
"CONFIG_V1": "1",
})

defer func() {
envvars.InstallOsBased()
}()

statusCode := RunImpl([]string{
statusCode := runner.RunImpl([]string{
"monaco",
"deploy",
"--environments", multiProjectEnvironmentsFile,
"--dry-run",
multiProjectFolder,
Expand All @@ -71,9 +82,17 @@ func TestIntegrationValidationMultiProject(t *testing.T) {

// Tests a dry run (validation)
func TestIntegrationValidationMultiProjectWithoutEndingSlashInPath(t *testing.T) {
envvars.InstallFakeEnvironment(map[string]string{
"CONFIG_V1": "1",
})

defer func() {
envvars.InstallOsBased()
}()

statusCode := RunImpl([]string{
statusCode := runner.RunImpl([]string{
"monaco",
"deploy",
"--environments", multiProjectEnvironmentsFile,
"--dry-run",
multiProjectFolderWithoutSlash,
Expand All @@ -85,7 +104,7 @@ func TestIntegrationValidationMultiProjectWithoutEndingSlashInPath(t *testing.T)
// tests a single project with dependencies
func TestIntegrationMultiProjectSingleProject(t *testing.T) {

RunIntegrationWithCleanup(t, multiProjectFolder, multiProjectEnvironmentsFile, "MultiProjectSingleProject", func(fs afero.Fs) {
RunLegacyIntegrationWithCleanup(t, multiProjectFolder, multiProjectEnvironmentsFile, "MultiProjectSingleProject", func(fs afero.Fs) {

environments, errs := environment.LoadEnvironmentList("", multiProjectEnvironmentsFile, fs)
FailOnAnyError(errs, "loading of environments failed")
Expand All @@ -95,8 +114,9 @@ func TestIntegrationMultiProjectSingleProject(t *testing.T) {

assert.Equal(t, projects[0].GetId(), "test-resources/integration-multi-project/cinema-infrastructure", "Check if dependent project `cinema-infrastructure` is loaded and will be deployed first.")

statusCode := RunImpl([]string{
statusCode := runner.RunImpl([]string{
"monaco",
"deploy",
"--environments", multiProjectEnvironmentsFile,
"-p", "star-trek",
multiProjectFolder,
Expand Down
Loading

0 comments on commit f4ceb79

Please sign in to comment.