summaryrefslogtreecommitdiff
path: root/tests/unittests/test_datasource/test_cloudsigma.py
blob: d62d542bd556b3abd80ee3a44d0aaf36088dd490 (plain)
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
# This file is part of cloud-init. See LICENSE file for license information.

import copy

from cloudinit.cs_utils import Cepko
from cloudinit import helpers
from cloudinit import sources
from cloudinit.sources import DataSourceCloudSigma

from cloudinit.tests import helpers as test_helpers

SERVER_CONTEXT = {
    "cpu": 1000,
    "cpus_instead_of_cores": False,
    "global_context": {"some_global_key": "some_global_val"},
    "mem": 1073741824,
    "meta": {
        "ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2E.../hQ5D5 john@doe",
        "cloudinit-user-data": "#cloud-config\n\n...",
    },
    "name": "test_server",
    "requirements": [],
    "smp": 1,
    "tags": ["much server", "very performance"],
    "uuid": "65b2fb23-8c03-4187-a3ba-8b7c919e8890",
    "vnc_password": "9e84d6cb49e46379",
    "vendor_data": {
        "location": "zrh",
        "cloudinit": "#cloud-config\n\n...",
    }
}

DS_PATH = 'cloudinit.sources.DataSourceCloudSigma.DataSourceCloudSigma'


class CepkoMock(Cepko):
    def __init__(self, mocked_context):
        self.result = mocked_context

    def all(self):
        return self


class DataSourceCloudSigmaTest(test_helpers.CiTestCase):
    def setUp(self):
        super(DataSourceCloudSigmaTest, self).setUp()
        self.paths = helpers.Paths({'run_dir': self.tmp_dir()})
        self.add_patch(DS_PATH + '.is_running_in_cloudsigma',
                       "m_is_container", return_value=True)
        self.datasource = DataSourceCloudSigma.DataSourceCloudSigma(
            "", "", paths=self.paths)
        self.datasource.cepko = CepkoMock(SERVER_CONTEXT)

    def test_get_hostname(self):
        self.datasource.get_data()
        self.assertEqual("test_server", self.datasource.get_hostname())
        self.datasource.metadata['name'] = ''
        self.assertEqual("65b2fb23", self.datasource.get_hostname())
        utf8_hostname = b'\xd1\x82\xd0\xb5\xd1\x81\xd1\x82'.decode('utf-8')
        self.datasource.metadata['name'] = utf8_hostname
        self.assertEqual("65b2fb23", self.datasource.get_hostname())

    def test_get_public_ssh_keys(self):
        self.datasource.get_data()
        self.assertEqual([SERVER_CONTEXT['meta']['ssh_public_key']],
                         self.datasource.get_public_ssh_keys())

    def test_get_instance_id(self):
        self.datasource.get_data()
        self.assertEqual(SERVER_CONTEXT['uuid'],
                         self.datasource.get_instance_id())

    def test_platform(self):
        """All platform-related attributes are set."""
        self.datasource.get_data()
        self.assertEqual(self.datasource.cloud_name, 'cloudsigma')
        self.assertEqual(self.datasource.platform_type, 'cloudsigma')
        self.assertEqual(self.datasource.subplatform, 'cepko (/dev/ttyS1)')

    def test_metadata(self):
        self.datasource.get_data()
        self.assertEqual(self.datasource.metadata, SERVER_CONTEXT)

    def test_user_data(self):
        self.datasource.get_data()
        self.assertEqual(self.datasource.userdata_raw,
                         SERVER_CONTEXT['meta']['cloudinit-user-data'])

    def test_encoded_user_data(self):
        encoded_context = copy.deepcopy(SERVER_CONTEXT)
        encoded_context['meta']['base64_fields'] = 'cloudinit-user-data'
        encoded_context['meta']['cloudinit-user-data'] = 'aGkgd29ybGQK'
        self.datasource.cepko = CepkoMock(encoded_context)
        self.datasource.get_data()

        self.assertEqual(self.datasource.userdata_raw, b'hi world\n')

    def test_vendor_data(self):
        self.datasource.get_data()
        self.assertEqual(self.datasource.vendordata_raw,
                         SERVER_CONTEXT['vendor_data']['cloudinit'])

    def test_lack_of_vendor_data(self):
        stripped_context = copy.deepcopy(SERVER_CONTEXT)
        del stripped_context["vendor_data"]
        self.datasource.cepko = CepkoMock(stripped_context)
        self.datasource.get_data()

        self.assertIsNone(self.datasource.vendordata_raw)

    def test_lack_of_cloudinit_key_in_vendor_data(self):
        stripped_context = copy.deepcopy(SERVER_CONTEXT)
        del stripped_context["vendor_data"]["cloudinit"]
        self.datasource.cepko = CepkoMock(stripped_context)
        self.datasource.get_data()

        self.assertIsNone(self.datasource.vendordata_raw)


class DsLoads(test_helpers.TestCase):
    def test_get_datasource_list_returns_in_local(self):
        deps = (sources.DEP_FILESYSTEM,)
        ds_list = DataSourceCloudSigma.get_datasource_list(deps)
        self.assertEqual(ds_list,
                         [DataSourceCloudSigma.DataSourceCloudSigma])

    def test_list_sources_finds_ds(self):
        found = sources.list_sources(
            ['CloudSigma'], (sources.DEP_FILESYSTEM,), ['cloudinit.sources'])
        self.assertEqual([DataSourceCloudSigma.DataSourceCloudSigma],
                         found)

# vi: ts=4 expandtab