def test_read_file_or_url_passes_params_to_readurl( self, m_readurl, timeout ): """read_file_or_url passes all params through to readurl.""" url = "http://hostname/path" response = "This is my url content\n" m_readurl.return_value = response params = { "url": url, "timeout": timeout, "retries": 2, "headers": {"somehdr": "val"}, "data": "data", "sec_between": 1, "ssl_details": {"cert_file": "/path/cert.pem"}, "headers_cb": "headers_cb", "exception_cb": "exception_cb", } assert response == read_file_or_url(**params) params.pop("url") # url is passed in as a positional arg assert m_readurl.call_args_list == [mock.call(url, **params)]
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))
def test_read_file_or_url_passes_params_to_readurl(self, m_readurl): """read_file_or_url passes all params through to readurl.""" url = 'http://hostname/path' response = 'This is my url content\n' m_readurl.return_value = response params = { 'url': url, 'timeout': 1, 'retries': 2, 'headers': { 'somehdr': 'val' }, 'data': 'data', 'sec_between': 1, 'ssl_details': { 'cert_file': '/path/cert.pem' }, 'headers_cb': 'headers_cb', 'exception_cb': 'exception_cb' } self.assertEqual(response, read_file_or_url(**params)) params.pop('url') # url is passed in as a positional arg self.assertEqual([mock.call(url, **params)], m_readurl.call_args_list)
def test_dhcp_discovery_run_in_sandbox_waits_on_lease_and_pid( self, m_subp, m_wait, m_kill, m_getppid): """dhcp_discovery waits for the presence of pidfile and dhcp.leases.""" m_subp.return_value = ("", "") tmpdir = self.tmp_dir() dhclient_script = os.path.join(tmpdir, "dhclient.orig") script_content = "#!/bin/bash\necho fake-dhclient" write_file(dhclient_script, script_content, mode=0o755) # Don't create pid or leases file pidfile = self.tmp_path("dhclient.pid", tmpdir) leasefile = self.tmp_path("dhcp.leases", tmpdir) m_wait.return_value = [pidfile] # Return the missing pidfile wait for m_getppid.return_value = 1 # Indicate that dhclient has daemonized self.assertEqual([], dhcp_discovery(dhclient_script, "eth9", tmpdir)) self.assertEqual( mock.call([pidfile, leasefile], maxwait=5, naplen=0.01), m_wait.call_args_list[0], ) self.assertIn( "WARNING: dhclient did not produce expected files: dhclient.pid", self.logs.getvalue(), ) m_kill.assert_not_called()
def test_full_registration(self, m_sman_cli): """ Registration with auto-attach, service-level, adding pools, and enabling and disabling yum repos """ call_lists = [] call_lists.append(["attach", "--pool=pool1", "--pool=pool3"]) call_lists.append( ["repos", "--disable=repo5", "--enable=repo2", "--enable=repo3"] ) call_lists.append(["attach", "--auto", "--servicelevel=self-support"]) reg = ( "The system has been registered with ID:" " 12345678-abde-abcde-1234-1234567890abc" ) m_sman_cli.side_effect = [ subp.ProcessExecutionError, (reg, "bar"), ("Service level set to: self-support", ""), ("pool1\npool3\n", ""), ("pool2\n", ""), ("", ""), ("Repo ID: repo1\nRepo ID: repo5\n", ""), ("Repo ID: repo2\nRepo ID: repo3\nRepo ID: repo4", ""), ("", ""), ] self.handle( self.name, self.config_full, self.cloud_init, self.log, self.args ) self.assertEqual(m_sman_cli.call_count, 9) for call in call_lists: self.assertIn(mock.call(call), m_sman_cli.call_args_list) self.assertIn( "rh_subscription plugin completed successfully", self.logs.getvalue(), )
def test_install_with_default_arguments(self, m_readurl, m_subp): """Install AIO with no arguments""" cc_puppet.install_puppet_aio() self.assertEqual([mock.call([mock.ANY, '--cleanup'], capture=False)], m_subp.call_args_list)
def test_maybe_install_squashfuse_skips_non_containers(self, m_container): """maybe_install_squashfuse does nothing when not on a container.""" m_container.return_value = False maybe_install_squashfuse(cloud=FakeCloud(None)) self.assertEqual([mock.call()], m_container.call_args_list) self.assertEqual("", self.logs.getvalue())
def test_handle_tries_to_install_squashfuse(self, m_squash): """If squashfuse_in_container is True, try installing squashfuse.""" cfg = {"snap": {"squashfuse_in_container": True}} mycloud = FakeCloud(None) handle("snap", cfg=cfg, cloud=mycloud, log=self.logger, args=None) self.assertEqual([mock.call(mycloud)], m_squash.call_args_list)
def test_lock_with_passwd_if_available(self, m_which, m_subp, m_is_snappy): """Lock with only passwd will use passwd.""" m_which.side_effect = lambda m: m in ('passwd', ) self.dist.lock_passwd("bob") self.assertEqual([mock.call(['passwd', '-l', 'bob'])], m_subp.call_args_list)
def test_distro_passed_through(self, m_get_physical_nics_by_mac): ds.OpenNebulaNetwork({}, mock.sentinel.distro) self.assertEqual( [mock.call(mock.sentinel.distro)], m_get_physical_nics_by_mac.call_args_list, )
def test_dhcp_discovery_outside_sandbox(self, m_subp, m_kill, m_getppid): """dhcp_discovery brings up the interface and runs dhclient. It also returns the parsed dhcp.leases file generated in the sandbox. """ m_subp.return_value = ("", "") tmpdir = self.tmp_dir() dhclient_script = os.path.join(tmpdir, "dhclient.orig") script_content = "#!/bin/bash\necho fake-dhclient" write_file(dhclient_script, script_content, mode=0o755) lease_content = dedent( """ lease { interface "eth9"; fixed-address 192.168.2.74; option subnet-mask 255.255.255.0; option routers 192.168.2.1; } """ ) lease_file = os.path.join(tmpdir, "dhcp.leases") write_file(lease_file, lease_content) pid_file = os.path.join(tmpdir, "dhclient.pid") my_pid = 1 write_file(pid_file, "%d\n" % my_pid) m_getppid.return_value = 1 # Indicate that dhclient has daemonized with mock.patch("os.access", return_value=False): self.assertCountEqual( [ { "interface": "eth9", "fixed-address": "192.168.2.74", "subnet-mask": "255.255.255.0", "routers": "192.168.2.1", } ], dhcp_discovery(dhclient_script, "eth9", tmpdir), ) # dhclient script got copied with open(os.path.join(tmpdir, "dhclient.orig")) as stream: self.assertEqual(script_content, stream.read()) # Interface was brought up before dhclient called from sandbox m_subp.assert_has_calls( [ mock.call( ["ip", "link", "set", "dev", "eth9", "up"], capture=True ), mock.call( [ os.path.join(tmpdir, "dhclient.orig"), "-1", "-v", "-lf", lease_file, "-pf", os.path.join(tmpdir, "dhclient.pid"), "eth9", "-sf", "/bin/true", ], capture=True, ), ] ) m_kill.assert_has_calls([mock.call(my_pid, signal.SIGKILL)])