def test_install(self):
        init_script_path = os.path.join(
            get_chaos_monkey_dir(), 'scripts', 'chaos-monkey-restart.conf')
        with open(init_script_path) as f:
            conf_content = f.read()
        with NamedTemporaryFile() as init_fd:
            restart_script_path = '/scripts/restart_chaos_monkey.py'
            runner_path = '/path/runner.py'
            init = Init(init_path=init_fd.name,
                        init_script_path=init_script_path,
                        restart_script_path=restart_script_path,
                        runner_path=runner_path)
            cmd_arg = '--include-command=deny-all workspace'
            expire_time = 65537.00
            init.install(cmd_arg, expire_time)

            conf_content = conf_content.format(
                restart_script_path=restart_script_path,
                runner_path=runner_path,
                expire_time=expire_time,
                cmd_arg=cmd_arg)

            with open(init_fd.name) as f:
                init_content = f.read()
            self.assertEqual(init_content, conf_content)
Beispiel #2
0
    def test_install(self):
        init_script_path = os.path.join(get_chaos_monkey_dir(), 'scripts',
                                        'chaos-monkey-restart.conf')
        with open(init_script_path) as f:
            conf_content = f.read()
        with NamedTemporaryFile() as init_fd:
            restart_script_path = '/scripts/restart_chaos_monkey.py'
            runner_path = '/path/runner.py'
            init = Init(init_path=init_fd.name,
                        init_script_path=init_script_path,
                        restart_script_path=restart_script_path,
                        runner_path=runner_path)
            cmd_arg = '--include-command=deny-all workspace'
            expire_time = 65537.00
            init.install(cmd_arg, expire_time)

            conf_content = conf_content.format(
                restart_script_path=restart_script_path,
                runner_path=runner_path,
                expire_time=expire_time,
                cmd_arg=cmd_arg)

            with open(init_fd.name) as f:
                init_content = f.read()
            self.assertEqual(init_content, conf_content)
Beispiel #3
0
 def test_uninstall(self):
     with NamedTemporaryFile(delete=False) as init_fd:
         init_fd.write('fake')
         init_fd.flush()
         os.fsync(init_fd)
         init = Init(init_path=init_fd.name,
                     init_script_path='fake',
                     restart_script_path='fake',
                     runner_path='fake')
         init.uninstall()
         self.assertIs(os.path.isfile(init_fd.name), False)
 def test_uninstall(self):
     with NamedTemporaryFile(delete=False) as init_fd:
         init_fd.write('fake')
         init_fd.flush()
         os.fsync(init_fd)
         init = Init(init_path=init_fd.name,
                     init_script_path='fake',
                     restart_script_path='fake',
                     runner_path='fake')
         init.uninstall()
         self.assertIs(os.path.isfile(init_fd.name), False)
Beispiel #5
0
 def test_remove_args_restart_in_middle_of_string(self):
     cmd_arg = ("--include-command restart-unit --enablement-timeout 1"
                " /home/myhome --restart --expire-time 946598401")
     expected = ("--include-command restart-unit --enablement-timeout 1"
                 " /home/myhome")
     result = Init._remove_args(cmd_arg)
     self.assertEqual(result, expected)
 def test_remove_args_restart_in_middle_of_string(self):
     cmd_arg = ("--include-command restart-unit --enablement-timeout 1"
                " /home/myhome --restart --expire-time 946598401")
     expected = ("--include-command restart-unit --enablement-timeout 1"
                 " /home/myhome")
     result = Init._remove_args(cmd_arg)
     self.assertEqual(result, expected)
 def test_upstart(self):
     init = Init.upstart()
     self.assertIsInstance(init, Init)
     self.assertEqual(init.init_path, '/etc/init/chaos-monkey-restart.conf')
     cm_dir = get_chaos_monkey_dir()
     self.assertEqual(init.init_script_path, os.path.join(
         cm_dir, 'scripts', 'chaos-monkey-restart.conf'))
     self.assertEqual(init.restart_script_path, os.path.join(
         cm_dir, 'scripts', 'restart_chaos_monkey.py'))
     self.assertEqual(init.runner_path, os.path.join(
         cm_dir, 'runner.py'))
Beispiel #8
0
 def test_upstart(self):
     init = Init.upstart()
     self.assertIsInstance(init, Init)
     self.assertEqual(init.init_path, '/etc/init/chaos-monkey-restart.conf')
     cm_dir = get_chaos_monkey_dir()
     self.assertEqual(
         init.init_script_path,
         os.path.join(cm_dir, 'scripts', 'chaos-monkey-restart.conf'))
     self.assertEqual(
         init.restart_script_path,
         os.path.join(cm_dir, 'scripts', 'restart_chaos_monkey.py'))
     self.assertEqual(init.runner_path, os.path.join(cm_dir, 'runner.py'))
Beispiel #9
0
    def _run_command(self, enablement_timeout):
        """Run a randomly selected chaos command."""
        chaos = random.choice(self.chaos_monkey.chaos)
        logging.info("{}".format(chaos.description))
        cmd_logger = logging.getLogger(self.cmd_log_name)
        cmd_logger.info(
            StructuredMessage(chaos.command_str, enablement_timeout))
        if chaos.command_str == Kill.restart_cmd:
            self.stop_chaos = True
            init = Init.upstart()
            init.install(cmd_arg=' '.join(sys.argv[1:]),
                         expire_time=self.expire_time)
        chaos.enable()
        if chaos.command_str == Kill.restart_cmd:
            return

        sleep(enablement_timeout)
        if chaos.disable:
            chaos.disable()
    def _run_command(self, enablement_timeout):
        """Run a randomly selected chaos command."""
        chaos = random.choice(self.chaos_monkey.chaos)
        logging.info("{}".format(chaos.description))
        cmd_logger = logging.getLogger(self.cmd_log_name)
        cmd_logger.info(StructuredMessage(
            chaos.command_str, enablement_timeout))
        if chaos.command_str == Kill.restart_cmd:
            self.stop_chaos = True
            init = Init.upstart()
            init.install(
                cmd_arg=' '.join(sys.argv[1:]), expire_time=self.expire_time)
        chaos.enable()
        if chaos.command_str == Kill.restart_cmd:
            return

        sleep(enablement_timeout)
        if chaos.disable:
            chaos.disable()
 def acquire_lock(self, restart=False):
     """Acquire a lock before running Chaos Monkey."""
     if not os.path.isdir(self.workspace):
         sys.stderr.write('Not a directory: {}\n'.format(self.workspace))
         sys.exit(-1)
     init = Init.upstart()
     init.uninstall()
     try:
         file_flag = ((os.O_CREAT | os.O_EXCL | os.O_WRONLY)
                      if not restart else (os.O_CREAT | os.O_WRONLY))
         lock_fd = os.open(self.lock_file, file_flag)
     except OSError as e:
         if e.errno == errno.EEXIST:
             sys.stderr.write('Lock file already exists: {}\n'.format(
                 self.lock_file))
         sys.exit(-1)
     os.write(lock_fd, str(os.getpid()))
     os.fsync(lock_fd)
     os.close(lock_fd)
     self.workspace_lock = True
     self.verify_lock()
Beispiel #12
0
 def acquire_lock(self, restart=False):
     """Acquire a lock before running Chaos Monkey."""
     if not os.path.isdir(self.workspace):
         sys.stderr.write('Not a directory: {}\n'.format(self.workspace))
         sys.exit(-1)
     init = Init.upstart()
     init.uninstall()
     try:
         file_flag = ((os.O_CREAT | os.O_EXCL
                       | os.O_WRONLY) if not restart else
                      (os.O_CREAT | os.O_WRONLY))
         lock_fd = os.open(self.lock_file, file_flag)
     except OSError as e:
         if e.errno == errno.EEXIST:
             sys.stderr.write('Lock file already exists: {}\n'.format(
                 self.lock_file))
         sys.exit(-1)
     os.write(lock_fd, str(os.getpid()))
     os.fsync(lock_fd)
     os.close(lock_fd)
     self.workspace_lock = True
     self.verify_lock()
Beispiel #13
0
 def test_remove_args_no_expire_time_and_no_restart(self):
     cmd_arg = ("--include-command restart-unit --enablement-timeout 1 "
                "/home/myhome")
     result = Init._remove_args(cmd_arg)
     self.assertEqual(result, cmd_arg)
 def test_remove_args_no_expire_time_and_no_restart(self):
     cmd_arg = ("--include-command restart-unit --enablement-timeout 1 "
                "/home/myhome")
     result = Init._remove_args(cmd_arg)
     self.assertEqual(result, cmd_arg)