summaryrefslogtreecommitdiff
path: root/tests/unittests/test_distros
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unittests/test_distros')
-rw-r--r--tests/unittests/test_distros/test_create_users.py151
-rw-r--r--tests/unittests/test_distros/test_generic.py2
-rw-r--r--tests/unittests/test_distros/test_hostname.py4
-rw-r--r--tests/unittests/test_distros/test_hosts.py4
-rw-r--r--tests/unittests/test_distros/test_netconfig.py4
-rw-r--r--tests/unittests/test_distros/test_resolv.py4
-rw-r--r--tests/unittests/test_distros/test_sysconfig.py4
-rwxr-xr-xtests/unittests/test_distros/test_user_data_normalize.py4
8 files changed, 177 insertions, 0 deletions
diff --git a/tests/unittests/test_distros/test_create_users.py b/tests/unittests/test_distros/test_create_users.py
new file mode 100644
index 00000000..9ded4f6c
--- /dev/null
+++ b/tests/unittests/test_distros/test_create_users.py
@@ -0,0 +1,151 @@
+# This file is part of cloud-init. See LICENSE file for license information.
+
+from cloudinit import distros
+from ..helpers import (TestCase, mock)
+
+
+class MyBaseDistro(distros.Distro):
+ # MyBaseDistro is here to test base Distro class implementations
+
+ def __init__(self, name="basedistro", cfg={}, paths={}):
+ super(MyBaseDistro, self).__init__(name, cfg, paths)
+
+ def install_packages(self, pkglist):
+ raise NotImplementedError()
+
+ def _write_network(self, settings):
+ raise NotImplementedError()
+
+ def package_command(self, cmd, args=None, pkgs=None):
+ raise NotImplementedError()
+
+ def update_package_sources(self):
+ raise NotImplementedError()
+
+ def apply_locale(self, locale, out_fn=None):
+ raise NotImplementedError()
+
+ def set_timezone(self, tz):
+ raise NotImplementedError()
+
+ def _read_hostname(self, filename, default=None):
+ raise NotImplementedError()
+
+ def _write_hostname(self, hostname, filename):
+ raise NotImplementedError()
+
+ def _read_system_hostname(self):
+ raise NotImplementedError()
+
+
+class TestCreateUser(TestCase):
+ def setUp(self):
+ super(TestCase, self).setUp()
+ self.dist = MyBaseDistro()
+
+ def _useradd2call(self, args):
+ # return a mock call for the useradd command in args
+ # with expected 'logstring'.
+ args = ['useradd'] + args
+ logcmd = [a for a in args]
+ for i in range(len(args)):
+ if args[i] in ('--password',):
+ logcmd[i + 1] = 'REDACTED'
+ return mock.call(args, logstring=logcmd)
+
+ @mock.patch("cloudinit.distros.util.subp")
+ def test_basic(self, m_subp):
+ user = 'foouser'
+ self.dist.create_user(user)
+ self.assertEqual(
+ m_subp.call_args_list,
+ [self._useradd2call([user, '-m']),
+ mock.call(['passwd', '-l', user])])
+
+ @mock.patch("cloudinit.distros.util.subp")
+ def test_no_home(self, m_subp):
+ user = 'foouser'
+ self.dist.create_user(user, no_create_home=True)
+ self.assertEqual(
+ m_subp.call_args_list,
+ [self._useradd2call([user, '-M']),
+ mock.call(['passwd', '-l', user])])
+
+ @mock.patch("cloudinit.distros.util.subp")
+ def test_system_user(self, m_subp):
+ # system user should have no home and get --system
+ user = 'foouser'
+ self.dist.create_user(user, system=True)
+ self.assertEqual(
+ m_subp.call_args_list,
+ [self._useradd2call([user, '--system', '-M']),
+ mock.call(['passwd', '-l', user])])
+
+ @mock.patch("cloudinit.distros.util.subp")
+ def test_explicit_no_home_false(self, m_subp):
+ user = 'foouser'
+ self.dist.create_user(user, no_create_home=False)
+ self.assertEqual(
+ m_subp.call_args_list,
+ [self._useradd2call([user, '-m']),
+ mock.call(['passwd', '-l', user])])
+
+ @mock.patch("cloudinit.distros.util.subp")
+ def test_unlocked(self, m_subp):
+ user = 'foouser'
+ self.dist.create_user(user, lock_passwd=False)
+ self.assertEqual(
+ m_subp.call_args_list,
+ [self._useradd2call([user, '-m'])])
+
+ @mock.patch("cloudinit.distros.util.subp")
+ def test_set_password(self, m_subp):
+ user = 'foouser'
+ password = 'passfoo'
+ self.dist.create_user(user, passwd=password)
+ self.assertEqual(
+ m_subp.call_args_list,
+ [self._useradd2call([user, '--password', password, '-m']),
+ mock.call(['passwd', '-l', user])])
+
+ @mock.patch("cloudinit.distros.util.is_group")
+ @mock.patch("cloudinit.distros.util.subp")
+ def test_group_added(self, m_subp, m_is_group):
+ m_is_group.return_value = False
+ user = 'foouser'
+ self.dist.create_user(user, groups=['group1'])
+ expected = [
+ mock.call(['groupadd', 'group1']),
+ self._useradd2call([user, '--groups', 'group1', '-m']),
+ mock.call(['passwd', '-l', user])]
+ self.assertEqual(m_subp.call_args_list, expected)
+
+ @mock.patch("cloudinit.distros.util.is_group")
+ @mock.patch("cloudinit.distros.util.subp")
+ def test_only_new_group_added(self, m_subp, m_is_group):
+ ex_groups = ['existing_group']
+ groups = ['group1', ex_groups[0]]
+ m_is_group.side_effect = lambda m: m in ex_groups
+ user = 'foouser'
+ self.dist.create_user(user, groups=groups)
+ expected = [
+ mock.call(['groupadd', 'group1']),
+ self._useradd2call([user, '--groups', ','.join(groups), '-m']),
+ mock.call(['passwd', '-l', user])]
+ self.assertEqual(m_subp.call_args_list, expected)
+
+ @mock.patch("cloudinit.distros.util.is_group")
+ @mock.patch("cloudinit.distros.util.subp")
+ def test_create_groups_with_whitespace_string(self, m_subp, m_is_group):
+ # groups supported as a comma delimeted string even with white space
+ m_is_group.return_value = False
+ user = 'foouser'
+ self.dist.create_user(user, groups='group1, group2')
+ expected = [
+ mock.call(['groupadd', 'group1']),
+ mock.call(['groupadd', 'group2']),
+ self._useradd2call([user, '--groups', 'group1,group2', '-m']),
+ mock.call(['passwd', '-l', user])]
+ self.assertEqual(m_subp.call_args_list, expected)
+
+# vi: ts=4 expandtab
diff --git a/tests/unittests/test_distros/test_generic.py b/tests/unittests/test_distros/test_generic.py
index 24ad115f..c9be277e 100644
--- a/tests/unittests/test_distros/test_generic.py
+++ b/tests/unittests/test_distros/test_generic.py
@@ -1,3 +1,5 @@
+# This file is part of cloud-init. See LICENSE file for license information.
+
from cloudinit import distros
from cloudinit import util
diff --git a/tests/unittests/test_distros/test_hostname.py b/tests/unittests/test_distros/test_hostname.py
index 5f28a868..f6d4dbe5 100644
--- a/tests/unittests/test_distros/test_hostname.py
+++ b/tests/unittests/test_distros/test_hostname.py
@@ -1,3 +1,5 @@
+# This file is part of cloud-init. See LICENSE file for license information.
+
import unittest
from cloudinit.distros.parsers import hostname
@@ -36,3 +38,5 @@ class TestHostnameHelper(unittest.TestCase):
bbbbd
'''
self.assertEqual(str(hn).strip(), expected_out.strip())
+
+# vi: ts=4 expandtab
diff --git a/tests/unittests/test_distros/test_hosts.py b/tests/unittests/test_distros/test_hosts.py
index ab867c6f..8aaa6e48 100644
--- a/tests/unittests/test_distros/test_hosts.py
+++ b/tests/unittests/test_distros/test_hosts.py
@@ -1,3 +1,5 @@
+# This file is part of cloud-init. See LICENSE file for license information.
+
import unittest
from cloudinit.distros.parsers import hosts
@@ -39,3 +41,5 @@ class TestHostsHelper(unittest.TestCase):
eh.del_entries('127.0.0.0')
self.assertEqual(eh.get_entry('127.0.0.0'), [])
+
+# vi: ts=4 expandtab
diff --git a/tests/unittests/test_distros/test_netconfig.py b/tests/unittests/test_distros/test_netconfig.py
index 36eae2dc..bde3bb50 100644
--- a/tests/unittests/test_distros/test_netconfig.py
+++ b/tests/unittests/test_distros/test_netconfig.py
@@ -1,3 +1,5 @@
+# This file is part of cloud-init. See LICENSE file for license information.
+
import os
from six import StringIO
@@ -379,3 +381,5 @@ ifconfig_vtnet0="DHCP"
'''
self.assertCfgEquals(expected_buf, str(write_buf))
self.assertEqual(write_buf.mode, 0o644)
+
+# vi: ts=4 expandtab
diff --git a/tests/unittests/test_distros/test_resolv.py b/tests/unittests/test_distros/test_resolv.py
index 9402b5ea..6b535a95 100644
--- a/tests/unittests/test_distros/test_resolv.py
+++ b/tests/unittests/test_distros/test_resolv.py
@@ -1,3 +1,5 @@
+# This file is part of cloud-init. See LICENSE file for license information.
+
from cloudinit.distros.parsers import resolv_conf
from cloudinit.distros import rhel_util
@@ -65,3 +67,5 @@ class TestResolvHelper(TestCase):
self.assertEqual(len(rp.search_domains), 6)
self.assertRaises(ValueError, rp.add_search_domain, 'bbb5.y.com')
self.assertEqual(len(rp.search_domains), 6)
+
+# vi: ts=4 expandtab
diff --git a/tests/unittests/test_distros/test_sysconfig.py b/tests/unittests/test_distros/test_sysconfig.py
index 8cb55522..235ecebb 100644
--- a/tests/unittests/test_distros/test_sysconfig.py
+++ b/tests/unittests/test_distros/test_sysconfig.py
@@ -1,3 +1,5 @@
+# This file is part of cloud-init. See LICENSE file for license information.
+
import re
from cloudinit.distros.parsers.sys_conf import SysConf
@@ -80,3 +82,5 @@ USEMD5=no'''
contents = str(conf)
self.assertIn("Z=d", contents)
self.assertIn("BLAH=b", contents)
+
+# vi: ts=4 expandtab
diff --git a/tests/unittests/test_distros/test_user_data_normalize.py b/tests/unittests/test_distros/test_user_data_normalize.py
index 33bf922d..88746e0a 100755
--- a/tests/unittests/test_distros/test_user_data_normalize.py
+++ b/tests/unittests/test_distros/test_user_data_normalize.py
@@ -1,3 +1,5 @@
+# This file is part of cloud-init. See LICENSE file for license information.
+
from cloudinit import distros
from cloudinit.distros import ug_util
from cloudinit import helpers
@@ -361,3 +363,5 @@ class TestUGNormalize(TestCase):
mock_subp.assert_any_call(groupcmd)
mock_subp.assert_any_call(addcmd, logstring=addcmd)
+
+# vi: ts=4 expandtab