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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
# This file is part of cloud-init. See LICENSE file for license information.
"""Base test case module."""
import crypt
import json
import re
import unittest
from cloudinit import util as c_util
SkipTest = unittest.SkipTest
class CloudTestCase(unittest.TestCase):
"""Base test class for verifiers."""
# data gets populated in get_suite.setUpClass
data = {}
conf = None
_cloud_config = None
release_conf = {} # The platform's os release configuration
expected_warnings = () # Subclasses set to ignore expected WARN logs
@property
def os_cfg(self):
return self.release_conf[self.os_name]['default']
def is_distro(self, distro_name):
return self.os_cfg['os'] == distro_name
@classmethod
def maybeSkipTest(cls):
"""Present to allow subclasses to override and raise a skipTest."""
pass
def assertPackageInstalled(self, name, version=None):
"""Check dpkg-query --show output for matching package name.
@param name: package base name
@param version: string representing a package version or part of a
version.
"""
pkg_out = self.get_data_file('package-versions')
pkg_match = re.search(
'^%s\t(?P<version>.*)$' % name, pkg_out, re.MULTILINE)
if pkg_match:
installed_version = pkg_match.group('version')
if not version:
return # Success
if installed_version.startswith(version):
return # Success
raise AssertionError(
'Expected package version %s-%s not found. Found %s' %
name, version, installed_version)
raise AssertionError('Package not installed: %s' % name)
def os_version_cmp(self, cmp_version):
"""Compare the version of the test to comparison_version.
@param: cmp_version: Either a float or a string representing
a release os from releases.yaml (e.g. centos66)
@return: -1 when version < cmp_version, 0 when version=cmp_version and
1 when version > cmp_version.
"""
version = self.release_conf[self.os_name]['default']['version']
if isinstance(cmp_version, str):
cmp_version = self.release_conf[cmp_version]['default']['version']
if version < cmp_version:
return -1
elif version == cmp_version:
return 0
else:
return 1
@property
def os_name(self):
return self.data.get('os_name', 'UNKNOWN')
@property
def platform(self):
return self.data.get('platform', 'UNKNOWN')
@property
def cloud_config(self):
"""Get the cloud-config used by the test."""
if not self._cloud_config:
self._cloud_config = c_util.load_yaml(self.conf)
return self._cloud_config
def get_config_entry(self, name):
"""Get a config entry from cloud-config ensuring that it is present."""
if name not in self.cloud_config:
raise AssertionError('Key "{}" not in cloud config'.format(name))
return self.cloud_config[name]
def get_data_file(self, name, decode=True):
"""Get data file failing test if it is not present."""
if name not in self.data:
raise AssertionError('File "{}" missing from collect data'
.format(name))
if not decode:
return self.data[name]
return self.data[name].decode('utf-8')
def get_instance_id(self):
"""Get recorded instance id."""
return self.get_data_file('instance-id').strip()
def get_status_data(self, data, version=None):
"""Parse result.json and status.json like data files.
@param data: data to load
@param version: cloud-init output version, defaults to 'v1'
@return_value: dict of data or None if missing
"""
if not version:
version = 'v1'
data = json.loads(data)
return data.get(version)
def get_datasource(self):
"""Get datasource name."""
data = self.get_status_data(self.get_data_file('result.json'))
return data.get('datasource')
def test_no_stages_errors(self):
"""Ensure that there were no errors in any stage."""
status = self.get_status_data(self.get_data_file('status.json'))
for stage in ('init', 'init-local', 'modules-config', 'modules-final'):
self.assertIn(stage, status)
self.assertEqual(len(status[stage]['errors']), 0,
'errors {} were encountered in stage {}'
.format(status[stage]['errors'], stage))
result = self.get_status_data(self.get_data_file('result.json'))
self.assertEqual(len(result['errors']), 0)
def test_no_warnings_in_log(self):
"""Unexpected warnings should not be found in the log."""
warnings = [
l for l in self.get_data_file('cloud-init.log').splitlines()
if 'WARN' in l]
joined_warnings = '\n'.join(warnings)
for expected_warning in self.expected_warnings:
self.assertIn(
expected_warning, joined_warnings,
msg="Did not find %s in cloud-init.log" % expected_warning)
# Prune expected from discovered warnings
warnings = [w for w in warnings if expected_warning not in w]
self.assertEqual(
[], warnings, msg="'WARN' found inside cloud-init.log")
def test_instance_data_json_ec2(self):
"""Validate instance-data.json content by ec2 platform.
This content is sourced by snapd when determining snapstore endpoints.
We validate expected values per cloud type to ensure we don't break
snapd.
"""
if self.platform != 'ec2':
raise SkipTest(
'Skipping ec2 instance-data.json on %s' % self.platform)
out = self.get_data_file('instance-data.json')
if not out:
if self.is_distro('ubuntu') and self.os_version_cmp('bionic') >= 0:
raise AssertionError(
'No instance-data.json found on %s' % self.os_name)
raise SkipTest(
'Skipping instance-data.json test.'
' OS: %s not bionic or newer' % self.os_name)
instance_data = json.loads(out)
self.assertCountEqual(['merged_cfg'], instance_data['sensitive_keys'])
ds = instance_data.get('ds', {})
v1_data = instance_data.get('v1', {})
metadata = ds.get('meta-data', {})
macs = metadata.get(
'network', {}).get('interfaces', {}).get('macs', {})
if not macs:
raise AssertionError('No network data from EC2 meta-data')
# Check meta-data items we depend on
expected_net_keys = [
'public-ipv4s', 'ipv4-associations', 'local-hostname',
'public-hostname']
for mac_data in macs.values():
for key in expected_net_keys:
self.assertIn(key, mac_data)
self.assertIsNotNone(
metadata.get('placement', {}).get('availability-zone'),
'Could not determine EC2 Availability zone placement')
self.assertIsNotNone(
v1_data['availability_zone'], 'expected ec2 availability_zone')
self.assertEqual('aws', v1_data['cloud_name'])
self.assertEqual('ec2', v1_data['platform'])
self.assertEqual(
'metadata (http://169.254.169.254)', v1_data['subplatform'])
self.assertIn('i-', v1_data['instance_id'])
self.assertIn('ip-', v1_data['local_hostname'])
self.assertIsNotNone(v1_data['region'], 'expected ec2 region')
self.assertIsNotNone(
re.match(r'\d\.\d+\.\d+-\d+-aws', v1_data['kernel_release']))
self.assertEqual(
'redacted for non-root user', instance_data['merged_cfg'])
self.assertEqual(self.os_cfg['os'], v1_data['variant'])
self.assertEqual(self.os_cfg['os'], v1_data['distro'])
self.assertEqual(
self.os_cfg['os'], instance_data["sys_info"]['dist'][0],
"Unexpected sys_info dist value")
self.assertEqual(self.os_name, v1_data['distro_release'])
self.assertEqual(
str(self.os_cfg['version']), v1_data['distro_version'])
self.assertEqual('x86_64', v1_data['machine'])
self.assertIsNotNone(
re.match(r'3.\d\.\d', v1_data['python_version']),
"unexpected python version: {ver}".format(
ver=v1_data["python_version"]))
def test_instance_data_json_lxd(self):
"""Validate instance-data.json content by lxd platform.
This content is sourced by snapd when determining snapstore endpoints.
We validate expected values per cloud type to ensure we don't break
snapd.
"""
if self.platform != 'lxd':
raise SkipTest(
'Skipping lxd instance-data.json on %s' % self.platform)
out = self.get_data_file('instance-data.json')
if not out:
if self.is_distro('ubuntu') and self.os_version_cmp('bionic') >= 0:
raise AssertionError(
'No instance-data.json found on %s' % self.os_name)
raise SkipTest(
'Skipping instance-data.json test.'
' OS: %s not bionic or newer' % self.os_name)
instance_data = json.loads(out)
v1_data = instance_data.get('v1', {})
self.assertCountEqual([], sorted(instance_data['base64_encoded_keys']))
self.assertEqual('unknown', v1_data['cloud_name'])
self.assertEqual('lxd', v1_data['platform'])
self.assertEqual(
'seed-dir (/var/lib/cloud/seed/nocloud-net)',
v1_data['subplatform'])
self.assertIsNone(
v1_data['availability_zone'],
'found unexpected lxd availability_zone %s' %
v1_data['availability_zone'])
self.assertIn('cloud-test', v1_data['instance_id'])
self.assertIn('cloud-test', v1_data['local_hostname'])
self.assertIsNone(
v1_data['region'],
'found unexpected lxd region %s' % v1_data['region'])
self.assertIsNotNone(
re.match(r'\d\.\d+\.\d+-\d+', v1_data['kernel_release']))
self.assertEqual(
'redacted for non-root user', instance_data['merged_cfg'])
self.assertEqual(self.os_cfg['os'], v1_data['variant'])
self.assertEqual(self.os_cfg['os'], v1_data['distro'])
self.assertEqual(
self.os_cfg['os'], instance_data["sys_info"]['dist'][0],
"Unexpected sys_info dist value")
self.assertEqual(self.os_name, v1_data['distro_release'])
self.assertEqual(
str(self.os_cfg['version']), v1_data['distro_version'])
self.assertEqual('x86_64', v1_data['machine'])
self.assertIsNotNone(
re.match(r'3.\d\.\d', v1_data['python_version']),
"unexpected python version: {ver}".format(
ver=v1_data["python_version"]))
def test_instance_data_json_kvm(self):
"""Validate instance-data.json content by nocloud-kvm platform.
This content is sourced by snapd when determining snapstore endpoints.
We validate expected values per cloud type to ensure we don't break
snapd.
"""
if self.platform != 'nocloud-kvm':
raise SkipTest(
'Skipping nocloud-kvm instance-data.json on %s' %
self.platform)
out = self.get_data_file('instance-data.json')
if not out:
if self.is_distro('ubuntu') and self.os_version_cmp('bionic') >= 0:
raise AssertionError(
'No instance-data.json found on %s' % self.os_name)
raise SkipTest(
'Skipping instance-data.json test.'
' OS: %s not bionic or newer' % self.os_name)
instance_data = json.loads(out)
v1_data = instance_data.get('v1', {})
self.assertCountEqual([], instance_data['base64_encoded_keys'])
self.assertEqual('unknown', v1_data['cloud_name'])
self.assertEqual('nocloud', v1_data['platform'])
subplatform = v1_data['subplatform']
self.assertIsNotNone(
re.match(r'config-disk \(\/dev\/[a-z]{3}\)', subplatform),
'kvm subplatform "%s" != "config-disk (/dev/...)"' % subplatform)
self.assertIsNone(
v1_data['availability_zone'],
'found unexpected kvm availability_zone %s' %
v1_data['availability_zone'])
self.assertIsNotNone(
re.match(r'[\da-f]{8}(-[\da-f]{4}){3}-[\da-f]{12}',
v1_data['instance_id']),
'kvm instance_id is not a UUID: %s' % v1_data['instance_id'])
self.assertIn('ubuntu', v1_data['local_hostname'])
self.assertIsNone(
v1_data['region'],
'found unexpected lxd region %s' % v1_data['region'])
self.assertIsNotNone(
re.match(r'\d\.\d+\.\d+-\d+', v1_data['kernel_release']))
self.assertEqual(
'redacted for non-root user', instance_data['merged_cfg'])
self.assertEqual(self.os_cfg['os'], v1_data['variant'])
self.assertEqual(self.os_cfg['os'], v1_data['distro'])
self.assertEqual(
self.os_cfg['os'], instance_data["sys_info"]['dist'][0],
"Unexpected sys_info dist value")
self.assertEqual(self.os_name, v1_data['distro_release'])
self.assertEqual(
str(self.os_cfg['version']), v1_data['distro_version'])
self.assertEqual('x86_64', v1_data['machine'])
self.assertIsNotNone(
re.match(r'3.\d\.\d', v1_data['python_version']),
"unexpected python version: {ver}".format(
ver=v1_data["python_version"]))
class PasswordListTest(CloudTestCase):
"""Base password test case class."""
def test_shadow_passwords(self):
"""Test shadow passwords."""
shadow = self.get_data_file('shadow')
users = {}
dupes = []
for line in shadow.splitlines():
user, encpw = line.split(":")[0:2]
if user in users:
dupes.append(user)
users[user] = encpw
jane_enc = "$5$iW$XsxmWCdpwIW8Yhv.Jn/R3uk6A4UaicfW5Xp7C9p9pg."
self.assertEqual([], dupes)
self.assertEqual(jane_enc, users['jane'])
mikey_enc = "$5$xZ$B2YGGEx2AOf4PeW48KC6.QyT1W2B4rZ9Qbltudtha89"
self.assertEqual(mikey_enc, users['mikey'])
# shadow entry is $N$salt$, so we encrypt with the same format
# and salt and expect the result.
tom = "mypassword123!"
fmtsalt = users['tom'][0:users['tom'].rfind("$") + 1]
tom_enc = crypt.crypt(tom, fmtsalt)
self.assertEqual(tom_enc, users['tom'])
harry_enc = ("$6$LF$9Z2p6rWK6TNC1DC6393ec0As.18KRAvKDbfsG"
"JEdWN3sRQRwpdfoh37EQ3yUh69tP4GSrGW5XKHxMLiKowJgm/")
dick_enc = "$1$ssisyfpf$YqvuJLfrrW6Cg/l53Pi1n1"
# these should have been changed to random values.
self.assertNotEqual(harry_enc, users['harry'])
self.assertTrue(users['harry'].startswith("$"))
self.assertNotEqual(dick_enc, users['dick'])
self.assertTrue(users['dick'].startswith("$"))
self.assertNotEqual(users['harry'], users['dick'])
def test_shadow_expected_users(self):
"""Test every tom, dick, and harry user in shadow."""
out = self.get_data_file('shadow')
self.assertIn('tom:', out)
self.assertIn('dick:', out)
self.assertIn('harry:', out)
self.assertIn('jane:', out)
self.assertIn('mikey:', out)
def test_sshd_config(self):
"""Test sshd config allows passwords."""
out = self.get_data_file('sshd_config')
self.assertIn('PasswordAuthentication yes', out)
# vi: ts=4 expandtab
|