Beispiel #1
0
    def test_str_conversion(self):
        self.assertEquals(self.get(1, convert_to=str), '1')
        self.assertEquals(self.get('ah', convert_to=str), 'ah')
        self.assertEquals(self.get(False, convert_to=str), 'False')
        self.assertEquals(self.get(1.3, convert_to=str), '1.3')
        self.assertEquals(self.get(1L, convert_to=str), '1')

        test_array = ["a", "b", "c"]

        # str -> ArrayOfStrings (must support different variations)
        arr = ArrayOfStrings(*test_array)
        self.assertEquals(self.get('a,b,c', convert_to=ArrayOfStrings), arr)
        self.assertEquals(self.get('a,b,  c', convert_to=ArrayOfStrings), arr)
        self.assertEquals(self.get('"a", "b", "c"', convert_to=ArrayOfStrings), arr)
        self.assertEquals(self.get("'a', 'b', 'c'", convert_to=ArrayOfStrings), arr)
        self.assertEquals(self.get("[a, b, c]" , convert_to=ArrayOfStrings), arr)
        self.assertEquals(self.get("['a', \"b\", c]" , convert_to=ArrayOfStrings), arr)

        # str -> JsonArray
        self.assertEquals(self.get(serialize(test_array), convert_to=JsonArray), JsonArray(*test_array))
        with self.assertRaises(BadMonitorConfiguration):
            # single quotes are invalid JSON
            self.assertEquals(self.get(str(test_array), convert_to=JsonArray), JsonArray(*test_array))

        # str -> JsonObject
        test_obj = {'a': 1, 'b': 'two', 'c': [1, 2, 3]}
        self.assertEquals(self.get(serialize(test_obj), convert_to=JsonObject), json_lib_parse(serialize(test_obj)))
Beispiel #2
0
    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)
Beispiel #3
0
    def test_arrayofstrings_conversion(self):
        # JsonArray -> list not supported
        test_array = ["a", "b", "c"]
        json_arr = ArrayOfStrings(*test_array)
        with self.assertRaises(BadMonitorConfiguration):
            self.get(json_arr, convert_to=list)

        # ArrayOfStrings -> JsonArray supported
        test_array = ["a", "b", "c"]
        json_arr = JsonArray(*test_array)
        self.assertEquals(self.get(json_arr, convert_to=JsonArray), JsonArray(*test_array))
Beispiel #4
0
    def test_list_conversion(self):
        # list -> JsonArray supported
        test_array = ["a", 1, False]
        json_arr = JsonArray(*test_array)
        self.assertEquals(
            self.get(list(json_arr), convert_to=JsonArray),
            JsonArray(*test_array),
        )

        # list -> ArrayOfStrings not supported
        test_array = ["a", "b", "c"]
        self.assertEquals(self.get(test_array, convert_to=ArrayOfStrings),
                          ArrayOfStrings(test_array))
 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"
Beispiel #7
0
    def test_jsonarray_conversion(self):
        # JsonArray -> list not supported
        test_array = ["a", "b", "c"]
        json_arr = JsonArray(*test_array)
        with self.assertRaises(BadMonitorConfiguration):
            self.get(json_arr, convert_to=list)

        # JsonArray -> ArrayOfStrings supported
        test_array = ["a", "b", "c"]
        json_arr = JsonArray(*test_array)
        self.assertEquals(self.get(json_arr, convert_to=ArrayOfStrings), ArrayOfStrings(*test_array))

        # JsonArray -> invalid ArrayOfStrings
        test_array = ["a", "b", 3]
        json_arr = JsonArray(*test_array)
        with self.assertRaises(BadMonitorConfiguration):
            self.get(json_arr, convert_to=ArrayOfStrings)
    def test_str_conversion(self):
        self.assertEquals(self.get(1, convert_to=six.text_type), "1")
        self.assertEquals(self.get("ah", convert_to=six.text_type), "ah")
        self.assertEquals(self.get(False, convert_to=six.text_type), "False")
        self.assertEquals(self.get(1.3, convert_to=six.text_type), "1.3")
        self.assertEquals(self.get(1, convert_to=six.text_type), "1")

        test_array = ["a", "b", "c"]

        # str -> ArrayOfStrings (must support different variations)
        arr = ArrayOfStrings(test_array)
        self.assertEquals(self.get("a,b,c", convert_to=ArrayOfStrings), arr)
        self.assertEquals(self.get("a,b,  c", convert_to=ArrayOfStrings), arr)
        self.assertEquals(self.get('"a", "b", "c"', convert_to=ArrayOfStrings),
                          arr)
        self.assertEquals(self.get("'a', 'b', 'c'", convert_to=ArrayOfStrings),
                          arr)
        self.assertEquals(self.get("[a, b, c]", convert_to=ArrayOfStrings),
                          arr)
        self.assertEquals(
            self.get("['a', \"b\", c]", convert_to=ArrayOfStrings), arr)

        # str -> JsonArray
        self.assertEquals(
            self.get(scalyr_util.json_encode(test_array),
                     convert_to=JsonArray),
            JsonArray(*test_array),
        )
        self.assertRaises(
            BadMonitorConfiguration,
            # single quotes are invalid JSON
            lambda: self.assertEquals(
                self.get(six.text_type(test_array), convert_to=JsonArray),
                JsonArray(*test_array),
            ),
        )

        # str -> JsonObject
        test_obj = {"a": 1, "b": "two", "c": [1, 2, 3]}
        self.assertEquals(
            self.get(scalyr_util.json_encode(test_obj), convert_to=JsonObject),
            scalyr_util.json_scalyr_config_decode(
                scalyr_util.json_encode(test_obj)),
        )
Beispiel #9
0
 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),
     })
Beispiel #11
0
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 str(value).lower() == "true"

        elif convert_to in (JsonArray, JsonObject):
            try:
                return json_lib.parse(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 (long, int):
        return float(value)
    if convert_to == long and convert_from == int:
        return long(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",
    )
class UrlMonitorTestRequest(unittest.TestCase):
    """
    Tests the formation of the request for monitoring a URL
    """

    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 tearDown(self):
        pass

    def test_get_request_no_headers(self):
        mock_logger = mock.MagicMock()
        config_data = {
            'url': 'fooUrl',
            'request_method': 'GET',
            'request_data': None,
            'request_headers': [],
            'module': self.module
        }
        config = MonitorConfig(content=config_data)
        url_monitor = UrlMonitor(monitor_config=config, logger=mock_logger)

        actual_request = url_monitor.build_request()
        self.assertEqual(actual_request.get_method(), 'GET')
        self.assertFalse(actual_request.has_data())
        self.assertEqual(actual_request.header_items(), [])

    def test_get_request_with_headers(self):
        mock_logger = mock.MagicMock()
        config_data = {
            'url': 'fooUrl',
            'request_method': 'GET',
            'request_data': None,
            'request_headers': self.legit_headers,
            'module': self.module
        }
        config = MonitorConfig(content=config_data)
        url_monitor = UrlMonitor(monitor_config=config, logger=mock_logger)

        actual_request = url_monitor.build_request()
        self.assertEqual(actual_request.get_method(), 'GET')
        self.assertFalse(actual_request.has_data())
        self.assertEqual(
            actual_request.header_items(),
            [('Header_foo', 'foo'), ('Header_bar', 'bar')]
        )

    def test_post_request_with_data(self):
        mock_logger = mock.MagicMock()
        config_data = {
            'url': 'fooUrl',
            'request_method': 'POST',
            'request_data': '{fakejsonthatisnotlegit}',
            'request_headers': self.legit_headers,
            'module': self.module
        }

        config = MonitorConfig(content=config_data)
        url_monitor = UrlMonitor(monitor_config=config, logger=mock_logger)
        actual_request = url_monitor.build_request()
        self.assertEqual(actual_request.get_method(), 'POST')
        self.assertEqual(actual_request.data, '{fakejsonthatisnotlegit}')
        self.assertEqual(
            actual_request.header_items(),
            [('Header_foo', 'foo'), ('Header_bar', 'bar')]
        )

    def test_malformed_headers(self):
        mock_logger = mock.MagicMock()
        config_data = {
            'url': 'fooUrl',
            'request_method': 'POST',
            'request_data': '{fakejsonthatisnotlegit}',
            'request_headers': 'not legit headers',
            'module': self.module
        }

        config = MonitorConfig(content=config_data)
        with self.assertRaises(Exception):
            _ = UrlMonitor(monitor_config=config, logger=mock_logger)
            "long": 1L,
            "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,
 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'
class UrlMonitorTestRequest(unittest.TestCase):
    """
    Tests the formation of the request for monitoring a URL
    """
    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 tearDown(self):
        pass

    def test_get_request_no_headers(self):
        mock_logger = mock.MagicMock()
        config_data = {
            "url": "http://fooUrl",
            "request_method": "GET",
            "request_data": None,
            "request_headers": [],
            "module": self.module,
        }
        config = MonitorConfig(content=config_data)
        url_monitor = UrlMonitor(monitor_config=config, logger=mock_logger)

        actual_request = url_monitor.build_request()
        self.assertEqual(actual_request.get_method(), "GET")
        self.assertFalse(actual_request.data is not None)
        self.assertEqual(actual_request.header_items(), [])

    def test_get_request_with_headers(self):
        mock_logger = mock.MagicMock()
        config_data = {
            "url": "http://fooUrl",
            "request_method": "GET",
            "request_data": None,
            "request_headers": self.legit_headers,
            "module": self.module,
        }
        config = MonitorConfig(content=config_data)
        url_monitor = UrlMonitor(monitor_config=config, logger=mock_logger)
        actual_request = url_monitor.build_request()
        self.assertEqual(actual_request.get_method(), "GET")
        self.assertFalse(actual_request.data is not None)
        self.assertEqual(
            sorted(actual_request.header_items()),
            sorted([("Header_foo", "foo"), ("Header_bar", "bar")]),
        )

    def test_post_request_with_data(self):
        mock_logger = mock.MagicMock()
        config_data = {
            "url": "http://fooUrl",
            "request_method": "POST",
            "request_data": "{fakejsonthatisnotlegit}",
            "request_headers": self.legit_headers,
            "module": self.module,
        }

        config = MonitorConfig(content=config_data)
        url_monitor = UrlMonitor(monitor_config=config, logger=mock_logger)
        actual_request = url_monitor.build_request()
        self.assertEqual(actual_request.get_method(), "POST")
        self.assertEqual(actual_request.data, "{fakejsonthatisnotlegit}")
        self.assertEqual(
            sorted(actual_request.header_items()),
            sorted([("Header_foo", "foo"), ("Header_bar", "bar")]),
        )

    def test_malformed_headers(self):
        mock_logger = mock.MagicMock()
        config_data = {
            "url": "fooUrl",
            "request_method": "POST",
            "request_data": "{fakejsonthatisnotlegit}",
            "request_headers": "not legit headers",
            "module": self.module,
        }

        config = MonitorConfig(content=config_data)
        self.assertRaises(
            Exception,
            lambda: UrlMonitor(monitor_config=config, logger=mock_logger))
Beispiel #16
0
            'long': 1L,
            '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,
class UrlMonitorTestRequest(unittest.TestCase):
    """
    Tests the formation of the request for monitoring a URL
    """
    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 tearDown(self):
        pass

    def test_get_request_no_headers(self):
        mock_logger = mock.MagicMock()
        config_data = {
            'url': 'fooUrl',
            'request_method': 'GET',
            'request_data': None,
            'request_headers': [],
            'module': self.module
        }
        config = MonitorConfig(content=config_data)
        url_monitor = UrlMonitor(monitor_config=config, logger=mock_logger)

        actual_request = url_monitor.build_request()
        self.assertEqual(actual_request.get_method(), 'GET')
        self.assertFalse(actual_request.has_data())
        self.assertEqual(actual_request.header_items(), [])

    def test_get_request_with_headers(self):
        mock_logger = mock.MagicMock()
        config_data = {
            'url': 'fooUrl',
            'request_method': 'GET',
            'request_data': None,
            'request_headers': self.legit_headers,
            'module': self.module
        }
        config = MonitorConfig(content=config_data)
        url_monitor = UrlMonitor(monitor_config=config, logger=mock_logger)

        actual_request = url_monitor.build_request()
        self.assertEqual(actual_request.get_method(), 'GET')
        self.assertFalse(actual_request.has_data())
        self.assertEqual(actual_request.header_items(),
                         [('Header_foo', 'foo'), ('Header_bar', 'bar')])

    def test_post_request_with_data(self):
        mock_logger = mock.MagicMock()
        config_data = {
            'url': 'fooUrl',
            'request_method': 'POST',
            'request_data': '{fakejsonthatisnotlegit}',
            'request_headers': self.legit_headers,
            'module': self.module
        }

        config = MonitorConfig(content=config_data)
        url_monitor = UrlMonitor(monitor_config=config, logger=mock_logger)
        actual_request = url_monitor.build_request()
        self.assertEqual(actual_request.get_method(), 'POST')
        self.assertEqual(actual_request.data, '{fakejsonthatisnotlegit}')
        self.assertEqual(actual_request.header_items(),
                         [('Header_foo', 'foo'), ('Header_bar', 'bar')])

    def test_malformed_headers(self):
        mock_logger = mock.MagicMock()
        config_data = {
            'url': 'fooUrl',
            'request_method': 'POST',
            'request_data': '{fakejsonthatisnotlegit}',
            'request_headers': 'not legit headers',
            'module': self.module
        }

        config = MonitorConfig(content=config_data)
        with self.assertRaises(Exception):
            _ = UrlMonitor(monitor_config=config, logger=mock_logger)