|
| 1 | +package com.netflix.spinnaker.keel.ec2.resource |
| 2 | + |
| 3 | +import com.netflix.spinnaker.keel.api.Moniker |
| 4 | +import com.netflix.spinnaker.keel.api.SimpleLocations |
| 5 | +import com.netflix.spinnaker.keel.api.SimpleRegionSpec |
| 6 | +import com.netflix.spinnaker.keel.api.actuation.TaskLauncher |
| 7 | +import com.netflix.spinnaker.keel.api.ec2.AllPorts |
| 8 | +import com.netflix.spinnaker.keel.api.ec2.EC2_SECURITY_GROUP_V1 |
| 9 | +import com.netflix.spinnaker.keel.api.ec2.ReferenceRule |
| 10 | +import com.netflix.spinnaker.keel.api.ec2.SecurityGroupOverride |
| 11 | +import com.netflix.spinnaker.keel.api.ec2.SecurityGroupRule.Protocol.TCP |
| 12 | +import com.netflix.spinnaker.keel.api.ec2.SecurityGroupSpec |
| 13 | +import com.netflix.spinnaker.keel.clouddriver.CloudDriverCache |
| 14 | +import com.netflix.spinnaker.keel.clouddriver.CloudDriverService |
| 15 | +import com.netflix.spinnaker.keel.orca.OrcaService |
| 16 | +import com.netflix.spinnaker.keel.test.resource |
| 17 | +import io.mockk.mockk |
| 18 | +import kotlinx.coroutines.runBlocking |
| 19 | +import org.junit.jupiter.api.Test |
| 20 | +import strikt.api.expect |
| 21 | +import strikt.assertions.hasSize |
| 22 | +import strikt.assertions.isNotNull |
| 23 | + |
| 24 | +class SecurityGroupOverrideTests { |
| 25 | + val cloudDriverService = mockk<CloudDriverService>() |
| 26 | + val cloudDriverCache = mockk<CloudDriverCache>() |
| 27 | + val orcaService = mockk<OrcaService>() |
| 28 | + val taskLauncher = mockk<TaskLauncher>() |
| 29 | + |
| 30 | + val securityGroupHandler = SecurityGroupHandler( |
| 31 | + cloudDriverService = cloudDriverService, |
| 32 | + cloudDriverCache = cloudDriverCache, |
| 33 | + orcaService = orcaService, |
| 34 | + taskLauncher = taskLauncher, |
| 35 | + resolvers = emptyList() |
| 36 | + ) |
| 37 | + |
| 38 | + @Test |
| 39 | + fun `can merge ingress rules specified in overrides`() { |
| 40 | + val spec = SecurityGroupSpec( |
| 41 | + moniker = Moniker( |
| 42 | + app = "fnord" |
| 43 | + ), |
| 44 | + locations = SimpleLocations( |
| 45 | + account = "test", |
| 46 | + regions = setOf( |
| 47 | + SimpleRegionSpec( |
| 48 | + name = "uk-east-17" |
| 49 | + ), |
| 50 | + SimpleRegionSpec( |
| 51 | + name = "ap-south-1" |
| 52 | + ) |
| 53 | + ) |
| 54 | + ), |
| 55 | + description = "catflap rubberplant marzipan", |
| 56 | + inboundRules = setOf( |
| 57 | + ReferenceRule( |
| 58 | + protocol = TCP, |
| 59 | + name = "common", |
| 60 | + portRange = AllPorts |
| 61 | + ) |
| 62 | + ), |
| 63 | + overrides = mapOf( |
| 64 | + "uk-east-17" to SecurityGroupOverride( |
| 65 | + inboundRules = setOf( |
| 66 | + ReferenceRule( |
| 67 | + protocol = TCP, |
| 68 | + name = "one-region-only", |
| 69 | + portRange = AllPorts |
| 70 | + ) |
| 71 | + ) |
| 72 | + ) |
| 73 | + ) |
| 74 | + ) |
| 75 | + |
| 76 | + val materialized = runBlocking { |
| 77 | + securityGroupHandler.desired(resource(kind = EC2_SECURITY_GROUP_V1.kind, spec = spec)) |
| 78 | + } |
| 79 | + |
| 80 | + expect { |
| 81 | + that(materialized["uk-east-17"]?.inboundRules).isNotNull().hasSize(2) |
| 82 | + that(materialized["ap-south-1"]?.inboundRules).isNotNull().hasSize(1) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments