|
@@ -18,17 +18,23 @@
|
|
|
|
|
|
package org.apache.ambari.server.controller.internal;
|
|
|
|
|
|
-import org.apache.ambari.server.controller.spi.Request;
|
|
|
-import org.apache.ambari.server.controller.spi.Resource;
|
|
|
-import org.apache.ambari.server.controller.utilities.PropertyHelper;
|
|
|
-import org.junit.Assert;
|
|
|
-import org.junit.Test;
|
|
|
+import static org.junit.Assert.assertEquals;
|
|
|
+import static org.junit.Assert.assertFalse;
|
|
|
+import static org.junit.Assert.assertNull;
|
|
|
+import static org.junit.Assert.assertTrue;
|
|
|
|
|
|
import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Map.Entry;
|
|
|
import java.util.Set;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+import org.apache.ambari.server.controller.spi.Request;
|
|
|
+import org.apache.ambari.server.controller.spi.Resource;
|
|
|
+import org.apache.ambari.server.controller.utilities.PropertyHelper;
|
|
|
+import org.junit.Test;
|
|
|
|
|
|
/**
|
|
|
* Base provider tests.
|
|
@@ -46,7 +52,7 @@ public class BaseProviderTest {
|
|
|
BaseProvider provider = new TestProvider(propertyIds);
|
|
|
|
|
|
Set<String> supportedPropertyIds = provider.getPropertyIds();
|
|
|
- Assert.assertTrue(supportedPropertyIds.containsAll(propertyIds));
|
|
|
+ assertTrue(supportedPropertyIds.containsAll(propertyIds));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -61,26 +67,28 @@ public class BaseProviderTest {
|
|
|
|
|
|
BaseProvider provider = new TestProvider(propertyIds);
|
|
|
|
|
|
- Assert.assertTrue(provider.checkPropertyIds(propertyIds).isEmpty());
|
|
|
+ assertTrue(provider.checkPropertyIds(propertyIds).isEmpty());
|
|
|
|
|
|
- Assert.assertTrue(provider.checkPropertyIds(Collections.singleton("cat1")).isEmpty());
|
|
|
- Assert.assertTrue(provider.checkPropertyIds(Collections.singleton("cat2")).isEmpty());
|
|
|
- Assert.assertTrue(provider.checkPropertyIds(Collections.singleton("cat3")).isEmpty());
|
|
|
- Assert.assertTrue(provider.checkPropertyIds(Collections.singleton("cat3/subcat3")).isEmpty());
|
|
|
- Assert.assertTrue(provider.checkPropertyIds(Collections.singleton("cat4/subcat4/map")).isEmpty());
|
|
|
+ assertTrue(provider.checkPropertyIds(Collections.singleton("cat1")).isEmpty());
|
|
|
+ assertTrue(provider.checkPropertyIds(Collections.singleton("cat2")).isEmpty());
|
|
|
+ assertTrue(provider.checkPropertyIds(Collections.singleton("cat3")).isEmpty());
|
|
|
+ assertTrue(provider.checkPropertyIds(Collections.singleton("cat3/subcat3")).isEmpty());
|
|
|
+ assertTrue(provider.checkPropertyIds(
|
|
|
+ Collections.singleton("cat4/subcat4/map")).isEmpty());
|
|
|
|
|
|
// note that key is not in the set of known property ids. We allow it if its parent is a known property.
|
|
|
// this allows for Map type properties where we want to treat the entries as individual properties
|
|
|
- Assert.assertTrue(provider.checkPropertyIds(Collections.singleton("cat4/subcat4/map/key")).isEmpty());
|
|
|
+ assertTrue(provider.checkPropertyIds(
|
|
|
+ Collections.singleton("cat4/subcat4/map/key")).isEmpty());
|
|
|
|
|
|
propertyIds.add("badprop");
|
|
|
propertyIds.add("badcat");
|
|
|
|
|
|
Set<String> unsupportedPropertyIds = provider.checkPropertyIds(propertyIds);
|
|
|
- Assert.assertFalse(unsupportedPropertyIds.isEmpty());
|
|
|
- Assert.assertEquals(2, unsupportedPropertyIds.size());
|
|
|
- Assert.assertTrue(unsupportedPropertyIds.contains("badprop"));
|
|
|
- Assert.assertTrue(unsupportedPropertyIds.contains("badcat"));
|
|
|
+ assertFalse(unsupportedPropertyIds.isEmpty());
|
|
|
+ assertEquals(2, unsupportedPropertyIds.size());
|
|
|
+ assertTrue(unsupportedPropertyIds.contains("badprop"));
|
|
|
+ assertTrue(unsupportedPropertyIds.contains("badcat"));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -96,24 +104,24 @@ public class BaseProviderTest {
|
|
|
|
|
|
Set<String> requestedPropertyIds = provider.getRequestPropertyIds(request, null);
|
|
|
|
|
|
- Assert.assertEquals(1, requestedPropertyIds.size());
|
|
|
- Assert.assertTrue(requestedPropertyIds.contains("foo"));
|
|
|
+ assertEquals(1, requestedPropertyIds.size());
|
|
|
+ assertTrue(requestedPropertyIds.contains("foo"));
|
|
|
|
|
|
request = PropertyHelper.getReadRequest("foo", "bar");
|
|
|
|
|
|
requestedPropertyIds = provider.getRequestPropertyIds(request, null);
|
|
|
|
|
|
- Assert.assertEquals(2, requestedPropertyIds.size());
|
|
|
- Assert.assertTrue(requestedPropertyIds.contains("foo"));
|
|
|
- Assert.assertTrue(requestedPropertyIds.contains("bar"));
|
|
|
+ assertEquals(2, requestedPropertyIds.size());
|
|
|
+ assertTrue(requestedPropertyIds.contains("foo"));
|
|
|
+ assertTrue(requestedPropertyIds.contains("bar"));
|
|
|
|
|
|
request = PropertyHelper.getReadRequest("foo", "baz", "bar", "cat", "cat1/prop1");
|
|
|
|
|
|
requestedPropertyIds = provider.getRequestPropertyIds(request, null);
|
|
|
|
|
|
- Assert.assertEquals(2, requestedPropertyIds.size());
|
|
|
- Assert.assertTrue(requestedPropertyIds.contains("foo"));
|
|
|
- Assert.assertTrue(requestedPropertyIds.contains("bar"));
|
|
|
+ assertEquals(2, requestedPropertyIds.size());
|
|
|
+ assertTrue(requestedPropertyIds.contains("foo"));
|
|
|
+ assertTrue(requestedPropertyIds.contains("bar"));
|
|
|
|
|
|
// ask for a property that isn't specified as supported, but its category is... the property
|
|
|
// should end up in the returned set for the case where the category is a Map property
|
|
@@ -121,9 +129,9 @@ public class BaseProviderTest {
|
|
|
|
|
|
requestedPropertyIds = provider.getRequestPropertyIds(request, null);
|
|
|
|
|
|
- Assert.assertEquals(2, requestedPropertyIds.size());
|
|
|
- Assert.assertTrue(requestedPropertyIds.contains("foo"));
|
|
|
- Assert.assertTrue(requestedPropertyIds.contains("cat1/sub1/prop1"));
|
|
|
+ assertEquals(2, requestedPropertyIds.size());
|
|
|
+ assertTrue(requestedPropertyIds.contains("foo"));
|
|
|
+ assertTrue(requestedPropertyIds.contains("cat1/sub1/prop1"));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -140,26 +148,26 @@ public class BaseProviderTest {
|
|
|
|
|
|
Resource resource = new ResourceImpl(Resource.Type.Service);
|
|
|
|
|
|
- Assert.assertNull(resource.getPropertyValue("foo"));
|
|
|
+ assertNull(resource.getPropertyValue("foo"));
|
|
|
|
|
|
BaseProvider.setResourceProperty(resource, "foo", "value1", propertyIds);
|
|
|
- Assert.assertEquals("value1", resource.getPropertyValue("foo"));
|
|
|
+ assertEquals("value1", resource.getPropertyValue("foo"));
|
|
|
|
|
|
BaseProvider.setResourceProperty(resource, "cat2/bar", "value2", propertyIds);
|
|
|
- Assert.assertEquals("value2", resource.getPropertyValue("cat2/bar"));
|
|
|
+ assertEquals("value2", resource.getPropertyValue("cat2/bar"));
|
|
|
|
|
|
- Assert.assertNull(resource.getPropertyValue("unsupported"));
|
|
|
+ assertNull(resource.getPropertyValue("unsupported"));
|
|
|
BaseProvider.setResourceProperty(resource, "unsupported", "valueX", propertyIds);
|
|
|
- Assert.assertNull(resource.getPropertyValue("unsupported"));
|
|
|
+ assertNull(resource.getPropertyValue("unsupported"));
|
|
|
|
|
|
// we should allow anything under the category cat5/sub5
|
|
|
BaseProvider.setResourceProperty(resource, "cat5/sub5/prop5", "value5", propertyIds);
|
|
|
- Assert.assertEquals("value5", resource.getPropertyValue("cat5/sub5/prop5"));
|
|
|
+ assertEquals("value5", resource.getPropertyValue("cat5/sub5/prop5"));
|
|
|
BaseProvider.setResourceProperty(resource, "cat5/sub5/sub5a/prop5a", "value5", propertyIds);
|
|
|
- Assert.assertEquals("value5", resource.getPropertyValue("cat5/sub5/sub5a/prop5a"));
|
|
|
+ assertEquals("value5", resource.getPropertyValue("cat5/sub5/sub5a/prop5a"));
|
|
|
// we shouldn't allow anything under the category cat5/sub7
|
|
|
BaseProvider.setResourceProperty(resource, "cat5/sub7/unsupported", "valueX", propertyIds);
|
|
|
- Assert.assertNull(resource.getPropertyValue("cat5/sub7/unsupported"));
|
|
|
+ assertNull(resource.getPropertyValue("cat5/sub7/unsupported"));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -177,7 +185,7 @@ public class BaseProviderTest {
|
|
|
// Adding an empty Map as a property should add the actual Map as a property
|
|
|
Map<String, String> emptyMapProperty = new HashMap<String, String>();
|
|
|
BaseProvider.setResourceProperty(resource, "cat1/emptyMapProperty", emptyMapProperty, propertyIds);
|
|
|
- Assert.assertTrue(resource.getPropertiesMap().containsKey("cat1/emptyMapProperty"));
|
|
|
+ assertTrue(resource.getPropertiesMap().containsKey("cat1/emptyMapProperty"));
|
|
|
|
|
|
Map<String, String> mapProperty = new HashMap<String, String>();
|
|
|
mapProperty.put("key1", "value1");
|
|
@@ -187,10 +195,10 @@ public class BaseProviderTest {
|
|
|
// Adding a property of type Map should add all of its keys as sub properties
|
|
|
// if the map property was requested
|
|
|
BaseProvider.setResourceProperty(resource, "cat1/mapProperty", mapProperty, propertyIds);
|
|
|
- Assert.assertNull(resource.getPropertyValue("cat1/mapProperty"));
|
|
|
- Assert.assertEquals("value1", resource.getPropertyValue("cat1/mapProperty/key1"));
|
|
|
- Assert.assertEquals("value2", resource.getPropertyValue("cat1/mapProperty/key2"));
|
|
|
- Assert.assertEquals("value3", resource.getPropertyValue("cat1/mapProperty/key3"));
|
|
|
+ assertNull(resource.getPropertyValue("cat1/mapProperty"));
|
|
|
+ assertEquals("value1", resource.getPropertyValue("cat1/mapProperty/key1"));
|
|
|
+ assertEquals("value2", resource.getPropertyValue("cat1/mapProperty/key2"));
|
|
|
+ assertEquals("value3", resource.getPropertyValue("cat1/mapProperty/key3"));
|
|
|
|
|
|
Map<String, Map<String, String>> mapMapProperty = new HashMap<String, Map<String, String>>();
|
|
|
Map<String, String> mapSubProperty1 = new HashMap<String, String>();
|
|
@@ -209,16 +217,23 @@ public class BaseProviderTest {
|
|
|
// Map of maps ... adding a property of type Map should add all of its keys as sub properties
|
|
|
// if the map property was requested
|
|
|
BaseProvider.setResourceProperty(resource, "cat2/mapMapProperty", mapMapProperty, propertyIds);
|
|
|
- Assert.assertNull(resource.getPropertyValue("cat2/mapMapProperty"));
|
|
|
- Assert.assertNull(resource.getPropertyValue("cat2/mapMapProperty/subMap1"));
|
|
|
- Assert.assertNull(resource.getPropertyValue("cat2/mapMapProperty/subMap2"));
|
|
|
- Assert.assertTrue(resource.getPropertiesMap().containsKey("cat2/mapMapProperty/subMap3"));
|
|
|
- Assert.assertEquals("value11", resource.getPropertyValue("cat2/mapMapProperty/subMap1/key1"));
|
|
|
- Assert.assertEquals("value12", resource.getPropertyValue("cat2/mapMapProperty/subMap1/key2"));
|
|
|
- Assert.assertEquals("value13", resource.getPropertyValue("cat2/mapMapProperty/subMap1/key3"));
|
|
|
- Assert.assertEquals("value21", resource.getPropertyValue("cat2/mapMapProperty/subMap2/key1"));
|
|
|
- Assert.assertEquals("value22", resource.getPropertyValue("cat2/mapMapProperty/subMap2/key2"));
|
|
|
- Assert.assertEquals("value23", resource.getPropertyValue("cat2/mapMapProperty/subMap2/key3"));
|
|
|
+ assertNull(resource.getPropertyValue("cat2/mapMapProperty"));
|
|
|
+ assertNull(resource.getPropertyValue("cat2/mapMapProperty/subMap1"));
|
|
|
+ assertNull(resource.getPropertyValue("cat2/mapMapProperty/subMap2"));
|
|
|
+ assertTrue(resource.getPropertiesMap().containsKey(
|
|
|
+ "cat2/mapMapProperty/subMap3"));
|
|
|
+ assertEquals("value11",
|
|
|
+ resource.getPropertyValue("cat2/mapMapProperty/subMap1/key1"));
|
|
|
+ assertEquals("value12",
|
|
|
+ resource.getPropertyValue("cat2/mapMapProperty/subMap1/key2"));
|
|
|
+ assertEquals("value13",
|
|
|
+ resource.getPropertyValue("cat2/mapMapProperty/subMap1/key3"));
|
|
|
+ assertEquals("value21",
|
|
|
+ resource.getPropertyValue("cat2/mapMapProperty/subMap2/key1"));
|
|
|
+ assertEquals("value22",
|
|
|
+ resource.getPropertyValue("cat2/mapMapProperty/subMap2/key2"));
|
|
|
+ assertEquals("value23",
|
|
|
+ resource.getPropertyValue("cat2/mapMapProperty/subMap2/key3"));
|
|
|
|
|
|
Map<String, String> mapProperty3 = new HashMap<String, String>();
|
|
|
mapProperty3.put("key1", "value1");
|
|
@@ -229,10 +244,10 @@ public class BaseProviderTest {
|
|
|
// should only add requested keys as sub properties ...
|
|
|
// only "cat3/mapProperty3/key2" was requested
|
|
|
BaseProvider.setResourceProperty(resource, "cat3/mapProperty3", mapProperty3, propertyIds);
|
|
|
- Assert.assertNull(resource.getPropertyValue("cat3/mapProperty3"));
|
|
|
- Assert.assertNull(resource.getPropertyValue("cat3/mapProperty3/key1"));
|
|
|
- Assert.assertEquals("value2", resource.getPropertyValue("cat3/mapProperty3/key2"));
|
|
|
- Assert.assertNull(resource.getPropertyValue("cat3/mapProperty3/key3"));
|
|
|
+ assertNull(resource.getPropertyValue("cat3/mapProperty3"));
|
|
|
+ assertNull(resource.getPropertyValue("cat3/mapProperty3/key1"));
|
|
|
+ assertEquals("value2", resource.getPropertyValue("cat3/mapProperty3/key2"));
|
|
|
+ assertNull(resource.getPropertyValue("cat3/mapProperty3/key3"));
|
|
|
|
|
|
Map<String, Map<String, String>> mapMapProperty4 = new HashMap<String, Map<String, String>>();
|
|
|
mapMapProperty4.put("subMap1", mapSubProperty1);
|
|
@@ -241,15 +256,19 @@ public class BaseProviderTest {
|
|
|
// should only add requested keys as sub properties ...
|
|
|
// only "cat4/mapMapProperty4/subMap1/key3" and "cat4/mapMapProperty4/subMap2" are requested
|
|
|
BaseProvider.setResourceProperty(resource, "cat4/mapMapProperty4", mapMapProperty4, propertyIds);
|
|
|
- Assert.assertNull(resource.getPropertyValue("cat4/mapMapProperty4"));
|
|
|
- Assert.assertNull(resource.getPropertyValue("cat4/mapMapProperty4/subMap1"));
|
|
|
- Assert.assertNull(resource.getPropertyValue("cat4/mapMapProperty4/subMap2"));
|
|
|
- Assert.assertNull(resource.getPropertyValue("cat4/mapMapProperty4/subMap1/key1"));
|
|
|
- Assert.assertNull(resource.getPropertyValue("cat4/mapMapProperty4/subMap1/key2"));
|
|
|
- Assert.assertEquals("value13", resource.getPropertyValue("cat4/mapMapProperty4/subMap1/key3"));
|
|
|
- Assert.assertEquals("value21", resource.getPropertyValue("cat4/mapMapProperty4/subMap2/key1"));
|
|
|
- Assert.assertEquals("value22", resource.getPropertyValue("cat4/mapMapProperty4/subMap2/key2"));
|
|
|
- Assert.assertEquals("value23", resource.getPropertyValue("cat4/mapMapProperty4/subMap2/key3"));
|
|
|
+ assertNull(resource.getPropertyValue("cat4/mapMapProperty4"));
|
|
|
+ assertNull(resource.getPropertyValue("cat4/mapMapProperty4/subMap1"));
|
|
|
+ assertNull(resource.getPropertyValue("cat4/mapMapProperty4/subMap2"));
|
|
|
+ assertNull(resource.getPropertyValue("cat4/mapMapProperty4/subMap1/key1"));
|
|
|
+ assertNull(resource.getPropertyValue("cat4/mapMapProperty4/subMap1/key2"));
|
|
|
+ assertEquals("value13",
|
|
|
+ resource.getPropertyValue("cat4/mapMapProperty4/subMap1/key3"));
|
|
|
+ assertEquals("value21",
|
|
|
+ resource.getPropertyValue("cat4/mapMapProperty4/subMap2/key1"));
|
|
|
+ assertEquals("value22",
|
|
|
+ resource.getPropertyValue("cat4/mapMapProperty4/subMap2/key2"));
|
|
|
+ assertEquals("value23",
|
|
|
+ resource.getPropertyValue("cat4/mapMapProperty4/subMap2/key3"));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -265,15 +284,44 @@ public class BaseProviderTest {
|
|
|
propertyIds.add(regexp2);
|
|
|
|
|
|
BaseProvider provider = new TestProvider(propertyIds);
|
|
|
- Assert.assertEquals(regexp, provider.getRegExpKey(propertyId));
|
|
|
- Assert.assertNull(provider.getRegExpKey(incorrectPropertyId));
|
|
|
- Assert.assertEquals("sub", provider.getRegexGroups(regexp, propertyId).get(0));
|
|
|
- Assert.assertEquals("sub2", provider.getRegexGroups(regexp2, propertyId2).get(1));
|
|
|
- Assert.assertTrue(provider.getRegexGroups(regexp, incorrectPropertyId).isEmpty());
|
|
|
+ Map.Entry<String, Pattern> regexEntry = provider.getRegexEntry(propertyId);
|
|
|
+
|
|
|
+ assertEquals(regexp, regexEntry.getKey());
|
|
|
+ assertNull(provider.getRegexEntry(incorrectPropertyId));
|
|
|
+ assertEquals("sub", provider.getRegexGroups(regexp, propertyId).get(0));
|
|
|
+ assertEquals("sub2", provider.getRegexGroups(regexp2, propertyId2).get(1));
|
|
|
+ assertTrue(provider.getRegexGroups(regexp, incorrectPropertyId).isEmpty());
|
|
|
}
|
|
|
|
|
|
- static class TestProvider extends BaseProvider {
|
|
|
+ @Test
|
|
|
+ public void testComplexMetricParsing() {
|
|
|
+ Set<String> propertyIds = new HashSet<String>();
|
|
|
+ propertyIds.add("metrics/flume/$1.substring(0)/CHANNEL/$2.replaceAll(\"[^-]+\",\"\")EventPutSuccessCount/rate/sum");
|
|
|
+ propertyIds.add("metrics/yarn/Queue/$1.replaceAll(\"([.])\",\"/\")/AppsCompleted");
|
|
|
+
|
|
|
+ TestProvider provider = new TestProvider(propertyIds);
|
|
|
+ Entry<String, Pattern> entry = provider.getRegexEntry("metrics/flume/flume");
|
|
|
+ assertEquals("metrics/flume/$1", entry.getKey());
|
|
|
+ assertEquals("metrics/flume/(\\S*)", entry.getValue().pattern());
|
|
|
+
|
|
|
+ entry = provider.getRegexEntry("metrics/flume/flume/CHANNEL");
|
|
|
+ assertEquals("metrics/flume/$1/CHANNEL", entry.getKey());
|
|
|
+ assertEquals("metrics/flume/(\\S*)/CHANNEL", entry.getValue().pattern());
|
|
|
+
|
|
|
+ entry = provider.getRegexEntry("metrics/flume/flume/CHANNEL/EventPutSuccessCount");
|
|
|
+ assertEquals("metrics/flume/$1/CHANNEL/$2EventPutSuccessCount", entry.getKey());
|
|
|
+ assertEquals("metrics/flume/(\\S*)/CHANNEL/(\\S*)EventPutSuccessCount",
|
|
|
+ entry.getValue().pattern());
|
|
|
+
|
|
|
+ entry = provider.getRegexEntry("metrics/flume/flume/CHANNEL/EventPutSuccessCount/rate");
|
|
|
+ assertEquals("metrics/flume/$1/CHANNEL/$2EventPutSuccessCount/rate",
|
|
|
+ entry.getKey());
|
|
|
+ assertEquals(
|
|
|
+ "metrics/flume/(\\S*)/CHANNEL/(\\S*)EventPutSuccessCount/rate",
|
|
|
+ entry.getValue().pattern());
|
|
|
+ }
|
|
|
|
|
|
+ static class TestProvider extends BaseProvider {
|
|
|
public TestProvider(Set<String> propertyIds) {
|
|
|
super(propertyIds);
|
|
|
}
|