Example #1
0
    def test_associated(self, mock_criteria):
        unit_1 = Mock()
        unit_1.metadata = {}
        unit_1.unit_key = {"name": "pulp1", "author": "john", "version": "1.0"}
        unit_2 = Mock()
        unit_2.metadata = {}
        unit_2.unit_key = {"name": "pulp2", "author": "josh", "version": "2.0"}
        units = [unit_1, unit_2]

        conduit = Mock()
        conduit.get_units = Mock(return_value=units)

        criteria = Mock()
        mock_criteria.return_value = criteria

        # test

        inventory = Inventory(conduit)

        # validation

        conduit.get_units.assert_called_once_with(criteria=criteria, as_generator=True)
        mock_criteria.assert_called_once_with(
            type_ids=[constants.TYPE_PUPPET_MODULE], unit_fields=Module.UNIT_KEY_NAMES
        )
        self.assertEqual(len(inventory.associated), 2)
        self.assertTrue(tuple(unit_1.unit_key.items()) in inventory.associated)
        self.assertTrue(tuple(unit_2.unit_key.items()) in inventory.associated)
    def test_wrap_round_robin(self):
        cluster = Mock(spec=Cluster)
        cluster.metadata = Mock(spec=Metadata)
        hosts = [Host(str(i), SimpleConvictionPolicy) for i in range(4)]
        for host in hosts:
            host.set_up()

        def get_replicas(keyspace, packed_key):
            index = struct.unpack(">i", packed_key)[0]
            return list(islice(cycle(hosts), index, index + 2))

        cluster.metadata.get_replicas.side_effect = get_replicas

        policy = TokenAwarePolicy(RoundRobinPolicy())
        policy.populate(cluster, hosts)

        for i in range(4):
            query = Statement(routing_key=struct.pack(">i", i), keyspace="keyspace_name")
            qplan = list(policy.make_query_plan(None, query))

            replicas = get_replicas(None, struct.pack(">i", i))
            other = set(h for h in hosts if h not in replicas)
            self.assertEqual(replicas, qplan[:2])
            self.assertEqual(other, set(qplan[2:]))

        # Should use the secondary policy
        for i in range(4):
            qplan = list(policy.make_query_plan())

            self.assertEqual(set(qplan), set(hosts))
Example #3
0
    def test_metadata_from_package(self):
        """Testing ExtensionInfo metadata from package"""
        app_name = "test_extension.dummy"
        project_name = "DummyExtension"
        module_name = "test_extension.dummy.submodule"
        extension_id = "%s:DummyExtension" % module_name
        htdocs_path = os.path.join(settings.MEDIA_ROOT, "ext", project_name)
        static_path = os.path.join(settings.STATIC_ROOT, "ext", extension_id)

        ext_class = Mock()
        ext_class.__module__ = module_name
        ext_class.id = extension_id
        ext_class.metadata = None

        entrypoint = FakeEntryPoint(ext_class, project_name=project_name)
        extension_info = ExtensionInfo(entrypoint, ext_class)
        metadata = entrypoint.dist.metadata

        self.assertEqual(extension_info.app_name, app_name)
        self.assertEqual(extension_info.author, metadata["Author"])
        self.assertEqual(extension_info.author_email, metadata["Author-email"])
        self.assertEqual(extension_info.description, metadata["Description"])
        self.assertFalse(extension_info.enabled)
        self.assertEqual(extension_info.installed_htdocs_path, htdocs_path)
        self.assertEqual(extension_info.installed_static_path, static_path)
        self.assertFalse(extension_info.installed)
        self.assertEqual(extension_info.license, metadata["License"])
        self.assertEqual(extension_info.metadata, metadata)
        self.assertEqual(extension_info.name, metadata["Name"])
        self.assertEqual(extension_info.summary, metadata["Summary"])
        self.assertEqual(extension_info.url, metadata["Home-page"])
        self.assertEqual(extension_info.version, metadata["Version"])
Example #4
0
 def test_retry_events_are_emitted(self):
     op = Mock()
     op.name = "DescribeInstances"
     op.metadata = {"protocol": "query"}
     op.has_streaming_output = False
     self.endpoint.make_request(op, request_dict())
     call_args = self.event_emitter.emit.call_args
     self.assertEqual(call_args[0][0], "needs-retry.ec2.DescribeInstances")
Example #5
0
    def test_metadata_from_package(self):
        """Testing ExtensionInfo metadata from package"""
        entrypoint = Mock()
        entrypoint.dist = Mock()

        test_author = "Test author lorem ipsum"
        test_description = "Test description lorem ipsum"
        test_email = "Test author@email.com"
        test_home_page = "http://www.example.com"
        test_license = "Test License MIT GPL Apache Drivers"
        test_module_name = "testextension.dummy.dummy"
        test_extension_id = "%s:DummyExtension" % test_module_name
        test_module_to_app = "testextension.dummy"
        test_project_name = "TestProjectName"
        test_summary = "Test summary lorem ipsum"
        test_version = "1.0"

        test_htdocs_path = os.path.join(settings.MEDIA_ROOT, "ext", test_project_name)
        test_static_path = os.path.join(settings.STATIC_ROOT, "ext", test_extension_id)

        test_metadata = {
            "Name": test_project_name,
            "Version": test_version,
            "Summary": test_summary,
            "Description": test_description,
            "Author": test_author,
            "Author-email": test_email,
            "License": test_license,
            "Home-page": test_home_page,
        }

        entrypoint.dist.get_metadata_lines = Mock(
            return_value=["%s: %s" % (key, value) for key, value in six.iteritems(test_metadata)]
        )

        entrypoint.dist.project_name = test_project_name
        entrypoint.dist.version = test_version

        ext_class = Mock()
        ext_class.__module__ = test_module_name
        ext_class.id = test_extension_id
        ext_class.metadata = None
        extension_info = ExtensionInfo(entrypoint, ext_class)

        self.assertEqual(extension_info.app_name, test_module_to_app)
        self.assertEqual(extension_info.author, test_author)
        self.assertEqual(extension_info.author_email, test_email)
        self.assertEqual(extension_info.description, test_description)
        self.assertFalse(extension_info.enabled)
        self.assertEqual(extension_info.installed_htdocs_path, test_htdocs_path)
        self.assertEqual(extension_info.installed_static_path, test_static_path)
        self.assertFalse(extension_info.installed)
        self.assertEqual(extension_info.license, test_license)
        self.assertEqual(extension_info.metadata, test_metadata)
        self.assertEqual(extension_info.name, test_project_name)
        self.assertEqual(extension_info.summary, test_summary)
        self.assertEqual(extension_info.url, test_home_page)
        self.assertEqual(extension_info.version, test_version)
    def test_statement_keyspace(self):
        hosts = [Host(str(i), SimpleConvictionPolicy) for i in range(4)]
        for host in hosts:
            host.set_up()

        cluster = Mock(spec=Cluster)
        cluster.metadata = Mock(spec=Metadata)
        replicas = hosts[2:]
        cluster.metadata.get_replicas.return_value = replicas

        child_policy = Mock()
        child_policy.make_query_plan.return_value = hosts
        child_policy.distance.return_value = HostDistance.LOCAL

        policy = TokenAwarePolicy(child_policy)
        policy.populate(cluster, hosts)

        # no keyspace, child policy is called
        keyspace = None
        routing_key = "routing_key"
        query = Statement(routing_key=routing_key)
        qplan = list(policy.make_query_plan(keyspace, query))
        self.assertEqual(hosts, qplan)
        self.assertEqual(cluster.metadata.get_replicas.call_count, 0)
        child_policy.make_query_plan.assert_called_once_with(keyspace, query)

        # working keyspace, no statement
        cluster.metadata.get_replicas.reset_mock()
        keyspace = "working_keyspace"
        routing_key = "routing_key"
        query = Statement(routing_key=routing_key)
        qplan = list(policy.make_query_plan(keyspace, query))
        self.assertEqual(replicas + hosts[:2], qplan)
        cluster.metadata.get_replicas.assert_called_with(keyspace, routing_key)

        # statement keyspace, no working
        cluster.metadata.get_replicas.reset_mock()
        working_keyspace = None
        statement_keyspace = "statement_keyspace"
        routing_key = "routing_key"
        query = Statement(routing_key=routing_key, keyspace=statement_keyspace)
        qplan = list(policy.make_query_plan(working_keyspace, query))
        self.assertEqual(replicas + hosts[:2], qplan)
        cluster.metadata.get_replicas.assert_called_with(statement_keyspace, routing_key)

        # both keyspaces set, statement keyspace used for routing
        cluster.metadata.get_replicas.reset_mock()
        working_keyspace = "working_keyspace"
        statement_keyspace = "statement_keyspace"
        routing_key = "routing_key"
        query = Statement(routing_key=routing_key, keyspace=statement_keyspace)
        qplan = list(policy.make_query_plan(working_keyspace, query))
        self.assertEqual(replicas + hosts[:2], qplan)
        cluster.metadata.get_replicas.assert_called_with(statement_keyspace, routing_key)
Example #7
0
 def test_retry_attempts_added_to_response_metadata(self):
     op = Mock(name="DescribeInstances")
     op.metadata = {"protocol": "query"}
     self.event_emitter.emit.side_effect = [
         [(None, None)],  # Request created.
         [(None, 0)],  # Check if retry needed. Retry needed.
         [(None, None)],  # Request created.
         [(None, None)],  # Check if retry needed. Retry not needed.
     ]
     parser = Mock()
     parser.parse.return_value = {"ResponseMetadata": {}}
     self.factory.return_value.create_parser.return_value = parser
     response = self.endpoint.make_request(op, request_dict())
     self.assertEqual(response[1]["ResponseMetadata"]["RetryAttempts"], 1)
Example #8
0
 def test_reset_stream_on_retry(self):
     op = Mock()
     body = RecordStreamResets("foobar")
     op.name = "PutObject"
     op.has_streaming_output = True
     op.metadata = {"protocol": "rest-xml"}
     request = request_dict()
     request["body"] = body
     self.event_emitter.emit.side_effect = [
         [(None, None)],  # Request created.
         [(None, 0)],  # Check if retry needed. Needs Retry.
         [(None, None)],  # Request created.
         [(None, 0)],  # Check if retry needed again. Needs Retry.
         [(None, None)],  # Request created.
         [(None, None)],  # Finally emit no rety is needed.
     ]
     self.endpoint.make_request(op, request)
     self.assertEqual(body.total_resets, 2)
Example #9
0
 def test_retry_events_can_alter_behavior(self):
     op = Mock()
     op.name = "DescribeInstances"
     op.metadata = {"protocol": "json"}
     self.event_emitter.emit.side_effect = [
         [(None, None)],  # Request created.
         [(None, 0)],  # Check if retry needed. Retry needed.
         [(None, None)],  # Request created.
         [(None, None)],  # Check if retry needed. Retry not needed.
     ]
     self.endpoint.make_request(op, request_dict())
     call_args = self.event_emitter.emit.call_args_list
     self.assertEqual(self.event_emitter.emit.call_count, 4)
     # Check that all of the events are as expected.
     self.assertEqual(call_args[0][0][0], "request-created.ec2.DescribeInstances")
     self.assertEqual(call_args[1][0][0], "needs-retry.ec2.DescribeInstances")
     self.assertEqual(call_args[2][0][0], "request-created.ec2.DescribeInstances")
     self.assertEqual(call_args[3][0][0], "needs-retry.ec2.DescribeInstances")
    def test_wrap_dc_aware(self):
        cluster = Mock(spec=Cluster)
        cluster.metadata = Mock(spec=Metadata)
        hosts = [Host(str(i), SimpleConvictionPolicy) for i in range(4)]
        for host in hosts:
            host.set_up()
        for h in hosts[:2]:
            h.set_location_info("dc1", "rack1")
        for h in hosts[2:]:
            h.set_location_info("dc2", "rack1")

        def get_replicas(keyspace, packed_key):
            index = struct.unpack(">i", packed_key)[0]
            # return one node from each DC
            if index % 2 == 0:
                return [hosts[0], hosts[2]]
            else:
                return [hosts[1], hosts[3]]

        cluster.metadata.get_replicas.side_effect = get_replicas

        policy = TokenAwarePolicy(DCAwareRoundRobinPolicy("dc1", used_hosts_per_remote_dc=1))
        policy.populate(cluster, hosts)

        for i in range(4):
            query = Statement(routing_key=struct.pack(">i", i), keyspace="keyspace_name")
            qplan = list(policy.make_query_plan(None, query))
            replicas = get_replicas(None, struct.pack(">i", i))

            # first should be the only local replica
            self.assertIn(qplan[0], replicas)
            self.assertEqual(qplan[0].datacenter, "dc1")

            # then the local non-replica
            self.assertNotIn(qplan[1], replicas)
            self.assertEqual(qplan[1].datacenter, "dc1")

            # then one of the remotes (used_hosts_per_remote_dc is 1, so we
            # shouldn't see two remotes)
            self.assertEqual(qplan[2].datacenter, "dc2")
            self.assertEqual(3, len(qplan))
Example #11
0
    def test_custom_metadata(self):
        """Testing ExtensionInfo metadata from Extension.metadata"""
        app_name = "test_extension.dummy"
        project_name = "DummyExtension"
        module_name = "test_extension.dummy.submodule"
        extension_id = "%s:DummyExtension" % module_name
        htdocs_path = os.path.join(settings.MEDIA_ROOT, "ext", project_name)
        metadata = {
            "Name": "OverrideName",
            "Version": "3.14159",
            "Summary": "Lorem ipsum dolor sit amet.",
            "Description": "Tempus fugit.",
            "Author": "Somebody",
            "Author-email": "somebody@example.com",
            "License": "None",
            "Home-page": "http://127.0.0.1/",
        }

        ext_class = Mock()
        ext_class.__module__ = module_name
        ext_class.metadata = metadata
        ext_class.id = extension_id

        entry_point = FakeEntryPoint(ext_class, project_name=project_name)

        extension_info = ExtensionInfo(entry_point, ext_class)

        self.assertEqual(extension_info.app_name, app_name)
        self.assertEqual(extension_info.author, metadata["Author"])
        self.assertEqual(extension_info.author_email, metadata["Author-email"])
        self.assertEqual(extension_info.description, metadata["Description"])
        self.assertFalse(extension_info.enabled)
        self.assertEqual(extension_info.installed_htdocs_path, htdocs_path)
        self.assertFalse(extension_info.installed)
        self.assertEqual(extension_info.license, metadata["License"])
        self.assertEqual(extension_info.metadata, metadata)
        self.assertEqual(extension_info.name, metadata["Name"])
        self.assertEqual(extension_info.summary, metadata["Summary"])
        self.assertEqual(extension_info.url, metadata["Home-page"])
        self.assertEqual(extension_info.version, metadata["Version"])
Example #12
0
    def test_wrap_round_robin(self):
        cluster = Mock(spec=Cluster)
        cluster.metadata = Mock(spec=Metadata)
        hosts = [Host(str(i), SimpleConvictionPolicy) for i in range(4)]

        def get_replicas(packed_key):
            index = struct.unpack(">i", packed_key)[0]
            return list(islice(cycle(hosts), index, index + 2))

        cluster.metadata.get_replicas.side_effect = get_replicas

        policy = TokenAwarePolicy(RoundRobinPolicy())
        policy.populate(cluster, hosts)

        for i in range(4):
            query = Query(routing_key=struct.pack(">i", i))
            qplan = list(policy.make_query_plan(query))

            replicas = get_replicas(struct.pack(">i", i))
            other = set(h for h in hosts if h not in replicas)
            self.assertEquals(replicas, qplan[:2])
            self.assertEquals(other, set(qplan[2:]))