Exemplo n.º 1
0
 def test_ntp_handler_real_distro_ntp_templates(self):
     """Test ntp handler renders the shipped distro ntp client templates."""
     pools = ['0.mycompany.pool.ntp.org', '3.mycompany.pool.ntp.org']
     servers = ['192.168.23.3', '192.168.23.4']
     for client in ['ntp', 'systemd-timesyncd', 'chrony']:
         for distro in cc_ntp.distros:
             distro_cfg = cc_ntp.distro_ntp_client_configs(distro)
             ntpclient = distro_cfg[client]
             confpath = (
                 os.path.join(self.new_root, ntpclient.get('confpath')[1:]))
             template = ntpclient.get('template_name')
             # find sourcetree template file
             root_dir = (
                 dirname(dirname(os.path.realpath(util.__file__))) +
                 '/templates')
             source_fn = self._get_template_path(template, distro,
                                                 basepath=root_dir)
             template_fn = self._get_template_path(template, distro)
             # don't fail if cloud-init doesn't have a template for
             # a distro,client pair
             if not os.path.exists(source_fn):
                 continue
             # Create a copy in our tmp_dir
             shutil.copy(source_fn, template_fn)
             cc_ntp.write_ntp_config_template(distro, servers=servers,
                                              pools=pools, path=confpath,
                                              template_fn=template_fn)
             content = util.load_file(confpath)
             if client in ['ntp', 'chrony']:
                 content_lines = content.splitlines()
                 expected_servers = self._get_expected_servers(servers,
                                                               distro,
                                                               client)
                 print('distro=%s client=%s' % (distro, client))
                 for sline in expected_servers:
                     self.assertIn(sline, content_lines,
                                   ('failed to render {0} conf'
                                    ' for distro:{1}'.format(client,
                                                             distro)))
                 expected_pools = self._get_expected_pools(pools, distro,
                                                           client)
                 if expected_pools != []:
                     for pline in expected_pools:
                         self.assertIn(pline, content_lines,
                                       ('failed to render {0} conf'
                                        ' for distro:{1}'.format(client,
                                                                 distro)))
             elif client == 'systemd-timesyncd':
                 expected_servers = self._get_expected_servers(servers,
                                                               distro,
                                                               client)
                 expected_pools = self._get_expected_pools(pools,
                                                           distro,
                                                           client)
                 expected_content = (
                     "# cloud-init generated file\n" +
                     "# See timesyncd.conf(5) for details.\n\n" +
                     "[Time]\nNTP=%s %s \n" % (expected_servers,
                                               expected_pools))
                 self.assertEqual(expected_content, content)
Exemplo n.º 2
0
 def test_distro_ntp_client_configs(self):
     """Test we have updated ntp client configs on different distros"""
     delta = copy.deepcopy(cc_ntp.DISTRO_CLIENT_CONFIG)
     base = copy.deepcopy(cc_ntp.NTP_CLIENT_CONFIG)
     # confirm no-delta distros match the base config
     for distro in cc_ntp.distros:
         if distro not in delta:
             result = cc_ntp.distro_ntp_client_configs(distro)
             self.assertEqual(base, result)
     # for distros with delta, ensure the merged config values match
     # what is set in the delta
     for distro in delta.keys():
         result = cc_ntp.distro_ntp_client_configs(distro)
         for client in delta[distro].keys():
             for key in delta[distro][client].keys():
                 self.assertEqual(delta[distro][client][key],
                                  result[client][key])
Exemplo n.º 3
0
 def test_ntp_system_config_overrides_distro_builtin_clients(self, m_which):
     """Test distro system_config overrides builtin preferred ntp clients"""
     system_client = 'chrony'
     sys_cfg = {'ntp_client': system_client}
     # no clients installed
     m_which.return_value = None
     for distro in cc_ntp.distros:
         mycloud = self._get_cloud(distro, sys_cfg=sys_cfg)
         distro_configs = cc_ntp.distro_ntp_client_configs(distro)
         expected_cfg = distro_configs[system_client]
         result = cc_ntp.select_ntp_client(None, mycloud.distro)
         self.assertEqual(sorted(expected_cfg), sorted(result))
         m_which.assert_has_calls([])
Exemplo n.º 4
0
 def test_ntp_user_config_overrides_system_cfg(self, m_which):
     """Test user-data overrides system_config ntp_client"""
     system_client = 'chrony'
     sys_cfg = {'ntp_client': system_client}
     user_client = 'systemd-timesyncd'
     # no clients installed
     m_which.return_value = None
     for distro in cc_ntp.distros:
         mycloud = self._get_cloud(distro, sys_cfg=sys_cfg)
         distro_configs = cc_ntp.distro_ntp_client_configs(distro)
         expected_cfg = distro_configs[user_client]
         result = cc_ntp.select_ntp_client(user_client, mycloud.distro)
         self.assertEqual(sorted(expected_cfg), sorted(result))
         m_which.assert_has_calls([])
Exemplo n.º 5
0
 def _mock_ntp_client_config(self, client=None, distro=None):
     if not client:
         client = 'ntp'
     if not distro:
         distro = 'ubuntu'
     dcfg = cc_ntp.distro_ntp_client_configs(distro)
     if client == 'systemd-timesyncd':
         template = TIMESYNCD_TEMPLATE
     else:
         template = NTP_TEMPLATE
     (confpath, _template_fn) = self._generate_template(template=template)
     ntpconfig = copy.deepcopy(dcfg[client])
     ntpconfig['confpath'] = confpath
     ntpconfig['template_name'] = os.path.basename(confpath)
     return ntpconfig
Exemplo n.º 6
0
 def test_user_cfg_ntp_client_auto_uses_distro_clients(self, m_which):
     """Test user_cfg.ntp_client='auto' defaults to distro search"""
     # nothing is installed
     m_which.return_value = None
     for distro in cc_ntp.distros:
         mycloud = self._get_cloud(distro)
         distro_configs = cc_ntp.distro_ntp_client_configs(distro)
         expected_client = mycloud.distro.preferred_ntp_clients[0]
         expected_cfg = distro_configs[expected_client]
         expected_calls = []
         for client in mycloud.distro.preferred_ntp_clients:
             cfg = distro_configs[client]
             expected_calls.append(mock.call(cfg['check_exe']))
         cc_ntp.select_ntp_client('auto', mycloud.distro)
         m_which.assert_has_calls(expected_calls)
         self.assertEqual(sorted(expected_cfg), sorted(cfg))
Exemplo n.º 7
0
 def test_ntp_distro_searches_all_preferred_clients(self, m_which):
     """Test select_ntp_client search all distro perferred clients"""
     # nothing is installed
     m_which.return_value = None
     for distro in cc_ntp.distros:
         mycloud = self._get_cloud(distro)
         distro_configs = cc_ntp.distro_ntp_client_configs(distro)
         expected_client = mycloud.distro.preferred_ntp_clients[0]
         expected_cfg = distro_configs[expected_client]
         expected_calls = []
         for client in mycloud.distro.preferred_ntp_clients:
             cfg = distro_configs[client]
             expected_calls.append(mock.call(cfg["check_exe"]))
         cc_ntp.select_ntp_client({}, mycloud.distro)
         m_which.assert_has_calls(expected_calls)
         self.assertEqual(sorted(expected_cfg), sorted(cfg))
Exemplo n.º 8
0
    def test_snappy_system_picks_timesyncd(self, m_which):
        """Test snappy systems prefer installed clients"""

        # we are on ubuntu-core here
        self.m_snappy.return_value = True

        # ubuntu core systems will have timesyncd installed
        m_which.side_effect = iter(
            [None, '/lib/systemd/systemd-timesyncd', None, None, None])
        distro = 'ubuntu'
        mycloud = self._get_cloud(distro)
        distro_configs = cc_ntp.distro_ntp_client_configs(distro)
        expected_client = 'systemd-timesyncd'
        expected_cfg = distro_configs[expected_client]
        expected_calls = []
        # we only get to timesyncd
        for client in mycloud.distro.preferred_ntp_clients[0:2]:
            cfg = distro_configs[client]
            expected_calls.append(mock.call(cfg['check_exe']))
        result = cc_ntp.select_ntp_client(None, mycloud.distro)
        m_which.assert_has_calls(expected_calls)
        self.assertEqual(sorted(expected_cfg), sorted(cfg))
        self.assertEqual(sorted(expected_cfg), sorted(result))