~ewanmellor/nova/lp835952

« back to all changes in this revision

Viewing changes to nova/tests/test_instance_types.py

  • Committer: Tarmac
  • Author(s): Ken Pepple
  • Date: 2011-08-25 16:55:33 UTC
  • mfrom: (1464.2.4 lp828596)
  • Revision ID: tarmac-20110825165533-ceq2v30txe9xglt0
added unit tests to instance_types for rainy day paths

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
        self.id = max_id["id"] + 1
48
48
        self.name = str(int(time.time()))
49
49
 
 
50
    def _nonexistent_flavor_name(self):
 
51
        """return an instance type name not in the DB"""
 
52
        nonexistent_flavor = "sdfsfsdf"
 
53
        flavors = instance_types.get_all_types()
 
54
        while nonexistent_flavor in flavors:
 
55
            nonexistent_flavor += "z"
 
56
        else:
 
57
            return nonexistent_flavor
 
58
 
 
59
    def _nonexistent_flavor_id(self):
 
60
        """return an instance type ID not in the DB"""
 
61
        nonexistent_flavor = 2700
 
62
        flavor_ids = [value["id"] for key, value in\
 
63
                    instance_types.get_all_types().iteritems()]
 
64
        while nonexistent_flavor in flavor_ids:
 
65
            nonexistent_flavor += 1
 
66
        else:
 
67
            return nonexistent_flavor
 
68
 
 
69
    def _existing_flavor(self):
 
70
        """return first instance type name"""
 
71
        return instance_types.get_all_types().keys()[0]
 
72
 
50
73
    def test_instance_type_create_then_delete(self):
51
74
        """Ensure instance types can be created"""
52
75
        starting_inst_list = instance_types.get_all_types()
84
107
                exception.InvalidInput,
85
108
                instance_types.create, self.name, 256, 1, "aa", self.flavorid)
86
109
 
87
 
    def test_non_existant_inst_type_shouldnt_delete(self):
 
110
    def test_non_existent_inst_type_shouldnt_delete(self):
88
111
        """Ensures that instance type creation fails with invalid args"""
89
112
        self.assertRaises(exception.ApiError,
90
 
                          instance_types.destroy, "sfsfsdfdfs")
 
113
                          instance_types.destroy,
 
114
                          self._nonexistent_flavor_name())
91
115
 
92
116
    def test_repeated_inst_types_should_raise_api_error(self):
93
117
        """Ensures that instance duplicates raises ApiError"""
97
121
        self.assertRaises(
98
122
                exception.ApiError,
99
123
                instance_types.create, new_name, 256, 1, 120, self.flavorid)
 
124
 
 
125
    def test_will_not_destroy_with_no_name(self):
 
126
        """Ensure destroy sad path of no name raises error"""
 
127
        self.assertRaises(exception.ApiError,
 
128
                          instance_types.destroy,
 
129
                          self._nonexistent_flavor_name())
 
130
 
 
131
    def test_will_not_purge_without_name(self):
 
132
        """Ensure purge without a name raises error"""
 
133
        self.assertRaises(exception.InvalidInstanceType,
 
134
                          instance_types.purge, None)
 
135
 
 
136
    def test_will_not_purge_with_wrong_name(self):
 
137
        """Ensure purge without correct name raises error"""
 
138
        self.assertRaises(exception.ApiError,
 
139
                          instance_types.purge,
 
140
                          self._nonexistent_flavor_name())
 
141
 
 
142
    def test_will_not_get_bad_default_instance_type(self):
 
143
        """ensures error raised on bad default instance type"""
 
144
        FLAGS.default_instance_type = self._nonexistent_flavor_name()
 
145
        self.assertRaises(exception.InstanceTypeNotFoundByName,
 
146
                          instance_types.get_default_instance_type)
 
147
 
 
148
    def test_will_not_get_instance_type_by_name_with_no_name(self):
 
149
        """Ensure get by name returns default flavor with no name"""
 
150
        self.assertEqual(instance_types.get_default_instance_type(),
 
151
                              instance_types.get_instance_type_by_name(None))
 
152
 
 
153
    def test_will_not_get_instance_type_with_bad_name(self):
 
154
        """Ensure get by name returns default flavor with bad name"""
 
155
        self.assertRaises(exception.InstanceTypeNotFound,
 
156
                          instance_types.get_instance_type,
 
157
                          self._nonexistent_flavor_name())
 
158
 
 
159
    def test_will_not_get_flavor_by_bad_flavor_id(self):
 
160
        """Ensure get by flavor raises error with wrong flavorid"""
 
161
        self.assertRaises(exception.InstanceTypeNotFound,
 
162
                          instance_types.get_instance_type_by_name,
 
163
                          self._nonexistent_flavor_id())