Skip to content

Commit

Permalink
TeamCity: Refactor config tests (GoogleCloudPlatform#9956)
Browse files Browse the repository at this point in the history
* Add test util for locating a subproject 2 layers deep

* Refactor tests to use new helper function

* Consolidate testing of service sweeper builds, refactor `getSubProject` to return non-nullable value

* Refactor new sweeper tests
  • Loading branch information
SarahFrench authored and balanaguharsha committed May 2, 2024
1 parent 5be4897 commit 36358ed
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,13 @@ class NightlyTestProjectsTests {
val project = googleCloudRootProject(testContextParameters())

// Find GA nightly test project
var gaProject: Project? = project.subProjects.find { p-> p.name == gaProjectName}
if (gaProject == null) {
Assert.fail("Could not find the Google (GA) project")
}
var gaNightlyTestProject: Project? = gaProject!!.subProjects.find { p-> p.name == nightlyTestsProjectName}
if (gaNightlyTestProject == null) {
Assert.fail("Could not find the Google (GA) Nightly Test project")
}
var gaNightlyTestProject = getSubProject(project, gaProjectName, nightlyTestsProjectName)

// Find Beta nightly test project
var betaProject: Project? = project.subProjects.find { p-> p.name == betaProjectName}
if (betaProject == null) {
Assert.fail("Could not find the Google (Beta) project")
}
var betaNightlyTestProject: Project? = betaProject!!.subProjects.find { p-> p.name == nightlyTestsProjectName}
if (betaNightlyTestProject == null) {
Assert.fail("Could not find the Google (GA) Nightly Test project")
}
var betaNightlyTestProject = getSubProject(project, betaProjectName, nightlyTestsProjectName)

(gaNightlyTestProject!!.buildTypes + betaNightlyTestProject!!.buildTypes).forEach{bt ->
// 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())
// Look for at least one CRON trigger
var found: Boolean = false
Expand Down
89 changes: 26 additions & 63 deletions mmv1/third_party/terraform/.teamcity/tests/sweepers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import projects.googleCloudRootProject

class SweeperTests {
@Test
fun projectSweeperProjectDoesNotSkipProjectSweep() {
fun projectSweeperDoesNotSkipProjectSweep() {
val project = googleCloudRootProject(testContextParameters())

// Find Project sweeper project
Expand All @@ -37,45 +37,42 @@ class SweeperTests {
}

@Test
fun gaNightlyProjectServiceSweeperSkipsProjectSweep() {
fun serviceSweepersSkipProjectSweeper() {
val project = googleCloudRootProject(testContextParameters())

// Find GA nightly test project
val gaProject: Project? = project.subProjects.find { p-> p.name == gaProjectName}
if (gaProject == null) {
Assert.fail("Could not find the Google (GA) project")
}
val gaNightlyTestProject: Project? = gaProject!!.subProjects.find { p-> p.name == nightlyTestsProjectName}
if (gaNightlyTestProject == null) {
Assert.fail("Could not find the Google (GA) Nightly Test project")
}
val gaNightlyTestProject = getSubProject(project, gaProjectName, nightlyTestsProjectName)
// Find GA MM Upstream project
val gaMmUpstreamProject = getSubProject(project, gaProjectName, mmUpstreamProjectName)

// 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")
// Find Beta nightly test project
val betaNightlyTestProject = getSubProject(project, betaProjectName, nightlyTestsProjectName)
// Find Beta MM Upstream project
val betaMmUpstreamProject = getSubProject(project, betaProjectName, mmUpstreamProjectName)

val allProjects: ArrayList<Project> = 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")
}

// 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 != "")
}

// 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, so project sweepers are skipped. Value = `${value}` ", value != "")
}

@Test
fun gaNightlyProjectServiceSweeperRunsInGoogle() {
val project = googleCloudRootProject(testContextParameters())

// Find GA nightly test project
val gaProject: Project? = project.subProjects.find { p-> p.name == gaProjectName}
if (gaProject == null) {
Assert.fail("Could not find the Google (GA) project")
}
val gaNightlyTestProject: Project? = gaProject!!.subProjects.find { p-> p.name == nightlyTestsProjectName}
if (gaNightlyTestProject == null) {
Assert.fail("Could not find the Google (GA) Nightly Test project")
}
val gaNightlyTestProject = getSubProject(project, gaProjectName, nightlyTestsProjectName)


// Find sweeper inside
val sweeper: BuildType? = gaNightlyTestProject!!.buildTypes.find { p-> p.name == ServiceSweeperName}
Expand All @@ -88,46 +85,12 @@ class SweeperTests {
assertEquals("./google/sweeper", value)
}

@Test
fun betaNightlyProjectServiceSweeperSkipsProjectSweep() {
val project = googleCloudRootProject(testContextParameters())

// Find Beta nightly test project
val betaProject: Project? = project.subProjects.find { p-> p.name == betaProjectName}
if (betaProject == null) {
Assert.fail("Could not find the Google (GA) project")
}
val betaNightlyTestProject: Project? = betaProject!!.subProjects.find { p-> p.name == nightlyTestsProjectName}
if (betaNightlyTestProject == null) {
Assert.fail("Could not find the Google (GA) Nightly Test project")
}

// 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")
}

// 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, so project sweepers are skipped. Value = `${value}` ", value != "")
}

@Test
fun betaNightlyProjectServiceSweeperRunsInGoogleBeta() {
val project = googleCloudRootProject(testContextParameters())

// Find Beta nightly test project
val betaProject: Project? = project.subProjects.find { p-> p.name == betaProjectName}
if (betaProject == null) {
Assert.fail("Could not find the Google (GA) project")
}
val betaNightlyTestProject: Project? = betaProject!!.subProjects.find { p-> p.name == nightlyTestsProjectName}
if (betaNightlyTestProject == null) {
Assert.fail("Could not find the Google (GA) Nightly Test project")
}
val betaNightlyTestProject = getSubProject(project, betaProjectName, nightlyTestsProjectName)

// Find sweeper inside
val sweeper: BuildType? = betaNightlyTestProject!!.buildTypes.find { p-> p.name == ServiceSweeperName}
Expand Down
18 changes: 18 additions & 0 deletions mmv1/third_party/terraform/.teamcity/tests/test_utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
package tests

import builds.AllContextParameters
import jetbrains.buildServer.configs.kotlin.Project
import org.junit.Assert

const val gaProjectName = "Google"
const val betaProjectName = "Google Beta"
const val nightlyTestsProjectName = "Nightly Tests"
const val mmUpstreamProjectName = "MM Upstream Testing"
const val projectSweeperProjectName = "Project Sweeper"

fun testContextParameters(): AllContextParameters {
Expand Down Expand Up @@ -49,4 +52,19 @@ fun testContextParameters(): AllContextParameters {
"zone",
"infraProject",
"vcrBucketName")
}

fun getSubProject(rootProject: Project, parentProjectName: String, subProjectName: String): Project {
// Find parent project within root
var parentProject: Project? = rootProject.subProjects.find { p-> p.name == parentProjectName}
if (parentProject == null) {
Assert.fail("Could not find the $parentProjectName project")
}
// Find subproject within parent identified above
var subProject: Project? = parentProject!!.subProjects.find { p-> p.name == subProjectName}
if (subProject == null) {
Assert.fail("Could not find the $subProjectName project")
}

return subProject!!
}

0 comments on commit 36358ed

Please sign in to comment.