def test_not_found(self):
        """The desired resource type isn't in the graph."""

        # The resource graph nodes in this test. Each entry is (resource_type,
        # native_id, native_name, attributes). We don't create edges for these
        # nodes, because the code paths being tested don't need the edges in
        # the resource graph.
        NODES = [
            (Host, "1234", "host 0", {
                "quality": "poor"
            }),
            (Host, "12345", "host 1", {
                "quality": "good"
            }),
            (Host, "123456", "host 2", {
                "quality": "poor"
            }),
        ]

        # Create the nodes for the test.
        for nodetype, native_id, native_name, attributes in NODES:
            if nodetype == Host:
                db_node = nodetype.objects.create(native_id=native_id,
                                                  native_name=native_name,
                                                  fqdn=native_name + ".com")
            else:
                db_node = nodetype.objects.create(native_id=native_id,
                                                  native_name=native_name)

            resource.instances.graph.add_node(
                GraphNode(uuid=db_node.uuid,
                          resourcetype=nodetype,
                          attributes=attributes))

        # Create a user, do the test.
        token = create_and_login()
        response = self.client.get(
            RESTYPE_DETAIL_URL % "<class 'goldstone.core.models.User'>",
            HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token)

        # Test the result.
        self.assertContains(response,
                            '{"nodes":[]}',
                            status_code=HTTP_404_NOT_FOUND)
    def test_mix(self):
        """The resource type graph is populated with a mixture of types, some
        of which are not present in the resource graph."""

        # The resource graph nodes in this test. Each entry is (resource_type,
        # native_id, native_name, attributes). We don't create edges for these
        # nodes, because the code paths being tested don't need the edges in
        # the resource graph.
        NODES = [
            (Host, "1234", "host 0", {
                "quality": "poor"
            }),
            (Host, "12345", "host 1", {
                "quality": "good"
            }),
            (Host, "123456", "host 2", {
                "quality": "poor"
            }),
            (AvailabilityZone, "a1", "availabilityzone 0", {
                "quality": "poor"
            }),
            (AvailabilityZone, "a2", "availabilityzone 1", {
                "quality": "good"
            }),
            (AvailabilityZone, "a3", "availabiltiyzone 2", {
                "quality": "poor"
            }),
            (Hypervisor, "f234", "hypervisor 0", {
                "quality": "poor"
            }),
            (Hypervisor, "f2345", "hypervisor 0", {
                "quality": "poor"
            }),
            (Hypervisor, "f23456", "hypervisor 0", {
                "quality": "good"
            }),
            (Aggregate, "dead1", "aggregate 0", {
                "quality": "poor"
            }),
            (Aggregate, "dead2", "aggregate 1", {
                "quality": "good"
            }),
            (Server, "beef1", "server 1", {
                "quality": "good"
            }),
            (Server, "beef2", "server 2", {
                "quality": "good"
            }),
            (Project, "p0", "project 0", {
                "quality": "poor"
            }),
            (Project, "p1", "project 1", {
                "quality": "poor"
            }),
            (Project, "p2", "project 2", {
                "quality": "good"
            }),
            (NeutronNetwork, "n1234", "network 0", {
                "quality": "good"
            }),
            (NeutronNetwork, "n12345", "network 1", {
                "quality": "good"
            }),
            (Limits, "l1234", "limits 0", {
                "quality": "good"
            }),
            (Limits, "l12345", "limits 1", {
                "quality": "good"
            }),
            (Limits, "l123456", "limits 2", {
                "quality": "good"
            }),
        ]

        # Expected test results.
        EXPECTED = [{
            u'label': u'hosts',
            u'resourcetype': u'hosts',
            u'integration': u'nova',
            u'present': True,
            u'unique_id': u"<class 'goldstone.core.models.Host'>"
        }, {
            u'label': None,
            u'resourcetype': u'regions',
            u'integration': u'keystone',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Region'>"
        }, {
            u'label': u'groups',
            u'resourcetype': u'groups',
            u'integration': u'keystone',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Group'>"
        }, {
            u'label': u'flavors',
            u'resourcetype': u'flavors',
            u'integration': u'nova',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Flavor'>"
        }, {
            u'label':
            u'floating ip addresses',
            u'resourcetype':
            u'floating ip addresses',
            u'integration':
            u'neutron',
            u'present':
            False,
            u'unique_id':
            u"<class 'goldstone.core.models.NeutronFloatingIP'>"
        }, {
            u'label': u'hypervisors',
            u'resourcetype': u'hypervisors',
            u'integration': u'nova',
            u'present': True,
            u'unique_id': u"<class 'goldstone.core.models.Hypervisor'>"
        }, {
            u'label': u'servers',
            u'resourcetype': u'servers',
            u'integration': u'nova',
            u'present': True,
            u'unique_id': u"<class 'goldstone.core.models.Server'>"
        }, {
            u'label': u'projects',
            u'resourcetype': u'projects',
            u'integration': u'keystone',
            u'present': True,
            u'unique_id': u"<class 'goldstone.core.models.Project'>"
        }, {
            u'label':
            u'routers',
            u'resourcetype':
            u'routers',
            u'integration':
            u'neutron',
            u'present':
            False,
            u'unique_id':
            u"<class 'goldstone.core.models.NeutronRouter'>"
        }, {
            u'label': u'users',
            u'resourcetype': u'users',
            u'integration': u'keystone',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.User'>"
        }, {
            u'label': u'limits',
            u'resourcetype': u'limits',
            u'integration': u'nova',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.NovaLimits'>"
        }, {
            u'label':
            u'ports',
            u'resourcetype':
            u'ports',
            u'integration':
            u'neutron',
            u'present':
            False,
            u'unique_id':
            u"<class 'goldstone.core.models.NeutronPort'>"
        }, {
            u'label': u'volume types',
            u'resourcetype': u'volume types',
            u'integration': u'cinder',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.VolumeType'>"
        }, {
            u'label':
            u'networks',
            u'resourcetype':
            u'networks',
            u'integration':
            u'neutron',
            u'present':
            True,
            u'unique_id':
            u"<class 'goldstone.core.models.NeutronNetwork'>"
        }, {
            u'label': u'limits',
            u'resourcetype': u'limits',
            u'integration': u'cinder',
            u'present': True,
            u'unique_id': u"<class 'goldstone.core.models.Limits'>"
        }, {
            u'label': u'images',
            u'resourcetype': u'images',
            u'integration': u'glance',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Image'>"
        }, {
            u'label':
            u'availability zones',
            u'resourcetype':
            u'availability zones',
            u'integration':
            u'nova',
            u'present':
            True,
            u'unique_id':
            u"<class 'goldstone.core.models.AvailabilityZone'>"
        }, {
            u'label':
            u'subnets',
            u'resourcetype':
            u'subnets',
            u'integration':
            u'neutron',
            u'present':
            False,
            u'unique_id':
            u"<class 'goldstone.core.models.NeutronSubnet'>"
        }, {
            u'label': u'volumes',
            u'resourcetype': u'volumes',
            u'integration': u'cinder',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Volume'>"
        }, {
            u'label': u'domains',
            u'resourcetype': u'domains',
            u'integration': u'keystone',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Domain'>"
        }, {
            u'label': u'tokens',
            u'resourcetype': u'tokens',
            u'integration': u'keystone',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Token'>"
        }, {
            u'label': u'keypairs',
            u'resourcetype': u'keypairs',
            u'integration': u'nova',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Keypair'>"
        }, {
            u'label':
            u'server groups',
            u'resourcetype':
            u'server groups',
            u'integration':
            u'nova',
            u'present':
            False,
            u'unique_id':
            u"<class 'goldstone.core.models.ServerGroup'>"
        }, {
            u'label': u'roles',
            u'resourcetype': u'roles',
            u'integration': u'keystone',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Role'>"
        }, {
            u'label': u'quota sets',
            u'resourcetype': u'quota sets',
            u'integration': u'cinder',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.QuotaSet'>"
        }, {
            u'label': u'services',
            u'resourcetype': u'services',
            u'integration': u'keystone',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Service'>"
        }, {
            u'label': u'snapshots',
            u'resourcetype': u'snapshots',
            u'integration': u'cinder',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Snapshot'>"
        }, {
            u'label': u'aggregates',
            u'resourcetype': u'aggregates',
            u'integration': u'nova',
            u'present': True,
            u'unique_id': u"<class 'goldstone.core.models.Aggregate'>"
        }, {
            u'label': u'cloudpipes',
            u'resourcetype': u'cloudpipes',
            u'integration': u'nova',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Cloudpipe'>"
        }, {
            u'label':
            u'quotas',
            u'resourcetype':
            u'quotas',
            u'integration':
            u'neutron',
            u'present':
            False,
            u'unique_id':
            u"<class 'goldstone.core.models.NeutronQuota'>"
        }, {
            u'label': u'credentials',
            u'resourcetype': u'credentials',
            u'integration': u'keystone',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Credential'>"
        }, {
            u'label': u'endpoints',
            u'resourcetype': u'endpoints',
            u'integration': u'keystone',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Endpoint'>"
        }, {
            u'label': u'qos specs',
            u'resourcetype': u'qos specs',
            u'integration': u'cinder',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.QOSSpec'>"
        }, {
            u'label': u'interfaces',
            u'resourcetype': u'interfaces',
            u'integration': u'nova',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Interface'>"
        }, {
            u'label': u'transfers',
            u'resourcetype': u'transfers',
            u'integration': u'cinder',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Transfer'>"
        }, {
            u'label': u'add-ons',
            u'resourcetype': u'add-ons',
            u'integration': u'add-on',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Addon'>"
        }, {
            u'label': u'cinder',
            u'resourcetype': u'cinder',
            u'integration': u'cinder',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Cinder'>"
        }, {
            u'label': u'glance',
            u'resourcetype': u'glance',
            u'integration': u'glance',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Glance'>"
        }, {
            u'label': u'keystone',
            u'resourcetype': u'keystone',
            u'integration': u'keystone',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Keystone'>"
        }, {
            u'label': u'nova',
            u'resourcetype': u'nova',
            u'integration': u'nova',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Nova'>"
        }, {
            u'label': u'neutron',
            u'resourcetype': u'neutron',
            u'integration': u'neutron',
            u'present': False,
            u'unique_id': u"<class 'goldstone.core.models.Neutron'>"
        }, {
            u'resourcetype': u'agents',
            u'unique_id': u"<class 'goldstone.core.models.NeutronAgent'>",
            u'integration': u'neutron',
            u'present': False,
            u'label': u'agents'
        }, {
            u'resourcetype': u'extensions',
            u'unique_id': u"<class 'goldstone.core.models.NeutronExtension'>",
            u'integration': u'neutron',
            u'present': False,
            u'label': u'extensions'
        }, {
            u'resourcetype': u'subnet pools',
            u'unique_id': u"<class 'goldstone.core.models.NeutronSubnetPool'>",
            u'integration': u'neutron',
            u'present': False,
            u'label': u'subnet pools'
        }, {
            u'resourcetype': u'security groups',
            u'unique_id': u"<class 'goldstone.core.models."
            u"NeutronSecurityGroup'>",
            u'integration': u'neutron',
            u'present': False,
            u'label': u'security groups'
        }, {
            u'resourcetype': u'security group rules',
            u'unique_id': u"<class 'goldstone.core.models."
            u"NeutronSecurityGroupRule'>",
            u'integration': u'neutron',
            u'present': False,
            u'label': u'security group rules'
        }]

        # This code is for ticket #105611698. Coming into this test, the
        # resource types graph should be in a known state, having been
        # initialized by goldstone.test_utils.Setup.setUp(). But sometimes it
        # isn't. This code block checks for a known initial state and asserts a
        # test failure if it's not there.
        bad = False

        for entry in resource.types.graph.nodes():
            try:
                entry().label()
                entry.integration()
            except Exception as exc:  # pylint: disable=W0703
                print "? %s doesn't have label or integration, exception: %s" \
                    % \
                    (entry, exc.message)
                bad = True

        self.assertFalse(bad, msg="initial test condition is bad")

        # End of ticket #105611698 code.

        # Create the nodes for the test.
        for nodetype, native_id, native_name, attributes in NODES:
            if nodetype == Host:
                db_node = nodetype.objects.create(native_id=native_id,
                                                  native_name=native_name,
                                                  fqdn=native_name + ".com")
            else:
                db_node = nodetype.objects.create(native_id=native_id,
                                                  native_name=native_name)

            resource.instances.graph.add_node(
                GraphNode(uuid=db_node.uuid,
                          resourcetype=nodetype,
                          attributes=attributes))

        # Create a user, do the test.
        token = create_and_login()
        response = self.client.get(RESTYPE_URL,
                                   HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD %
                                   token)

        # Test the result.
        # pylint: disable=E1101
        self.assertEqual(response.status_code, HTTP_200_OK)

        self.maxDiff = None
        content = json.loads(response.content)["nodes"]
        self.assertItemsEqual(content, EXPECTED)
    def test_mix(self):
        """The desired resource type is in the graph."""

        # The resource graph nodes in this test. Each entry is (resource_type,
        # native_id, native_name, attributes). We don't create edges for these
        # nodes, because the code paths being tested don't need the edges in
        # the resource graph.
        NODES = [
            (Host, "1234", "host 0", {
                "quality": "poor"
            }),
            (Host, "12345", "host 1", {
                "quality": "good"
            }),
            (Host, "123456", "host 2", {
                "quality": "poor"
            }),
            (AvailabilityZone, "a1", "availabilityzone 0", {
                "quality": "poor"
            }),
            (AvailabilityZone, "a2", "availabilityzone 1", {
                "quality": "good"
            }),
            (AvailabilityZone, "a3", "availabiltiyzone 2", {
                "quality": "poor"
            }),
            (Hypervisor, "f234", "hypervisor 0", {
                "quality": "poor"
            }),
            (Hypervisor, "f2345", "hypervisor 0", {
                "quality": "poor"
            }),
            (Hypervisor, "f23456", "hypervisor 0", {
                "quality": "good"
            }),
            (Aggregate, "dead1", "aggregate 0", {
                "quality": "poor"
            }),
            (Aggregate, "dead2", "aggregate 1", {
                "quality": "good"
            }),
            (Server, "beef1", "server 1", {
                "quality": "good"
            }),
            (Server, "beef2", "server 2", {
                "quality": "good"
            }),
            (Project, "p0", "project 0", {
                "quality": "poor"
            }),
            (Project, "p1", "project 1", {
                "quality": "poor"
            }),
            (Project, "p2", "project 2", {
                "quality": "good"
            }),
            (NeutronNetwork, "n1234", "network 0", {
                "quality": "good"
            }),
            (NeutronNetwork, "n12345", "network 1", {
                "quality": "good"
            }),
            (Limits, "l1234", "limits 0", {
                "quality": "good"
            }),
            (Limits, "l12345", "limits 1", {
                "quality": "good"
            }),
            (Limits, "l123456", "limits 2", {
                "quality": "good"
            }),
        ]

        # Expected test results, without the uuids.
        EXPECTED = [
            {
                u'attributes': {
                    u'quality': u'good'
                },
                u'native_id': u'p2',
                u'native_name': u'project 2'
            },
            {
                u'attributes': {
                    u'quality': u'poor'
                },
                u'native_id': u'p0',
                u'native_name': u'project 0'
            },
            {
                u'attributes': {
                    u'quality': u'poor'
                },
                u'native_id': u'p1',
                u'native_name': u'project 1'
            },
        ]

        # Create the nodes for the test.
        for nodetype, native_id, native_name, attributes in NODES:
            if nodetype == Host:
                db_node = nodetype.objects.create(native_id=native_id,
                                                  native_name=native_name,
                                                  fqdn=native_name + ".com")
            else:
                db_node = nodetype.objects.create(native_id=native_id,
                                                  native_name=native_name)

            resource.instances.graph.add_node(
                GraphNode(uuid=db_node.uuid,
                          resourcetype=nodetype,
                          attributes=attributes))

        # Create a user, do the test.
        token = create_and_login()
        response = self.client.get(
            RESTYPE_DETAIL_URL % "<class 'goldstone.core.models.Project'>",
            HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token)

        # Test the result.
        # pylint: disable=E1101
        self.assertEqual(response.status_code, HTTP_200_OK)

        content = json.loads(response.content)["nodes"]
        for entry in content:
            del entry["uuid"]
        self.assertItemsEqual(content, EXPECTED)
Esempio n. 4
0
    def test_mix(self):
        """The desired resource is in the graph."""

        # The resource graph nodes in this test. Each entry is (resource_type,
        # native_id, native_name, attributes). We don't create edges for these
        # nodes, because the code paths being tested don't need the edges in
        # the resource graph.
        NODES = [
            (Host, "1234", "host 0", {
                "quality": "poor"
            }),
            (Host, "12345", "host 1", {
                "quality": "good"
            }),
            (Host, "123456", "host 2", {
                "quality": "poor"
            }),
            (AvailabilityZone, "a1", "availabilityzone 0", {
                "quality": "poor"
            }),
            (AvailabilityZone, "a2", "availabilityzone 1", {
                "quality": "good"
            }),
            (AvailabilityZone, "a3", "availabiltiyzone 2", {
                "quality": "poor"
            }),
            (Hypervisor, "f234", "hypervisor 0", {
                "quality": "poor"
            }),
            (Hypervisor, "f2345", "hypervisor 0", {
                "quality": "poor"
            }),
            (Hypervisor, "f23456", "hypervisor 0", {
                "quality": "good"
            }),
            (Aggregate, "dead1", "aggregate 0", {
                "quality": "poor"
            }),
            (Aggregate, "dead2", "aggregate 1", {
                "quality": "good"
            }),
            (Server, "beef1", "server 1", {
                "quality": "good"
            }),
            (Server, "beef2", "server 2", {
                "quality": "good"
            }),
            (Project, "p0", "project 0", {
                "quality": "poor"
            }),
            (Project, "p1", "project 1", {
                "quality": "poor"
            }),
            (Project, "p2", "project 2", {
                "quality": "good"
            }),
            (Network, "n1234", "network 0", {
                "quality": "good"
            }),
            (Network, "n12345", "network 1", {
                "quality": "good"
            }),
            (Limits, "l1234", "limits 0", {
                "quality": "good"
            }),
            (Limits, "l12345", "limits 1", {
                "quality": "good"
            }),
            (Limits, "l123456", "limits 2", {
                "quality": "good"
            }),
        ]

        # Create the nodes for the test.
        for nodetype, native_id, native_name, attributes in NODES:
            if nodetype == Host:
                db_node = nodetype.objects.create(native_id=native_id,
                                                  native_name=native_name,
                                                  fqdn=native_name + ".com")
            else:
                db_node = nodetype.objects.create(native_id=native_id,
                                                  native_name=native_name)

            resource.instances.graph.add_node(
                GraphNode(uuid=db_node.uuid,
                          resourcetype=nodetype,
                          attributes=attributes))

        # Create a user.
        token = create_and_login()

        # Get the UUID of one of the nodes we just made, and calculate the
        # expected result. We deliberately do this crudely.
        response = self.client.get(RES_URL,
                                   HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD %
                                   token)

        # pylint: disable=E1101
        uuid = json.loads(response.content)["nodes"][1]["uuid"]

        for node in resource.instances.graph.nodes():
            if node.uuid == uuid:
                break
        else:
            node = None  # Should never happen.

        row = PolyResource.objects.get(uuid=uuid)  # Should always succeed.

        expected = {
            "native_id": row.native_id,
            "native_name": row.native_name,
            "attributes": node.attributes
        }

        # Now we can do the test.
        response = self.client.get(RES_DETAIL_URL % uuid,
                                   HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD %
                                   token)

        self.assertEqual(response.status_code, HTTP_200_OK)
        self.assertEqual(json.loads(response.content), expected)
Esempio n. 5
0
    def test_mix(self):
        """The resource graph is populated with a mixture of nodes."""

        # pylint: disable=R0914

        # The resource graph nodes in this test. Each entry is (resource_type,
        # native_id, native_name, attributes).
        NODES = [
            (Host, "1234", "host 0", {
                "quality": "poor"
            }),
            (Host, "12345", "host 1", {
                "quality": "good"
            }),
            (Host, "123456", "host 2", {
                "quality": "poor"
            }),
            (AvailabilityZone, "a1", "availabilityzone 0", {
                "quality": "poor"
            }),
            (AvailabilityZone, "a2", "availabilityzone 1", {
                "quality": "good"
            }),
            (AvailabilityZone, "a3", "availabiltiyzone 2", {
                "quality": "poor"
            }),
            (Hypervisor, "f234", "hypervisor 0", {
                "quality": "poor"
            }),
            (Hypervisor, "f2345", "hypervisor 0", {
                "quality": "poor"
            }),
            (Hypervisor, "f23456", "hypervisor 0", {
                "quality": "good"
            }),
            (Aggregate, "dead1", "aggregate 0", {
                "quality": "poor"
            }),
            (Aggregate, "dead2", "aggregate 1", {
                "quality": "good"
            }),
            (Server, "beef1", "server 1", {
                "quality": "good"
            }),
            (Server, "beef2", "server 2", {
                "quality": "good"
            }),
            (Project, "p0", "project 0", {
                "quality": "poor"
            }),
            (Project, "p1", "project 1", {
                "quality": "poor"
            }),
            (Project, "p2", "project 2", {
                "quality": "good"
            }),
            (Network, "n1234", "network 0", {
                "quality": "good"
            }),
            (Network, "n12345", "network 1", {
                "quality": "good"
            }),
            (Limits, "l1234", "limits 0", {
                "quality": "good"
            }),
            (Limits, "l12345", "limits 1", {
                "quality": "good"
            }),
            (Limits, "l123456", "limits 2", {
                "quality": "good"
            }),
        ]

        # The resource graph edges in this test. Each entry is (from, to,
        # attributes). The edge attributes here are meaningless.
        EDGES = [
            # Hosts
            ("1234", "dead1", {
                TYPE: ALLOCATED_TO
            }),
            ("1234", "f2345", {
                TYPE: APPLIES_TO
            }),
            ("1234", "f23456", {
                TYPE: ASSIGNED_TO
            }),
            # Availablity Zones
            ("a3", "dead2", {
                TYPE: CONSUMES
            }),
            ("a2", "123456", {
                TYPE: CONTAINS
            }),
            # Hypervisors
            ("f23456", "beef2", {
                TYPE: DEFINES
            }),
            # Projects
            ("p1", "n1234", {
                TYPE: INSTANCE_OF
            }),
            ("p1", "n12345", {
                TYPE: MANAGES
            }),
            ("p2", "n1234", {
                TYPE: MEMBER_OF
            }),
            ("p2", "n12345", {
                TYPE: OWNS
            }),
            ("p0", "l1234", {
                TYPE: ROUTES_TO
            }),
            ("p0", "l12345", {
                TYPE: SUBSCRIBED_TO
            }),
            ("p0", "l123456", {
                TYPE: USES
            }),
            ("p2", "l1234", {
                TYPE: ALLOCATED_TO
            }),
            ("p2", "l12345", {
                TYPE: APPLIES_TO
            }),
            ("p2", "l123456", {
                TYPE: ASSIGNED_TO
            }),
        ]

        # Expected node results, sans UUIDs.
        EXPECTED_NODES = [
            {
                u'native_id': u'p2',
                u'native_name': u'project 2',
                u'resourcetype': {
                    u'label': u'projects',
                    u'resourcetype': u'projects',
                    u'unique_id': u"<class 'goldstone.core.models.Project'>"
                },
            },
            {
                u'native_id': u'beef2',
                u'native_name': u'server 2',
                u'resourcetype': {
                    u'label': u'servers',
                    u'resourcetype': u'servers',
                    u'unique_id': u"<class 'goldstone.core.models.Server'>"
                },
            },
            {
                u'native_id': u'beef1',
                u'native_name': u'server 1',
                u'resourcetype': {
                    u'label': u'servers',
                    u'resourcetype': u'servers',
                    u'unique_id': u"<class 'goldstone.core.models.Server'>"
                },
            },
            {
                u'native_id': u'a2',
                u'native_name': u'availabilityzone 1',
                u'resourcetype': {
                    u'label':
                    u'availability zones',
                    u'resourcetype':
                    u'availability zones',
                    u'unique_id':
                    u"<class 'goldstone.core.models.AvailabilityZone'>"
                },
            },
            {
                u'native_id': u'12345',
                u'native_name': u'host 1',
                u'resourcetype': {
                    u'label': u'hosts',
                    u'resourcetype': u'hosts',
                    u'unique_id': u"<class 'goldstone.core.models.Host'>"
                },
            },
            {
                u'native_id': u'p1',
                u'native_name': u'project 1',
                u'resourcetype': {
                    u'label': u'projects',
                    u'resourcetype': u'projects',
                    u'unique_id': u"<class 'goldstone.core.models.Project'>"
                },
            },
            {
                u'native_id': u'1234',
                u'native_name': u'host 0',
                u'resourcetype': {
                    u'label': u'hosts',
                    u'resourcetype': u'hosts',
                    u'unique_id': u"<class 'goldstone.core.models.Host'>"
                },
            },
            {
                u'native_id': u'l123456',
                u'native_name': u'limits 2',
                u'resourcetype': {
                    u'label': u'limits',
                    u'resourcetype': u'limits',
                    u'unique_id': u"<class 'goldstone.core.models.Limits'>"
                },
            },
            {
                u'native_id': u'dead1',
                u'native_name': u'aggregate 0',
                u'resourcetype': {
                    u'label': u'aggregates',
                    u'resourcetype': u'aggregates',
                    u'unique_id': u"<class 'goldstone.core.models.Aggregate'>"
                },
            },
            {
                u'native_id': u'dead2',
                u'native_name': u'aggregate 1',
                u'resourcetype': {
                    u'label': u'aggregates',
                    u'resourcetype': u'aggregates',
                    u'unique_id': u"<class 'goldstone.core.models.Aggregate'>"
                },
            },
            {
                u'native_id': u'f23456',
                u'native_name': u'hypervisor 0',
                u'resourcetype': {
                    u'label': u'hypervisors',
                    u'resourcetype': u'hypervisors',
                    u'unique_id': u"<class 'goldstone.core.models.Hypervisor'>"
                },
            },
            {
                u'native_id': u'p0',
                u'native_name': u'project 0',
                u'resourcetype': {
                    u'label': u'projects',
                    u'resourcetype': u'projects',
                    u'unique_id': u"<class 'goldstone.core.models.Project'>"
                },
            },
            {
                u'native_id': u'f2345',
                u'native_name': u'hypervisor 0',
                u'resourcetype': {
                    u'label': u'hypervisors',
                    u'resourcetype': u'hypervisors',
                    u'unique_id': u"<class 'goldstone.core.models.Hypervisor'>"
                },
            },
            {
                u'native_id': u'n12345',
                u'native_name': u'network 1',
                u'resourcetype': {
                    u'label': u'networks',
                    u'resourcetype': u'networks',
                    u'unique_id': u"<class 'goldstone.core.models.Network'>"
                },
            },
            {
                u'native_id': u'a1',
                u'native_name': u'availabilityzone 0',
                u'resourcetype': {
                    u'label':
                    u'availability zones',
                    u'resourcetype':
                    u'availability zones',
                    u'unique_id':
                    u"<class 'goldstone.core.models.AvailabilityZone'>"
                },
            },
            {
                u'native_id': u'n1234',
                u'native_name': u'network 0',
                u'resourcetype': {
                    u'label': u'networks',
                    u'resourcetype': u'networks',
                    u'unique_id': u"<class 'goldstone.core.models.Network'>"
                },
            },
            {
                u'native_id': u'123456',
                u'native_name': u'host 2',
                u'resourcetype': {
                    u'label': u'hosts',
                    u'resourcetype': u'hosts',
                    u'unique_id': u"<class 'goldstone.core.models.Host'>"
                },
            },
            {
                u'native_id': u'l12345',
                u'native_name': u'limits 1',
                u'resourcetype': {
                    u'label': u'limits',
                    u'resourcetype': u'limits',
                    u'unique_id': u"<class 'goldstone.core.models.Limits'>"
                },
            },
            {
                u'native_id': u'l1234',
                u'native_name': u'limits 0',
                u'resourcetype': {
                    u'label': u'limits',
                    u'resourcetype': u'limits',
                    u'unique_id': u"<class 'goldstone.core.models.Limits'>"
                },
            },
            {
                u'native_id': u'f234',
                u'native_name': u'hypervisor 0',
                u'resourcetype': {
                    u'label': u'hypervisors',
                    u'resourcetype': u'hypervisors',
                    u'unique_id': u"<class 'goldstone.core.models.Hypervisor'>"
                },
            },
            {
                u'native_id': u'a3',
                u'native_name': u'availabiltiyzone 2',
                u'resourcetype': {
                    u'label':
                    u'availability zones',
                    u'resourcetype':
                    u'availability zones',
                    u'unique_id':
                    u"<class 'goldstone.core.models.AvailabilityZone'>"
                },
            },
        ]

        # Create the nodes for the test.
        for nodetype, native_id, native_name, attributes in NODES:
            if nodetype == Host:
                db_node = nodetype.objects.create(native_id=native_id,
                                                  native_name=native_name,
                                                  fqdn=native_name + ".com")
            else:
                db_node = nodetype.objects.create(native_id=native_id,
                                                  native_name=native_name)

            resource.instances.graph.add_node(
                GraphNode(uuid=db_node.uuid,
                          resourcetype=nodetype,
                          attributes=attributes))

        # Force the instance graph to be re-evaluated now.
        resource.instances._graph = None  # pylint: disable=W0212

        # Create the edges for the test.
        for source_id, destination_id, attr_dict in EDGES:
            # Locate the source and destination nodes in the resource graph.
            source_row = PolyResource.objects.get(native_id=source_id)
            destination_row = \
                PolyResource.objects.get(native_id=destination_id)

            source_node = [
                x for x in resource.instances.graph.nodes()
                if x.uuid == source_row.uuid
            ][0]
            destination_node = [
                x for x in resource.instances.graph.nodes()
                if x.uuid == destination_row.uuid
            ][0]

            resource.instances.graph.add_edge(source_node,
                                              destination_node,
                                              attr_dict=attr_dict)

        # Create a user, do the test.
        token = create_and_login()

        response = self.client.get(RES_URL,
                                   HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD %
                                   token)

        # Test the results.
        # pylint: disable=E1101
        self.assertEqual(response.status_code, HTTP_200_OK)

        # Test the result's edges.
        content = json.loads(response.content)
        self.assertEqual(len(EDGES), len(content["edges"]))

        edge_types_uses = [x[2][TYPE] for x in EDGES]
        edge_types_actual = [x[TYPE] for x in content["edges"]]

        # For every edge attribute dictionary...
        for entry in set(edge_types_uses):
            # The count of this type used in this test must be found in the
            # response. This isn't a complete verification, but is good enough.
            self.assertEqual(edge_types_uses.count(entry),
                             edge_types_actual.count(entry))

        # Test the result's nodes, sans UUIDs.
        for entry in content["nodes"]:
            del entry["uuid"]

        self.assertItemsEqual(content["nodes"], EXPECTED_NODES)
Esempio n. 6
0
    def test_mix(self):
        """The resource graph is populated with a mixture of nodes."""
        from goldstone.core import resource

        # The resource graph nodes in this test. Each entry is (resource_type,
        # native_id, native_name, attributes).
        NODES = [
            (Host, "1234", "host 0", {
                "quality": "poor"
            }),
            (Host, "12345", "host 1", {
                "quality": "good"
            }),
            (Host, "123456", "host 2", {
                "quality": "poor"
            }),
            (AvailabilityZone, "a1", "availabilityzone 0", {
                "quality": "poor"
            }),
            (AvailabilityZone, "a2", "availabilityzone 1", {
                "quality": "good"
            }),
            (AvailabilityZone, "a3", "availabiltiyzone 2", {
                "quality": "poor"
            }),
            (Hypervisor, "f234", "hypervisor 0", {
                "quality": "poor"
            }),
            (Hypervisor, "f2345", "hypervisor 0", {
                "quality": "poor"
            }),
            (Hypervisor, "f23456", "hypervisor 0", {
                "quality": "good"
            }),
            (Aggregate, "dead1", "aggregate 0", {
                "quality": "poor"
            }),
            (Aggregate, "dead2", "aggregate 1", {
                "quality": "good"
            }),
            (Server, "beef1", "server 1", {
                "quality": "good"
            }),
            (Server, "beef2", "server 2", {
                "quality": "good"
            }),
            (Project, "p0", "project 0", {
                "quality": "poor"
            }),
            (Project, "p1", "project 1", {
                "quality": "poor"
            }),
            (Project, "p2", "project 2", {
                "quality": "good"
            }),
            (NeutronNetwork, "n1234", "network 0", {
                "quality": "good"
            }),
            (NeutronNetwork, "n12345", "network 1", {
                "quality": "good"
            }),
            (Limits, "l1234", "limits 0", {
                "quality": "good"
            }),
            (Limits, "l12345", "limits 1", {
                "quality": "good"
            }),
            (Limits, "l123456", "limits 2", {
                "quality": "good"
            }),
        ]

        # The resource graph edges in this test. Each entry is (from, to,
        # attributes). The edge attributes here are meaningless.
        EDGES = [
            # Hosts
            ("1234", "dead1", {
                TYPE: ALLOCATED_TO
            }),
            ("1234", "f2345", {
                TYPE: APPLIES_TO
            }),
            ("1234", "f23456", {
                TYPE: ASSIGNED_TO
            }),
            # Availablity Zones
            ("a3", "dead2", {
                TYPE: CONSUMES
            }),
            ("a2", "123456", {
                TYPE: CONTAINS
            }),
            # Hypervisors
            ("f23456", "beef2", {
                TYPE: DEFINES
            }),
            # Projects
            ("p1", "n1234", {
                TYPE: INSTANCE_OF
            }),
            ("p1", "n12345", {
                TYPE: MANAGES
            }),
            ("p2", "n1234", {
                TYPE: MEMBER_OF
            }),
            ("p2", "n12345", {
                TYPE: OWNS
            }),
            ("p0", "l1234", {
                TYPE: ROUTES_TO
            }),
            ("p0", "l12345", {
                TYPE: SUBSCRIBED_TO
            }),
            ("p0", "l123456", {
                TYPE: USES
            }),
            ("p2", "l1234", {
                TYPE: ALLOCATED_TO
            }),
            ("p2", "l12345", {
                TYPE: APPLIES_TO
            }),
            ("p2", "l123456", {
                TYPE: ASSIGNED_TO
            }),
        ]

        # The query strings to try, and the expected results. Each entry is
        # query_string: (the expected node results, the expected number of
        # edges).
        QUERIES = {
            "native_name=%5eh": [[
                {
                    u'native_id': u'12345',
                    u'native_name': u'host 1',
                    u'resourcetype': {
                        u'label': u'hosts',
                        u'resourcetype': u'hosts',
                        u'unique_id': u"<class 'goldstone.core.models.Host'>"
                    },
                },
                {
                    u'native_id': u'1234',
                    u'native_name': u'host 0',
                    u'resourcetype': {
                        u'label': u'hosts',
                        u'resourcetype': u'hosts',
                        u'unique_id': u"<class 'goldstone.core.models.Host'>"
                    },
                },
                {
                    u'native_id': u'f23456',
                    u'native_name': u'hypervisor 0',
                    u'resourcetype': {
                        u'label': u'hypervisors',
                        u'resourcetype': u'hypervisors',
                        u'unique_id':
                        u"<class 'goldstone.core.models.Hypervisor'>"
                    },
                },
                {
                    u'native_id': u'f2345',
                    u'native_name': u'hypervisor 0',
                    u'resourcetype': {
                        u'label': u'hypervisors',
                        u'resourcetype': u'hypervisors',
                        u'unique_id':
                        u"<class 'goldstone.core.models.Hypervisor'>"
                    },
                },
                {
                    u'native_id': u'123456',
                    u'native_name': u'host 2',
                    u'resourcetype': {
                        u'label': u'hosts',
                        u'resourcetype': u'hosts',
                        u'unique_id': u"<class 'goldstone.core.models.Host'>"
                    },
                },
                {
                    u'native_id': u'f234',
                    u'native_name': u'hypervisor 0',
                    u'resourcetype': {
                        u'label': u'hypervisors',
                        u'resourcetype': u'hypervisors',
                        u'unique_id':
                        u"<class 'goldstone.core.models.Hypervisor'>"
                    },
                },
            ], 5],
            "native_name=imi": [[
                {
                    u'native_id': u'l123456',
                    u'native_name': u'limits 2',
                    u'resourcetype': {
                        u'label': u'limits',
                        u'resourcetype': u'limits',
                        u'unique_id': u"<class 'goldstone.core.models.Limits'>"
                    },
                },
                {
                    u'native_id': u'l12345',
                    u'native_name': u'limits 1',
                    u'resourcetype': {
                        u'label': u'limits',
                        u'resourcetype': u'limits',
                        u'unique_id': u"<class 'goldstone.core.models.Limits'>"
                    },
                },
                {
                    u'native_id': u'l1234',
                    u'native_name': u'limits 0',
                    u'resourcetype': {
                        u'label': u'limits',
                        u'resourcetype': u'limits',
                        u'unique_id': u"<class 'goldstone.core.models.Limits'>"
                    },
                },
            ], 6],
            "native_id=derosa": [[], 0],
            "integration_name=neutron%20OR%20keystone": [[
                {
                    u'native_id': u'p2',
                    u'native_name': u'project 2',
                    u'resourcetype': {
                        u'label': u'projects',
                        u'resourcetype': u'projects',
                        u'unique_id':
                        u"<class 'goldstone.core.models.Project'>"
                    },
                },
                {
                    u'native_id': u'p1',
                    u'native_name': u'project 1',
                    u'resourcetype': {
                        u'label': u'projects',
                        u'resourcetype': u'projects',
                        u'unique_id':
                        u"<class 'goldstone.core.models.Project'>"
                    },
                },
                {
                    u'native_id': u'p0',
                    u'native_name': u'project 0',
                    u'resourcetype': {
                        u'label': u'projects',
                        u'resourcetype': u'projects',
                        u'unique_id':
                        u"<class 'goldstone.core.models.Project'>"
                    },
                },
                {
                    u'native_id': u'n12345',
                    u'native_name': u'network 1',
                    u'resourcetype': {
                        u'label':
                        u'networks',
                        u'resourcetype':
                        u'networks',
                        u'unique_id':
                        u"<class 'goldstone.core.models.NeutronNetwork'>"
                    },
                },
                {
                    u'native_id': u'n1234',
                    u'native_name': u'network 0',
                    u'resourcetype': {
                        u'label':
                        u'networks',
                        u'resourcetype':
                        u'networks',
                        u'unique_id':
                        u"<class 'goldstone.core.models.NeutronNetwork'>"
                    },
                },
            ], 10],
            "integration_name=nova&native_id=45": [[
                {
                    u'native_id': u'12345',
                    u'native_name': u'host 1',
                    u'resourcetype': {
                        u'label': u'hosts',
                        u'resourcetype': u'hosts',
                        u'unique_id': u"<class 'goldstone.core.models.Host'>"
                    },
                },
                {
                    u'native_id': u'f23456',
                    u'native_name': u'hypervisor 0',
                    u'resourcetype': {
                        u'label': u'hypervisors',
                        u'resourcetype': u'hypervisors',
                        u'unique_id':
                        u"<class 'goldstone.core.models.Hypervisor'>"
                    },
                },
                {
                    u'native_id': u'f2345',
                    u'native_name': u'hypervisor 0',
                    u'resourcetype': {
                        u'label': u'hypervisors',
                        u'resourcetype': u'hypervisors',
                        u'unique_id':
                        u"<class 'goldstone.core.models.Hypervisor'>"
                    },
                },
                {
                    u'native_id': u'123456',
                    u'native_name': u'host 2',
                    u'resourcetype': {
                        u'label': u'hosts',
                        u'resourcetype': u'hosts',
                        u'unique_id': u"<class 'goldstone.core.models.Host'>"
                    },
                },
            ], 4],
            "integration_name=neutron&native_name=1": [[
                {
                    u'native_id': u'n12345',
                    u'native_name': u'network 1',
                    u'resourcetype': {
                        u'label':
                        u'networks',
                        u'resourcetype':
                        u'networks',
                        u'unique_id':
                        u"<class 'goldstone.core.models.NeutronNetwork'>"
                    },
                },
            ], 2]
        }

        # Create the nodes for the test.
        for nodetype, native_id, native_name, attributes in NODES:
            if nodetype == Host:
                db_node = nodetype.objects.create(native_id=native_id,
                                                  native_name=native_name,
                                                  fqdn=native_name + ".com")
            else:
                db_node = nodetype.objects.create(native_id=native_id,
                                                  native_name=native_name)

            resource.instances.graph.add_node(
                GraphNode(uuid=db_node.uuid,
                          resourcetype=nodetype,
                          attributes=attributes))

        # Force the instance graph to be re-evaluated now.
        resource.instances._graph = None  # pylint: disable=W0212

        # Create the edges for the test.
        for source_id, destination_id, attr_dict in EDGES:
            # Locate the source and destination nodes in the resource graph.
            source_row = PolyResource.objects.get(native_id=source_id)
            destination_row = \
                PolyResource.objects.get(native_id=destination_id)

            source_node = [
                x for x in resource.instances.graph.nodes()
                if x.uuid == source_row.uuid
            ][0]
            destination_node = [
                x for x in resource.instances.graph.nodes()
                if x.uuid == destination_row.uuid
            ][0]

            resource.instances.graph.add_edge(source_node,
                                              destination_node,
                                              attr_dict=attr_dict)

        # Create a user.
        token = create_and_login()

        # Do the test.
        for query_string, value in QUERIES.iteritems():
            expected_nodes = value[0]
            expected_edges = value[1]

            response = self.client.get(
                RES_URL_FILTER % query_string,
                HTTP_AUTHORIZATION=AUTHORIZATION_PAYLOAD % token)

            # Test the results.
            # pylint: disable=E1101
            self.assertEqual(response.status_code, HTTP_200_OK)

            # Test the result's edge count.
            content = json.loads(response.content)
            self.assertEqual(expected_edges, len(content["edges"]))

            # Test the result's nodes, sans UUIDs.
            for entry in content["nodes"]:
                del entry["uuid"]

            self.assertItemsEqual(content["nodes"], expected_nodes)