Example #1
0
    def test_mysql_mode_locks_unlocks_tables(
        self, mock_create_dir, mock_get_lvm_info, mock_get_vol_fs_type, mock_popen
    ):
        mock_get_vol_fs_type.return_value = "xfs"
        mock_get_lvm_info.return_value = {"volgroup": "lvm_volgroup", "srcvol": "lvm_device", "snap_path": "snap_path"}
        mock_process = Mock()
        mock_process.communicate.return_value = "", ""
        mock_process.returncode = 0
        mock_popen.return_value = mock_process

        backup_opt = Mock()
        backup_opt.snapshot = True
        backup_opt.lvm_auto_snap = ""
        backup_opt.path_to_backup = "/just/a/path"
        backup_opt.lvm_dirmount = "/var/mountpoint"
        backup_opt.lvm_snapperm = "ro"
        backup_opt.mode = "mysql"
        backup_opt.mysql_db_inst = Mock()
        mock_cursor = Mock()
        backup_opt.mysql_db_inst.cursor.return_value = mock_cursor

        self.assertTrue(lvm.lvm_snap(backup_opt))

        first_call = call("FLUSH TABLES WITH READ LOCK")
        second_call = call("UNLOCK TABLES")
        self.assertEquals(first_call, mock_cursor.execute.call_args_list[0])
        self.assertEquals(second_call, mock_cursor.execute.call_args_list[1])
Example #2
0
 def test_raises_stderr_on_failure(self, Popen):
     result = Mock()
     result.returncode = 1
     result.communicate.return_value = (b"", b"error")
     Popen.return_value = result
     with self.assertRaises(AssetHandlerError):
         Exec().run("input")
Example #3
0
    def test_snapshot_mount_error_raises_Exception(
        self, mock_create_dir, mock_get_lvm_info, mock_get_vol_fs_type, mock_popen, mock_lvm_snap_remove
    ):
        mock_get_vol_fs_type.return_value = "xfs"
        mock_get_lvm_info.return_value = {"volgroup": "lvm_volgroup", "srcvol": "lvm_device", "snap_path": "snap_path"}
        mock_lvcreate_process, mock_mount_process = Mock(), Mock()

        mock_lvcreate_process.communicate.return_value = "", ""
        mock_lvcreate_process.returncode = 0

        mock_mount_process.communicate.return_value = "", "mount error"
        mock_mount_process.returncode = 1

        mock_popen.side_effect = [mock_lvcreate_process, mock_mount_process]

        backup_opt = Mock()
        backup_opt.snapshot = True
        backup_opt.lvm_auto_snap = ""
        backup_opt.path_to_backup = "/just/a/path"
        backup_opt.lvm_dirmount = "/var/mountpoint"
        backup_opt.lvm_snapperm = "ro"

        with self.assertRaises(Exception) as cm:
            lvm.lvm_snap(backup_opt)
        the_exception = cm.exception
        self.assertIn("lvm snapshot mounting error", the_exception.message)

        mock_lvm_snap_remove.assert_called_once_with(backup_opt)
Example #4
0
    def set_up(self):
        when(execution.LOGGER).info(ANY_ARGUMENTS).then_return(None)
        when(execution.LOGGER).error(ANY_ARGUMENTS).then_return(None)

        process_mock = Mock()
        process_mock.returncode = 0
        self.process_mock = process_mock
Example #5
0
 def test_run_job_no_watchdog(
     self, m_tempfile, m_safe_dump, m_mkdir, m_environ, m_popen, m_t_config, m_symlink_log, m_sleep
 ):
     config = {
         "suite_path": "suite/path",
         "config": {"foo": "bar"},
         "verbose": True,
         "owner": "the_owner",
         "archive_path": "archive/path",
         "name": "the_name",
         "description": "the_description",
         "worker_log": "worker/log.log",
         "job_id": "1",
     }
     m_tmp = MagicMock()
     temp_file = Mock()
     temp_file.name = "the_name"
     m_tmp.__enter__.return_value = temp_file
     m_tempfile.return_value = m_tmp
     env = dict(PYTHONPATH="python/path")
     m_environ.copy.return_value = env
     m_p = Mock()
     m_p.returncode = 1
     m_popen.return_value = m_p
     m_t_config.results_server = False
     worker.run_job(config, "teuth/bin/path", "archive/dir", verbose=False)
     m_symlink_log.assert_called_with(config["worker_log"], config["archive_path"])
Example #6
0
    def test_failing_git_command(self):
        """
        Make sure we properly wrap up any failing git commands in an exception
        """
        code = Code(self.build)

        proc_mock = Mock()
        proc_mock.returncode = 1
        response = Response(proc_mock, "", "herp derp")
        code._command = Mock(return_value=response)
        code._which_git = Mock(return_value="lol")
        code.update()
Example #7
0
    def test_which_git(self):
        """
        Ensure that we can locate the git binary. We should store the path to
        the git binary.
        """

        proc_mock = Mock()
        proc_mock.returncode = 0
        response = Response(proc_mock, "/usr/local/bin/git", "")
        code = Code(self.build)
        code._command = Mock(return_value=response)

        assert_equal(code._which_git(), "/usr/local/bin/git")
    def _mock_uploader_subprocess(self, emulate_success):
        self.mock_dict = {}

        popen_mock = Mock()
        popen_mock.communicate.return_value = ("scp_out dump", "scp_err dump")

        if emulate_success:
            popen_mock.returncode = 0
        else:
            popen_mock.returncode = 1

        subprocess_mock = Mock()
        subprocess_mock.Popen.return_value = popen_mock
        subprocess_mock.PIPE = "MYPIPE"

        tempfile_mock = Mock()
        tempfile_mock.mkstemp.return_value = ("my_handle", "my_tempfile_name")

        self.mock_dict["tempfile"] = tempfile_mock
        self.mock_dict["subprocess"] = subprocess_mock
        self.mock_dict["os"] = Mock()

        self.IMS.module_uploader.modules = self.mock_dict
    def setUp(self):
        mock_host_rpm_builder = Mock(HostRpmBuilder)
        mock_host_rpm_builder.hostname = "berweb01"
        mock_host_rpm_builder.thread_name = "thread-0"
        mock_host_rpm_builder.logger = Mock()
        mock_host_rpm_builder.work_dir = "/path/to/working/directory"
        mock_host_rpm_builder.rpm_build_dir = "/path/to/rpm/build/directory"
        mock_host_rpm_builder._tar_sources.return_value = "/path/to/tarred_sources.tar.gz"

        mock_process = Mock()
        mock_process.communicate.return_value = ("stdout", "stderr")
        mock_process.returncode = 0

        self.mock_host_rpm_builder = mock_host_rpm_builder
        self.mock_process = mock_process
Example #10
0
 def test_recoverable_error(self, mock_popen):
     mock_popen_instance = Mock()
     mock_popen_instance.communicate.return_value = ("stdout", "stderr")
     mock_popen_instance.returncode = 0
     mock_popen.return_value = mock_popen_instance
     self.aptd._call_apport_recoverable_error("msg", "traceback-error", "custom:dupes:signature")
     mock_popen.assert_called_with(
         [APPORT_RECOVERABLE_ERROR], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
     )
     # check that we really send the right data
     args, kwargs = mock_popen_instance.communicate.call_args
     self.assertEqual(
         kwargs["input"].split("\0"),
         ["DialogBody", "msg", "Traceback", "traceback-error", "DuplicateSignature", "custom:dupes:signature"],
     )
Example #11
0
    def test_using_mount(self, mock_get_mount_from_path, mock_lvm_guess, mock_popen):
        mock_get_mount_from_path.return_value = "/home/somedir", "some-snap-path"
        mock_lvm_guess.side_effect = [(None, None, None), ("vg_test", "lv_test", "lvm_device")]
        mounts = "/dev/mapper/vg_prova-lv_prova_vol1 /home/pippo ext4 rw,relatime,data=ordered 0 0"
        mocked_open_function = mock_open(read_data=mounts)
        mock_process = Mock()
        mock_process.returncode = 0
        mock_popen.return_value = mock_process
        mock_process.communicate.return_value = "", ""

        with patch("__builtin__.open", mocked_open_function):
            res = lvm.get_lvm_info("lvm_auto_snap_value")

        expected_result = {"volgroup": "vg_test", "snap_path": "some-snap-path", "srcvol": "lvm_device"}
        self.assertEquals(res, expected_result)
    def test_downloadStream_should_timeout_on_stall(self):
        ospatcher = patch("os.path.getsize")
        ospatcher.start()
        import os

        os.path.getsize = Mock()
        os.path.getsize.return_value = 1234

        patcher = patch("subprocess.Popen")
        patcher.start()
        import subprocess

        popen = Mock()
        communicate = Mock()
        communicate.communicate.return_value = ("", "filesize    1234568\n")
        communicate.returncode = None
        popen.return_value = communicate
        subprocess.Popen = popen
        sys.modules["__main__"].xbmcvfs.exists.return_value = True

        downloader = self.SimpleDownloader.SimpleDownloader()
        communicate.stdout.read.return_value = "out"
        downloader._showMessage = Mock()
        downloader._updateProgress = Mock()
        downloader._downloadStream(
            "filename",
            {
                "videoid": "someid",
                "url": "rtmp://url",
                "Title": "some_title",
                "download_path": "some_path",
                "path_incomplete": "incomplete_path",
                "total_size": 12340.0,
                "cmd_call": ["rtmpdump", "-r", "rtmp://url", "-o", "incomplete_path"],
            },
        )

        ospatcher.stop()
        patcher.stop()

        result = popen.call_args_list[0][0][0]
        print repr(result)
        assert "rtmpdump" in result
        assert "-r" in result
        assert "rtmp://url" in result
        assert "-o" in result
        assert "incomplete_path" in result
        assert len(result) == 5
Example #13
0
    def test_run_success(self, monkeypatch):
        job = DoitUnstableNoContinue(DODO_FILE, "t1", THIS_PATH)
        doit_result = SAMPLE_RESULT
        mock_result = Mock(return_value=doit_result)
        monkeypatch.setattr(job, "read_json_result", mock_result)
        mock_process = Mock()
        mock_process.returncode = 0  # success (ignore failed on data)

        yi = job.run()
        # 1) execute first time
        (run1, pause1) = yi.next()
        assert isinstance(run1, ProcessTask)
        assert "run" in run1.cmd
        assert isinstance(pause1, TaskPause)
        run1.proc = mock_process
        py.test.raises(StopIteration, yi.next)
Example #14
0
    def test_ok(self, mock_create_dir, mock_get_lvm_info, mock_get_vol_fs_type, mock_popen, mock_validate_lvm_params):
        mock_get_lvm_info.return_value = {"volgroup": "lvm_volgroup", "srcvol": "lvm_device", "snap_path": "snap_path"}
        mock_process = Mock()
        mock_process.communicate.return_value = "", ""
        mock_process.returncode = 0
        mock_popen.return_value = mock_process

        backup_opt = Mock()
        backup_opt.snapshot = True
        backup_opt.lvm_auto_snap = ""
        backup_opt.path_to_backup = "/just/a/path"
        backup_opt.lvm_dirmount = "/var/mountpoint"
        backup_opt.lvm_snapperm = "ro"
        backup_opt.lvm_volgroup = ""
        backup_opt.lvm_srcvol = ""

        self.assertTrue(lvm.lvm_snap(backup_opt))
Example #15
0
    def test_run_error(self, monkeypatch):
        job = DoitUnstableNoContinue(DODO_FILE, "t1", THIS_PATH)
        doit_result = None
        mock_result = Mock(return_value=doit_result)
        monkeypatch.setattr(job, "read_json_result", mock_result)
        mock_process = Mock()
        mock_process.returncode = 1

        yi = job.run()
        # 1) execute first time
        (run1, pause1) = yi.next()
        run1.proc = mock_process
        # 2) try again
        (run2, pause2) = yi.next()
        run2.proc = mock_process
        # 3 give-up
        py.test.raises(StopIteration, yi.next)
Example #16
0
 def test_run_job_with_watchdog(
     self, m_tempfile, m_safe_dump, m_mkdir, m_environ, m_popen, m_t_config, m_run_watchdog
 ):
     config = {
         "suite_path": "suite/path",
         "config": {"foo": "bar"},
         "verbose": True,
         "owner": "the_owner",
         "archive_path": "archive/path",
         "name": "the_name",
         "description": "the_description",
         "job_id": "1",
     }
     m_tmp = MagicMock()
     temp_file = Mock()
     temp_file.name = "the_name"
     m_tmp.__enter__.return_value = temp_file
     m_tempfile.return_value = m_tmp
     env = dict(PYTHONPATH="python/path")
     m_environ.copy.return_value = env
     m_p = Mock()
     m_p.returncode = 0
     m_popen.return_value = m_p
     m_t_config.results_server = True
     worker.run_job(config, "teuth/bin/path", "archive/dir", verbose=False)
     m_run_watchdog.assert_called_with(m_p, config)
     expected_args = [
         "teuth/bin/path/teuthology",
         "-v",
         "--lock",
         "--block",
         "--owner",
         "the_owner",
         "--archive",
         "archive/path",
         "--name",
         "the_name",
         "--description",
         "the_description",
         "--",
         "the_name",
     ]
     m_popen.assert_called_with(args=expected_args, env=env)
Example #17
0
    def test_snapshot_fails(
        self, mock_create_dir, mock_get_lvm_info, mock_get_vol_fs_type, mock_popen, mock_validate_lvm_params
    ):
        mock_get_lvm_info.return_value = {"volgroup": "lvm_volgroup", "srcvol": "lvm_device", "snap_path": "snap_path"}
        mock_process = Mock()
        mock_process.communicate.return_value = "", ""
        mock_process.returncode = 1
        mock_popen.return_value = mock_process

        backup_opt = Mock()
        backup_opt.snapshot = True
        backup_opt.lvm_auto_snap = ""
        backup_opt.path_to_backup = "/just/a/path"
        backup_opt.lvm_dirmount = "/var/mountpoint"
        backup_opt.lvm_snapperm = "ro"

        with self.assertRaises(Exception) as cm:
            lvm.lvm_snap(backup_opt)
        the_exception = cm.exception
        self.assertIn("lvm snapshot creation error", the_exception.message)
Example #18
0
    def test_run_with_failures(self, monkeypatch):
        job = DoitUnstableNoContinue(DODO_FILE, "t1", THIS_PATH)
        doit_result = SAMPLE_RESULT
        mock_result = Mock(return_value=doit_result)
        monkeypatch.setattr(job, "read_json_result", mock_result)
        mock_process = Mock()
        mock_process.returncode = 1

        yi = job.run()
        # 1) execute first time
        (run1, pause1) = yi.next()
        assert isinstance(run1, ProcessTask)
        assert "run" in run1.cmd
        assert isinstance(pause1, TaskPause)
        run1.proc = mock_process
        # 2) execute second time
        (run2, pause2) = yi.next()
        run2.proc = mock_process
        assert "run" in run2.cmd
        # 3) execute ignore
        (ignore, pause3) = yi.next()
        assert "ignore" in ignore.cmd
        # final result is fail
        assert "fail" == job.final_result["t1"]["result"]
Example #19
0
 def test_raises_on_popen_returncode_not_0(self, mock_popen):
     mock_process = Mock()
     mock_process.communicate.return_value = "", ""
     mock_process.returncode = 1
     mock_popen.return_value = mock_process
     self.assertRaises(Exception, lvm._lvremove, "path")
Example #20
0
 def test_return_none_on_success(self, mock_popen):
     mock_process = Mock()
     mock_process.communicate.return_value = "", ""
     mock_process.returncode = 0
     mock_popen.return_value = mock_process
     self.assertIsNone(lvm._lvremove("logicalvolume"))
Example #21
0
 def test_return_none_on_success(self, mock_popen):
     mock_process = Mock()
     mock_process.communicate.return_value = "", ""
     mock_process.returncode = 0
     mock_popen.return_value = mock_process
     self.assertIsNone(lvm._umount("path"))
Example #22
0
 def test_returns_stdout_on_success(self, Popen):
     result = Mock()
     result.returncode = 0
     result.communicate.return_value = (b"output", b"")
     Popen.return_value = result
     self.assertEqual(Exec().run("input"), "output")
Example #23
0
 def test_successful_run(self, popen):
     proc = Mock()
     proc.returncode = 0
     popen.return_value = proc
     self.assertEquals(self.bevel._run("foo", ["bar"]), 0)