Skip to content

Commit

Permalink
TeamCity: Enable 'MM Upstream' projects (#8646) (hashicorp#6076)
Browse files Browse the repository at this point in the history
* Make adding a cron trigger dependent on the user-supplied environment value

* Rename helpers.kt to test_utils.kt

* Add unit tests for `ShouldAddTrigger` function

* Update capitalisation of test util function names

* Add `ENVIRONMENT` param to TeamCity projects, making it visible to non-admins

* Fix copyright header and package for new helpers.kt file

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Aug 15, 2023
1 parent 766fd07 commit 20d1dec
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .changelog/8646.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none

```
22 changes: 19 additions & 3 deletions .teamcity/components/generated/project.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ fun GoogleBeta(environment: String, manualVcsRoot: AbsoluteId, branchRef: String
val preSweeperConfig = buildConfigurationForSweeper("Pre-Sweeper", sweepers, providerName, manualVcsRoot, configuration)
val postSweeperConfig = buildConfigurationForSweeper("Post-Sweeper", sweepers, providerName, manualVcsRoot, configuration)

// Add trigger to last step of build chain (post-sweeper)
val triggerConfig = NightlyTriggerConfiguration(environment, branchRef)
postSweeperConfig.addTrigger(triggerConfig)
// Add trigger to last step of build chain (post-sweeper) if the project allows
if (ShouldAddTrigger(environment)){
val triggerConfig = NightlyTriggerConfiguration(environment, branchRef)
postSweeperConfig.addTrigger(triggerConfig)
}


return Project{

Expand Down Expand Up @@ -56,6 +59,9 @@ fun GoogleBeta(environment: String, manualVcsRoot: AbsoluteId, branchRef: String

params {
param("BRANCH_NAME", branchRef)

// Not used, but making `environment` a param makes the value visible to non-admins in TeamCity
param("ENVIRONMENT", environment)
}
}
}
Expand Down Expand Up @@ -85,6 +91,16 @@ fun buildConfigurationForSweeper(sweeperName: String, packages: Map<String, Map<
return s.sweeperBuildConfig(sweeperName, sweeperPath, providerName, manualVcsRoot, defaultParallelism, environmentVariables)
}

fun ShouldAddTrigger(environment: String): Boolean {
if (environment == MM_UPSTREAM) {
// The 'MM uptream' projects are only ever used for ad hoc builds,
// never run on a schedule, so no cron trigger is needed
return false
}

return true
}

class NightlyTriggerConfiguration(environment: String, branchRef: String, nightlyTestsEnabled: Boolean = true, startHour: Int = defaultStartHour, daysOfWeek: String = defaultDaysOfWeek, daysOfMonth: String = defaultDaysOfMonth) {

// Default values are used below unless
Expand Down
1 change: 1 addition & 0 deletions .teamcity/components/generated/settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ const val defaultBuildTimeoutDuration = 60 * 12 //12 hours in minutes
// Values that `environment` parameter is checked against,
// when deciding to change how TeamCity objects are configured
const val MAJOR_RELEASE_TESTING = "major-release-5.0.0-testing"
const val MM_UPSTREAM = "mm-upstream"
6 changes: 3 additions & 3 deletions .teamcity/tests/generated/configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import useTeamCityGoTest
class ConfigurationTests {
@Test
fun buildShouldFailOnError() {
val project = GoogleBeta("default", TestVcsRootId(), "refs/heads/main", TestConfiguration())
val project = GoogleBeta("default", testVcsRootId(), "refs/heads/main", testConfiguration())
project.buildTypes.forEach { bt ->
assertTrue("Build '${bt.id}' should fail on errors!", bt.failureConditions.errorMessage)
}
}

@Test
fun buildShouldHaveGoTestFeature() {
val project = GoogleBeta("default", TestVcsRootId(), "refs/heads/main",TestConfiguration())
val project = GoogleBeta("default", testVcsRootId(), "refs/heads/main",testConfiguration())
project.buildTypes.forEach{ bt ->
var exists = false
bt.features.items.forEach { f ->
Expand All @@ -42,7 +42,7 @@ class ConfigurationTests {
// Once I have the ability to run tests I'll address this - writing new tests for the new config
// @Test
// fun buildShouldHaveTrigger() {
// val project = Google("default", TestVcsRootId(), "refs/heads/main", TestConfiguration())
// val project = Google("default", testVcsRootId(), "refs/heads/main", testConfiguration())
// var exists = false
// project.buildTypes.forEach{ bt ->
// bt.triggers.items.forEach { t ->
Expand Down
2 changes: 1 addition & 1 deletion .teamcity/tests/generated/vcs_roots.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.junit.Test
class VcsTests {
@Test
fun buildsHaveCleanCheckOut() {
val project = GoogleBeta("default", TestVcsRootId(), "refs/heads/main", TestConfiguration())
val project = GoogleBeta("default", testVcsRootId(), "refs/heads/main", testConfiguration())
project.buildTypes.forEach { bt ->
assertTrue("Build '${bt.id}' doesn't use clean checkout", bt.vcs.cleanCheckout)
}
Expand Down
29 changes: 22 additions & 7 deletions .teamcity/tests/helpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,29 @@

package tests

import jetbrains.buildServer.configs.kotlin.AbsoluteId
import org.junit.Assert.assertTrue
import org.junit.Assert.assertFalse
import org.junit.Test
import ShouldAddTrigger
import MM_UPSTREAM
import MAJOR_RELEASE_TESTING

import ClientConfiguration
class HelperTests {
@Test
fun funShouldAddTrigger_random_string() {
val environment = "foobar"
assertTrue("Cron triggers should be added to projects with a random environment value" , ShouldAddTrigger(environment))
}

fun TestConfiguration() : ClientConfiguration {
return ClientConfiguration("custId", "org", "org2", "billingAccount", "billingAccount2", "masterBillingAccount", "credentials", "project", "orgDomain", "projectNumber", "region", "serviceAccount", "zone", "firestoreProject", "identityUser")
}
@Test
fun funShouldAddTrigger_MAJOR_RELEASE_TESTING() {
val environment = MAJOR_RELEASE_TESTING
assertTrue("Cron triggers should be added to projects used for testing the 5.0.0 major release" , ShouldAddTrigger(environment))
}

fun TestVcsRootId() : AbsoluteId {
return AbsoluteId("TerraformProviderFoobar")
@Test
fun funShouldAddTrigger_MM_UPSTREAM() {
val environment = MM_UPSTREAM
assertFalse("Cron triggers should NOT be added to projects using the MM upstream repo" , ShouldAddTrigger(environment))
}
}
20 changes: 20 additions & 0 deletions .teamcity/tests/test_utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/

// this file is copied from mmv1, any changes made here will be overwritten

package tests

import jetbrains.buildServer.configs.kotlin.AbsoluteId

import ClientConfiguration

fun testConfiguration() : ClientConfiguration {
return ClientConfiguration("custId", "org", "org2", "billingAccount", "billingAccount2", "masterBillingAccount", "credentials", "project", "orgDomain", "projectNumber", "region", "serviceAccount", "zone", "firestoreProject", "identityUser")
}

fun testVcsRootId() : AbsoluteId {
return AbsoluteId("TerraformProviderFoobar")
}

0 comments on commit 20d1dec

Please sign in to comment.