def test_skips_missing_puppet_key_in_cloudconfig(self, m_auto): """Cloud-config containing no 'puppet' key is skipped.""" cfg = {} cc_puppet.handle('notimportant', cfg, self.cloud, LOG, None) self.assertIn("no 'puppet' configuration found", self.logs.getvalue()) self.assertEqual(0, m_auto.call_count)
def test_handler_puppet_writes_csr_attributes_file(self, m_subp, m_auto): """When csr_attributes is provided creates file in PUPPET_CSR_ATTRIBUTES_PATH.""" mycloud = self._get_cloud('ubuntu') mycloud.distro = mock.MagicMock() cfg = { 'puppet': { 'csr_attributes': { 'custom_attributes': { '1.2.840.113549.1.9.7': '342thbjkt82094y0uthhor289jnqthpc2290' }, 'extension_requests': { 'pp_uuid': 'ED803750-E3C7-44F5-BB08-41A04433FE2E', 'pp_image_name': 'my_ami_image', 'pp_preshared_key': '342thbjkt82094y0uthhor289jnqthpc2290' } } } } csr_attributes = 'cloudinit.config.cc_puppet.' \ 'PUPPET_CSR_ATTRIBUTES_PATH' with mock.patch(csr_attributes, self.csr_attributes_path): cc_puppet.handle('notimportant', cfg, mycloud, LOG, None) content = util.load_file(self.csr_attributes_path) expected = textwrap.dedent("""\ custom_attributes: 1.2.840.113549.1.9.7: 342thbjkt82094y0uthhor289jnqthpc2290 extension_requests: pp_image_name: my_ami_image pp_preshared_key: 342thbjkt82094y0uthhor289jnqthpc2290 pp_uuid: ED803750-E3C7-44F5-BB08-41A04433FE2E """) self.assertEqual(expected, content)
def test_puppet_config_installs_puppet_aio(self, m_subp, m_aio, _): """Cloud-config with 'puppet' key installs when 'install_type' is 'aio'.""" self.cloud.distro = mock.MagicMock() cfg = {'puppet': {'install': True, 'install_type': 'aio'}} cc_puppet.handle('notimportant', cfg, self.cloud, LOG, None) m_aio.assert_called_with(cc_puppet.AIO_INSTALL_URL, None, None, True)
def test_puppet_config_installs_puppet_version(self, m_subp, _): """Cloud-config 'puppet' configuration can specify a version.""" self.cloud.distro = mock.MagicMock() cfg = {'puppet': {'version': '3.8'}} cc_puppet.handle('notimportant', cfg, self.cloud, LOG, None) self.assertEqual([mock.call(('puppet', '3.8'))], self.cloud.distro.install_packages.call_args_list)
def test_handler_skips_missing_puppet_key_in_cloudconfig(self, m_auto): """Cloud-config containing no 'puppet' key is skipped.""" mycloud = self._get_cloud('ubuntu') cfg = {} cc_puppet.handle('notimportant', cfg, mycloud, LOG, None) self.assertIn( "no 'puppet' configuration found", self.logs.getvalue()) self.assertEqual(0, m_auto.call_count)
def test_empty_puppet_config_installs_puppet(self, m_subp, m_auto): """Cloud-config empty 'puppet' configuration installs latest puppet.""" self.cloud.distro = mock.MagicMock() cfg = {'puppet': {}} cc_puppet.handle('notimportant', cfg, self.cloud, LOG, None) self.assertEqual([mock.call(('puppet', None))], self.cloud.distro.install_packages.call_args_list)
def test_puppet_config_installs_puppet_on_true(self, m_subp, _): """Cloud-config with 'puppet' key installs when 'install' is True.""" self.cloud.distro = mock.MagicMock() cfg = {'puppet': {'install': True}} cc_puppet.handle('notimportant', cfg, self.cloud, LOG, None) self.assertEqual([mock.call(('puppet', None))], self.cloud.distro.install_packages.call_args_list)
def test_puppet_runs_puppet_if_requested(self, m_subp, m_auto): """Run puppet with default args if 'exec' is set to True.""" cfg = {'puppet': {'exec': True}} cc_puppet.handle('notimportant', cfg, self.cloud, LOG, None) self.assertEqual(1, m_auto.call_count) self.assertIn( [mock.call(['puppet', 'agent', '--test'], capture=False)], m_subp.call_args_list)
def test_puppet_skips_puppetd(self, m_subp, m_auto): """Run puppet with default args if 'exec' is set to True.""" cfg = {'puppet': {'start_service': False}} cc_puppet.handle('notimportant', cfg, self.cloud, LOG, None) self.assertEqual(0, m_auto.call_count) self.assertNotIn( [mock.call(['service', 'puppet', 'start'], capture=False)], m_subp.call_args_list)
def test_puppet_config_starts_puppet_service(self, m_subp, m_auto): """Cloud-config 'puppet' configuration starts puppet.""" cfg = {'puppet': {'install': False}} cc_puppet.handle('notimportant', cfg, self.cloud, LOG, None) self.assertEqual(1, m_auto.call_count) self.assertIn( [mock.call(['service', 'puppet', 'start'], capture=False)], m_subp.call_args_list)
def test_handler_empty_puppet_config_installs_puppet(self, m_subp, m_auto): """Cloud-config empty 'puppet' configuration installs latest puppet.""" mycloud = self._get_cloud('ubuntu') mycloud.distro = mock.MagicMock() cfg = {'puppet': {}} cc_puppet.handle('notimportant', cfg, mycloud, LOG, None) self.assertEqual( [mock.call(('puppet', None))], mycloud.distro.install_packages.call_args_list)
def test_handler_puppet_config_starts_puppet_service(self, m_subp, m_auto): """Cloud-config 'puppet' configuration starts puppet.""" mycloud = self._get_cloud('ubuntu') cfg = {'puppet': {'install': False}} cc_puppet.handle('notimportant', cfg, mycloud, LOG, None) self.assertEqual(1, m_auto.call_count) self.assertEqual( [mock.call(['service', 'puppet', 'start'], capture=False)], m_subp.call_args_list)
def test_handler_puppet_config_installs_puppet_version(self, m_subp, _): """Cloud-config 'puppet' configuration can specify a version.""" mycloud = self._get_cloud('ubuntu') mycloud.distro = mock.MagicMock() cfg = {'puppet': {'version': '3.8'}} cc_puppet.handle('notimportant', cfg, mycloud, LOG, None) self.assertEqual( [mock.call(('puppet', '3.8'))], mycloud.distro.install_packages.call_args_list)
def test_handler_puppet_config_installs_puppet_on_true(self, m_subp, _): """Cloud-config with 'puppet' key installs when 'install' is True.""" mycloud = self._get_cloud('ubuntu') mycloud.distro = mock.MagicMock() cfg = {'puppet': {'install': True}} cc_puppet.handle('notimportant', cfg, mycloud, LOG, None) self.assertEqual( [mock.call(('puppet', None))], mycloud.distro.install_packages.call_args_list)
def test_puppet_config_starts_puppet_service(self, m_subp, m_auto): """Cloud-config 'puppet' configuration starts puppet.""" cfg = {"puppet": {"install": False}} cc_puppet.handle("notimportant", cfg, self.cloud, LOG, None) self.assertEqual(1, m_auto.call_count) self.assertIn( [mock.call(["service", "puppet", "start"], capture=False)], m_subp.call_args_list, )
def test_puppet_skips_puppetd(self, m_subp, m_auto): """Run puppet with default args if 'exec' is set to True.""" cfg = {"puppet": {"start_service": False}} cc_puppet.handle("notimportant", cfg, self.cloud, LOG, None) self.assertEqual(0, m_auto.call_count) self.assertNotIn( [mock.call(["service", "puppet", "start"], capture=False)], m_subp.call_args_list, )
def test_puppet_runs_puppet_if_requested(self, m_subp, m_auto): """Run puppet with default args if 'exec' is set to True.""" cfg = {"puppet": {"exec": True}} cc_puppet.handle("notimportant", cfg, self.cloud, LOG, None) self.assertEqual(1, m_auto.call_count) self.assertIn( [mock.call(["puppet", "agent", "--test"], capture=False)], m_subp.call_args_list, )
def test_puppet_config_installs_puppet_version(self, m_subp, _): """Cloud-config 'puppet' configuration can specify a version.""" self.cloud.distro = mock.MagicMock() cfg = {"puppet": {"version": "3.8"}} cc_puppet.handle("notimportant", cfg, self.cloud, LOG, None) self.assertEqual( [mock.call(("puppet", "3.8"))], self.cloud.distro.install_packages.call_args_list, )
def test_handler_puppet_config_updates_puppet_conf(self, m_subp, m_auto): """When 'conf' is provided update values in PUPPET_CONF_PATH.""" mycloud = self._get_cloud('ubuntu') cfg = { 'puppet': { 'conf': {'agent': {'server': 'puppetmaster.example.org'}}}} util.write_file(self.conf, '[agent]\nserver = origpuppet\nother = 3') puppet_conf_path = 'cloudinit.config.cc_puppet.PUPPET_CONF_PATH' mycloud.distro = mock.MagicMock() with mock.patch(puppet_conf_path, self.conf): cc_puppet.handle('notimportant', cfg, mycloud, LOG, None) content = util.load_file(self.conf) expected = '[agent]\nserver = puppetmaster.example.org\nother = 3\n\n' self.assertEqual(expected, content)
def test_handler_puppet_config_updates_puppet_conf(self, m_subp, m_auto): """When 'conf' is provided update values in PUPPET_CONF_PATH.""" mycloud = self._get_cloud('ubuntu') cfg = { 'puppet': { 'conf': {'agent': {'server': 'puppetmaster.example.org'}}}} util.write_file(self.conf, '[agent]\nserver = origpuppet\nother = 3') puppet_conf_path = 'cloudinit.config.cc_puppet.PUPPET_CONF_PATH' mycloud.distro = mock.MagicMock() with mock.patch(puppet_conf_path, self.conf): cc_puppet.handle('notimportant', cfg, mycloud, LOG, None) content = util.load_file(self.conf) expected = '[agent]\nserver = puppetmaster.example.org\nother = 3\n\n' self.assertEqual(expected, content)
def test_puppet_config_installs_puppet_aio_without_cleanup( self, m_subp, m_aio, _): """Cloud-config with 'puppet' key installs when 'install_type' is 'aio' and no cleanup.""" mycloud = self._get_cloud('ubuntu') mycloud.distro = mock.MagicMock() cfg = { 'puppet': { 'install': True, 'cleanup': False, 'install_type': 'aio' } } cc_puppet.handle('notimportant', cfg, mycloud, LOG, None) m_aio.assert_called_with(cc_puppet.AIO_INSTALL_URL, None, None, False)
def test_puppet_config_installs_puppet_aio_without_cleanup( self, m_subp, m_aio, _): """Cloud-config with 'puppet' key installs when 'install_type' is 'aio' and no cleanup.""" self.cloud.distro = mock.MagicMock() cfg = { "puppet": { "install": True, "cleanup": False, "install_type": "aio", } } cc_puppet.handle("notimportant", cfg, self.cloud, LOG, None) m_aio.assert_called_with(cc_puppet.AIO_INSTALL_URL, None, None, False)
def test_puppet_config_installs_puppet_aio_with_collection( self, m_subp, m_aio, _): """Cloud-config with 'puppet' key installs when 'install_type' is 'aio' and 'collection' is specified.""" mycloud = self._get_cloud('ubuntu') mycloud.distro = mock.MagicMock() cfg = { 'puppet': { 'install': True, 'collection': 'puppet6', 'install_type': 'aio' } } cc_puppet.handle('notimportant', cfg, mycloud, LOG, None) m_aio.assert_called_with(cc_puppet.AIO_INSTALL_URL, None, 'puppet6', True)
def test_puppet_runs_puppet_with_args_string_if_requested( self, m_subp, m_auto): """Run puppet with 'exec_args' string if 'exec' is set to True.""" cfg = { 'puppet': { 'exec': True, 'exec_args': '--onetime --detailed-exitcodes' } } cc_puppet.handle('notimportant', cfg, self.cloud, LOG, None) self.assertEqual(1, m_auto.call_count) self.assertIn([ mock.call(['puppet', 'agent', '--onetime', '--detailed-exitcodes'], capture=False) ], m_subp.call_args_list)
def test_puppet_config_installs_puppet_aio_with_collection( self, m_subp, m_aio, _): """Cloud-config with 'puppet' key installs when 'install_type' is 'aio' and 'collection' is specified.""" self.cloud.distro = mock.MagicMock() cfg = { "puppet": { "install": True, "collection": "puppet6", "install_type": "aio", } } cc_puppet.handle("notimportant", cfg, self.cloud, LOG, None) m_aio.assert_called_with(cc_puppet.AIO_INSTALL_URL, None, "puppet6", True)
def test_puppet_config_installs_puppet_aio_with_custom_url( self, m_subp, m_aio, _): """Cloud-config with 'puppet' key installs when 'install_type' is 'aio' and 'aio_install_url' is specified.""" self.cloud.distro = mock.MagicMock() cfg = { "puppet": { "install": True, "aio_install_url": "http://test.url/path/to/script.sh", "install_type": "aio", } } cc_puppet.handle("notimportant", cfg, self.cloud, LOG, None) m_aio.assert_called_with("http://test.url/path/to/script.sh", None, None, True)
def test_puppet_config_installs_puppet_aio_with_custom_url( self, m_subp, m_aio, _): """Cloud-config with 'puppet' key installs when 'install_type' is 'aio' and 'aio_install_url' is specified.""" self.cloud.distro = mock.MagicMock() cfg = { 'puppet': { 'install': True, 'aio_install_url': 'http://test.url/path/to/script.sh', 'install_type': 'aio' } } cc_puppet.handle('notimportant', cfg, self.cloud, LOG, None) m_aio.assert_called_with('http://test.url/path/to/script.sh', None, None, True)
def test_puppet_writes_csr_attributes_file( self, m_subp, m_default, m_auto ): """When csr_attributes is provided creates file in PUPPET_CSR_ATTRIBUTES_PATH.""" def _fake_get_config_value(puppet_bin, setting): return self.csr_attributes_path m_default.side_effect = _fake_get_config_value self.cloud.distro = mock.MagicMock() cfg = { "puppet": { "csr_attributes": { "custom_attributes": { "1.2.840.113549.1.9.7": ( "342thbjkt82094y0uthhor289jnqthpc2290" ) }, "extension_requests": { "pp_uuid": "ED803750-E3C7-44F5-BB08-41A04433FE2E", "pp_image_name": "my_ami_image", "pp_preshared_key": ( "342thbjkt82094y0uthhor289jnqthpc2290" ), }, } } } cc_puppet.handle("notimportant", cfg, self.cloud, LOG, None) content = util.load_file(self.csr_attributes_path) expected = textwrap.dedent( """\ custom_attributes: 1.2.840.113549.1.9.7: 342thbjkt82094y0uthhor289jnqthpc2290 extension_requests: pp_image_name: my_ami_image pp_preshared_key: 342thbjkt82094y0uthhor289jnqthpc2290 pp_uuid: ED803750-E3C7-44F5-BB08-41A04433FE2E """ ) self.assertEqual(expected, content)
def test_puppet_config_updates_puppet_conf( self, m_subp, m_default, m_auto ): """When 'conf' is provided update values in PUPPET_CONF_PATH.""" def _fake_get_config_value(puppet_bin, setting): return self.conf m_default.side_effect = _fake_get_config_value cfg = { "puppet": { "conf": {"agent": {"server": "puppetserver.example.org"}} } } util.write_file(self.conf, "[agent]\nserver = origpuppet\nother = 3") self.cloud.distro = mock.MagicMock() cc_puppet.handle("notimportant", cfg, self.cloud, LOG, None) content = util.load_file(self.conf) expected = "[agent]\nserver = puppetserver.example.org\nother = 3\n\n" self.assertEqual(expected, content)
def test_puppet_runs_puppet_with_args_string_if_requested( self, m_subp, m_auto): """Run puppet with 'exec_args' string if 'exec' is set to True.""" cfg = { "puppet": { "exec": True, "exec_args": "--onetime --detailed-exitcodes", } } cc_puppet.handle("notimportant", cfg, self.cloud, LOG, None) self.assertEqual(1, m_auto.call_count) self.assertIn( [ mock.call( ["puppet", "agent", "--onetime", "--detailed-exitcodes"], capture=False, ) ], m_subp.call_args_list, )
def test_puppet_config_updates_puppet_conf(self, m_subp, m_default, m_auto): """When 'conf' is provided update values in PUPPET_CONF_PATH.""" def _fake_get_config_value(puppet_bin, setting): return self.conf m_default.side_effect = _fake_get_config_value mycloud = self._get_cloud('ubuntu') cfg = { 'puppet': { 'conf': { 'agent': { 'server': 'puppetserver.example.org' } } } } util.write_file(self.conf, '[agent]\nserver = origpuppet\nother = 3') mycloud.distro = mock.MagicMock() cc_puppet.handle('notimportant', cfg, mycloud, LOG, None) content = util.load_file(self.conf) expected = '[agent]\nserver = puppetserver.example.org\nother = 3\n\n' self.assertEqual(expected, content)