def test_ntp_handler_schema_validation_allows_empty_ntp_config(self, m_select): """Ntp schema validation allows for an empty ntp: configuration.""" valid_empty_configs = [{'ntp': {}}, {'ntp': None}] for valid_empty_config in valid_empty_configs: for distro in cc_ntp.distros: mycloud = self._get_cloud(distro) ntpconfig = self._mock_ntp_client_config(distro=distro) confpath = ntpconfig['confpath'] m_select.return_value = ntpconfig cc_ntp.handle('cc_ntp', valid_empty_config, mycloud, None, []) if distro == 'alpine': # _mock_ntp_client_config call above did not specify a # client value and so it defaults to "ntp" which on # Alpine Linux only supports servers and not pools. servers = cc_ntp.generate_server_names(mycloud.distro.name) self.assertEqual( "servers {0}\npools []\n".format(servers), util.load_file(confpath)) else: pools = cc_ntp.generate_server_names(mycloud.distro.name) self.assertEqual( "servers []\npools {0}\n".format(pools), util.load_file(confpath)) self.assertNotIn('Invalid config:', self.logs.getvalue())
def test_ntp_the_whole_package(self, m_sysd, m_select): """Test enabled config renders template, and restarts service """ cfg = {'ntp': {'enabled': True}} for distro in cc_ntp.distros: mycloud = self._get_cloud(distro) ntpconfig = self._mock_ntp_client_config(distro=distro) confpath = ntpconfig['confpath'] service_name = ntpconfig['service_name'] m_select.return_value = ntpconfig pools = cc_ntp.generate_server_names(mycloud.distro.name) # force uses systemd path m_sysd.return_value = True with mock.patch('cloudinit.config.cc_ntp.util') as m_util: # allow use of util.mergemanydict m_util.mergemanydict.side_effect = util.mergemanydict # default client is present m_util.which.return_value = True # use the config 'enabled' value m_util.is_false.return_value = util.is_false( cfg['ntp']['enabled']) cc_ntp.handle('notimportant', cfg, mycloud, None, None) m_util.subp.assert_called_with( ['systemctl', 'reload-or-restart', service_name], capture=True) self.assertEqual("servers []\npools {0}\n".format(pools), util.load_file(confpath))
def test_defaults_pools_empty_lists_sles(self): """write_ntp_config_template defaults opensuse pools upon empty config. When both pools and servers are empty, default NR_POOL_SERVERS get configured. """ distro = "sles" default_pools = cc_ntp.generate_server_names(distro) (confpath, template_fn) = self._generate_template() cc_ntp.write_ntp_config_template( distro, servers=[], pools=[], path=confpath, template_fn=template_fn, template=None, ) for pool in default_pools: self.assertIn("opensuse", pool) self.assertEqual( "servers []\npools {0}\n".format(default_pools), util.load_file(confpath), ) self.assertIn( "Adding distro default ntp pool servers: {0}".format( ",".join(default_pools)), self.logs.getvalue(), )
def test_ntp_handler_schema_validation_allows_empty_ntp_config( self, m_select): """Ntp schema validation allows for an empty ntp: configuration.""" valid_empty_configs = [{'ntp': {}}, {'ntp': None}] for valid_empty_config in valid_empty_configs: for distro in cc_ntp.distros: mycloud = self._get_cloud(distro) ntpconfig = self._mock_ntp_client_config(distro=distro) confpath = ntpconfig['confpath'] m_select.return_value = ntpconfig cc_ntp.handle('cc_ntp', valid_empty_config, mycloud, None, []) pools = cc_ntp.generate_server_names(mycloud.distro.name) self.assertEqual("servers []\npools {0}\n".format(pools), util.load_file(confpath)) self.assertNotIn('Invalid config:', self.logs.getvalue())
def ntp_conf_render(self, distro): """ntp_conf_render Test rendering of a ntp.conf from template for a given distro """ cfg = {'ntp': {}} mycloud = self._get_cloud(distro) distro_names = cc_ntp.generate_server_names(distro) with mock.patch.object(templater, 'render_to_file') as mocktmpl: with mock.patch.object(os.path, 'isfile', return_value=True): with mock.patch.object(util, 'rename'): cc_ntp.write_ntp_config_template(cfg, mycloud) mocktmpl.assert_called_once_with( ('/etc/cloud/templates/ntp.conf.%s.tmpl' % distro), '/etc/ntp.conf', {'servers': [], 'pools': distro_names})
def test_write_ntp_config_template_defaults_pools_w_empty_lists(self): """write_ntp_config_template defaults pools servers upon empty config. When both pools and servers are empty, default NR_POOL_SERVERS get configured. """ distro = 'ubuntu' pools = cc_ntp.generate_server_names(distro) servers = [] (confpath, template_fn) = self._generate_template() mock_path = 'cloudinit.config.cc_ntp.temp_utils._TMPDIR' with mock.patch(mock_path, self.new_root): cc_ntp.write_ntp_config_template(distro, servers=servers, pools=pools, path=confpath, template_fn=template_fn, template=None) self.assertEqual("servers []\npools {0}\n".format(pools), util.load_file(confpath))
def ntp_conf_render(self, distro): """ntp_conf_render Test rendering of a ntp.conf from template for a given distro """ cfg = {'ntp': {}} mycloud = self._get_cloud(distro) distro_names = cc_ntp.generate_server_names(distro) with mock.patch.object(templater, 'render_to_file') as mocktmpl: with mock.patch.object(os.path, 'isfile', return_value=True): with mock.patch.object(util, 'rename'): cc_ntp.write_ntp_config_template(cfg, mycloud) mocktmpl.assert_called_once_with( ('/etc/cloud/templates/ntp.conf.%s.tmpl' % distro), '/etc/ntp.conf', { 'servers': [], 'pools': distro_names })
def test_ntp_the_whole_package(self, m_sysd, m_select, m_subp, m_dsubp): """Test enabled config renders template, and restarts service""" cfg = {"ntp": {"enabled": True}} for distro in cc_ntp.distros: mycloud = self._get_cloud(distro) ntpconfig = self._mock_ntp_client_config(distro=distro) confpath = ntpconfig["confpath"] service_name = ntpconfig["service_name"] m_select.return_value = ntpconfig hosts = cc_ntp.generate_server_names(mycloud.distro.name) uses_systemd = True expected_service_call = [ "systemctl", "reload-or-restart", service_name, ] expected_content = "servers []\npools {0}\n".format(hosts) if distro == "alpine": uses_systemd = False expected_service_call = ["rc-service", service_name, "restart"] # _mock_ntp_client_config call above did not specify a client # value and so it defaults to "ntp" which on Alpine Linux only # supports servers and not pools. expected_content = "servers {0}\npools []\n".format(hosts) m_sysd.return_value = uses_systemd with mock.patch("cloudinit.config.cc_ntp.util") as m_util: # allow use of util.mergemanydict m_util.mergemanydict.side_effect = util.mergemanydict # default client is present m_subp.which.return_value = True # use the config 'enabled' value m_util.is_false.return_value = util.is_false( cfg["ntp"]["enabled"]) cc_ntp.handle("notimportant", cfg, mycloud, None, None) m_dsubp.subp.assert_called_with(expected_service_call, capture=True) self.assertEqual(expected_content, util.load_file(confpath))
def test_defaults_pools_empty_lists_sles(self): """write_ntp_config_template defaults opensuse pools upon empty config. When both pools and servers are empty, default NR_POOL_SERVERS get configured. """ distro = 'sles' default_pools = cc_ntp.generate_server_names(distro) (confpath, template_fn) = self._generate_template() cc_ntp.write_ntp_config_template(distro, servers=[], pools=[], path=confpath, template_fn=template_fn, template=None) content = util.read_file_or_url('file://' + confpath).contents for pool in default_pools: self.assertIn('opensuse', pool) self.assertEqual("servers []\npools {0}\n".format(default_pools), content.decode()) self.assertIn( "Adding distro default ntp pool servers: {0}".format( ",".join(default_pools)), self.logs.getvalue())