Skip to content

Commit

Permalink
TeamCity: test utils refactor (GoogleCloudPlatform#10290)
Browse files Browse the repository at this point in the history
  • Loading branch information
SarahFrench authored and pawelJas committed May 16, 2024
1 parent b202cfc commit cea5211
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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<Boolean> = arrayListOf()
p.buildTypes.forEach{bt ->
bt.features.items.forEach { f ->
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
}
}
}
Expand All @@ -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")
}
}
}
Expand All @@ -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")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
73 changes: 28 additions & 45 deletions mmv1/third_party/terraform/.teamcity/tests/sweepers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<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")
}
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
Expand All @@ -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() )
}
}
32 changes: 18 additions & 14 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,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"
Expand Down Expand Up @@ -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!!
}
}
Loading

0 comments on commit cea5211

Please sign in to comment.