~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2011 University of Southern California
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
"""
Unit Tests for instance types extra specs code
"""

from nova import context
from nova import db
from nova import test


class InstanceTypeExtraSpecsTestCase(test.TestCase):

    def setUp(self):
        super(InstanceTypeExtraSpecsTestCase, self).setUp()
        self.context = context.get_admin_context()
        values = dict(name="cg1.4xlarge",
                      memory_mb=22000,
                      vcpus=8,
                      root_gb=1690,
                      ephemeral_gb=2000,
                      flavorid=105)
        specs = dict(cpu_arch="x86_64",
                        cpu_model="Nehalem",
                        xpu_arch="fermi",
                        xpus=2,
                        xpu_model="Tesla 2050")
        values['extra_specs'] = specs
        ref = db.instance_type_create(self.context,
                                          values)
        self.instance_type_id = ref["id"]
        self.flavorid = ref["flavorid"]

    def tearDown(self):
        # Remove the instance type from the database
        db.instance_type_destroy(self.context, "cg1.4xlarge")
        super(InstanceTypeExtraSpecsTestCase, self).tearDown()

    def test_instance_type_specs_get(self):
        expected_specs = dict(cpu_arch="x86_64",
                                 cpu_model="Nehalem",
                                 xpu_arch="fermi",
                                 xpus="2",
                                 xpu_model="Tesla 2050")
        actual_specs = db.instance_type_extra_specs_get(
                              self.context,
                              self.flavorid)
        self.assertEquals(expected_specs, actual_specs)

    def test_instance_type_extra_specs_delete(self):
        expected_specs = dict(cpu_arch="x86_64",
                                 cpu_model="Nehalem",
                                 xpu_arch="fermi",
                                 xpus="2")
        db.instance_type_extra_specs_delete(self.context,
                                      self.flavorid,
                                      "xpu_model")
        actual_specs = db.instance_type_extra_specs_get(
                              self.context,
                              self.flavorid)
        self.assertEquals(expected_specs, actual_specs)

    def test_instance_type_extra_specs_update(self):
        expected_specs = dict(cpu_arch="x86_64",
                                 cpu_model="Sandy Bridge",
                                 xpu_arch="fermi",
                                 xpus="2",
                                 xpu_model="Tesla 2050")
        db.instance_type_extra_specs_update_or_create(
                              self.context,
                              self.flavorid,
                              dict(cpu_model="Sandy Bridge"))
        actual_specs = db.instance_type_extra_specs_get(
                              self.context,
                              self.flavorid)
        self.assertEquals(expected_specs, actual_specs)

    def test_instance_type_extra_specs_create(self):
        expected_specs = dict(cpu_arch="x86_64",
                                 cpu_model="Nehalem",
                                 xpu_arch="fermi",
                                 xpus="2",
                                 xpu_model="Tesla 2050",
                                 net_arch="ethernet",
                                 net_mbps="10000")
        db.instance_type_extra_specs_update_or_create(
                              self.context,
                              self.flavorid,
                              dict(net_arch="ethernet",
                                   net_mbps=10000))
        actual_specs = db.instance_type_extra_specs_get(
                              self.context,
                              self.flavorid)
        self.assertEquals(expected_specs, actual_specs)

    def test_instance_type_get_with_extra_specs(self):
        instance_type = db.instance_type_get(
                            self.context,
                            self.instance_type_id)
        self.assertEquals(instance_type['extra_specs'],
                          dict(cpu_arch="x86_64",
                               cpu_model="Nehalem",
                               xpu_arch="fermi",
                               xpus="2",
                               xpu_model="Tesla 2050"))
        instance_type = db.instance_type_get(
                            self.context,
                            5)
        self.assertEquals(instance_type['extra_specs'], {})

    def test_instance_type_get_by_name_with_extra_specs(self):
        instance_type = db.instance_type_get_by_name(
                            self.context,
                            "cg1.4xlarge")
        self.assertEquals(instance_type['extra_specs'],
                          dict(cpu_arch="x86_64",
                               cpu_model="Nehalem",
                               xpu_arch="fermi",
                               xpus="2",
                               xpu_model="Tesla 2050"))

        instance_type = db.instance_type_get_by_name(
                            self.context,
                            "m1.small")
        self.assertEquals(instance_type['extra_specs'], {})

    def test_instance_type_get_by_flavor_id_with_extra_specs(self):
        instance_type = db.instance_type_get_by_flavor_id(
                            self.context,
                            105)
        self.assertEquals(instance_type['extra_specs'],
                          dict(cpu_arch="x86_64",
                               cpu_model="Nehalem",
                               xpu_arch="fermi",
                               xpus="2",
                               xpu_model="Tesla 2050"))

        instance_type = db.instance_type_get_by_flavor_id(
                            self.context,
                            2)
        self.assertEquals(instance_type['extra_specs'], {})

    def test_instance_type_get_all(self):
        specs = dict(cpu_arch="x86_64",
                        cpu_model="Nehalem",
                        xpu_arch="fermi",
                        xpus='2',
                        xpu_model="Tesla 2050")

        types = db.instance_type_get_all(self.context)

        name2specs = {}
        for instance_type in types:
            name = instance_type['name']
            name2specs[name] = instance_type['extra_specs']

        self.assertEquals(name2specs['cg1.4xlarge'], specs)
        self.assertEquals(name2specs['m1.small'], {})