This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
TestAWSResource.java
172 lines (151 loc) · 6.4 KB
/
TestAWSResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
*
* Copyright 2012 Netflix, 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.
*
*/
// CHECKSTYLE IGNORE Javadoc
// CHECKSTYLE IGNORE MagicNumberCheck
/*
*
* Copyright 2012 Netflix, 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.simianarmy.aws.janitor;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.netflix.simianarmy.Resource;
import com.netflix.simianarmy.aws.AWSResource;
import com.netflix.simianarmy.aws.AWSResourceType;
public class TestAWSResource {
/** Make sure the getFieldToValue returns the right field and values.
* @throws Exception **/
@Test
public void testFieldToValueMapWithoutNullForInstance() throws Exception {
Date now = new Date();
Resource resource = getTestingResource(now);
Map<String, String> resourceFieldValueMap = resource.getFieldToValueMap();
verifyMapsAreEqual(resourceFieldValueMap,
getTestingFieldValueMap(now, getTestingFields()));
}
/**
* When all fields are null, the map returned is empty.
*/
@Test
public void testFieldToValueMapWithNull() {
Resource resource = new AWSResource();
Map<String, String> resourceFieldValueMap = resource.getFieldToValueMap();
// The only value in the map is the boolean of opt out
Assert.assertEquals(resourceFieldValueMap.size(), 1);
}
@Test
public void testParseFieldToValueMap() throws Exception {
Date now = new Date();
Map<String, String> map = getTestingFieldValueMap(now, getTestingFields());
AWSResource resource = AWSResource.parseFieldtoValueMap(map);
Map<String, String> resourceFieldValueMap = resource.getFieldToValueMap();
verifyMapsAreEqual(resourceFieldValueMap, map);
}
@Test
public void testClone() {
Date now = new Date();
Resource resource = getTestingResource(now);
Resource clone = resource.cloneResource();
verifyMapsAreEqual(clone.getFieldToValueMap(), resource.getFieldToValueMap());
verifyTagsAreEqual(clone, resource);
}
private void verifyMapsAreEqual(Map<String, String> map1, Map<String, String> map2) {
Assert.assertFalse(map1 == null ^ map2 == null);
Assert.assertEquals(map1.size(), map2.size());
for (Map.Entry<String, String> entry : map1.entrySet()) {
Assert.assertEquals(entry.getValue(), map2.get(entry.getKey()));
}
}
private void verifyTagsAreEqual(Resource r1, Resource r2) {
Collection<String> keys1 = r1.getAllTagKeys();
Collection<String> keys2 = r2.getAllTagKeys();
Assert.assertEquals(keys1.size(), keys2.size());
for (String key : keys1) {
Assert.assertEquals(r1.getTag(key), r2.getTag(key));
}
}
private Map<String, String> getTestingFieldValueMap(Date defaultDate, Map<String, String> additionalFields)
throws Exception {
Field[] fields = AWSResource.class.getFields();
Map<String, String> fieldToValue = new HashMap<String, String>();
String dateString = AWSResource.DATE_FORMATTER.print(defaultDate.getTime());
for (Field field : fields) {
if (field.getName().startsWith("FIELD_")) {
String value;
String key = (String) (field.get(null));
if (field.getName().endsWith("TIME")) {
value = dateString;
} else if (field.getName().equals("FIELD_STATE")) {
value = "MARKED";
} else if (field.getName().equals("FIELD_RESOURCE_TYPE")) {
value = "INSTANCE";
} else if (field.getName().equals("FIELD_OPT_OUT_OF_JANITOR")) {
value = "false";
} else {
value = (String) (field.get(null));
}
fieldToValue.put(key, value);
}
}
if (additionalFields != null) {
fieldToValue.putAll(additionalFields);
}
return fieldToValue;
}
private Resource getTestingResource(Date now) {
String id = "resourceId";
Resource resource = new AWSResource().withId(id).withRegion("region").withResourceType(AWSResourceType.INSTANCE)
.withState(Resource.CleanupState.MARKED).withDescription("description")
.withExpectedTerminationTime(now).withActualTerminationTime(now)
.withLaunchTime(now).withMarkTime(now).withNnotificationTime(now).withOwnerEmail("ownerEmail")
.withTerminationReason("terminationReason").withOptOutOfJanitor(false);
((AWSResource) resource).setAWSResourceState("awsResourceState");
for (Map.Entry<String, String> field : getTestingFields().entrySet()) {
resource.setAdditionalField(field.getKey(), field.getValue());
}
for (int i = 1; i < 10; i++) {
resource.setTag("tagKey_" + i, "tagValue_" + i);
}
return resource;
}
private Map<String, String> getTestingFields() {
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < 10; i++) {
map.put("name" + i, "value" + i);
}
return map;
}
}