Skip to content

Commit

Permalink
#1337 Testing the AV targets branches
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoraboeuf committed Jul 29, 2024
1 parent 75bd0f3 commit cb8e767
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class AutoVersioningPromotionListenerServiceImpl(
.flatMap { branch ->
autoVersioningConfigurationService.getAutoVersioning(branch)
?.configurations
?.filter { config ->
config.sourceProject == promotionLevel.project.name &&
config.sourcePromotion == promotionLevel.name
}
?.map { config ->
AutoVersioningConfiguredBranch(
branch = branch,
Expand Down Expand Up @@ -126,6 +130,8 @@ class AutoVersioningPromotionListenerServiceImpl(
}
return match
} else {
// This is not supposed to happen since configurations are already filtered on
// source project & source promotion level name
tracking.reject(branchTrail, "Project & promotion names not matching")
return false
}
Expand All @@ -140,11 +146,13 @@ class AutoVersioningPromotionListenerServiceImpl(
val cacheKey = LatestSourceBranchCacheKey(branchTrail.branch, sourceBranch.project, branchTrail.configuration)
val latestSourceBranch = cache.getOrPut(cacheKey) {
LatestSourceBranchCacheValue(
autoVersioningConfigurationService.getLatestBranch(
branchTrail.branch,
sourceBranch.project,
branchTrail.configuration
)
logger.logTime("AV Source Branch get [target=${branchTrail.branch.project.name}/${branchTrail.branch.name}][source=${sourceBranch.project.name}][config=${branchTrail.configuration.targetPath}]") {
autoVersioningConfigurationService.getLatestBranch(
branchTrail.branch,
sourceBranch.project,
branchTrail.configuration
)
}
)
}.branch
// We want the promoted build to be on the latest source branch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,66 @@ abstract class AbstractAutoVersioningTestSupport : AbstractQLKTITSupport() {
autoVersioningConfigurationService.setAutoVersioning(this, init)
}

protected fun withThreeDependencies(
code: (
target: Branch,
dep1: Branch,
dep2: Branch,
dep3: Branch,
) -> Unit,
) {
asAdmin {
val dep1 = project<Branch> {
branch {
promotionLevel("GOLD")
}
}
val dep2 = project<Branch> {
branch {
promotionLevel("GOLD")
}
}
val dep3 = project<Branch> {
branch {
promotionLevel("GOLD")
}
}
mockSCMTester.withMockSCMRepository {
project {
branch {
configureMockSCMBranch()
autoVersioningConfigurationService.setupAutoVersioning(
this,
AutoVersioningConfig(
configurations = listOf(
AutoVersioningTestFixtures.sourceConfig(
sourceProject = dep1.project.name,
sourceBranch = dep1.name,
sourcePromotion = "GOLD",
targetPath = "dep1.properties",
),
AutoVersioningTestFixtures.sourceConfig(
sourceProject = dep2.project.name,
sourceBranch = dep2.name,
sourcePromotion = "GOLD",
targetPath = "dep2.properties",
),
AutoVersioningTestFixtures.sourceConfig(
sourceProject = dep3.project.name,
sourceBranch = dep3.name,
sourcePromotion = "GOLD",
targetPath = "dep3.properties",
),
)
)
)
code(this, dep1, dep2, dep3)
}
}
}
}
}

protected fun withPromotionLevelTargets(
code: (
pl: PromotionLevel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ internal class AutoVersioningConfigurationServiceIT : AbstractAutoVersioningTest
@Autowired
protected lateinit var eventSubscriptionService: EventSubscriptionService

@Test
fun `Getting the branches configured to a promotion`() {
withThreeDependencies { target, dep1, _, _ ->
val targets = autoVersioningConfigurationService.getBranchesConfiguredFor(dep1.project.name, "GOLD")
assertEquals(
listOf(target),
targets
)
}
}

@Test
fun `Using &same expression in source branch returns the same branch as the promoted build`() {
asAdmin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import net.nemerosa.ontrack.extension.av.AbstractAutoVersioningTestSupport
import net.nemerosa.ontrack.extension.av.tracking.AutoVersioningTrackingService
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import kotlin.jvm.optionals.getOrNull
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
Expand All @@ -13,6 +14,36 @@ class AutoVersioningPromotionListenerServiceIT : AbstractAutoVersioningTestSuppo
@Autowired
private lateinit var autoVersioningTrackingService: AutoVersioningTrackingService

@Autowired
private lateinit var autoVersioningPromotionListenerService: AutoVersioningPromotionListenerService

@Test
fun `Checking that AV targets only eligible project and promotion`() {
withThreeDependencies { target, dep1, _, _ ->
val pl1 = structureService.findPromotionLevelByName(
dep1.project.name, dep1.name, "GOLD"
).getOrNull() ?: error("Promotion level not found")
val tracking = autoVersioningTrackingService.startInMemoryTrail()
autoVersioningPromotionListenerService.getConfiguredBranches(pl1, tracking)
assertNotNull(tracking.trail, "Trail created") { trail ->
assertEquals(1, trail.branches.size)
val branchTrail = trail.branches.first()
assertEquals(
target.id,
branchTrail.branch.id
)
assertEquals(
true,
branchTrail.isEligible()
)
assertEquals(
dep1.project.name,
branchTrail.configuration.sourceProject
)
}
}
}

@Test
fun `Getting the trail of a promotion run`() {
withPromotionLevelTargets { pl, app1, app2 ->
Expand Down

0 comments on commit cb8e767

Please sign in to comment.