|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# |
|
3
|
|
|
# Copyright (c) 2013-2016 Online SAS and Contributors. All Rights Reserved. |
|
4
|
|
|
# Julien Castets <[email protected]> |
|
5
|
|
|
# |
|
6
|
|
|
# Licensed under the BSD 2-Clause License (the "License"); you may not use this |
|
7
|
|
|
# file except in compliance with the License. You may obtain a copy of the |
|
8
|
|
|
# License at https://opensource.org/licenses/BSD-2-Clause |
|
9
|
|
|
|
|
10
|
|
|
import json |
|
11
|
|
|
import unittest |
|
12
|
|
|
import uuid |
|
13
|
|
|
|
|
14
|
|
|
from scaleway.apis import MetadataAPI |
|
15
|
|
|
from six.moves.urllib.parse import parse_qs, urlparse |
|
16
|
|
|
|
|
17
|
|
|
from . import FakeAPITestCase |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
class TestMetadataAPI(FakeAPITestCase, unittest.TestCase): |
|
21
|
|
|
|
|
22
|
|
|
def setUp(self): |
|
23
|
|
|
super(TestMetadataAPI, self).setUp() |
|
24
|
|
|
self.api = MetadataAPI() |
|
25
|
|
|
|
|
26
|
|
|
def make_fake_metadata_api(self): |
|
27
|
|
|
""" Fakes the Metadata API. |
|
28
|
|
|
""" |
|
29
|
|
|
# Response returned by fake_route_conf |
|
30
|
|
|
json_response = { |
|
31
|
|
|
'id': str(uuid.uuid4()), |
|
32
|
|
|
'name': 'super name', |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
def fake_route_conf(_, uri, headers): |
|
36
|
|
|
""" Fakes the /conf route. |
|
37
|
|
|
|
|
38
|
|
|
Returns metadata of a running server. Our tests don't need to have |
|
39
|
|
|
all the metadata of a server, so only a few values are returned. |
|
40
|
|
|
|
|
41
|
|
|
If ?format=json is set, return a JSON dict with a application/json |
|
42
|
|
|
content |
|
43
|
|
|
type. |
|
44
|
|
|
|
|
45
|
|
|
If no format is given, return a text/plain response with a "shell" |
|
46
|
|
|
format. |
|
47
|
|
|
""" |
|
48
|
|
|
querystring = parse_qs(urlparse(uri).query) |
|
49
|
|
|
|
|
50
|
|
|
if 'json' in querystring.get('format', []): |
|
51
|
|
|
return 200, headers, json.dumps(json_response) |
|
52
|
|
|
|
|
53
|
|
|
headers['content-type'] = 'text/plain' |
|
54
|
|
|
return 200, headers, '\n'.join( |
|
55
|
|
|
'%s="%s"' % (key, value) |
|
56
|
|
|
for key, value in json_response.items() |
|
57
|
|
|
) |
|
58
|
|
|
|
|
59
|
|
|
self.fake_endpoint(self.api, 'conf/', body=fake_route_conf) |
|
60
|
|
|
return json_response |
|
61
|
|
|
|
|
62
|
|
|
def test_get(self): |
|
63
|
|
|
expected_response = self.make_fake_metadata_api() |
|
64
|
|
|
self.assertEqual(self.api.get_metadata(), expected_response) |
|
65
|
|
|
|
|
66
|
|
|
shell_response = self.api.get_metadata(as_shell=True) |
|
67
|
|
|
self.assertIn('id="%(id)s"' % expected_response, |
|
68
|
|
|
shell_response.decode('utf8')) |
|
69
|
|
|
self.assertIn('name="%(name)s"' % expected_response, |
|
70
|
|
|
shell_response.decode('utf8')) |
|
71
|
|
|
|