• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018, OpenCensus Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.opencensus.resource;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.junit.Assert.assertEquals;
21 
22 import com.google.common.testing.EqualsTester;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.Map;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.JUnit4;
33 
34 /** Unit tests for {@link Resource}. */
35 @RunWith(JUnit4.class)
36 public class ResourceTest {
37   @Rule public final ExpectedException thrown = ExpectedException.none();
38   private static final Resource DEFAULT_RESOURCE =
39       Resource.create(null, Collections.<String, String>emptyMap());
40   private static final Resource DEFAULT_RESOURCE_1 =
41       Resource.create("default", Collections.singletonMap("a", "100"));
42   private Resource resource1;
43   private Resource resource2;
44 
45   @Before
setUp()46   public void setUp() {
47     Map<String, String> labelMap1 = new HashMap<String, String>();
48     labelMap1.put("a", "1");
49     labelMap1.put("b", "2");
50     Map<String, String> labelMap2 = new HashMap<String, String>();
51     labelMap2.put("a", "1");
52     labelMap2.put("b", "3");
53     labelMap2.put("c", "4");
54     resource1 = Resource.create("t1", labelMap1);
55     resource2 = Resource.create("t2", labelMap2);
56   }
57 
58   @Test
testMaxLength()59   public void testMaxLength() {
60     assertThat(Resource.MAX_LENGTH).isEqualTo(255);
61   }
62 
63   @Test
testParseResourceType()64   public void testParseResourceType() {
65     String rawEnvType = "k8s.io/container";
66     String envType = Resource.parseResourceType(rawEnvType);
67     assertThat(envType).isNotNull();
68     assertEquals(rawEnvType, envType);
69   }
70 
71   @Test
testParseResourceType_Null()72   public void testParseResourceType_Null() {
73     String envType = Resource.parseResourceType(null);
74     assertThat(envType).isNull();
75   }
76 
77   @Test
testParseResourceType_DisallowUnprintableChars()78   public void testParseResourceType_DisallowUnprintableChars() {
79     thrown.expect(IllegalArgumentException.class);
80     thrown.expectMessage(
81         "Type should be a ASCII string with a length greater than 0 and not exceed "
82             + "255 characters.");
83     Resource.parseResourceType("\2ab\3cd");
84   }
85 
86   @Test
testParseResourceType_DisallowTypeNameOverMaxLength()87   public void testParseResourceType_DisallowTypeNameOverMaxLength() {
88     char[] chars = new char[Resource.MAX_LENGTH + 1];
89     Arrays.fill(chars, 'k');
90     String type = new String(chars);
91     thrown.expect(IllegalArgumentException.class);
92     thrown.expectMessage(
93         "Type should be a ASCII string with a length greater than 0 and not exceed "
94             + "255 characters.");
95     Resource.parseResourceType(type);
96   }
97 
98   @Test
testParseResourceLabels()99   public void testParseResourceLabels() {
100     Map<String, String> expectedLabelsMap = new HashMap<String, String>();
101     expectedLabelsMap.put("k8s.io/pod/name", "pod-xyz-123");
102     expectedLabelsMap.put("k8s.io/container/name", "c1");
103     expectedLabelsMap.put("k8s.io/namespace/name", "default");
104 
105     String rawEnvLabels =
106         "k8s.io/pod/name=\"pod-xyz-123\",k8s.io/container/name=\"c1\","
107             + "k8s.io/namespace/name=\"default\"";
108     Map<String, String> labelsMap = Resource.parseResourceLabels(rawEnvLabels);
109     assertEquals(expectedLabelsMap, labelsMap);
110     assertEquals(3, labelsMap.size());
111   }
112 
113   @Test
testParseResourceLabels_WithSpaces()114   public void testParseResourceLabels_WithSpaces() {
115     Map<String, String> expectedLabelsMap = new HashMap<String, String>();
116     expectedLabelsMap.put("example.org/test-1", "test $ \\\"");
117     expectedLabelsMap.put("Abc", "Def");
118 
119     String rawEnvLabels = "example.org/test-1=\"test $ \\\"\" ,  Abc=\"Def\"";
120     Map<String, String> labelsMap = Resource.parseResourceLabels(rawEnvLabels);
121     assertEquals(expectedLabelsMap, labelsMap);
122     assertEquals(2, labelsMap.size());
123   }
124 
125   @Test
testParseResourceLabels_SingleKey()126   public void testParseResourceLabels_SingleKey() {
127     Map<String, String> expectedLabelsMap = new HashMap<String, String>();
128     expectedLabelsMap.put("single", "key");
129 
130     String rawEnvLabels = "single=\"key\"";
131     Map<String, String> labelsMap = Resource.parseResourceLabels(rawEnvLabels);
132     assertEquals(1, labelsMap.size());
133     assertEquals(expectedLabelsMap, labelsMap);
134   }
135 
136   @Test
testParseResourceLabels_Null()137   public void testParseResourceLabels_Null() {
138     Map<String, String> labelsMap = Resource.parseResourceLabels(null);
139     assertThat(labelsMap).isNotNull();
140     assertThat(labelsMap).isEmpty();
141   }
142 
143   @Test
testParseResourceLabels_DisallowUnprintableChars()144   public void testParseResourceLabels_DisallowUnprintableChars() {
145     String rawEnvLabels = "example.org/test-1=\2ab\3cd";
146     thrown.expect(IllegalArgumentException.class);
147     thrown.expectMessage(
148         "Label value should be a ASCII string with a length not exceed 255 characters.");
149     Resource.parseResourceLabels(rawEnvLabels);
150   }
151 
152   @Test
testParseResourceLabels_DisallowLabelKeyOverMaxLength()153   public void testParseResourceLabels_DisallowLabelKeyOverMaxLength() {
154     char[] chars = new char[Resource.MAX_LENGTH + 1];
155     Arrays.fill(chars, 'k');
156     String rawEnvLabels = new String(chars) + "=test-1";
157     thrown.expect(IllegalArgumentException.class);
158     thrown.expectMessage(
159         "Label key should be a ASCII string with a length greater than 0 and not exceed "
160             + "255 characters.");
161     Resource.parseResourceLabels(rawEnvLabels);
162   }
163 
164   @Test
testParseResourceLabels_DisallowLabelValueOverMaxLength()165   public void testParseResourceLabels_DisallowLabelValueOverMaxLength() {
166     char[] chars = new char[Resource.MAX_LENGTH + 1];
167     Arrays.fill(chars, 'k');
168     String rawEnvLabels = "example.org/test-1=" + new String(chars);
169     thrown.expect(IllegalArgumentException.class);
170     thrown.expectMessage(
171         "Label value should be a ASCII string with a length not exceed 255 characters.");
172     Resource.parseResourceLabels(rawEnvLabels);
173   }
174 
175   @Test
create()176   public void create() {
177     Map<String, String> labelMap = new HashMap<String, String>();
178     labelMap.put("a", "1");
179     labelMap.put("b", "2");
180     Resource resource = Resource.create("t1", labelMap);
181     assertThat(resource.getType()).isNotNull();
182     assertThat(resource.getType()).isEqualTo("t1");
183     assertThat(resource.getLabels()).isNotNull();
184     assertThat(resource.getLabels().size()).isEqualTo(2);
185     assertThat(resource.getLabels()).isEqualTo(labelMap);
186 
187     Resource resource1 = Resource.create(null, Collections.<String, String>emptyMap());
188     assertThat(resource1.getType()).isNull();
189     assertThat(resource1.getLabels()).isNotNull();
190     assertThat(resource1.getLabels()).isEmpty();
191   }
192 
193   @Test
testResourceEquals()194   public void testResourceEquals() {
195     Map<String, String> labelMap1 = new HashMap<String, String>();
196     labelMap1.put("a", "1");
197     labelMap1.put("b", "2");
198     Map<String, String> labelMap2 = new HashMap<String, String>();
199     labelMap2.put("a", "1");
200     labelMap2.put("b", "3");
201     labelMap2.put("c", "4");
202     new EqualsTester()
203         .addEqualityGroup(Resource.create("t1", labelMap1), Resource.create("t1", labelMap1))
204         .addEqualityGroup(Resource.create("t2", labelMap2))
205         .testEquals();
206   }
207 
208   @Test
testMergeResources()209   public void testMergeResources() {
210     Map<String, String> expectedLabelMap = new HashMap<String, String>();
211     expectedLabelMap.put("a", "1");
212     expectedLabelMap.put("b", "2");
213     expectedLabelMap.put("c", "4");
214 
215     Resource resource =
216         Resource.mergeResources(Arrays.asList(DEFAULT_RESOURCE, resource1, resource2));
217     assertThat(resource.getType()).isEqualTo("t1");
218     assertThat(resource.getLabels()).isEqualTo(expectedLabelMap);
219   }
220 
221   @Test
testMergeResources_Resource1()222   public void testMergeResources_Resource1() {
223     Map<String, String> expectedLabelMap = new HashMap<String, String>();
224     expectedLabelMap.put("a", "1");
225     expectedLabelMap.put("b", "2");
226 
227     Resource resource = Resource.mergeResources(Arrays.asList(DEFAULT_RESOURCE, resource1));
228     assertThat(resource.getType()).isEqualTo("t1");
229     assertThat(resource.getLabels()).isEqualTo(expectedLabelMap);
230   }
231 
232   @Test
testMergeResources_Resource1_Null()233   public void testMergeResources_Resource1_Null() {
234     Map<String, String> expectedLabelMap = new HashMap<String, String>();
235     expectedLabelMap.put("a", "1");
236     expectedLabelMap.put("b", "3");
237     expectedLabelMap.put("c", "4");
238 
239     Resource resource = Resource.mergeResources(Arrays.asList(DEFAULT_RESOURCE, null, resource2));
240     assertThat(resource.getType()).isEqualTo("t2");
241     assertThat(resource.getLabels()).isEqualTo(expectedLabelMap);
242   }
243 
244   @Test
testMergeResources_Resource2_Null()245   public void testMergeResources_Resource2_Null() {
246     Map<String, String> expectedLabelMap = new HashMap<String, String>();
247     expectedLabelMap.put("a", "1");
248     expectedLabelMap.put("b", "2");
249 
250     Resource resource = Resource.mergeResources(Arrays.asList(DEFAULT_RESOURCE, resource1, null));
251     assertThat(resource.getType()).isEqualTo("t1");
252     assertThat(resource.getLabels()).isEqualTo(expectedLabelMap);
253   }
254 
255   @Test
testMergeResources_DefaultResource()256   public void testMergeResources_DefaultResource() {
257     Map<String, String> expectedLabelMap = new HashMap<String, String>();
258     expectedLabelMap.put("a", "100");
259     expectedLabelMap.put("b", "2");
260     expectedLabelMap.put("c", "4");
261 
262     Resource resource =
263         Resource.mergeResources(Arrays.asList(DEFAULT_RESOURCE_1, resource1, resource2));
264     assertThat(resource.getType()).isEqualTo("default");
265     assertThat(resource.getLabels()).isEqualTo(expectedLabelMap);
266   }
267 }
268