~hudson-openstack/nova/trunk

« back to all changes in this revision

Viewing changes to nova/tests/api/openstack/contrib/test_createserverext.py

  • Committer: Tarmac
  • Author(s): Tushar Patil
  • Date: 2011-08-31 23:50:08 UTC
  • mfrom: (1509.1.2 nova)
  • Revision ID: tarmac-20110831235008-v6a2l721nx94yuev
Validates that user-data is b64 encoded.

Show diffs side-by-side

added added

removed removed

Lines of Context:
76
76
            def __init__(self):
77
77
                self.injected_files = None
78
78
                self.networks = None
 
79
                self.user_data = None
79
80
 
80
81
            def create(self, *args, **kwargs):
81
82
                if 'injected_files' in kwargs:
87
88
                    self.networks = kwargs['requested_networks']
88
89
                else:
89
90
                    self.networks = None
 
91
 
 
92
                if 'user_data' in kwargs:
 
93
                    self.user_data = kwargs['user_data']
 
94
 
90
95
                return [{'id': '1234', 'display_name': 'fakeinstance',
91
96
                         'uuid': FAKE_UUID,
92
97
                         'created_at': "",
119
124
            server['networks'] = network_list
120
125
        return {'server': server}
121
126
 
 
127
    def _create_user_data_request_dict(self, user_data):
 
128
        server = {}
 
129
        server['name'] = 'new-server-test'
 
130
        server['imageRef'] = 1
 
131
        server['flavorRef'] = 1
 
132
        server['user_data'] = user_data
 
133
        return {'server': server}
 
134
 
122
135
    def _get_create_request_json(self, body_dict):
123
136
        req = webob.Request.blank('/v1.1/123/os-create-server-ext')
124
137
        req.headers['Content-Type'] = 'application/json'
178
191
            self._run_create_instance_with_mock_compute_api(request)
179
192
        return request, response, compute_api.networks
180
193
 
 
194
    def _create_instance_with_user_data_json(self, networks):
 
195
        body_dict = self._create_user_data_request_dict(networks)
 
196
        request = self._get_create_request_json(body_dict)
 
197
        compute_api, response = \
 
198
            self._run_create_instance_with_mock_compute_api(request)
 
199
        return request, response, compute_api.user_data
 
200
 
181
201
    def _create_instance_with_networks_xml(self, networks):
182
202
        body_dict = self._create_networks_request_dict(networks)
183
203
        request = self._get_create_request_xml(body_dict)
304
324
        self.assertEquals(response.status_int, 202)
305
325
        self.assertEquals(compute_api.networks,
306
326
                          [('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', None)])
 
327
 
 
328
    def test_create_instance_with_userdata(self):
 
329
        user_data_contents = '#!/bin/bash\necho "Oh no!"\n'
 
330
        user_data_contents = base64.b64encode(user_data_contents)
 
331
        request, response, user_data = \
 
332
                self._create_instance_with_user_data_json(user_data_contents)
 
333
        self.assertEquals(response.status_int, 202)
 
334
        self.assertEquals(user_data, user_data_contents)
 
335
 
 
336
    def test_create_instance_with_userdata_none(self):
 
337
        user_data_contents = None
 
338
        request, response, user_data = \
 
339
                self._create_instance_with_user_data_json(user_data_contents)
 
340
        self.assertEquals(response.status_int, 202)
 
341
        self.assertEquals(user_data, user_data_contents)
 
342
 
 
343
    def test_create_instance_with_userdata_with_non_b64_content(self):
 
344
        user_data_contents = '#!/bin/bash\necho "Oh no!"\n'
 
345
        request, response, user_data = \
 
346
                self._create_instance_with_user_data_json(user_data_contents)
 
347
        self.assertEquals(response.status_int, 400)
 
348
        self.assertEquals(user_data, None)