From 6646eef90dac015f114cf1b3d1737b08c06ee00f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Jervidalo?= Date: Wed, 24 Apr 2024 15:49:40 +0200 Subject: [PATCH] Add tests for MigrationRunner --- front50-migrations/front50-migrations.gradle | 1 + .../front50/migrations/MigrationRunner.java | 15 ++++--- .../front50/config/MigrationConfigTest.java | 19 +++++++++ .../migrations/MigrationRunnerSpec.groovy | 40 +++++++++++++++++++ 4 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 front50-migrations/src/test/groovy/com/netflix/spinnaker/front50/config/MigrationConfigTest.java create mode 100644 front50-migrations/src/test/groovy/com/netflix/spinnaker/front50/migrations/MigrationRunnerSpec.groovy diff --git a/front50-migrations/front50-migrations.gradle b/front50-migrations/front50-migrations.gradle index 11107ebcf..acf51478b 100644 --- a/front50-migrations/front50-migrations.gradle +++ b/front50-migrations/front50-migrations.gradle @@ -24,4 +24,5 @@ dependencies { implementation "org.springframework.boot:spring-boot-autoconfigure" testImplementation project(":front50-test") + testImplementation "org.spockframework:spock-spring" } diff --git a/front50-migrations/src/main/java/com/netflix/spinnaker/front50/migrations/MigrationRunner.java b/front50-migrations/src/main/java/com/netflix/spinnaker/front50/migrations/MigrationRunner.java index cceb3d9e9..abefd4872 100644 --- a/front50-migrations/src/main/java/com/netflix/spinnaker/front50/migrations/MigrationRunner.java +++ b/front50-migrations/src/main/java/com/netflix/spinnaker/front50/migrations/MigrationRunner.java @@ -26,9 +26,12 @@ import org.springframework.stereotype.Component; /** - * Migration runner runs all the registered migrations as scheduled. By default migration runner - * will not run and need to set `migrations.enabled` property to - * enable it. + * Migration runner runs all the registered migrations as scheduled. By default, migration runner + * will not run and need to set migrations.enabled property to enable + * it. The interval between migration runs can be set using migrations.intervalMs, and + * the initial delay before running the first migration can be set using + * migrations.initialDelayMs (can be useful if you initialize migrations in plugins, and they + * need some time to start up). Default values are 8 hours and 10 seconds respectively. * *

Note: Ideally migrations should be running only on one instance. */ @@ -43,8 +46,10 @@ public MigrationRunner(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } - /** Run migrations every 8hrs. */ - @Scheduled(fixedDelay = 28800000, initialDelay = 10000) + /** Run migrations every 8hrs (by default). */ + @Scheduled( + fixedDelayString = "${migrations.intervalMs:28800000}", + initialDelayString = "${migrations.initialDelayMs:10000}") void run() { applicationContext.getBeansOfType(Migration.class).values().stream() .filter(Migration::isValid) diff --git a/front50-migrations/src/test/groovy/com/netflix/spinnaker/front50/config/MigrationConfigTest.java b/front50-migrations/src/test/groovy/com/netflix/spinnaker/front50/config/MigrationConfigTest.java new file mode 100644 index 000000000..36a7c36d2 --- /dev/null +++ b/front50-migrations/src/test/groovy/com/netflix/spinnaker/front50/config/MigrationConfigTest.java @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2024 Schibsted ASA. All rights reserved + */ + +package com.netflix.spinnaker.front50.config; + +import com.netflix.spinnaker.front50.migrations.MigrationRunner; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; + +@TestConfiguration +public class MigrationConfigTest { + + @Bean + public MigrationRunner migrationRunner(ApplicationContext applicationContext) { + return new MigrationRunner(applicationContext); + } +} diff --git a/front50-migrations/src/test/groovy/com/netflix/spinnaker/front50/migrations/MigrationRunnerSpec.groovy b/front50-migrations/src/test/groovy/com/netflix/spinnaker/front50/migrations/MigrationRunnerSpec.groovy new file mode 100644 index 000000000..cfde9305c --- /dev/null +++ b/front50-migrations/src/test/groovy/com/netflix/spinnaker/front50/migrations/MigrationRunnerSpec.groovy @@ -0,0 +1,40 @@ +package com.netflix.spinnaker.front50.migrations + +import com.netflix.spinnaker.front50.config.MigrationConfigTest +import org.spockframework.spring.SpringBean +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.test.context.ContextConfiguration +import spock.lang.Specification +import spock.lang.Subject + +@ContextConfiguration(classes = [MigrationConfigTest, MigrationRunner]) +class MigrationRunnerSpec extends Specification { + @SpringBean(name = "myFirstMigration") + Migration myFirstMigration = Mock() + + @SpringBean(name = "mySecondMigration") + Migration mySecondMigration = Mock() + + @SpringBean(name = "disabledMigration") + Migration disabledMigration = Mock() + + @Subject + @Autowired + MigrationRunner migrationRunner + + def setup() { + myFirstMigration.isValid() >> true + mySecondMigration.isValid() >> true + disabledMigration.isValid() >> false + } + + def "should run all enabled migrations"() { + when: + migrationRunner.run() + + then: + 1 * myFirstMigration.run() + 1 * mySecondMigration.run() + 0 * disabledMigration.run() + } +}