Skip to content

Commit

Permalink
Added tests for Keys. Changed separator. (#2101)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkirillov authored and robzienert committed Nov 16, 2017
1 parent 2121c80 commit f3616a4
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* * Copyright 2017 Lookout, Inc.
* Copyright 2017 Lookout, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,7 +34,7 @@ public enum Namespace {
CONTAINER_INSTANCES,
TASK_DEFINITIONS;

final String ns;
public final String ns;

Namespace() {
ns = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, this.name());
Expand All @@ -45,7 +45,27 @@ public String toString() {
}
}

public static final String SEPARATOR = ":";
public static final String SEPARATOR = ";";

@Override
public String getCloudProvider() {
return ID;
}

@Override
public Map<String, String> parseKey(String key) {
return parse(key);
}

@Override
public Boolean canParseType(String type) {
for (Namespace key : Namespace.values()) {
if (key.toString().equals(type)) {
return true;
}
}
return false;
}

public static Map<String, String> parse(String key) {
String[] parts = key.split(SEPARATOR);
Expand Down Expand Up @@ -74,7 +94,7 @@ public static Map<String, String> parse(String key) {
result.put("clusterName", parts[4]);
break;
case TASKS:
result.put("taskName", parts[4]);
result.put("taskId", parts[4]);
break;
case CONTAINER_INSTANCES:
result.put("containerInstanceArn", parts[4]);
Expand All @@ -91,56 +111,40 @@ public static Map<String, String> parse(String key) {
return result;
}

@Override
public Boolean canParseField(String type) {
return false;
}

public static String getServiceKey(String account, String region, String serviceName) {
return ID + SEPARATOR + Namespace.SERVICES + SEPARATOR + account + SEPARATOR + region + SEPARATOR + serviceName;
return buildKey(Namespace.SERVICES.ns, account, region, serviceName);
}

public static String getClusterKey(String account, String region, String clusterName) {
return ID + SEPARATOR + Namespace.ECS_CLUSTERS + SEPARATOR + account + SEPARATOR + region + SEPARATOR + clusterName;
return buildKey(Namespace.ECS_CLUSTERS.ns, account, region, clusterName);
}

public static String getTaskKey(String account, String region, String taskId) {
return ID + SEPARATOR + Namespace.TASKS + SEPARATOR + account + SEPARATOR + region + SEPARATOR + taskId;
return buildKey(Namespace.TASKS.ns, account, region, taskId);
}

public static String getTaskHealthKey(String account, String region, String taskId) {
return ID + SEPARATOR + HEALTH + SEPARATOR + account + SEPARATOR + region + SEPARATOR + taskId;
return buildKey(HEALTH.getNs(), account, region, taskId);
}

public static String getContainerInstanceKey(String account, String region, String containerInstanceArn) {
return ID + SEPARATOR + Namespace.CONTAINER_INSTANCES + SEPARATOR + account + SEPARATOR + region + SEPARATOR + containerInstanceArn;
return buildKey(Namespace.CONTAINER_INSTANCES.ns, account, region, containerInstanceArn);
}

public static String getTaskDefinitionKey(String account, String region, String taskDefinitionArn) {
return ID + SEPARATOR + Namespace.TASK_DEFINITIONS + SEPARATOR + account + SEPARATOR + region + SEPARATOR + taskDefinitionArn;
return buildKey(Namespace.TASK_DEFINITIONS.ns, account, region, taskDefinitionArn);
}

public static String getIamRoleKey(String account, String iamRoleName) {
return ID + SEPARATOR + Namespace.IAM_ROLE + SEPARATOR + account + SEPARATOR + iamRoleName;
}

@Override
public String getCloudProvider() {
return ID;
}

@Override
public Map<String, String> parseKey(String key) {
return parse(key);
}

@Override
public Boolean canParseType(String type) {
for (Namespace key : Namespace.values()) {
if (key.toString().equals(type)) {
return true;
}
}
return false;
}

@Override
public Boolean canParseField(String type) {
return false;
private static String buildKey(String namespace,String account, String region, String identifier){
return ID + SEPARATOR + namespace + SEPARATOR + account + SEPARATOR + region + SEPARATOR + identifier;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright 2017 Lookout, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.clouddriver.ecs.cache

import spock.lang.Specification

import static com.netflix.spinnaker.clouddriver.ecs.EcsCloudProvider.ID
import static com.netflix.spinnaker.clouddriver.ecs.cache.Keys.Namespace.*
import static com.netflix.spinnaker.clouddriver.ecs.cache.Keys.SEPARATOR
import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.HEALTH;

class KeysSpec extends Specification {

def buildKey(String namespace, String account, String region, String identifier) {
return ID + SEPARATOR + namespace + SEPARATOR + account + SEPARATOR + region + SEPARATOR + identifier
}

def buildParsedKey(String account, String region, String namespace, Map keySpecificMap) {
return [provider: ID, type: namespace, account: account, region: region] << keySpecificMap
}

def 'should parse a given key properly'() {
expect:
Keys.parse(buildKey(namespace, account, region, identifier as String)) == parsedKey

where:
account | region | namespace | identifier | parsedKey
'test-account-1' | 'us-west-1' | TASKS.ns | '1dc5c17a-422b-4dc4-b493-371970c6c4d6' | buildParsedKey(account, region, namespace, [taskId: identifier])
'test-account-2' | 'us-west-2' | SERVICES.ns | 'test-stack-detail-v001' | buildParsedKey(account, region, namespace, [serviceName: identifier])
'test-account-3' | 'us-west-3' | ECS_CLUSTERS.ns | 'test-cluster-1' | buildParsedKey(account, region, namespace, [clusterName: identifier])
'test-account-4' | 'us-west-4' | CONTAINER_INSTANCES.ns | 'arn:aws:ecs:' + region + ':012345678910:container-instance/14e8cce9-0b16-4af4-bfac-a85f7587aa98' | buildParsedKey(account, region, namespace, [containerInstanceArn: identifier])
'test-account-5' | 'us-west-5' | TASK_DEFINITIONS.ns | 'arn:aws:ecs:' + region + ':012345678910:task-definition/hello_world:10' | buildParsedKey(account, region, namespace, [taskDefinitionArn: identifier])

}

def 'should parse a given iam role key properly'() {
expect:
Keys.parse(ID + SEPARATOR + IAM_ROLE.ns + SEPARATOR + account + SEPARATOR + roleName) == [provider: ID, type: IAM_ROLE.ns, account: account, roleName: roleName]

where:
account | roleName
'test-account-1' | 'iam-role-name-1'
'test-account-2' | 'iam-role-name-2'
}

def 'should generate the proper iam role key'() {
expect:
Keys.getIamRoleKey(account, roleName) == ID + SEPARATOR + IAM_ROLE.ns + SEPARATOR + account + SEPARATOR + roleName

where:
account | roleName
'test-account-1' | 'am-role-name-1'
'test-account-2' | 'am-role-name-2'
}

def 'should generate the proper task key'() {
expect:
Keys.getTaskKey(account, region, taskId) == buildKey(TASKS.ns, account, region, taskId)

where:
region | account | taskId
'us-west-1' | 'test-account-1' | '1dc5c17a-422b-4dc4-b493-371970c6c4d6'
'us-west-2' | 'test-account-2' | 'deadbeef-422b-4dc4-b493-371970c6c4d6'
}

def 'should generate the proper service key'() {
expect:
Keys.getServiceKey(account, region, serviceName) == buildKey(SERVICES.ns, account, region, serviceName)

where:
region | account | serviceName
'us-west-1' | 'test-account-1' | '1dc5c17a-422b-4dc4-b493-371970c6c4d6'
'us-west-2' | 'test-account-2' | 'deadbeef-422b-4dc4-b493-371970c6c4d6'
}

def 'should generate the proper cluster key'() {
expect:
Keys.getClusterKey(account, region, clusterName) == buildKey(ECS_CLUSTERS.ns, account, region, clusterName)

where:
region | account | clusterName
'us-west-1' | 'test-account-1' | 'test-cluster-1'
'us-west-2' | 'test-account-2' | 'test-cluster-2'
}

def 'should generate the proper container instance key'() {
expect:
Keys.getContainerInstanceKey(account, region, containerArn) == buildKey(CONTAINER_INSTANCES.ns, account, region, containerArn)

where:
region | account | containerArn
'us-west-1' | 'test-account-1' | 'arn:aws:ecs:' + region + ':012345678910:container-instance/14e8cce9-0b16-4af4-bfac-a85f7587aa98'
'us-west-2' | 'test-account-2' | 'arn:aws:ecs:' + region + ':012345678910:container-instance/deadbeef-0b16-4af4-bfac-a85f7587aa98'
}

def 'should generate the proper task definition key'() {
expect:
Keys.getTaskDefinitionKey(account, region, taskDefArn) == buildKey(TASK_DEFINITIONS.ns, account, region, taskDefArn)

where:
region | account | taskDefArn
'us-west-1' | 'test-account-1' | 'arn:aws:ecs:' + region + ':012345678910:task-definition/hello_world:10'
'us-west-2' | 'test-account-2' | 'arn:aws:ecs:' + region + ':012345678910:task-definition/hello_world:20'
}

def 'should generate the proper task health key'() {
expect:
Keys.getTaskHealthKey(account, region, taskId) == buildKey(HEALTH.ns, account, region, taskId)

where:
region | account | taskId
'us-west-1' | 'test-account-1' | '1dc5c17a-422b-4dc4-b493-371970c6c4d6'
'us-west-2' | 'test-account-2' | 'deadbeef-422b-4dc4-b493-371970c6c4d6'
}
}

0 comments on commit f3616a4

Please sign in to comment.