Last active
February 16, 2019 16:53
-
-
Save dmcg/29f7ff02913fc9756952aea611b721f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ResourceStateMonitorTests : JUnit5Minutests { | |
// If these are too expensive to keep recreating | |
companion object { | |
val resourceRepository = InMemoryResourceRepository() | |
val plugin1 = mock<ResourcePlugin>() | |
val plugin2 = mock<ResourcePlugin>() | |
} | |
class Fixture { | |
val resourceRepository = resourceRepository | |
val plugin1 = plugin1 | |
val plugin2 = plugin2 | |
val monitor = ResourceStateMonitor(resourceRepository, listOf(plugin1, plugin2)) | |
val aResource = Resource( | |
apiVersion = SPINNAKER_API_V1.subApi("plugin1"), | |
kind = "foo", | |
metadata = ResourceMetadata( | |
name = ResourceName("resource1"), | |
resourceVersion = 1234L, | |
uid = UUID.randomUUID() | |
), | |
spec = DummyResource("whatever") | |
) | |
init { | |
whenever(plugin1.apiVersion) doReturn SPINNAKER_API_V1.subApi("plugin1") | |
whenever(plugin1.supportedKinds) doReturn mapOf(ResourceKind(SPINNAKER_API_V1.subApi("plugin1").group, "foo", "foos") to DummyResource::class.java) | |
whenever(plugin2.apiVersion) doReturn SPINNAKER_API_V1.subApi("plugin2") | |
whenever(plugin2.supportedKinds) doReturn mapOf(ResourceKind(SPINNAKER_API_V1.subApi("plugin2").group, "bar", "bars") to DummyResource::class.java, ResourceKind(SPINNAKER_API_V1.subApi("plugin2").group, "baz", "bazzes") to DummyResource::class.java) | |
} | |
fun givenCurrent(plugin: ResourcePlugin, resource: Resource, state: ResourceState) { | |
whenever(plugin.current(resource)) doReturn state | |
} | |
fun close() { | |
resourceRepository.dropAll() | |
reset(plugin1, plugin2) | |
} | |
} | |
fun tests() = rootContext<Fixture> { | |
fixture { | |
Fixture() | |
} | |
after { | |
fixture.close() | |
} | |
context("a managed resource exists") { | |
modifyFixture { | |
resourceRepository.store(resource) | |
} | |
context("the current state matches the desired state") { | |
modifyFixture { | |
givenCurrent(plugin1, aResource, ResourceState(aResource.spec)) | |
} | |
test("the resource is not updated") { | |
monitor.validateManagedResources() | |
verify(plugin1, never()).create(any()) | |
verify(plugin1, never()).update(any()) | |
verify(plugin1, never()).delete(any()) | |
} | |
test("only the relevant plugin is queried") { | |
monitor.validateManagedResources() | |
verify(plugin2, never()).current(any()) | |
} | |
} | |
context("the current state is missing") { | |
modifyFixture { | |
givenCurrent(plugin1, aResource, ResourceMissing) | |
} | |
test("the resource is created") { | |
monitor.validateManagedResources() | |
verify(plugin1).create(aResource) | |
} | |
} | |
context("the current state is wrong") { | |
modifyFixture { | |
givenCurrent(plugin1, aResource, ResourceState(DummyResource("some other state that does not match")) | |
} | |
test("the resource is updated") { | |
monitor.validateManagedResources() | |
verify(plugin1).update(aResource) | |
} | |
} | |
} | |
} | |
} | |
private data class DummyResource(val state: String) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment