def test_remove_manifest(self): """Tests removing a manifest.""" inst = instance.Instance('/does/not/exist', './test.yml') inst.instance = 'test#1' inst.settings['reconnect'] = 0 inst.remove_manifest() self.assertIsNone(inst.instance) inst.instance = 'test#1' inst.settings['reconnect'] = 600 inst.start_time = 0 time.time.return_value = 500 inst.remove_manifest() self.assertEquals('test#1', inst.instance) inst.instance = 'test#1' inst.settings['reconnect'] = 600 inst.start_time = 0 time.time.return_value = 700 inst.remove_manifest() self.assertIsNone(inst.instance) self.assertEqual(2, treadmill.subproc.check_call.call_count)
def test_read_manifest_file(self): """Tests reading the manifest file with settings.""" inst = instance.Instance('/does/not/exist', './test.yml') manifest_content = """ name: test2 stop: false reconnect: 6000 --- services: - command: /bin/sleep 1m name: sleep1m restart: limit: 0 interval: 60 memory: 150M cpu: 10% disk: 100M """ mocked_open = mock.mock_open(read_data=manifest_content) with mock.patch('treadmill.spawn.instance.open', mocked_open, create=True): inst._read_manifest_file() self.assertEqual('test2', inst.settings['name']) self.assertEqual(False, inst.settings['stop']) self.assertEqual(6000, inst.settings['reconnect']) self.assertEqual('150M', inst.manifest['memory'])
def test_stop(self): """Tests stop logic.""" inst = instance.Instance('/does/not/exist', './test.yml') inst.instance = 'test#1' inst.stop(0) self.assertIsNone(inst.instance) inst.instance = 'test#1' inst.settings['stop'] = False inst.stop(1) self.assertEquals('test#1', inst.instance) inst.instance = 'test#1' inst.settings['stop'] = True inst.stop(1) self.assertIsNone(inst.instance) inst.instance = 'test#1' inst.settings['stop'] = False inst.stop(11) self.assertIsNone(inst.instance)
def test_parse_instance(self): """Tests parsing the instance result.""" inst = instance.Instance('/does/not/exist', './test.yml') inst._parse_instance(' proid.name#000123 ') self.assertEqual('proid.name#000123', inst.instance) self.assertEqual('000123', inst.instance_no)
def test_read_manifest_file(self): """Tests reading the manifest file with settings.""" inst = instance.Instance('./test.yml') io.open.assert_called_with('./test.yml', 'r') self.assertEqual('treadmld.test2', inst.name) self.assertEqual(False, inst.settings['stop']) self.assertEqual(True, inst.settings['reconnect']) self.assertEqual(6000, inst.settings['reconnect_timeout']) self.assertEqual('150M', inst.manifest['memory'])
def test_read_instance_file(self): """Tests reading the instance file.""" inst = instance.Instance('/does/not/exist', './test.yml') mocked_open = mock.mock_open(read_data='name#1') with mock.patch('treadmill.spawn.instance.open', mocked_open, create=True): inst._read_instance_file() self.assertEqual('name#1', inst.instance)
def _create_instance(self, path): """Create an spawn instance.""" job, bucket, running = spawn_utils.get_instance_path(path, self.paths) _LOGGER.debug('Creating - (%r, %r)', job, running) if os.path.exists(running): _LOGGER.debug('Create %r failed - already exists', running) return inst = instance.Instance(path) data_dir = os.path.join(job, spawn.JOB_DATA_DIR) fs.mkdir_safe(job) fs.mkdir_safe(data_dir) utils.create_script( os.path.join(job, 'run'), 'spawn.run', id=inst.id, name=inst.name, service_exit=inst.settings.get('service_exit', False), **subproc.get_aliases() ) utils.create_script( os.path.join(job, 'finish'), 'spawn.finish', id=inst.id, stop=inst.settings.get('stop', False), reconnect=inst.settings.get('reconnect', False), reconnect_timeout=inst.settings.get('reconnect_timeout', 0), **subproc.get_aliases() ) with io.open(os.path.join(data_dir, 'manifest'), 'w') as f: f.writelines( utils.json_genencode(inst.manifest) ) with io.open(os.path.join(job, 'timeout-finish'), 'w') as f: f.write(six.text_type(spawn.JOB_FINISH_TIMEOUT)) fs.symlink_safe(running, job) self._scan(bucket)
def _create_instance(self, path): """Create an spawn instance.""" job, bucket, running = spawn_utils.get_instance_path(path, self.paths) _LOGGER.debug('Creating - (%r, %r)', job, running) if os.path.exists(running): _LOGGER.debug('Create %r failed - already exists', running) return inst = instance.Instance(path) data_dir = os.path.join(job, spawn.JOB_DATA_DIR) fs.mkdir_safe(job) fs.mkdir_safe(data_dir) utils.create_script( os.path.join(job, 'run'), 'spawn.run', id=inst.id, name=inst.name, cellapi=self.paths.cellapi_sock, zk2fs=self.paths.zk_mirror_dir) utils.create_script( os.path.join(job, 'finish'), 'spawn.finish', id=inst.id, cellapi=self.paths.cellapi_sock, cleanup=self.paths.cleanup_dir, stop=inst.settings['stop'], reconnect=inst.settings['reconnect'], reconnect_timeout=inst.settings['reconnect_timeout']) with open(os.path.join(data_dir, 'manifest'), 'w') as f: json.dump(inst.manifest, f) with open(os.path.join(job, 'timeout-finish'), 'w') as f: f.write(str(spawn.JOB_FINISH_TIMEOUT)) fs.symlink_safe(running, job) self._scan(bucket)
def run(approot, manifest_path): """Spawn instance run process.""" inst = instance.Instance(approot, manifest_path) if inst.manifest is None: _LOGGER.error('No manifest found') sys.exit(1) sock = utils.open_socket(inst.id) if sock is None: _LOGGER.error('Could not open socket %r', inst.id) sys.exit(2) if not inst.run(): _LOGGER.error('TM run failed') sys.exit(3) # Redirect STDIN and original STDOUT to socket os.dup2(sock.fileno(), sys.stdin.fileno()) os.dup2(sock.fileno(), sys.stdout.fileno()) # Now wait on event utils.exec_fstrace(inst.get_watch_path())
def finish(approot, manifest_path, exit_code): """Spawn instance finish process.""" inst = instance.Instance(approot, manifest_path) inst.stop(exit_code) inst.remove_manifest()