def test_base(self): test_array = ["a", 1, False] test_obj = {"a": 100, "b": 200} config = MonitorConfig({ "int": 1, "bool": True, "string": "hi", "unicode": u"bye", "float": 1.4, "long": 1, "JsonArray": JsonArray(*test_array), "JsonObject": JsonObject(**test_obj), }) self.assertEquals(len(config), 8) self.assertTrue("int" in config) self.assertFalse("foo" in config) self.assertEquals(config["int"], 1) self.assertEquals(config["bool"], True) self.assertEquals(config["string"], "hi") self.assertEquals(config["unicode"], u"bye") self.assertEquals(config["float"], 1.4) self.assertEquals(config["long"], 1) self.assertEquals(config["JsonArray"], JsonArray(*test_array)) self.assertEquals(config["JsonObject"], JsonObject(**test_obj)) count = 0 for _ in config: count += 1 self.assertEquals(count, 8)
def setUp(self): self.legit_headers = JsonArray() self.legit_headers.add( JsonObject({ 'header': 'header_foo', 'value': 'foo' })) self.legit_headers.add( JsonObject({ 'header': 'header_bar', 'value': 'bar' })) self.module = 'scalyr_agent.builtin_monitors.url_monitor'
def setUp(self): super(UrlMonitorTestRequest, self).setUp() self.legit_headers = JsonArray() self.legit_headers.add( JsonObject({ "header": "header_foo", "value": "foo" })) self.legit_headers.add( JsonObject({ "header": "header_bar", "value": "bar" })) self.module = "scalyr_agent.builtin_monitors.url_monitor"
def test_base(self): test_array = ["a", 1, False] test_obj = {"a": 100, "b": 200} config = MonitorConfig({ "int": 1, "bool": True, "string": "hi", "unicode": u"bye", "float": 1.4, "long": 1L, "JsonArray": JsonArray(*test_array), "JsonObject": JsonObject(**test_obj), })
def test_base(self): test_array = ["a", 1, False] test_obj = {"a": 100, "b": 200} config = MonitorConfig({ 'int': 1, 'bool': True, 'string': 'hi', 'unicode': u'bye', 'float': 1.4, 'long': 1L, 'JsonArray': JsonArray(*test_array), 'JsonObject': JsonObject(**test_obj), })
"JsonArray": JsonArray(*test_array), "JsonObject": JsonObject(**test_obj), }) self.assertEquals(len(config), 8) self.assertTrue("int" in config) self.assertFalse("foo" in config) self.assertEquals(config["int"], 1) self.assertEquals(config["bool"], True) self.assertEquals(config["string"], "hi") self.assertEquals(config["unicode"], u"bye") self.assertEquals(config["float"], 1.4) self.assertEquals(config["long"], 1L) self.assertEquals(config["JsonArray"], JsonArray(*test_array)) self.assertEquals(config["JsonObject"], JsonObject(**test_obj)) count = 0 for _ in config: count += 1 self.assertEquals(count, 8) def test_int_conversion(self): self.assertEquals(self.get(1, convert_to=int), 1) self.assertEquals(self.get("12", convert_to=int), 12) self.assertEquals(self.get(u"13", convert_to=int), 13) self.assertRaises(BadMonitorConfiguration, self.get, 2.0, convert_to=int)
'JsonArray': JsonArray(*test_array), 'JsonObject': JsonObject(**test_obj), }) self.assertEquals(len(config), 8) self.assertTrue('int' in config) self.assertFalse('foo' in config) self.assertEquals(config['int'], 1) self.assertEquals(config['bool'], True) self.assertEquals(config['string'], 'hi') self.assertEquals(config['unicode'], u'bye') self.assertEquals(config['float'], 1.4) self.assertEquals(config['long'], 1L) self.assertEquals(config['JsonArray'], JsonArray(*test_array)) self.assertEquals(config['JsonObject'], JsonObject(**test_obj)) count = 0 for _ in config: count += 1 self.assertEquals(count, 8) def test_int_conversion(self): self.assertEquals(self.get(1, convert_to=int), 1) self.assertEquals(self.get('12', convert_to=int), 12) self.assertEquals(self.get(u'13', convert_to=int), 13) self.assertRaises(BadMonitorConfiguration, self.get, 2.0, convert_to=int)
def convert_config_param(field_name, value, convert_to, is_environment_variable=False): """Convert monitor config values to a different type according to the ALLOWED_CONVERSIONS matrix None is an invalid input and will raise BadConfiguration error. Empty strings will convert into str, bool, ArrayOfStrings, SpaceAndCommaSeparatedArrayOfStrings but raises exception for int, float, JsonArray, JsonObject """ convert_from = type(value) kind = "environment variable" if not is_environment_variable: kind = "config param" conversion_allowed = False if convert_from in ALLOWED_CONVERSIONS: if convert_to in set([convert_from]) | ALLOWED_CONVERSIONS[convert_from]: conversion_allowed = True if not conversion_allowed: raise BadConfiguration( 'Prohibited conversion of %s "%s" from %s to %s' % (kind, field_name, convert_from, convert_to), field_name, "illegalConversion", ) # If no type change, simply return unconverted value if convert_from == convert_to: return value # Anything is allowed to go to str/unicode if convert_to in STRING_TYPES: return convert_to(value) if convert_from == list and convert_to == JsonArray: try: return JsonArray(*value) except JsonConversionException: raise BadConfiguration( 'Could not convert value %s for field "%s" from %s to %s' % (value, field_name, convert_from, convert_to), field_name, "notJsonArray", ) if convert_from in (list, JsonArray) and convert_to in ( ArrayOfStrings, SpaceAndCommaSeparatedArrayOfStrings, ): list_of_strings = [] for item in value: if type(item) not in STRING_TYPES: raise BadConfiguration( 'Non-string element found in value %s for field "%s"' % (value, field_name), field_name, "notArrayOfStrings", ) list_of_strings.append(item) return convert_to(list_of_strings) # Anything is allowed to go from string/unicode to the conversion type, as long as it can be parsed. # Special-case handle bool and JsonArray if convert_from in STRING_TYPES: if convert_to == bool: return six.text_type(value).lower() == "true" elif convert_to in (JsonArray, JsonObject): try: # Special case for empty objects if convert_to == JsonObject and not value: return JsonObject() # Needs to be json_lib.parse since it is parsing configuration. return scalyr_util.json_scalyr_config_decode(value) except JsonParseException: raise BadConfiguration( 'Could not parse value %s for field "%s" as %s' % (value, field_name, convert_to), field_name, "notJsonObject", ) elif convert_to in (ArrayOfStrings, SpaceAndCommaSeparatedArrayOfStrings): try: # ArrayOfStrings and it's return parse_array_of_strings(value, convert_to.separators) except TypeError: raise BadConfiguration( 'Could not parse value %s for field "%s" as %s' % (value, field_name, convert_to), field_name, "notArrayOfStrings", ) elif convert_to in NUMERIC_TYPES: try: return convert_to(value) except ValueError: raise BadConfiguration( 'Could not parse value %s for field "%s" as numeric type %s' % (value, field_name, convert_to), field_name, "notNumber", ) if convert_from not in NUMERIC_TYPES: raise BadConfiguration( 'Type conversion for field "%s" from %s to %s not implemented.' % (field_name, convert_from, convert_to), field_name, "notNumber", ) if convert_to == bool: raise BadConfiguration( 'A numeric value %s was given for boolean field "%s"' % (value, field_name), field_name, "notBoolean", ) if convert_to not in NUMERIC_TYPES: raise BadConfiguration( 'Type conversion for field "%s" from %s to %s not implemented.' % (field_name, convert_from, convert_to), field_name, "unsupportedConversion", ) # At this point, we are trying to convert a number to another number type. We only allow int to long # and long, int to float. if convert_to == float and convert_from in six.integer_types: return float(value) if convert_to in six.integer_types: return int(value) raise BadConfiguration( 'A numeric value of %s was given for field "%s" but a %s is required.' % (value, field_name, convert_to), field_name, "wrongType", )