Skip to content

Commit

Permalink
TeamCity: Make service sweepers run before project sweepers, assert t…
Browse files Browse the repository at this point in the history
…hat in a test (#10233) (#17710)

[upstream:fbcec50e00e7d9a571fe49ab1dcc6c56e9fae19c]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Mar 27, 2024
1 parent b52ffdb commit a503511
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/10233.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none

```
2 changes: 1 addition & 1 deletion .teamcity/components/projects/reused/nightly_tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fun nightlyTests(parentProject:String, providerName: String, vcsRoot: GitVcsRoot
else -> throw Exception("Provider name not supplied when generating a nightly test subproject")
}
val serviceSweeperConfig = BuildConfigurationForServiceSweeper(providerName, ServiceSweeperName, sweepersList, projectId, vcsRoot, sharedResources, config)
val sweeperTrigger = NightlyTriggerConfiguration(startHour=12) // Override hour
val sweeperTrigger = NightlyTriggerConfiguration(startHour=11) // Override hour
serviceSweeperConfig.addTrigger(sweeperTrigger)

return Project {
Expand Down
38 changes: 38 additions & 0 deletions .teamcity/tests/sweepers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

package tests

import ProjectSweeperName
import ServiceSweeperName
import jetbrains.buildServer.configs.kotlin.BuildType
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

Expand Down Expand Up @@ -102,4 +104,40 @@ class SweeperTests {
val value = sweeper!!.params.findRawParam("PACKAGE_PATH")!!.value
assertEquals("./google-beta/sweeper", value)
}

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

// Find GA nightly test project's service sweeper
val gaNightlyTests: Project = getSubProject(project, gaProjectName, nightlyTestsProjectName)
val sweeperGa: BuildType = getBuildFromProject(gaNightlyTests, ServiceSweeperName)

// Find Beta nightly test project's service sweeper
val betaNightlyTests : Project = getSubProject(project, 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 projectSweeper: BuildType = getBuildFromProject(projectSweeperProject!!, ProjectSweeperName)

// Check only one schedule trigger is on the builds in question
assertTrue(sweeperGa.triggers.items.size == 1)
assertTrue(sweeperBeta.triggers.items.size == 1)
assertTrue(projectSweeper.triggers.items.size == 1)

// Assert that the hour value that sweeper builds are triggered at is less than the hour value that project sweeper builds are triggered at
// i.e. sweeper builds are triggered first
val stGa = sweeperGa.triggers.items[0] as ScheduleTrigger
val cronGa = stGa.schedulingPolicy as ScheduleTrigger.SchedulingPolicy.Cron
val stBeta = sweeperBeta.triggers.items[0] as ScheduleTrigger
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() )
}
}
15 changes: 13 additions & 2 deletions .teamcity/tests/test_utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
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

Expand Down Expand Up @@ -56,15 +58,24 @@ fun testContextParameters(): AllContextParameters {

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

return subProject!!
}

fun getBuildFromProject(parentProject: Project, buildName: String): BuildType {
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}")
}

return buildType!!
}

0 comments on commit a503511

Please sign in to comment.