1# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15"""
16test_inventory
17----------------------------------
18
19Functional tests for `shade` inventory methods.
20"""
21
22from openstack.cloud import inventory
23from openstack.tests.functional import base
24from openstack.tests.functional.cloud.util import pick_flavor
25
26
27class TestInventory(base.BaseFunctionalTest):
28    def setUp(self):
29        super(TestInventory, self).setUp()
30        # This needs to use an admin account, otherwise a public IP
31        # is not allocated from devstack.
32        self.inventory = inventory.OpenStackInventory(cloud='devstack-admin')
33        self.server_name = self.getUniqueString('inventory')
34        self.flavor = pick_flavor(
35            self.user_cloud.list_flavors(get_extra=False))
36        if self.flavor is None:
37            self.assertTrue(False, 'no sensible flavor available')
38        self.image = self.pick_image()
39        self.addCleanup(self._cleanup_server)
40        server = self.operator_cloud.create_server(
41            name=self.server_name, image=self.image, flavor=self.flavor,
42            wait=True, auto_ip=True, network='public')
43        self.server_id = server['id']
44
45    def _cleanup_server(self):
46        self.user_cloud.delete_server(self.server_id, wait=True)
47
48    def _test_host_content(self, host):
49        self.assertEqual(host['image']['id'], self.image.id)
50        self.assertNotIn('links', host['image'])
51        self.assertNotIn('id', host['flavor'])
52        self.assertNotIn('links', host['flavor'])
53        self.assertNotIn('links', host)
54        self.assertIsInstance(host['volumes'], list)
55        self.assertIsInstance(host['metadata'], dict)
56        self.assertIn('interface_ip', host)
57        self.assertIn('ram', host['flavor'])
58
59    def _test_expanded_host_content(self, host):
60        self.assertEqual(host['image']['name'], self.image.name)
61        self.assertEqual(host['flavor']['name'], self.flavor.name)
62
63    def test_get_host(self):
64        host = self.inventory.get_host(self.server_id)
65        self.assertIsNotNone(host)
66        self.assertEqual(host['name'], self.server_name)
67        self._test_host_content(host)
68        self._test_expanded_host_content(host)
69        host_found = False
70        for host in self.inventory.list_hosts():
71            if host['id'] == self.server_id:
72                host_found = True
73                self._test_host_content(host)
74        self.assertTrue(host_found)
75
76    def test_get_host_no_detail(self):
77        host = self.inventory.get_host(self.server_id, expand=False)
78        self.assertIsNotNone(host)
79        self.assertEqual(host['name'], self.server_name)
80
81        self.assertEqual(host['image']['id'], self.image.id)
82        self.assertNotIn('links', host['image'])
83        self.assertNotIn('name', host['name'])
84        self.assertNotIn('id', host['flavor'])
85        self.assertNotIn('links', host['flavor'])
86        self.assertNotIn('name', host['flavor'])
87        self.assertIn('ram', host['flavor'])
88
89        host_found = False
90        for host in self.inventory.list_hosts(expand=False):
91            if host['id'] == self.server_id:
92                host_found = True
93                self._test_host_content(host)
94        self.assertTrue(host_found)
95