コード例 #1
0
    def setUp(self):
        self.stubs = stubout.StubOutForTesting()
        config_dict = {
            'atompub': {
                'url': 'http://127.0.0.1:9000/test/test_feed',
                'user': '******',
                'key': 'key',
                'interval': 30,
                'max_wait': 600,
                'retries': 1,
                'failures_before_reauth': 5,
                'timeout': '120',
            },
            'event_feed': {
                'feed_title': 'feed_title',
                'feed_host': 'feed_host',
                'use_https': False,
                'port': 'port',
                'atom_categories': "DATACENTER=ORD1, REGION=PREPROD-ORD"
            },
            'handler_auth': {
                'method': 'no_auth'
            },
            'cufpub': {
                'url': 'http://127.0.0.1:9000/test/test_feed',
                'user': '******',
                'key': 'key',
                'interval': 30,
                'max_wait': 600,
                'retries': 1,
                'failures_before_reauth': 5,
                'timeout': '120',
            },
            'nova': {
                'nova_flavor_field_name': 'dummy_flavor_field_name'
            }
        }

        self.handler = CufPub()

        def get(*args, **kwargs):
            val = None
            for arg in args:
                if val:
                    val = val.get(arg)
                else:
                    val = config_dict.get(arg)
                    if not val:
                        return None or kwargs.get('default')
            return val

        def config_with(*args):
            return functools.partial(get, args)

        self.stubs.Set(yagi.config, 'config_with', config_with)
        self.stubs.Set(yagi.config, 'get', get)
コード例 #2
0
    def test_send_notification_successfully(self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(201), None

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a": "b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)
        http_conn.send_notification(endpoint, endpoint, payload_body)
コード例 #3
0
ファイル: test_http_connection.py プロジェクト: klmitch/yagi
    def test_send_notification_successfully(self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(201), None

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a":"b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)
        http_conn.send_notification(endpoint, endpoint, payload_body)
コード例 #4
0
ファイル: test_http_connection.py プロジェクト: klmitch/yagi
    def test_response_status_400_raises_invalid_content_exception(self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(400), None
        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a":"b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)

        self.assertRaises(InvalidContentException, http_conn.send_notification,
                          endpoint, endpoint, payload_body)
コード例 #5
0
ファイル: test_http_connection.py プロジェクト: klmitch/yagi
    def test_response_too_large_error_and_status_is_201_does_not_raise_exception(self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            raise http_util.ResponseTooLargeError("desc",MockResponse(201),
                                                  "content")

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a":"b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)

        http_conn.send_notification(endpoint, endpoint, payload_body)
コード例 #6
0
ファイル: test_http_connection.py プロジェクト: klmitch/yagi
    def test_duplicate_entry_to_atomhopper_response_409(self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(409), None

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a":"b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)

        status = http_conn.send_notification(endpoint, endpoint, payload_body)
        self.assertEqual(status,409)
コード例 #7
0
    def test_response_other_than_201_raises_exception(self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(400), None

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a": "b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)

        self.assertRaises(MessageDeliveryFailed, http_conn.send_notification,
                          endpoint, endpoint, payload_body)
コード例 #8
0
    def test_response_401_raises_unauthorized_exception(self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(401), None

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a": "b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)

        self.assertRaises(UnauthorizedException, http_conn.send_notification,
                          endpoint, endpoint, payload_body)
コード例 #9
0
    def test_duplicate_entry_to_atomhopper_response_409(self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(409), None

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a": "b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)

        status = http_conn.send_notification(endpoint, endpoint, payload_body)
        self.assertEqual(status, 409)
コード例 #10
0
ファイル: test_http_connection.py プロジェクト: klmitch/yagi
    def test_response_other_than_201_or_400_raises_exception(self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(405), None

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a":"b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)

        self.assertRaises(MessageDeliveryFailed,http_conn.send_notification,
                          endpoint, endpoint, payload_body)
コード例 #11
0
    def test_response_too_large_error_and_status_is_201_does_not_raise_exception(
            self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            raise http_util.ResponseTooLargeError("desc", MockResponse(201),
                                                  "content")

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a": "b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)

        http_conn.send_notification(endpoint, endpoint, payload_body)
コード例 #12
0
ファイル: test_cufpub.py プロジェクト: gocloudxyz/yagi
    def setUp(self):
        self.stubs = stubout.StubOutForTesting()
        self.mox = mox.Mox()
        config_dict = {
            'atompub': {
                'url': 'http://127.0.0.1:9000/test/%(event_type)s',
                'user': '******',
                'key': 'key',
                'interval': 30,
                'max_wait': 600,
                'retries': 1,
                'failures_before_reauth': 5,
                'timeout': '120',
            },
            'event_feed': {
                'feed_title': 'feed_title',
                'feed_host': 'feed_host',
                'use_https': False,
                'port': 'port',
                'atom_categories': "DATACENTER=ORD1, REGION=PREPROD-ORD"

            },
            'handler_auth': {
                'method': 'no_auth'
            },
            'cufpub': {
                'url': 'http://127.0.0.1:9000/test/%(event_type)s',
                'user': '******',
                'key': 'key',
                'interval': 30,
                'max_wait': 600,
                'retries': 1,
                'failures_before_reauth': 5,
                'timeout': '120',

            },
            'nova': {
                'nova_flavor_field_name': 'dummy_flavor_field_name'
            }
        }

        self.handler = CufPub()

        def get(*args, **kwargs):
            val = None
            for arg in args:
                if val:
                    val = val.get(arg)
                else:
                    val = config_dict.get(arg)
                    if not val:
                        return None or kwargs.get('default')
            return val

        def config_with(*args):
            return functools.partial(get, args)

        self.stubs.Set(yagi.config, 'config_with', config_with)
        self.stubs.Set(yagi.config, 'get', get)
コード例 #13
0
    def test_send_notification_successfully(self):
        self.called = False
        content = ("""<atom:entry xmlns:atom="http://www.w3.org/2005/Atom">"""
        """<atom:id>urn:uuid:95347e4d-4737-4438-b774-6a9219d78d2a</atom:id>"""
        """</atom:entry>""")

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(201), content

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a":"b"}
        self.mox.ReplayAll()

        self.stubs.Set(httplib2.Http, 'request', mock_request)
        http_conn.send_notification(endpoint, endpoint, payload_body)
        self.mox.VerifyAll()
コード例 #14
0
ファイル: test_http_connection.py プロジェクト: xroot88/yagi
    def test_send_notification_successfully(self):
        self.called = False
        content = (
            """<atom:entry xmlns:atom="http://www.w3.org/2005/Atom">"""
            """<atom:id>urn:uuid:95347e4d-4737-4438-b774-6a9219d78d2a</atom:id>"""
            """</atom:entry>""")

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(201), content

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a": "b"}
        self.mox.ReplayAll()

        self.stubs.Set(httplib2.Http, 'request', mock_request)
        http_conn.send_notification(endpoint, endpoint, payload_body)
        self.mox.VerifyAll()
コード例 #15
0
ファイル: test_http_connection.py プロジェクト: xroot88/yagi
    def setUp(self):
        self.stubs = stubout.StubOutForTesting()
        self.mox = mox.Mox()
        config_dict = {
            'atompub': {
                'url': 'http://127.0.0.1:9000/test/%(event_type)s',
                'user': '******',
                'key': 'key',
                'interval': 30,
                'max_wait': 600,
                'retries': 1,
                'failures_before_reauth': 5,
                'timeout': '120',
            },
            'event_feed': {
                'feed_title': 'feed_title',
                'feed_host': 'feed_host',
                'use_https': False,
                'port': 'port'
            },
            'handler_auth': {
                'method': 'no_auth'
            },
            'cufpub': {
                'url': 'http://127.0.0.1:9000/test/%(event_type)s',
                'user': '******',
                'key': 'key',
                'interval': 30,
                'max_wait': 600,
                'retries': 1,
                'failures_before_reauth': 5,
                'timeout': '120',
            }
        }

        self.handler = CufPub()

        def get(*args, **kwargs):
            val = None
            for arg in args:
                if val:
                    val = val.get(arg)
                else:
                    val = config_dict.get(arg)
                    if not val:
                        return None or kwargs.get('default')
            return val

        def config_with(*args):
            return functools.partial(get, args)

        self.stubs.Set(yagi.config, 'config_with', config_with)
        self.stubs.Set(yagi.config, 'get', get)
コード例 #16
0
ファイル: test_base_handler.py プロジェクト: sunjiyun26/yagi
    def setUp(self):
        self.mox = mox.Mox()
        self.stubs = stubout.StubOutForTesting()

        self.handler = CufPub()

        def get(*args, **kwargs):
            val = None
            for arg in args:
                if val:
                    val = val.get(arg)
                else:
                    val = self.config_dict.get(arg)
                    if not val:
                        return None or kwargs.get('default')
            return val

        def config_with(*args):
            return functools.partial(get, args)

        self.stubs.Set(yagi.config, 'config_with', config_with)
        self.stubs.Set(yagi.config, 'get', get)
コード例 #17
0
class CufPubTests(unittest.TestCase):
    """Tests to ensure the ATOM CUF Pub code holds together as expected"""
    def setUp(self):
        self.stubs = stubout.StubOutForTesting()
        config_dict = {
            'atompub': {
                'url': 'http://127.0.0.1:9000/test/test_feed',
                'user': '******',
                'key': 'key',
                'interval': 30,
                'max_wait': 600,
                'retries': 1,
                'failures_before_reauth': 5,
                'timeout': '120',
            },
            'event_feed': {
                'feed_title': 'feed_title',
                'feed_host': 'feed_host',
                'use_https': False,
                'port': 'port',
                'atom_categories': "DATACENTER=ORD1, REGION=PREPROD-ORD"
            },
            'handler_auth': {
                'method': 'no_auth'
            },
            'cufpub': {
                'url': 'http://127.0.0.1:9000/test/test_feed',
                'user': '******',
                'key': 'key',
                'interval': 30,
                'max_wait': 600,
                'retries': 1,
                'failures_before_reauth': 5,
                'timeout': '120',
            },
            'nova': {
                'nova_flavor_field_name': 'dummy_flavor_field_name'
            }
        }

        self.handler = CufPub()

        def get(*args, **kwargs):
            val = None
            for arg in args:
                if val:
                    val = val.get(arg)
                else:
                    val = config_dict.get(arg)
                    if not val:
                        return None or kwargs.get('default')
            return val

        def config_with(*args):
            return functools.partial(get, args)

        self.stubs.Set(yagi.config, 'config_with', config_with)
        self.stubs.Set(yagi.config, 'get', get)

    def tearDown(self):
        self.stubs.UnsetAll()

    @mock.patch(
        'httplib2.Http.request',
        return_value=(
            MockResponse(201),
            """<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"><atom:id>"""
            """urn:uuid:95347e4d-4737-4438-b774-6a9219d78d2a</atom:id>"""))
    def test_notify_for_instance_exists_message(self, mock_request):
        original_message_id = '425b23c9-9add-409f-938e-c131f304602a'
        messages = [
            MockMessage({
                'event_type': 'compute.instance.exists',
                'message_id': 'some_uuid',
                'original_message_id': original_message_id,
                'payload': {
                    'tenant_id': '2882',
                    'access_ip_v4': '5.79.20.138',
                    'access_ip_v6': '2a00:1a48:7804:0110:a8a0:fa07:ff08:157a',
                    'audit_period_beginning': '2012-09-15 11:51:11',
                    'audit_period_ending': '2012-09-16 11:51:11',
                    'display_name': 'test',
                    'bandwidth': {
                        'private': {
                            'bw_in': '0',
                            'bw_out': '264902'
                        },
                        'public': {
                            'bw_in': '1001',
                            'bw_out': '19992'
                        }
                    },
                    'image_meta': {
                        'com.rackspace__1__options': '1'
                    },
                    'instance_id': '56',
                    'dummy_flavor_field_name': '10',
                    'launched_at': '2012-09-15 12:51:11',
                    'deleted_at': '',
                    'instance_type': 'm1.nano',
                    'state': 'active',
                    'state_description': ''
                }
            })
        ]

        cuf_xml_body = (
            """<?xml version="1.0" encoding="utf-8"?>\n"""
            """<atom:entry xmlns:atom="http://www.w3.org"""
            """/2005/Atom"><atom:category term="compute.instance."""
            """exists.verified.cuf"></atom:category><atom:category term"""
            """="original_message_id:425b23c9-9add-409f-938e-c131f304602a">"""
            """</atom:category><atom:title type="text">Server</atom:title>"""
            """<atom:content type="application/xml"><event xmlns="http://"""
            """docs.rackspace.com/core/event" xmlns:nova="http://docs"""
            """.rackspace.com/event/nova" version="1" """
            """id="10c3e7b3-2ac3-5c93-bc4f-ae32e61d9190" resourceId="56" """
            """resourceName="test" dataCenter="ORD1" region="PREPROD-ORD" """
            """tenantId="2882" startTime="2012-09-15T12:51:11Z" """
            """endTime="2012-09-16T11:51:11Z" type="USAGE"><nova:product """
            """version="1" serviceCode="CloudServersOpenStack" resourceType"""
            """="SERVER" flavorId="10" flavorName="m1.nano" status="ACTIVE" """
            """osLicenseType="RHEL" bandwidthIn="1001" bandwidthOut"""
            """="19992"/></event></atom:content></atom:entry>""")

        self.handler.handle_messages(messages, dict())

        self.assertTrue(mock_request.called)
        mock_request.assert_called_with(
            'http://127.0.0.1:9000/test/test_feed',
            'POST',
            body=cuf_xml_body,
            headers={'Content-Type': 'application/atom+xml'})

    @mock.patch(
        'httplib2.Http.request',
        return_value=(
            MockResponse(201),
            """<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"><atom:id>"""
            """urn:uuid:95347e4d-4737-4438-b774-6a9219d78d2a</atom:id>"""))
    def test_notify_for_image_exists_message_for_one_image(self, mock_request):
        original_message_id = '425b23c9-9add-409f-938e-c131f304602a'
        messages = [
            MockMessage({
                "event_type": "image.exists",
                "timestamp": "2013-09-02 16:09:16.247932",
                "message_id": "18b59543-2e99-4208-ba53-22726c02bd67",
                "original_message_id": original_message_id,
                "priority": "INFO",
                "publisher_id": "ubuntu",
                "payload": {
                    "owner":
                    "owner1",
                    "audit_period_ending":
                    "2013-09-02 23:59:59.999999",
                    "audit_period_beginning":
                    "2013-09-02 00:00:00",
                    "images": [{
                        "status": "active",
                        "name": "image1",
                        "created_at": "2013-09-02 16:08:10",
                        "properties": {
                            "image_type": "snapshot",
                            "instance_uuid": "inst_uuid1"
                        },
                        "deleted_at": None,
                        "id": "image1",
                        "size": 12345
                    }]
                }
            })
        ]

        cuf_xml_body = (
            """<?xml version="1.0" encoding="utf-8"?>\n"""
            """<atom:entry xmlns="http://docs.rackspace."""
            """com/core/event" xmlns:atom="http://www.w3.org/2005/Atom" """
            """xmlns:glance="http://docs.rackspace.com/usage/glance">"""
            """<atom:category term="image.exists.verified.cuf"></atom:category>"""
            """<atom:category term="original_message_id:425b23c9-9add-409f-938e-c131f304602a"></atom:category>"""
            """<atom:title type="text">Glance"""
            """</atom:title><atom:content type="application/xml"><"""
            """event endTime="2013-09-02T23:59:59Z" """
            """startTime="2013-09-02T16:08:10Z" region="PREPROD-ORD" """
            """dataCenter="ORD1" type="USAGE" """
            """id="3ec2aa55-1f5c-59b9-b7c9-f05a8dc5b9e3" resourceId="image1" """
            """tenantId="owner1" version="1"> <glance:product """
            """storage="12345" serviceCode="Glance" """
            """version="1"/></event>"""
            """</atom:content></atom:entry>""")

        self.handler.handle_messages(messages, dict())

        self.assertTrue(mock_request.called)
        mock_request.assert_called_with(
            'http://127.0.0.1:9000/test/test_feed',
            'POST',
            body=cuf_xml_body,
            headers={'Content-Type': 'application/atom+xml'})

    @mock.patch(
        'httplib2.Http.request',
        return_value=(
            MockResponse(201),
            """<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"><atom:id>"""
            """urn:uuid:95347e4d-4737-4438-b774-6a9219d78d2a</atom:id>"""))
    def test_notify_for_image_exists_message_for_more_than_one_image(
            self, mock_request):
        original_message_id = '425b23c9-9add-409f-938e-c131f304602a'
        messages = [
            MockMessage({
                "event_type": "image.exists",
                "timestamp": "2013-09-02 16:09:16.247932",
                "message_id": "18b59543-2e99-4208-ba53-22726c02bd67",
                "original_message_id": original_message_id,
                "priority": "INFO",
                "publisher_id": "ubuntu",
                "payload": {
                    "owner":
                    "owner1",
                    "audit_period_ending":
                    "2013-09-02 23:59:59.999999",
                    "audit_period_beginning":
                    "2013-09-02 00:00:00",
                    "images": [{
                        "status": "active",
                        "name": "image1",
                        "created_at": "2013-09-02 16:08:10",
                        "properties": {
                            "image_type": "snapshot",
                            "instance_uuid": "inst_uuid1"
                        },
                        "deleted_at": None,
                        "id": "image1",
                        "size": 12345
                    }, {
                        "status": "deleted",
                        "name": "image2",
                        "created_at": "2013-09-02 16:05:17",
                        "properties": {
                            "image_type": "snapshot",
                            "instance_uuid": "inst_uuid2"
                        },
                        "deleted_at": "2013-09-02 16:08:46",
                        "id": "image2",
                        "size": 67890
                    }]
                }
            })
        ]

        cuf_xml_body = (
            """<?xml version="1.0" encoding="utf-8"?>\n"""
            """<atom:entry xmlns="http://docs.rackspace."""
            """com/core/event" xmlns:atom="http://www.w3.org/2005/Atom" """
            """xmlns:glance="http://docs.rackspace.com/usage/glance">"""
            """<atom:category term="image.exists.verified.cuf"></atom:category>"""
            """<atom:category term="original_message_id:425b23c9-9add-409f-938e-c131f304602a"></atom:category>"""
            """<atom:title type="text">Glance</atom:title><atom:content type="application/xml">"""
            """<event endTime="2013-09-02T23:59:59Z" startTime="2013-09-02T"""
            """16:08:10Z" region="PREPROD-ORD" dataCenter="ORD1" type="USAGE" """
            """id="3ec2aa55-1f5c-59b9-b7c9-f05a8dc5b9e3" resourceId="image1" """
            """tenantId="owner1" version="1"> <glance:product storage="12345" """
            """serviceCode="Glance" """
            """version="1"/></event><event """
            """endTime="2013-09-02T16:08:46Z" startTime="2013-09-02T16:05:17Z" """
            """region="PREPROD-ORD" dataCenter="ORD1" type="USAGE" """
            """id="8809eae4-2e0e-52a6-b95a-a608ee3acb91" resourceId="image2" """
            """tenantId="owner1" version="1"> <glance:product storage="67890" """
            """serviceCode="Glance" """
            """version="1"/></event>"""
            """</atom:content></atom:entry>""")

        self.handler.handle_messages(messages, dict())

        self.assertTrue(mock_request.called)
        mock_request.assert_called_with(
            'http://127.0.0.1:9000/test/test_feed',
            'POST',
            body=cuf_xml_body,
            headers={'Content-Type': 'application/atom+xml'})

    @mock.patch('httplib2.Http.request',
                return_value=(MockResponse(404), """Bogus, dude!"""))
    def test_notify_fails(self, mock_request):
        messages = [
            MockMessage({
                'event_type': 'compute.instance.exists',
                'message_id': 'some_uuid',
                '_unique_id': 'e53d007a-fc23-11e1-975c-cfa6b29bb814',
                'payload': {
                    'tenant_id': '2882',
                    'access_ip_v4': '5.79.20.138',
                    'access_ip_v6': '2a00:1a48:7804:0110:a8a0:fa07:ff08:157a',
                    'audit_period_beginning': '2012-09-15 11:51:11',
                    'audit_period_ending': '2012-09-16 11:51:11',
                    'bandwidth': {
                        'private': {
                            'bw_in': 0,
                            'bw_out': 264902
                        },
                        'public': {
                            'bw_in': 1001,
                            'bw_out': 19992
                        }
                    },
                    'image_meta': {
                        'com.rackspace__1__options': '1'
                    },
                    'instance_id': '56',
                    'dummy_flavor_field_name': '40',
                    'launched_at': '2012-09-15 12:51:11',
                    'deleted_at': '',
                    'instance_type': 'm1.nano',
                    'state': 'active',
                    'state_description': ''
                }
            })
        ]

        self.handler.handle_messages(messages, dict())

        self.assertTrue(mock_request.called)

    @mock.patch('httplib2.Http.request',
                return_value=(MockResponse(404), """Bogus, dude!"""))
    def test_malformed_message_should_not_raise_exception(self, mock_request):
        messages = [
            MockMessage({
                'event_type': 'compute.instance.exists',
                'message_id': 'some_uuid',
                '_unique_id': 'e53d007a-fc23-11e1-975c-cfa6b29bb814',
                'payload': {
                    'tenant_id': '2882',
                    'access_ip_v4': '5.79.20.138',
                    'access_ip_v6': '2a00:1a48:7804:0110:a8a0:fa07:ff08:157a',
                    'audit_period_beginning': '2012-09-15 11:51:11',
                    'bandwidth': {
                        'private': {
                            'bw_in': 0,
                            'bw_out': 264902
                        },
                        'public': {
                            'bw_in': 1001,
                            'bw_out': 19992
                        }
                    },
                    'image_meta': {
                        'com.rackspace__1__options': '1'
                    },
                    'instance_id': '56',
                    'dummy_flavor_field_name': '40',
                    'launched_at': '2012-09-15 12:51:11',
                    'deleted_at': ''
                }
            })
        ]
        self.handler.handle_messages(messages, dict())

        self.assertFalse(mock_request.called)

    @mock.patch(
        'httplib2.Http.request',
        return_value=(
            MockResponse(201),
            """<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"><atom:id>"""
            """urn:uuid:95347e4d-4737-4438-b774-6a9219d78d2a</atom:id>"""))
    def test_notify_for_pub_ipv4_exists_message(self, mock_request):
        """This essentially checks that 'ip.exists' is a valid event_type"""
        original_message_id = '425b23c9-9add-409f-938e-c131f304602a'
        messages = [
            MockMessage({
                "_unique_id": original_message_id,
                "event_type": "ip.exists",
                "timestamp": "2016-06-13T23:59:59Z",
                "message_id": "18b59543-2e99-4208-ba53-22726c02bd67",
                "priority": "INFO",
                "publisher_id": "ubuntu",
                "payload": {
                    "endTime": "2016-06-13T23:59:59Z",
                    "startTime": "2016-06-13T00:00:00Z",
                    "id": "ffc6f692-b31b-4ea8-8056-fbbaefa86e34",
                    "ip_address": "10.69.221.27",
                    "ip_type": "fixed",
                    "tenant_id": "404"
                }
            })
        ]

        cuf_xml_body = (
            """<?xml version="1.0" encoding="utf-8"?>\n"""
            """<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"><atom:category """
            """term="neutron.ip.exists.verified.cuf"></atom:category><atom:category """
            """term="original_message_id:425b23c9-9add-409f-938e-c131f304602a"></atom:category><atom:title """
            """type="text">NeutronPubIPv4</atom:title><atom:content """
            """type="application/xml"><event """
            """xmlns="http://docs.rackspace.com/core/event" """
            """xmlns:neutron="http://docs.rackspace.com/usage/neutron/public-ip-usage" """
            """id="3ac28cdc-54ed-55ac-9f51-e00d166d4044" version="1" """
            """resourceId="ffc6f692-b31b-4ea8-8056-fbbaefa86e34" """
            """resourceName="10.69.221.27" tenantId="404" """
            """startTime="2016-06-13T00:00:00Z" endTime="2016-06-13T23:59:59Z" """
            """type="USAGE" dataCenter="ORD1" region="PREPROD-ORD"> <neutron:product """
            """serviceCode="CloudNetworks" resourceType="IP" version="1" ipType="fixed"/> """
            """</event></atom:content></atom:entry>""")

        self.handler.handle_messages(messages, dict())
        self.assertTrue(mock_request.called)
        mock_request.assert_called_with(
            'http://127.0.0.1:9000/test/test_feed',
            'POST',
            body=cuf_xml_body,
            headers={'Content-Type': 'application/atom+xml'})
コード例 #18
0
ファイル: test_cufpub.py プロジェクト: pombredanne/yagi
class CufPubTests(unittest.TestCase):
    """Tests to ensure the ATOM CUF Pub code holds together as expected"""

    def setUp(self):
        self.stubs = stubout.StubOutForTesting()
        self.mox = mox.Mox()
        config_dict = {
            'atompub': {
                'url': 'http://127.0.0.1:9000/test/%(event_type)s',
                'user': '******',
                'key': 'key',
                'interval': 30,
                'max_wait': 600,
                'retries': 1,
                'failures_before_reauth': 5
            },
            'event_feed': {
                'feed_title': 'feed_title',
                'feed_host': 'feed_host',
                'use_https': False,
                'port': 'port',
                'atom_categories': "DATACENTER=ORD1, REGION=PREPROD-ORD"

            },
            'handler_auth': {
                'method': 'no_auth'
            },
            'cufpub': {
                'url': 'http://127.0.0.1:9000/test/%(event_type)s',
                'user': '******',
                'key': 'key',
                'interval': 30,
                'max_wait': 600,
                'retries': 1,
                'failures_before_reauth': 5

            }
        }

        self.handler = CufPub()

        def get(*args, **kwargs):
            val = None
            for arg in args:
                if val:
                    val = val.get(arg)
                else:
                    val = config_dict.get(arg)
                    if not val:
                        return None or kwargs.get('default')
            return val

        def config_with(*args):
            return functools.partial(get, args)

        self.stubs.Set(yagi.config, 'config_with', config_with)
        self.stubs.Set(yagi.config, 'get', get)

    def tearDown(self):
        self.stubs.UnsetAll()
        self.mox.UnsetStubs()

    def test_notify_for_instance_exists_message(self):
        messages = [MockMessage(
            {
                'event_type': 'compute.instance.exists',
                'message_id': 'some_uuid',
                '_unique_id': 'e53d007a-fc23-11e1-975c-cfa6b29bb814',
                'payload':
                    {'tenant_id': '2882',
                     'access_ip_v4': '5.79.20.138',
                     'access_ip_v6': '2a00:1a48:7804:0110:a8a0:fa07:ff08:157a',
                     'audit_period_beginning': '2012-09-15 11:51:11',
                     'audit_period_ending': '2012-09-16 11:51:11',
                     'bandwidth': {'private': {'bw_in': '0', 'bw_out': '264902'},
                                   'public': {'bw_in': '1001', 'bw_out': '19992'}
                                  },
                     'image_meta': {'com.rackspace__1__options': '1'},

                     'instance_id': '56',
                     'instance_type_id': '10',
                     'launched_at': '2012-09-15 12:51:11',
                     'deleted_at': ''}
            }
        )]

        body = ("""<?xml version="1.0" encoding="utf-8"?>\n<?atom """
        """feed="glance/events"?><atom:entry xmlns="http://www.w3.org"""
        """/2005/Atom"><category term="compute.instance.exists.verified.cuf">"""
        """</category><atom:title type="text">Server</atom:title>"""
        """<atom:content type="application/xml">&lt;event xmlns="http://"""
        """docs.rackspace.com/core/event" xmlns:nova="http://docs"""
        """.rackspace.com/event/nova" version"""
        """="1" tenantId="2882" id="e53d007a-fc23-11e1-975c-cfa6b29bb814" """
        """resourceId="56" type="USAGE" dataCenter="PREPROD-ORD" """
        """region="ORD1" startTime="2012-09-15 12:51:11" """
        """endTime="2012-09-16 11:51:11"&gt;&lt;nova:product version="1" """
        """serviceCode="CloudServersOpenStack" resourceType="SERVER" """
        """flavor="10" isRedHat="true" isMSSQL="false" isMSSQLWeb="false" """
        """isWindows="false" isSELinux="false" isManaged="false" """
        """bandwidthIn="1001" bandwidthOut="19992"/&gt;&lt;/event&gt"""
        """;</atom:content></atom:entry></atom>""")
        self.mox.StubOutWithMock(httplib2.Http, """request""")
        httplib2.Http.request('http://127.0.0.1:9000/test/%(event_type)s',
                              'POST', body=body,
                              headers={'Content-Type': 'application/atom+xml'}
        ).AndReturn((MockResponse(201), None))

        self.mox.ReplayAll()

        self.handler.handle_messages(messages, dict())

        self.mox.VerifyAll()

    def test_notify_for_image_exists_message_for_one_image(self):
        messages = [MockMessage(
            {"event_type": "image.exists",
            "timestamp": "2013-09-02 16:09:16.247932",
            "message_id": "18b59543-2e99-4208-ba53-22726c02bd67",
            "priority": "INFO",
            "publisher_id": "ubuntu",
            "payload": {
                "owner": "owner1",
                "audit_period_ending": "2013-09-02 23:59:59.999999",
                "audit_period_beginning": "2013-09-02 00:00:00",
                "images": [{
                    "status": "active",
                    "name": "image1",
                    "created_at": "2013-09-02 16:08:10",
                    "properties": {
                        "image_type": "snapshot",
                        "instance_uuid": "inst_uuid1"
                    },
                    "deleted_at": None,
                    "id": "image1",
                    "size": 12345
                }]
        }}
        )]

        cuf_xml_body = ("""<?xml version="1.0" encoding="utf-8"?>\n<?atom """
        """feed="glance/events"?><atom:entry xmlns="http://docs.rackspace."""
        """com/core/event" xmlns:atom="http://www.w3.org/2005/Atom" """
        """xmlns:glance="http://docs.rackspace.com/usage/glance">"""
        """<category term="image.exists.verified.cuf"></category>"""
        """<atom:title type="text">Glance"""
        """</atom:title><atom:content type="application/xml">&lt;events&gt;&lt;"""
        """event endTime="2013-09-02 23:59:59.999999" """
        """startTime="2013-09-02 16:08:10" region="ORD1" """
        """dataCenter="PREPROD-ORD" type="USAGE" """
        """id="a70508f3-7254-4abc-9e14-49de6bb4d628" resourceId="image1" """
        """tenantId="owner1" version="1"&gt; &lt;glance:product """
        """storage="12345" serverId="inst_uuid1" serviceCode="Glance" """
        """serverName="" resourceType="snapshot" version="1"/&gt;&lt;/event&gt;&lt;/events&gt;"""
        """</atom:content></atom:entry></atom>""")

        self.mox.StubOutWithMock(httplib2.Http, 'request')
        httplib2.Http.request('http://127.0.0.1:9000/test/%(event_type)s',
                              'POST', body=cuf_xml_body,
                              headers={'Content-Type': 'application/atom+xml'}
        ).AndReturn((MockResponse(201), None))
        self.mox.StubOutWithMock(uuid, 'uuid4')
        uuid.uuid4().AndReturn("a70508f3-7254-4abc-9e14-49de6bb4d628")
        uuid.uuid4().AndReturn("a70508f3-7254-4abc-9e14-49de6bb4d628")
        self.mox.ReplayAll()

        self.handler.handle_messages(messages, dict())

        self.mox.VerifyAll()

    def test_notify_for_image_exists_message_for_more_than_one_image(self):
        messages = [MockMessage(
            {"event_type": "image.exists",
            "timestamp": "2013-09-02 16:09:16.247932",
            "message_id": "18b59543-2e99-4208-ba53-22726c02bd67",
            "priority": "INFO",
            "publisher_id": "ubuntu",
            "payload": {
                "owner": "owner1",
                "audit_period_ending": "2013-09-02 23:59:59.999999",
                "audit_period_beginning": "2013-09-02 00:00:00",
                "images": [{
                    "status": "active",
                    "name": "image1",
                    "created_at": "2013-09-02 16:08:10",
                    "properties": {
                        "image_type": "snapshot",
                        "instance_uuid": "inst_uuid1"
                    },
                    "deleted_at": None,
                    "id": "image1",
                    "size": 12345
                },
                {
                    "status": "deleted",
                    "name": "image2",
                    "created_at": "2013-09-02 16:05:17",
                    "properties": {
                        "image_type": "snapshot",
                        "instance_uuid": "inst_uuid2"
                    },
                    "deleted_at": "2013-09-02 16:08:46",
                    "id": "image2",
                    "size": 67890
                }]
        }}
        )]

        cuf_xml_body = ("""<?xml version="1.0" encoding="utf-8"?>\n<?atom """
        """feed="glance/events"?><atom:entry xmlns="http://docs.rackspace."""
        """com/core/event" xmlns:atom="http://www.w3.org/2005/Atom" """
        """xmlns:glance="http://docs.rackspace.com/usage/glance">"""
        """<category term="image.exists.verified.cuf"></category>"""
        """<atom:title type="text">Glance</atom:title><atom:content type="application/xml">"""
        """&lt;events&gt;&lt;event endTime="2013-09-02 23:59:59.999999" startTime="2013-09-02 """
        """16:08:10" region="ORD1" dataCenter="PREPROD-ORD" type="USAGE" """
        """id="a70508f3-7254-4abc-9e14-49de6bb4d628" resourceId="image1" """
        """tenantId="owner1" version="1"&gt; &lt;glance:product storage="12345" """
        """serverId="inst_uuid1" serviceCode="Glance" serverName="" """
        """resourceType="snapshot" version="1"/&gt;&lt;/event&gt;&lt;event """
        """endTime="2013-09-02 16:08:46" startTime="2013-09-02 16:05:17" """
        """region="ORD1" dataCenter="PREPROD-ORD" type="USAGE" """
        """id="a70508f3-7254-4abc-9e14-49de6bb4d628" resourceId="image2" """
        """tenantId="owner1" version="1"&gt; &lt;glance:product storage="67890" """
        """serverId="inst_uuid2" serviceCode="Glance" serverName="" """
        """resourceType="snapshot" version="1"/&gt;&lt;/event&gt;&lt;/events&gt;"""
        """</atom:content></atom:entry></atom>""")

        self.mox.StubOutWithMock(httplib2.Http, 'request')
        httplib2.Http.request('http://127.0.0.1:9000/test/%(event_type)s',
                              'POST', body=cuf_xml_body,
                              headers={'Content-Type': 'application/atom+xml'}
        ).AndReturn((MockResponse(201), None))
        self.mox.StubOutWithMock(uuid, 'uuid4')
        for i in range(0,3):
            uuid.uuid4().AndReturn("a70508f3-7254-4abc-9e14-49de6bb4d628")
        self.mox.ReplayAll()

        self.handler.handle_messages(messages, dict())

        self.mox.VerifyAll()

    def test_notify_fails(self):
        messages = [MockMessage(
            {
                'event_type': 'compute.instance.exists',
                'message_id': 'some_uuid',
                '_unique_id': 'e53d007a-fc23-11e1-975c-cfa6b29bb814',
                'payload':
                    {'tenant_id': '2882',
                     'access_ip_v4': '5.79.20.138',
                     'access_ip_v6': '2a00:1a48:7804:0110:a8a0:fa07:ff08:157a',
                     'audit_period_beginning': '2012-09-15 11:51:11',
                     'audit_period_ending': '2012-09-16 11:51:11',
                     'bandwidth': {'private': {'bw_in': 0, 'bw_out': 264902},
                                   'public': {'bw_in': 1001, 'bw_out': 19992}
                                  },
                     'image_meta': {'com.rackspace__1__options': '1'},

                     'instance_id': '56',
                     'instance_type_id': '10',
                     'launched_at': '2012-09-15 12:51:11',
                     'deleted_at': ''}
            }
        )]
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(404), None

        self.stubs.Set(httplib2.Http, 'request', mock_request)
        self.handler.handle_messages(messages, dict())
        self.assertEqual(self.called, True)

    def test_malformed_message_should_not_raise_exception(self):
        messages = [MockMessage(
            {
                'event_type': 'compute.instance.exists',
                'message_id': 'some_uuid',
                '_unique_id': 'e53d007a-fc23-11e1-975c-cfa6b29bb814',
                'payload':
                    {'tenant_id': '2882',
                     'access_ip_v4': '5.79.20.138',
                     'access_ip_v6': '2a00:1a48:7804:0110:a8a0:fa07:ff08:157a',
                     'audit_period_beginning': '2012-09-15 11:51:11',
                     'bandwidth': {'private': {'bw_in': 0, 'bw_out': 264902},
                                   'public': {'bw_in': 1001, 'bw_out': 19992}
                                  },
                     'image_meta': {'com.rackspace__1__options': '1'},

                     'instance_id': '56',
                     'instance_type_id': '10',
                     'launched_at': '2012-09-15 12:51:11',
                     'deleted_at': ''}
            }
        )]
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(404), None

        self.stubs.Set(httplib2.Http, 'request', mock_request)
        self.handler.handle_messages(messages, dict())
        self.assertEqual(self.called, False)
コード例 #19
0
ファイル: test_cufpub.py プロジェクト: rackerlabs/yagi
class CufPubTests(unittest.TestCase):
    """Tests to ensure the ATOM CUF Pub code holds together as expected"""

    def setUp(self):
        self.stubs = stubout.StubOutForTesting()
        config_dict = {
            'atompub': {
                'url': 'http://127.0.0.1:9000/test/test_feed',
                'user': '******',
                'key': 'key',
                'interval': 30,
                'max_wait': 600,
                'retries': 1,
                'failures_before_reauth': 5,
                'timeout': '120',
            },
            'event_feed': {
                'feed_title': 'feed_title',
                'feed_host': 'feed_host',
                'use_https': False,
                'port': 'port',
                'atom_categories': "DATACENTER=ORD1, REGION=PREPROD-ORD"

            },
            'handler_auth': {
                'method': 'no_auth'
            },
            'cufpub': {
                'url': 'http://127.0.0.1:9000/test/test_feed',
                'user': '******',
                'key': 'key',
                'interval': 30,
                'max_wait': 600,
                'retries': 1,
                'failures_before_reauth': 5,
                'timeout': '120',

            },
            'nova': {
                'nova_flavor_field_name': 'dummy_flavor_field_name'
            }
        }

        self.handler = CufPub()

        def get(*args, **kwargs):
            val = None
            for arg in args:
                if val:
                    val = val.get(arg)
                else:
                    val = config_dict.get(arg)
                    if not val:
                        return None or kwargs.get('default')
            return val

        def config_with(*args):
            return functools.partial(get, args)

        self.stubs.Set(yagi.config, 'config_with', config_with)
        self.stubs.Set(yagi.config, 'get', get)

    def tearDown(self):
        self.stubs.UnsetAll()

    @mock.patch('httplib2.Http.request', return_value=(MockResponse(201),
        """<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"><atom:id>"""
        """urn:uuid:95347e4d-4737-4438-b774-6a9219d78d2a</atom:id>"""))
    def test_notify_for_instance_exists_message(self, mock_request):
        original_message_id = '425b23c9-9add-409f-938e-c131f304602a'
        messages = [MockMessage(
            {
                'event_type': 'compute.instance.exists',
                'message_id': 'some_uuid',
                'original_message_id': original_message_id,
                'payload':
                    {'tenant_id': '2882',
                     'access_ip_v4': '5.79.20.138',
                     'access_ip_v6': '2a00:1a48:7804:0110:a8a0:fa07:ff08:157a',
                     'audit_period_beginning': '2012-09-15 11:51:11',
                     'audit_period_ending': '2012-09-16 11:51:11',
                     'display_name': 'test',
                     'bandwidth': {'private': {'bw_in': '0', 'bw_out': '264902'},
                                   'public': {'bw_in': '1001', 'bw_out': '19992'}
                                  },
                     'image_meta': {'com.rackspace__1__options': '1'},

                     'instance_id': '56',
                     'dummy_flavor_field_name': '10',
                     'launched_at': '2012-09-15 12:51:11',
                     'deleted_at': '',
                     'instance_type': 'm1.nano',
                     'state': 'active',
                     'state_description': ''}
            }
        )]

        cuf_xml_body = ("""<?xml version="1.0" encoding="utf-8"?>\n"""
        """<atom:entry xmlns:atom="http://www.w3.org"""
        """/2005/Atom"><atom:category term="compute.instance."""
        """exists.verified.cuf"></atom:category><atom:category term"""
        """="original_message_id:425b23c9-9add-409f-938e-c131f304602a">"""
        """</atom:category><atom:title type="text">Server</atom:title>"""
        """<atom:content type="application/xml"><event xmlns="http://"""
        """docs.rackspace.com/core/event" xmlns:nova="http://docs"""
        """.rackspace.com/event/nova" version="1" """
        """id="10c3e7b3-2ac3-5c93-bc4f-ae32e61d9190" resourceId="56" """
        """resourceName="test" dataCenter="ORD1" region="PREPROD-ORD" """
        """tenantId="2882" startTime="2012-09-15T12:51:11Z" """
        """endTime="2012-09-16T11:51:11Z" type="USAGE"><nova:product """
        """version="1" serviceCode="CloudServersOpenStack" resourceType"""
        """="SERVER" flavorId="10" flavorName="m1.nano" status="ACTIVE" """
        """osLicenseType="RHEL" bandwidthIn="1001" bandwidthOut"""
        """="19992"/></event></atom:content></atom:entry>""")

        self.handler.handle_messages(messages, dict())

        self.assertTrue(mock_request.called)
        mock_request.assert_called_with('http://127.0.0.1:9000/test/test_feed',
                                        'POST', body=cuf_xml_body,
                                         headers={'Content-Type': 'application/atom+xml'})

    @mock.patch('httplib2.Http.request', return_value=(MockResponse(201),
        """<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"><atom:id>"""
        """urn:uuid:95347e4d-4737-4438-b774-6a9219d78d2a</atom:id>"""))
    def test_notify_for_image_exists_message_for_one_image(self, mock_request):
        original_message_id = '425b23c9-9add-409f-938e-c131f304602a'
        messages = [MockMessage(
            {"event_type": "image.exists",
            "timestamp": "2013-09-02 16:09:16.247932",
            "message_id": "18b59543-2e99-4208-ba53-22726c02bd67",
            "original_message_id": original_message_id,
            "priority": "INFO",
            "publisher_id": "ubuntu",
            "payload": {
                "owner": "owner1",
                "audit_period_ending": "2013-09-02 23:59:59.999999",
                "audit_period_beginning": "2013-09-02 00:00:00",
                "images": [{
                    "status": "active",
                    "name": "image1",
                    "created_at": "2013-09-02 16:08:10",
                    "properties": {
                        "image_type": "snapshot",
                        "instance_uuid": "inst_uuid1"
                    },
                    "deleted_at": None,
                    "id": "image1",
                    "size": 12345
                }]
        }}
        )]

        cuf_xml_body = ("""<?xml version="1.0" encoding="utf-8"?>\n"""
        """<atom:entry xmlns="http://docs.rackspace."""
        """com/core/event" xmlns:atom="http://www.w3.org/2005/Atom" """
        """xmlns:glance="http://docs.rackspace.com/usage/glance">"""
        """<atom:category term="image.exists.verified.cuf"></atom:category>"""
        """<atom:category term="original_message_id:425b23c9-9add-409f-938e-c131f304602a"></atom:category>"""
        """<atom:title type="text">Glance"""
        """</atom:title><atom:content type="application/xml"><"""
        """event endTime="2013-09-02T23:59:59Z" """
        """startTime="2013-09-02T16:08:10Z" region="PREPROD-ORD" """
        """dataCenter="ORD1" type="USAGE" """
        """id="3ec2aa55-1f5c-59b9-b7c9-f05a8dc5b9e3" resourceId="image1" """
        """tenantId="owner1" version="1"> <glance:product """
        """storage="12345" serviceCode="Glance" """
        """version="1"/></event>"""
        """</atom:content></atom:entry>""")

        self.handler.handle_messages(messages, dict())

        self.assertTrue(mock_request.called)
        mock_request.assert_called_with('http://127.0.0.1:9000/test/test_feed',
                                        'POST', body=cuf_xml_body,
                                         headers={'Content-Type': 'application/atom+xml'})

    @mock.patch('httplib2.Http.request', return_value=(MockResponse(201),
        """<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"><atom:id>"""
        """urn:uuid:95347e4d-4737-4438-b774-6a9219d78d2a</atom:id>"""))
    def test_notify_for_image_exists_message_for_more_than_one_image(self, mock_request):
        original_message_id = '425b23c9-9add-409f-938e-c131f304602a'
        messages = [MockMessage(
            {"event_type": "image.exists",
            "timestamp": "2013-09-02 16:09:16.247932",
            "message_id": "18b59543-2e99-4208-ba53-22726c02bd67",
            "original_message_id": original_message_id,
            "priority": "INFO",
            "publisher_id": "ubuntu",
            "payload": {
                "owner": "owner1",
                "audit_period_ending": "2013-09-02 23:59:59.999999",
                "audit_period_beginning": "2013-09-02 00:00:00",
                "images": [{
                    "status": "active",
                    "name": "image1",
                    "created_at": "2013-09-02 16:08:10",
                    "properties": {
                        "image_type": "snapshot",
                        "instance_uuid": "inst_uuid1"
                    },
                    "deleted_at": None,
                    "id": "image1",
                    "size": 12345
                },
                {
                    "status": "deleted",
                    "name": "image2",
                    "created_at": "2013-09-02 16:05:17",
                    "properties": {
                        "image_type": "snapshot",
                        "instance_uuid": "inst_uuid2"
                    },
                    "deleted_at": "2013-09-02 16:08:46",
                    "id": "image2",
                    "size": 67890
                }]
        }}
        )]

        cuf_xml_body = ("""<?xml version="1.0" encoding="utf-8"?>\n"""
        """<atom:entry xmlns="http://docs.rackspace."""
        """com/core/event" xmlns:atom="http://www.w3.org/2005/Atom" """
        """xmlns:glance="http://docs.rackspace.com/usage/glance">"""
        """<atom:category term="image.exists.verified.cuf"></atom:category>"""
        """<atom:category term="original_message_id:425b23c9-9add-409f-938e-c131f304602a"></atom:category>"""
        """<atom:title type="text">Glance</atom:title><atom:content type="application/xml">"""
        """<event endTime="2013-09-02T23:59:59Z" startTime="2013-09-02T"""
        """16:08:10Z" region="PREPROD-ORD" dataCenter="ORD1" type="USAGE" """
        """id="3ec2aa55-1f5c-59b9-b7c9-f05a8dc5b9e3" resourceId="image1" """
        """tenantId="owner1" version="1"> <glance:product storage="12345" """
        """serviceCode="Glance" """
        """version="1"/></event><event """
        """endTime="2013-09-02T16:08:46Z" startTime="2013-09-02T16:05:17Z" """
        """region="PREPROD-ORD" dataCenter="ORD1" type="USAGE" """
        """id="8809eae4-2e0e-52a6-b95a-a608ee3acb91" resourceId="image2" """
        """tenantId="owner1" version="1"> <glance:product storage="67890" """
        """serviceCode="Glance" """
        """version="1"/></event>"""
        """</atom:content></atom:entry>""")

        self.handler.handle_messages(messages, dict())

        self.assertTrue(mock_request.called)
        mock_request.assert_called_with('http://127.0.0.1:9000/test/test_feed',
                                        'POST', body=cuf_xml_body,
                                         headers={'Content-Type': 'application/atom+xml'})

    @mock.patch('httplib2.Http.request', return_value=(MockResponse(404), """Bogus, dude!"""))
    def test_notify_fails(self, mock_request):
        messages = [MockMessage(
            {
                'event_type': 'compute.instance.exists',
                'message_id': 'some_uuid',
                '_unique_id': 'e53d007a-fc23-11e1-975c-cfa6b29bb814',
                'payload':
                    {'tenant_id': '2882',
                     'access_ip_v4': '5.79.20.138',
                     'access_ip_v6': '2a00:1a48:7804:0110:a8a0:fa07:ff08:157a',
                     'audit_period_beginning': '2012-09-15 11:51:11',
                     'audit_period_ending': '2012-09-16 11:51:11',
                     'bandwidth': {'private': {'bw_in': 0, 'bw_out': 264902},
                                   'public': {'bw_in': 1001, 'bw_out': 19992}
                                  },
                     'image_meta': {'com.rackspace__1__options': '1'},

                     'instance_id': '56',
                     'dummy_flavor_field_name': '40',
                     'launched_at': '2012-09-15 12:51:11',
                     'deleted_at': '',
                     'instance_type': 'm1.nano',
                     'state': 'active',
                     'state_description': ''}
            }
        )]

        self.handler.handle_messages(messages, dict())

        self.assertTrue(mock_request.called)

    @mock.patch('httplib2.Http.request', return_value=(MockResponse(404), """Bogus, dude!"""))
    def test_malformed_message_should_not_raise_exception(self, mock_request):
        messages = [MockMessage(
            {
                'event_type': 'compute.instance.exists',
                'message_id': 'some_uuid',
                '_unique_id': 'e53d007a-fc23-11e1-975c-cfa6b29bb814',
                'payload':
                    {'tenant_id': '2882',
                     'access_ip_v4': '5.79.20.138',
                     'access_ip_v6': '2a00:1a48:7804:0110:a8a0:fa07:ff08:157a',
                     'audit_period_beginning': '2012-09-15 11:51:11',
                     'bandwidth': {'private': {'bw_in': 0, 'bw_out': 264902},
                                   'public': {'bw_in': 1001, 'bw_out': 19992}
                                  },
                     'image_meta': {'com.rackspace__1__options': '1'},

                     'instance_id': '56',
                     'dummy_flavor_field_name': '40',
                     'launched_at': '2012-09-15 12:51:11',
                     'deleted_at': ''}
            }
        )]
        self.handler.handle_messages(messages, dict())

        self.assertFalse(mock_request.called)


    @mock.patch('httplib2.Http.request', return_value=(MockResponse(201),
        """<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"><atom:id>"""
        """urn:uuid:95347e4d-4737-4438-b774-6a9219d78d2a</atom:id>"""))
    def test_notify_for_pub_ipv4_exists_message(self, mock_request):
        """This essentially checks that 'ip.exists' is a valid event_type"""
        original_message_id = '425b23c9-9add-409f-938e-c131f304602a'
        messages = [MockMessage(
            {
                "_unique_id": original_message_id,
                "event_type": "ip.exists",
                "timestamp": "2016-06-13T23:59:59Z",
                "message_id": "18b59543-2e99-4208-ba53-22726c02bd67",
                "priority": "INFO",
                "publisher_id": "ubuntu",
                "payload": {
                    "endTime": "2016-06-13T23:59:59Z",
                    "startTime": "2016-06-13T00:00:00Z",
                    "id": "ffc6f692-b31b-4ea8-8056-fbbaefa86e34",
                    "ip_address": "10.69.221.27",
                    "ip_type": "fixed",
                    "tenant_id": "404"
                }
            }
        )]

        cuf_xml_body = (
            """<?xml version="1.0" encoding="utf-8"?>\n"""
            """<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"><atom:category """
            """term="neutron.ip.exists.verified.cuf"></atom:category><atom:category """
            """term="original_message_id:425b23c9-9add-409f-938e-c131f304602a"></atom:category><atom:title """
            """type="text">NeutronPubIPv4</atom:title><atom:content """
            """type="application/xml"><event """
            """xmlns="http://docs.rackspace.com/core/event" """
            """xmlns:neutron="http://docs.rackspace.com/usage/neutron/public-ip-usage" """
            """id="3ac28cdc-54ed-55ac-9f51-e00d166d4044" version="1" """
            """resourceId="ffc6f692-b31b-4ea8-8056-fbbaefa86e34" """
            """resourceName="10.69.221.27" tenantId="404" """
            """startTime="2016-06-13T00:00:00Z" endTime="2016-06-13T23:59:59Z" """
            """type="USAGE" dataCenter="ORD1" region="PREPROD-ORD"> <neutron:product """
            """serviceCode="CloudNetworks" resourceType="IP" version="1" ipType="fixed"/> """
            """</event></atom:content></atom:entry>"""
        )

        self.handler.handle_messages(messages, dict())
        self.assertTrue(mock_request.called)
        mock_request.assert_called_with('http://127.0.0.1:9000/test/test_feed',
                                        'POST', body=cuf_xml_body,
                                         headers={'Content-Type': 'application/atom+xml'})
コード例 #20
0
ファイル: test_cufpub.py プロジェクト: anujm/yagi
class CufPubTests(unittest.TestCase):
    """Tests to ensure the ATOM CUF Pub code holds together as expected"""

    def setUp(self):
        self.stubs = stubout.StubOutForTesting()
        config_dict = {
            'atompub': {
                'url': 'http://127.0.0.1:9000/test/%(event_type)s',
                'user': '******',
                'key': 'key',
                'interval': 30,
                'max_wait': 600,
                'retries': 1,
                'failures_before_reauth': 5
            },
            'event_feed': {
                'feed_title': 'feed_title',
                'feed_host': 'feed_host',
                'use_https': False,
                'port': 'port',
                'atom_categories': {'DATACENTER': 'ORD1',
                                     'REGION': 'PREPROD-ORD'}

            },
            'handler_auth': {
                'method': 'no_auth'
            },
            'cufpub': {
                'url': 'http://127.0.0.1:9000/test/%(event_type)s',
                'user': '******',
                'key': 'key',
                'interval': 30,
                'max_wait': 600,
                'retries': 1,
                'failures_before_reauth': 5

            }
        }

        self.handler = CufPub()

        def get(*args, **kwargs):
            val = None
            for arg in args:
                if val:
                    val = val.get(arg)
                else:
                    val = config_dict.get(arg)
                    if not val:
                        return None or kwargs.get('default')
            return val

        def config_with(*args):
            return functools.partial(get, args)

        self.stubs.Set(yagi.config, 'config_with', config_with)
        self.stubs.Set(yagi.config, 'get', get)

    def tearDown(self):
        self.stubs.UnsetAll()

    def test_notify(self):
        messages = [MockMessage(
            {
                'event_type': 'test',
                'message_id': 'some_uuid',
                '_unique_id': 'e53d007a-fc23-11e1-975c-cfa6b29bb814',
                'payload':
                    {'tenant_id': '2882',
                     'access_ip_v4': '5.79.20.138',
                     'access_ip_v6': '2a00:1a48:7804:0110:a8a0:fa07:ff08:157a',
                     'audit_period_beginning': '2012-09-15 11:51:11',
                     'audit_period_ending': '2012-09-16 11:51:11',
                     'bandwidth': {'private': {'bw_in': 0, 'bw_out': 264902},
                                   'public': {'bw_in': 1001, 'bw_out': 19992}
                                  },
                     'image_meta': {'com.rackspace__1__options': '1'},

                     'instance_id': '56',
                     'instance_type_id': '10',
                     'launched_at': '2012-09-15 12:51:11',
                     'deleted_at': ''}
            }
        )]

        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(201), None

        self.stubs.Set(httplib2.Http, 'request', mock_request)
        self.handler.handle_messages(messages, dict())
        self.assertEqual(self.called, True)

    def test_notify_fails(self):
        messages = [MockMessage(
            {
                'event_type': 'test',
                'message_id': 'some_uuid',
                '_unique_id': 'e53d007a-fc23-11e1-975c-cfa6b29bb814',
                'payload':
                    {'tenant_id': '2882',
                     'access_ip_v4': '5.79.20.138',
                     'access_ip_v6': '2a00:1a48:7804:0110:a8a0:fa07:ff08:157a',
                     'audit_period_beginning': '2012-09-15 11:51:11',
                     'audit_period_ending': '2012-09-16 11:51:11',
                     'bandwidth': {'private': {'bw_in': 0, 'bw_out': 264902},
                                   'public': {'bw_in': 1001, 'bw_out': 19992}
                                  },
                     'image_meta': {'com.rackspace__1__options': '1'},

                     'instance_id': '56',
                     'instance_type_id': '10',
                     'launched_at': '2012-09-15 12:51:11',
                     'deleted_at': ''}
            }
        )]
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(404), None

        self.stubs.Set(httplib2.Http, 'request', mock_request)
        self.handler.handle_messages(messages, dict())
        self.assertEqual(self.called, True)

    def test_malformed_message_should_not_raise_exception(self):
        messages = [MockMessage(
            {
                'event_type': 'test',
                'message_id': 'some_uuid',
                '_unique_id': 'e53d007a-fc23-11e1-975c-cfa6b29bb814',
                'payload':
                    {'tenant_id': '2882',
                     'access_ip_v4': '5.79.20.138',
                     'access_ip_v6': '2a00:1a48:7804:0110:a8a0:fa07:ff08:157a',
                     'audit_period_beginning': '2012-09-15 11:51:11',
                     'bandwidth': {'private': {'bw_in': 0, 'bw_out': 264902},
                                   'public': {'bw_in': 1001, 'bw_out': 19992}
                                  },
                     'image_meta': {'com.rackspace__1__options': '1'},

                     'instance_id': '56',
                     'instance_type_id': '10',
                     'launched_at': '2012-09-15 12:51:11',
                     'deleted_at': ''}
            }
        )]
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(404), None

        self.stubs.Set(httplib2.Http, 'request', mock_request)
        self.handler.handle_messages(messages, dict())
        self.assertEqual(self.called, False)