metalsmith/metalsmith/test/test_instance_config.py

144 lines
5.1 KiB
Python

# Copyright 2018 Red Hat, Inc.
#
# 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.
import json
import mock
from openstack.baremetal import configdrive
import testtools
import metalsmith
from metalsmith import _utils
from metalsmith import instance_config
class TestGenericConfig(testtools.TestCase):
CLASS = instance_config.GenericConfig
def setUp(self):
super(TestGenericConfig, self).setUp()
self.node = mock.Mock(id='1234')
self.node.name = 'node name'
def _check(self, config, expected_metadata, expected_userdata=None,
cloud_init=True):
expected_m = {'public_keys': {},
'uuid': '1234',
'name': 'node name',
'hostname': 'example.com',
'launch_index': 0,
'availability_zone': '',
'files': [],
'meta': {}}
expected_m.update(expected_metadata)
self.node.instance_info = {_utils.HOSTNAME_FIELD:
expected_m.get('hostname')}
with mock.patch.object(configdrive, 'build', autospec=True) as mb:
result = config.build_configdrive(self.node)
mb.assert_called_once_with(expected_m, mock.ANY)
self.assertIs(result, mb.return_value)
user_data = mb.call_args[1].get('user_data')
if expected_userdata:
self.assertIsNotNone(user_data)
user_data = user_data.decode('utf-8')
if cloud_init:
header, user_data = user_data.split('\n', 1)
self.assertEqual('#cloud-config', header)
user_data = json.loads(user_data)
self.assertEqual(expected_userdata, user_data)
def test_default(self):
config = self.CLASS()
self._check(config, {})
def test_ssh_keys(self):
config = self.CLASS(ssh_keys=['abc', 'def'])
self._check(config, {'public_keys': {'0': 'abc', '1': 'def'}})
def test_ssh_keys_as_dict(self):
config = self.CLASS(ssh_keys={'default': 'abc'})
self._check(config, {'public_keys': {'default': 'abc'}})
def test_custom_user_data(self):
config = self.CLASS(user_data='{"answer": 42}')
self._check(config, {}, {"answer": 42}, cloud_init=False)
class TestCloudInitConfig(TestGenericConfig):
CLASS = instance_config.CloudInitConfig
def test_add_user(self):
config = self.CLASS()
config.add_user('admin')
self._check(config, {},
{'users': [{'name': 'admin',
'groups': ['wheel']}]})
def test_add_user_admin(self):
config = self.CLASS()
config.add_user('admin', admin=False)
self._check(config, {},
{'users': [{'name': 'admin'}]})
def test_add_user_sudo(self):
config = self.CLASS()
config.add_user('admin', sudo=True)
self._check(config, {},
{'users': [{'name': 'admin',
'groups': ['wheel'],
'sudo': 'ALL=(ALL) NOPASSWD:ALL'}]})
def test_add_user_passwd(self):
config = self.CLASS()
config.add_user('admin', password_hash='123')
self._check(config, {},
{'users': [{'name': 'admin',
'groups': ['wheel'],
'passwd': '123'}]})
def test_add_user_with_keys(self):
config = self.CLASS(ssh_keys=['abc', 'def'])
config.add_user('admin')
self._check(config, {'public_keys': {'0': 'abc', '1': 'def'}},
{'users': [{'name': 'admin',
'groups': ['wheel'],
'ssh_authorized_keys': ['abc', 'def']}]})
# Overriding tests since CloudInitConfig does not support plain strings
# for user_data, only dictionaries.
def test_custom_user_data(self):
config = self.CLASS(user_data={'answer': 42})
self._check(config, {}, {'answer': 42})
def test_custom_user_data_with_users(self):
config = self.CLASS(user_data={'answer': 42})
config.add_user('admin')
self._check(config, {},
{'users': [{'name': 'admin',
'groups': ['wheel']}],
'answer': 42})
def test_user_data_not_dict(self):
self.assertRaises(TypeError, self.CLASS, user_data="string")
config = self.CLASS()
config.user_data = "string"
self.assertRaises(TypeError, config.populate_user_data)
class TestDeprecatedInstanceConfig(TestCloudInitConfig):
CLASS = metalsmith.InstanceConfig