Skip to content

Commit

Permalink
Add tests for MigrationRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
jervi committed Apr 24, 2024
1 parent 9039d2a commit 6646eef
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
1 change: 1 addition & 0 deletions front50-migrations/front50-migrations.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ dependencies {
implementation "org.springframework.boot:spring-boot-autoconfigure"

testImplementation project(":front50-test")
testImplementation "org.spockframework:spock-spring"
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
import org.springframework.stereotype.Component;

/**
* Migration runner runs all the registered migrations as scheduled. By default migration runner
* will <strong>not</strong> run and need to set <strong>`migrations.enabled`</strong> property to
* enable it.
* Migration runner runs all the registered migrations as scheduled. By default, migration runner
* will <strong>not</strong> run and need to set <code>migrations.enabled</code> property to enable
* it. The interval between migration runs can be set using <code>migrations.intervalMs</code>, and
* the initial delay before running the first migration can be set using <code>
* migrations.initialDelayMs</code> (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.
*
* <p>Note: Ideally migrations should be running only on one instance.
*/
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 6646eef

Please sign in to comment.