diff --git a/mmv1/third_party/terraform/.teamcity/tests/build_configuration_features.kt b/mmv1/third_party/terraform/.teamcity/tests/build_configuration_features.kt index d606524fef92..5624a5651479 100644 --- a/mmv1/third_party/terraform/.teamcity/tests/build_configuration_features.kt +++ b/mmv1/third_party/terraform/.teamcity/tests/build_configuration_features.kt @@ -8,7 +8,6 @@ package tests import builds.UseTeamCityGoTest -import jetbrains.buildServer.configs.kotlin.Project import org.junit.Assert.assertTrue import org.junit.Assert.fail import org.junit.Test @@ -17,19 +16,13 @@ import projects.googleCloudRootProject class BuildConfigurationFeatureTests { @Test fun buildShouldFailOnError() { - val project = googleCloudRootProject(testContextParameters()) - // Find Google (GA) project - var gaProject: Project? = project.subProjects.find { p-> p.name == gaProjectName} - if (gaProject == null) { - fail("Could not find the Google (GA) project") - } - // Find Google Beta project - var betaProject: Project? = project.subProjects.find { p-> p.name == betaProjectName} - if (betaProject == null) { - fail("Could not find the Google (GA) project") - } + val root = googleCloudRootProject(testContextParameters()) + + val gaProject = getSubProject(root, gaProjectName) + val betaProject = getSubProject(root, betaProjectName) + val projectSweeperProject = getSubProject(root, projectSweeperProjectName) - (gaProject!!.subProjects + betaProject!!.subProjects).forEach{p -> + (gaProject.subProjects + betaProject.subProjects + projectSweeperProject.subProjects).forEach{p -> p.buildTypes.forEach{bt -> assertTrue("Build '${bt.id}' should fail on errors!", bt.failureConditions.errorMessage) } @@ -38,19 +31,13 @@ class BuildConfigurationFeatureTests { @Test fun buildShouldHaveGoTestFeature() { - val project = googleCloudRootProject(testContextParameters()) - // Find Google (GA) project - var gaProject: Project? = project.subProjects.find { p-> p.name == gaProjectName} - if (gaProject == null) { - fail("Could not find the Google (GA) project") - } - // Find Google Beta project - var betaProject: Project? = project.subProjects.find { p-> p.name == betaProjectName} - if (betaProject == null) { - fail("Could not find the Google (GA) project") - } + val root = googleCloudRootProject(testContextParameters()) + + val gaProject = getSubProject(root, gaProjectName) + val betaProject = getSubProject(root, betaProjectName) + val projectSweeperProject = getSubProject(root, projectSweeperProjectName) - (gaProject!!.subProjects + betaProject!!.subProjects).forEach{p -> + (gaProject.subProjects + betaProject.subProjects + projectSweeperProject.subProjects).forEach{p -> var exists: ArrayList = arrayListOf() p.buildTypes.forEach{bt -> bt.features.items.forEach { f -> @@ -65,19 +52,19 @@ class BuildConfigurationFeatureTests { @Test fun nonVCRBuildShouldHaveSaveArtifactsToGCS() { - val project = googleCloudRootProject(testContextParameters()) + val root = googleCloudRootProject(testContextParameters()) // Find GA nightly test project - var gaNightlyTestProject = getSubProject(project, gaProjectName, nightlyTestsProjectName) + var gaNightlyTestProject = getNestedProjectFromRoot(root, gaProjectName, nightlyTestsProjectName) // Find GA MM Upstream project - var gaMMUpstreamProject = getSubProject(project, gaProjectName, mmUpstreamProjectName) + var gaMMUpstreamProject = getNestedProjectFromRoot(root, gaProjectName, mmUpstreamProjectName) // Find Beta nightly test project - var betaNightlyTestProject = getSubProject(project, betaProjectName, nightlyTestsProjectName) + var betaNightlyTestProject = getNestedProjectFromRoot(root, betaProjectName, nightlyTestsProjectName) // Find Beta MM Upstream project - var betaMMUpstreamProject = getSubProject(project, betaProjectName, mmUpstreamProjectName) + var betaMMUpstreamProject = getNestedProjectFromRoot(root, betaProjectName, mmUpstreamProjectName) (gaNightlyTestProject.buildTypes + gaMMUpstreamProject.buildTypes + betaNightlyTestProject.buildTypes + betaMMUpstreamProject.buildTypes).forEach{bt -> var found: Boolean = false diff --git a/mmv1/third_party/terraform/.teamcity/tests/context_parameters.kt b/mmv1/third_party/terraform/.teamcity/tests/context_parameters.kt index 373ad78e29fe..2e69a2874a58 100644 --- a/mmv1/third_party/terraform/.teamcity/tests/context_parameters.kt +++ b/mmv1/third_party/terraform/.teamcity/tests/context_parameters.kt @@ -11,7 +11,7 @@ import builds.AccTestConfiguration import builds.getBetaAcceptanceTestConfig import builds.getGaAcceptanceTestConfig import builds.getVcrAcceptanceTestConfig -import org.junit.Assert +import org.junit.Assert.fail import org.junit.Test import kotlin.reflect.full.memberProperties @@ -22,7 +22,7 @@ class ContextParameterHandlingTests { for (prop in AccTestConfiguration::class.memberProperties) { val value = prop.get(config).toString() if (value.contains("Beta")||value.contains("Vcr")) { - Assert.fail("Found config value $value which isn't a GA value") + fail("Found config value $value which isn't a GA value") } } } @@ -33,7 +33,7 @@ class ContextParameterHandlingTests { for (prop in AccTestConfiguration::class.memberProperties) { val value = prop.get(config).toString() if (value.contains("Ga")||value.contains("Vcr")) { - Assert.fail("Found config value $value which isn't a Beta value") + fail("Found config value $value which isn't a Beta value") } } } @@ -44,7 +44,7 @@ class ContextParameterHandlingTests { for (prop in AccTestConfiguration::class.memberProperties) { val value = prop.get(config).toString() if (value.contains("Ga")||value.contains("Beta")) { - Assert.fail("Found config value $value which isn't a VCR value") + fail("Found config value $value which isn't a VCR value") } } } diff --git a/mmv1/third_party/terraform/.teamcity/tests/nightly_tests_project.kt b/mmv1/third_party/terraform/.teamcity/tests/nightly_tests_project.kt index 3eece9950b51..b3ad82c4de7d 100644 --- a/mmv1/third_party/terraform/.teamcity/tests/nightly_tests_project.kt +++ b/mmv1/third_party/terraform/.teamcity/tests/nightly_tests_project.kt @@ -7,27 +7,25 @@ package tests +import jetbrains.buildServer.configs.kotlin.triggers.ScheduleTrigger import org.junit.Assert.assertTrue import org.junit.Test -import jetbrains.buildServer.configs.kotlin.Project -import jetbrains.buildServer.configs.kotlin.triggers.ScheduleTrigger -import org.junit.Assert import projects.googleCloudRootProject class NightlyTestProjectsTests { @Test fun allBuildsShouldHaveTrigger() { - val project = googleCloudRootProject(testContextParameters()) + val root = googleCloudRootProject(testContextParameters()) // Find GA nightly test project - var gaNightlyTestProject = getSubProject(project, gaProjectName, nightlyTestsProjectName) + var gaNightlyTestProject = getNestedProjectFromRoot(root, gaProjectName, nightlyTestsProjectName) // Find Beta nightly test project - var betaNightlyTestProject = getSubProject(project, betaProjectName, nightlyTestsProjectName) + var betaNightlyTestProject = getNestedProjectFromRoot(root, betaProjectName, nightlyTestsProjectName) // Make assertions about builds in both nightly test projects (gaNightlyTestProject.buildTypes + betaNightlyTestProject.buildTypes).forEach{bt -> - assertTrue("Build configuration `${bt.name}` contains at least one trigger", bt.triggers.items.isNotEmpty()) + assertTrue("Build configuration `${bt.name}` should contain at least one trigger", bt.triggers.items.isNotEmpty()) // Look for at least one CRON trigger var found: Boolean = false lateinit var schedulingTrigger: ScheduleTrigger @@ -38,7 +36,8 @@ class NightlyTestProjectsTests { break } } - assertTrue("Build configuration `${bt.name}` contains a CRON trigger", found) + + assertTrue("Build configuration `${bt.name}` should contain a CRON/'schedulingTrigger' trigger", found) // Check that nightly test is being ran on main branch var isDefault: Boolean = false diff --git a/mmv1/third_party/terraform/.teamcity/tests/sweepers.kt b/mmv1/third_party/terraform/.teamcity/tests/sweepers.kt index 34f9dfd2c5a8..e4f55a2a7a82 100644 --- a/mmv1/third_party/terraform/.teamcity/tests/sweepers.kt +++ b/mmv1/third_party/terraform/.teamcity/tests/sweepers.kt @@ -10,118 +10,101 @@ package tests import ProjectSweeperName import ServiceSweeperName import jetbrains.buildServer.configs.kotlin.BuildType +import jetbrains.buildServer.configs.kotlin.Project +import jetbrains.buildServer.configs.kotlin.triggers.ScheduleTrigger import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Test -import jetbrains.buildServer.configs.kotlin.Project -import jetbrains.buildServer.configs.kotlin.triggers.ScheduleTrigger -import org.junit.Assert import projects.googleCloudRootProject class SweeperTests { @Test fun projectSweeperDoesNotSkipProjectSweep() { - val project = googleCloudRootProject(testContextParameters()) + val root = googleCloudRootProject(testContextParameters()) // Find Project sweeper project - val projectSweeperProject: Project? = project.subProjects.find { p-> p.name == projectSweeperProjectName} - if (projectSweeperProject == null) { - Assert.fail("Could not find the Project Sweeper project") - } + val projectSweeperProject = getSubProject(root, projectSweeperProjectName) // For the project sweeper to be skipped, SKIP_PROJECT_SWEEPER needs a value // See https://github.com/GoogleCloudPlatform/magic-modules/blob/501429790939717ca6dce76dbf4b1b82aef4e9d9/mmv1/third_party/terraform/services/resourcemanager/resource_google_project_sweeper.go#L18-L26 - projectSweeperProject!!.buildTypes.forEach{bt -> + projectSweeperProject.buildTypes.forEach{bt -> val value = bt.params.findRawParam("env.SKIP_PROJECT_SWEEPER")!!.value - assertTrue("env.SKIP_PROJECT_SWEEPER is set to an empty value, so project sweepers are NOT skipped. Value = `${value}` ", value == "") + assertTrue("env.SKIP_PROJECT_SWEEPER should be set to an empty value, so project sweepers are NOT skipped in the ${projectSweeperProject.name} project. Value = `${value}` ", value == "") } } @Test fun serviceSweepersSkipProjectSweeper() { - val project = googleCloudRootProject(testContextParameters()) + val root = googleCloudRootProject(testContextParameters()) // Find GA nightly test project - val gaNightlyTestProject = getSubProject(project, gaProjectName, nightlyTestsProjectName) + val gaNightlyTestProject = getNestedProjectFromRoot(root, gaProjectName, nightlyTestsProjectName) // Find GA MM Upstream project - val gaMmUpstreamProject = getSubProject(project, gaProjectName, mmUpstreamProjectName) + val gaMmUpstreamProject = getNestedProjectFromRoot(root, gaProjectName, mmUpstreamProjectName) // Find Beta nightly test project - val betaNightlyTestProject = getSubProject(project, betaProjectName, nightlyTestsProjectName) + val betaNightlyTestProject = getNestedProjectFromRoot(root, betaProjectName, nightlyTestsProjectName) // Find Beta MM Upstream project - val betaMmUpstreamProject = getSubProject(project, betaProjectName, mmUpstreamProjectName) + val betaMmUpstreamProject = getNestedProjectFromRoot(root, betaProjectName, mmUpstreamProjectName) val allProjects: ArrayList = arrayListOf(gaNightlyTestProject, gaMmUpstreamProject, betaNightlyTestProject, betaMmUpstreamProject) allProjects.forEach{ project -> // Find sweeper inside - val sweeper: BuildType? = project.buildTypes.find { p-> p.name == ServiceSweeperName} - if (sweeper == null) { - Assert.fail("Could not find the sweeper build in the ${project.name} project") - } + val sweeper = getBuildFromProject(project, ServiceSweeperName) // For the project sweeper to be skipped, SKIP_PROJECT_SWEEPER needs a value // See https://github.com/GoogleCloudPlatform/magic-modules/blob/501429790939717ca6dce76dbf4b1b82aef4e9d9/mmv1/third_party/terraform/services/resourcemanager/resource_google_project_sweeper.go#L18-L26 - val value = sweeper!!.params.findRawParam("env.SKIP_PROJECT_SWEEPER")!!.value - assertTrue("env.SKIP_PROJECT_SWEEPER is set to a non-empty string in the sweeper build in the ${project.name} project. This means project sweepers are skipped. Value = `${value}` ", value != "") + val value = sweeper.params.findRawParam("env.SKIP_PROJECT_SWEEPER")!!.value + assertTrue("env.SKIP_PROJECT_SWEEPER should be set to a non-empty string so project sweepers are skipped in the ${project.name} project. Value = `${value}` ", value != "") } } @Test fun gaNightlyProjectServiceSweeperRunsInGoogle() { - val project = googleCloudRootProject(testContextParameters()) + val root = googleCloudRootProject(testContextParameters()) // Find GA nightly test project - val gaNightlyTestProject = getSubProject(project, gaProjectName, nightlyTestsProjectName) - + val gaNightlyTestProject = getNestedProjectFromRoot(root, gaProjectName, nightlyTestsProjectName) // Find sweeper inside - val sweeper: BuildType? = gaNightlyTestProject!!.buildTypes.find { p-> p.name == ServiceSweeperName} - if (sweeper == null) { - Assert.fail("Could not find the sweeper build in the Google (GA) Nightly Test project") - } + val sweeper = getBuildFromProject(gaNightlyTestProject, ServiceSweeperName) // Check PACKAGE_PATH is in google (not google-beta) - val value = sweeper!!.params.findRawParam("PACKAGE_PATH")!!.value + val value = sweeper.params.findRawParam("PACKAGE_PATH")!!.value assertEquals("./google/sweeper", value) } @Test fun betaNightlyProjectServiceSweeperRunsInGoogleBeta() { - val project = googleCloudRootProject(testContextParameters()) + val root = googleCloudRootProject(testContextParameters()) // Find Beta nightly test project - val betaNightlyTestProject = getSubProject(project, betaProjectName, nightlyTestsProjectName) + val betaNightlyTestProject = getNestedProjectFromRoot(root, betaProjectName, nightlyTestsProjectName) // Find sweeper inside - val sweeper: BuildType? = betaNightlyTestProject!!.buildTypes.find { p-> p.name == ServiceSweeperName} - if (sweeper == null) { - Assert.fail("Could not find the sweeper build in the Google (GA) Nightly Test project") - } + val sweeper: BuildType = getBuildFromProject(betaNightlyTestProject, ServiceSweeperName) // Check PACKAGE_PATH is in google-beta - val value = sweeper!!.params.findRawParam("PACKAGE_PATH")!!.value + val value = sweeper.params.findRawParam("PACKAGE_PATH")!!.value assertEquals("./google-beta/sweeper", value) } @Test fun projectSweepersRunAfterServiceSweepers() { - val project = googleCloudRootProject(testContextParameters()) + val root = googleCloudRootProject(testContextParameters()) // Find GA nightly test project's service sweeper - val gaNightlyTests: Project = getSubProject(project, gaProjectName, nightlyTestsProjectName) + val gaNightlyTests: Project = getNestedProjectFromRoot(root, gaProjectName, nightlyTestsProjectName) val sweeperGa: BuildType = getBuildFromProject(gaNightlyTests, ServiceSweeperName) // Find Beta nightly test project's service sweeper - val betaNightlyTests : Project = getSubProject(project, betaProjectName, nightlyTestsProjectName) + val betaNightlyTests : Project = getNestedProjectFromRoot(root, betaProjectName, nightlyTestsProjectName) val sweeperBeta: BuildType = getBuildFromProject(betaNightlyTests, ServiceSweeperName) // Find Project sweeper project's build - val projectSweeperProject : Project? = project.subProjects.find { p-> p.name == projectSweeperProjectName} - if (projectSweeperProject == null) { - Assert.fail("Could not find the Project Sweeper project") - } + val projectSweeperProject = getSubProject(root, projectSweeperProjectName) val projectSweeper: BuildType = getBuildFromProject(projectSweeperProject!!, ProjectSweeperName) // Check only one schedule trigger is on the builds in question @@ -137,7 +120,7 @@ class SweeperTests { val cronBeta = stBeta.schedulingPolicy as ScheduleTrigger.SchedulingPolicy.Cron val stProject = projectSweeper.triggers.items[0] as ScheduleTrigger val cronProject = stProject.schedulingPolicy as ScheduleTrigger.SchedulingPolicy.Cron - assertTrue("Service sweeper for the GA Nightly Test project is triggered at an earlier hour than the project sweeper", cronGa.hours.toString() < cronProject.hours.toString()) // Values are strings like "11", "12" - assertTrue("Service sweeper for the Beta Nightly Test project is triggered at an earlier hour than the project sweeper", cronBeta.hours.toString() < cronProject.hours.toString() ) + assertTrue("Service sweeper for the GA Nightly Test project should be triggered at an earlier hour than the project sweeper", cronGa.hours.toString() < cronProject.hours.toString()) // Values are strings like "11", "12" + assertTrue("Service sweeper for the Beta Nightly Test project should be triggered at an earlier hour than the project sweeper", cronBeta.hours.toString() < cronProject.hours.toString() ) } } diff --git a/mmv1/third_party/terraform/.teamcity/tests/test_utils.kt b/mmv1/third_party/terraform/.teamcity/tests/test_utils.kt index 05f976436ca1..a1d667790be6 100644 --- a/mmv1/third_party/terraform/.teamcity/tests/test_utils.kt +++ b/mmv1/third_party/terraform/.teamcity/tests/test_utils.kt @@ -8,10 +8,9 @@ package tests import builds.AllContextParameters -import jetbrains.buildServer.BuildProject import jetbrains.buildServer.configs.kotlin.BuildType import jetbrains.buildServer.configs.kotlin.Project -import org.junit.Assert +import org.junit.Assert.fail const val gaProjectName = "Google" const val betaProjectName = "Google Beta" @@ -54,26 +53,31 @@ fun testContextParameters(): AllContextParameters { "credentialsGCS") } -fun getSubProject(rootProject: Project, parentProjectName: String, subProjectName: String): Project { +// getNestedProjectFromRoot allows you to retrieve a project that's 2 levels deep from the root project, +// Using the names of the parent and desired project. +// E.g. Root > Project A > Project B - you need to supply the inputs "Project A" and "Project B" +fun getNestedProjectFromRoot(root: Project, parentName: String, nestedProjectName: String): Project { // Find parent project within root - val parentProject: Project? = rootProject.subProjects.find { p-> p.name == parentProjectName} - if (parentProject == null) { - Assert.fail("Could not find the $parentProjectName project") - } + val parent: Project = getSubProject(root, parentName) // Find subproject within parent identified above - val subProject: Project? = parentProject!!.subProjects.find { p-> p.name == subProjectName} + return getSubProject(parent, nestedProjectName) +} + +// getSubProject allows you to retrieve a project nested inside a given project you've already identified, +// using its name. +fun getSubProject(parent: Project, subProjectName: String): Project { + val subProject: Project? = parent.subProjects.find { p-> p.name == subProjectName} if (subProject == null) { - Assert.fail("Could not find the $subProjectName project") + fail("Could not find the $subProjectName project inside ${parent.name}") } - return subProject!! } +// getBuildFromProject allows you to retrieve a build configuration from an identified project using its name fun getBuildFromProject(parentProject: Project, buildName: String): BuildType { - val buildType: BuildType? = parentProject!!.buildTypes.find { p-> p.name == buildName} + val buildType: BuildType? = parentProject.buildTypes.find { p-> p.name == buildName} if (buildType == null) { - Assert.fail("Could not find the '$buildName' build in project ${parentProject.name}") + fail("Could not find the '$buildName' build in project ${parentProject.name}") } - return buildType!! -} +} \ No newline at end of file diff --git a/mmv1/third_party/terraform/.teamcity/tests/vcs_roots.kt b/mmv1/third_party/terraform/.teamcity/tests/vcs_roots.kt index ae56484af86e..2da91554416c 100644 --- a/mmv1/third_party/terraform/.teamcity/tests/vcs_roots.kt +++ b/mmv1/third_party/terraform/.teamcity/tests/vcs_roots.kt @@ -14,9 +14,23 @@ import projects.googleCloudRootProject class VcsTests { @Test fun buildsHaveCleanCheckOut() { - val project = googleCloudRootProject(testContextParameters()) - project.buildTypes.forEach { bt -> - assertTrue("Build '${bt.id}' doesn't use clean checkout", bt.vcs.cleanCheckout) + val root = googleCloudRootProject(testContextParameters()) + + val gaProject = getSubProject(root, gaProjectName) + val betaProject = getSubProject(root, betaProjectName) + val projectSweeperProject = getSubProject(root, betaProjectName) + + val allProjects = arrayListOf(gaProject, betaProject, projectSweeperProject) + + allProjects.forEach { p -> + p.subProjects.forEach { sp-> + // Test is created on assumption of project structure having max 2 layers of nested project (Root > Project A > Project B) + assertTrue("TeamCity configuration is nested deeper than this test checks; test should be rewritten", sp.subProjects.size == 0) + + sp.buildTypes.forEach{ bt -> + assertTrue("Build '${bt.id}' should use clean checkout", bt.vcs.cleanCheckout) + } + } } } }