def test_deprecated_msg_param(self): with warnings.catch_warnings(record=True) as w: assertions.assert_is_not(False, None, msg="This is a message") assertions.assert_equal(len(w), 1) assert issubclass(w[-1].category, DeprecationWarning) assertions.assert_in("msg is deprecated", str(w[-1].message))
def test_load_response_content_success(self): content = 'not:valid:json' http_response = build_file_mock(content) response = client.load_response_content(http_response) assert_equal(response.error, client.DECODE_ERROR) assert_in('No JSON object', response.msg) assert_equal(response.content, content)
def test_build_action_run_collection(self): collection = ActionRunFactory.build_action_run_collection(self.job_run) assert_equal(collection.action_graph, self.action_graph) assert_in('act1', collection.run_map) assert_in('act2', collection.run_map) assert_length(collection.run_map, 2) assert_equal(collection.run_map['act1'].action_name, 'act1')
def test_build_action_run_collection(self): collection = ActionRunFactory.build_action_run_collection( self.job_run, self.action_runner) assert_equal(collection.action_graph, self.action_graph) assert_in('act1', collection.run_map) assert_in('act2', collection.run_map) assert_length(collection.run_map, 2) assert_equal(collection.run_map['act1'].action_name, 'act1')
def test_valid_time_delta_invalid(self): exception = assert_raises( ConfigError, config_utils.valid_time_delta, 'no time', self.context, ) assert_in('not a valid time delta: no time', str(exception))
def test_build_url_request_with_data(self): data = {'param': 'is_set', 'other': 1} request = client.build_url_request(self.url, data) assert request.has_header('User-agent') assert_equal(request.get_method(), 'POST') assert_equal(request.get_full_url(), self.url) assert_in('param=is_set', request.data) assert_in('other=1', request.data)
def test_build_url_request_with_data(self): data = {'param': 'is_set', 'other': 1} request = client.build_url_request(self.url, data) assert request.has_header('User-agent') assert_equal(request.get_method(), 'POST') assert_equal(request.get_full_url(), self.url) assert_in('param=is_set', request.get_data()) assert_in('other=1', request.get_data())
def test_deprecated_msg_param(self): with warnings.catch_warnings(record=True) as w: assertions.assert_all_not_match_regex("qux", ["foobar", "barbaz"], msg="This is a message") assertions.assert_equal(len(w), 1) assert issubclass(w[-1].category, DeprecationWarning) assertions.assert_in("msg is deprecated", str(w[-1].message))
def test_proxy_list(self): the_list = range(3) validator = mock.Mock(return_value=the_list) value_proxy = proxy.ValueProxy(validator, self.value_cache, 'the_list') assert_equal(value_proxy, the_list) assert_in(2, value_proxy) assert_equal(value_proxy[:1], [0]) assert_equal(len(value_proxy), 3)
def test__str__(self): self.collection._is_run_blocked = lambda r: r.action_name != 'cleanup' expected = [ "ActionRunCollection", "second_name(scheduled:blocked)", "action_name(scheduled:blocked)", "cleanup(scheduled)" ] for expectation in expected: assert_in(expectation, str(self.collection))
def test_deprecated_msg_param(self): with warnings.catch_warnings(record=True) as w: assertions.assert_dict_subset({"a": 1, "b": 2}, {"a": 1, "b": 2, "c": 3}, msg="This is a message") assertions.assert_equal(len(w), 1) assert issubclass(w[-1].category, DeprecationWarning) assertions.assert_in("msg is deprecated", str(w[-1].message))
def test_check_if_pidfile_exists_file_exists(self): self.pidfile.__exit__(None, None, None) with open(self.filename, 'w') as fh: fh.write('123\n') with mock.patch.object(PIDFile, 'is_process_running') as mock_method: mock_method.return_value = True exception = assert_raises(SystemExit, PIDFile, self.filename) assert_in('Daemon running as 123', str(exception))
def test_validate_with_none(self): expected_msg = "A StubObject is required" exception = assert_raises( ConfigError, self.validator.validate, None, config_utils.NullConfigContext, ) assert_in(expected_msg, str(exception))
def test_build_getter(self): validator = mock.Mock() getter = getters.build_getter(validator) assert callable(getter), "Getter is not callable" value_proxy = getter('the_name') namespace = config.get_namespace(config.DEFAULT) assert_in(id(value_proxy), namespace.value_proxies) assert_equal(value_proxy.config_key, "the_name") assert_equal(value_proxy.namespace, namespace)
def test_deprecated_msg_param(self): with warnings.catch_warnings(record=True) as w: assertions.assert_subset(set([1, 2]), set([1, 2, 3]), msg="This is a message") assertions.assert_equal(len(w), 1) assert issubclass(w[-1].category, DeprecationWarning) assertions.assert_in("msg is deprecated", str(w[-1].message))
def test_build_new_run_manual(self): autospec_method(self.run_collection.remove_old_runs) run_time = datetime.datetime(2012, 3, 14, 15, 9, 26) mock_job = build_mock_job() job_run = self.run_collection.build_new_run( mock_job, run_time, self.mock_node, True) assert_in(job_run, self.run_collection.runs) self.run_collection.remove_old_runs.assert_called_with() assert_equal(job_run.run_num, 5) assert job_run.manual
def test_build_getter_with_getter_namespace(self): validator = mock.Mock() name = 'the stars' getter = getters.build_getter(validator, getter_namespace=name) assert callable(getter), "Getter is not callable" value_proxy = getter('the_name') namespace = config.get_namespace(name) assert_in(id(value_proxy), namespace.value_proxies) assert_equal(value_proxy.config_key, "the_name") assert_equal(value_proxy.namespace, namespace)
def test_client_returns_zero_on_success(self): server_process = subprocess.Popen( ["python", "-m", "testify.test_program", "testing_suite.example_test", "--serve", "9001"], stdout=open(os.devnull, "w"), stderr=open(os.devnull, "w"), ) # test_call has the side-effect of asserting the return code is 0 ret = test_call(["python", "-m", "testify.test_program", "--connect", "localhost:9001"]) assert_in("PASSED", ret) assert_equal(server_process.wait(), 0)
def test_restore_state(self): count = 3 state_data = [dict(instance_number=i * 3, node="node") for i in xrange(count)] autospec_method(self.collection._build_instance) created = self.collection.restore_state(state_data) assert_length(created, count) assert_equal(set(created), set(self.collection.instances)) expected = [mock.call(self.node_pool.get_by_hostname.return_value, d["instance_number"]) for d in state_data] for expected_call in expected: assert_in(expected_call, self.collection._build_instance.mock_calls)
def test_rerun_with_failure_limit(self): proc = subprocess.Popen( (sys.executable, "-m", "testify.test_program", "--rerun-test-file=/dev/stdin", "--failure-limit", "1"), stdin=subprocess.PIPE, stdout=subprocess.PIPE, ) stdout, _ = proc.communicate( b"test.fails_two_tests FailsTwoTests.test1\n" b"test.fails_two_tests FailsTwoTests.test2\n" ) assert_in(b"FAILED. 1 test / 1 case: 0 passed, 1 failed.", stdout)
def test_getters(self): get_conf = getters.NamespaceGetters(self.namespace) proxies = [ get_conf.get_bool('is_it'), get_conf.get_time('when'), get_conf.get_list_of_bool('options') ] namespace = config.get_namespace(get_conf.namespace) for proxy in proxies: assert_in(id(proxy), namespace.value_proxies)
def test_invalid(self): exception = assert_raises( ConfigError, self.validator, 'c', self.context, ) assert_in( 'Value at is not in %s: ' % str(set(self.enum)), str(exception), )
def test_build_new_run_manual(self): self.run_collection.remove_old_runs = Turtle() run_time = datetime.datetime(2012, 3, 14, 15, 9, 26) job = Turtle(name="thejob") job.action_graph.action_map = {} node = MockNode("thenode") job_run = self.run_collection.build_new_run(job, run_time, node, True) assert_in(job_run, self.run_collection.runs) assert_call(self.run_collection.remove_old_runs, 0) assert_equal(job_run.run_num, 5) assert job_run.manual
def test_end_to_end_basic(self): self.start_with_config(SINGLE_ECHO_CONFIG) client = self.sandbox.client assert_equal( self.client.config('MASTER')['config'], SINGLE_ECHO_CONFIG) # reconfigure and confirm results second_config = DOUBLE_ECHO_CONFIG + TOUCH_CLEANUP_FMT self.sandbox.tronfig(second_config) events = summarize_events(client.events()) assert_in(('', 'restoring'), events) assert_in(('MASTER.echo_job.0', 'created'), events) assert_equal(client.config('MASTER')['config'], second_config) # reconfigure, by uploading a third configuration self.sandbox.tronfig(ALT_NAMESPACED_ECHO_CONFIG, name='ohce') self.sandbox.client.home() # run the job and check its output echo_job_name = 'MASTER.echo_job' job_url = client.get_url(echo_job_name) action_url = client.get_url('MASTER.echo_job.1.echo_action') self.sandbox.tronctl('start', echo_job_name) def wait_on_cleanup(): return (len(client.job(job_url)['runs']) >= 2 and client.action_runs(action_url)['state'] == actionrun.ActionRun.STATE_SUCCEEDED.name) sandbox.wait_on_sandbox(wait_on_cleanup) echo_action_run = client.action_runs(action_url) another_action_url = client.get_url( 'MASTER.echo_job.1.another_echo_action') other_act_run = client.action_runs(another_action_url) assert_equal(echo_action_run['state'], actionrun.ActionRun.STATE_SUCCEEDED.name) assert_equal(echo_action_run['stdout'], ['Echo!']) assert_equal(other_act_run['state'], actionrun.ActionRun.STATE_FAILED.name) now = datetime.datetime.now() stdout = now.strftime( 'Today is %Y-%m-%d, which is the same as %Y-%m-%d') assert_equal(other_act_run['stdout'], [stdout]) job_runs_url = client.get_url('%s.1' % echo_job_name) assert_equal( client.job_runs(job_runs_url)['state'], actionrun.ActionRun.STATE_FAILED.name)
def test_deprecated_msg_param(self): with warnings.catch_warnings(record=True) as w: assertions.assert_dicts_equal({ "a": 1, "b": 2 }, { "a": 1, "b": 2 }, msg="This is a message") assertions.assert_equal(len(w), 1) assert issubclass(w[-1].category, DeprecationWarning) assertions.assert_in("msg is deprecated", str(w[-1].message))
def test_rerun_with_failure_limit(self): proc = subprocess.Popen( ( sys.executable, '-m', 'testify.test_program', '--rerun-test-file=/dev/stdin', '--failure-limit', '1', ), stdin=subprocess.PIPE, stdout=subprocess.PIPE, ) stdout, _ = proc.communicate( b'test.fails_two_tests FailsTwoTests.test1\n' b'test.fails_two_tests FailsTwoTests.test2\n' ) assert_in(b'FAILED. 1 test / 1 case: 0 passed, 1 failed.', stdout)
def test_restore_state(self): count = 3 state_data = [ dict(instance_number=i * 3, node='node') for i in xrange(count) ] autospec_method(self.collection._build_instance) created = self.collection.restore_state(state_data) assert_length(created, count) assert_equal(set(created), set(self.collection.instances)) expected = [ mock.call(self.node_pool.get_by_hostname.return_value, d['instance_number']) for d in state_data ] for expected_call in expected: assert_in(expected_call, self.collection._build_instance.mock_calls)
def test_end_to_end_basic(self): self.start_with_config(SINGLE_ECHO_CONFIG) client = self.sandbox.client assert_equal(self.client.config('MASTER')['config'], SINGLE_ECHO_CONFIG) # reconfigure and confirm results second_config = DOUBLE_ECHO_CONFIG + TOUCH_CLEANUP_FMT self.sandbox.tronfig(second_config) events = summarize_events(client.events()) assert_in(('', 'restoring'), events) assert_in(('MASTER.echo_job.0', 'created'), events) assert_equal(client.config('MASTER')['config'], second_config) # reconfigure, by uploading a third configuration self.sandbox.tronfig(ALT_NAMESPACED_ECHO_CONFIG, name='ohce') self.sandbox.client.home() # run the job and check its output echo_job_name = 'MASTER.echo_job' job_url = client.get_url(echo_job_name) action_url = client.get_url('MASTER.echo_job.1.echo_action') self.sandbox.tronctl('start', echo_job_name) def wait_on_cleanup(): return (len(client.job(job_url)['runs']) >= 2 and client.action_runs(action_url)['state'] == actionrun.ActionRun.STATE_SUCCEEDED.name) sandbox.wait_on_sandbox(wait_on_cleanup) echo_action_run = client.action_runs(action_url) another_action_url = client.get_url('MASTER.echo_job.1.another_echo_action') other_act_run = client.action_runs(another_action_url) assert_equal(echo_action_run['state'], actionrun.ActionRun.STATE_SUCCEEDED.name) assert_equal(echo_action_run['stdout'], ['Echo!']) assert_equal(other_act_run['state'], actionrun.ActionRun.STATE_FAILED.name) now = datetime.datetime.now() stdout = now.strftime('Today is %Y-%m-%d, which is the same as %Y-%m-%d') assert_equal(other_act_run['stdout'], [stdout]) job_runs_url = client.get_url('%s.1' % echo_job_name) assert_equal(client.job_runs(job_runs_url)['state'], actionrun.ActionRun.STATE_FAILED.name)
def test_rerun_with_failure_limit(self): proc = subprocess.Popen( ( sys.executable, '-m', 'testify.test_program', '--rerun-test-file=/dev/stdin', '--failure-limit', '1', ), stdin=subprocess.PIPE, stdout=subprocess.PIPE, ) stdout, _ = proc.communicate( b'test.fails_two_tests FailsTwoTests.test1\n' b'test.fails_two_tests FailsTwoTests.test2\n') assert_in(b'FAILED. 1 test / 1 case: 0 passed, 1 failed.', stdout)
def test_client_returns_zero_on_success(self): server_process = subprocess.Popen( [ 'python', '-m', 'testify.test_program', 'testing_suite.example_test', '--serve', '9001', ], stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'), ) # test_call has the side-effect of asserting the return code is 0 ret = test_call([ 'python', '-m', 'testify.test_program', '--connect', 'localhost:9001', ]) assert_in('PASSED', ret) assert_equal(server_process.wait(), 0)
def test_service_failed_restart(self): config = BASIC_CONFIG + dedent(""" services: - name: service_restart node: local pid_file: "/tmp/file_dne" command: "sleep 1; cat /bogus/file/DNE" monitor_interval: 1 restart_delay: 2 """) self.start_with_config(config) service_name = 'MASTER.service_restart' service_url = self.client.get_url(service_name) self.sandbox.tronctl('start', service_name) waiter = sandbox.build_waiter_func(self.client.service, service_url) waiter(service.ServiceState.FAILED) service_content = self.client.service(service_url) expected = 'cat: /bogus/file/DNE: No such file or directory' assert_in(service_content['instances'][0]['failures'][0], expected) waiter(service.ServiceState.STARTING)
def test_render_template(self): config_content = "asdf asdf" container = self.manager.load.return_value = mock.create_autospec( config_parse.ConfigContainer, ) container.get_node_names.return_value = ['one', 'two', 'three'] container.get_master.return_value.command_context = {'zing': 'stars'} content = self.controller.render_template(config_content) assert_in('# one\n# three\n# two\n', content) assert_in('# %-30s: %s' % ('zing', 'stars'), content) assert_in(config_content, content)
def test_render_template(self): config_content = "asdf asdf" container = self.manager.load.return_value = mock.create_autospec( config_parse.ConfigContainer) container.get_node_names.return_value = ['one', 'two', 'three'] container.get_master.return_value.command_context = {'zing': 'stars'} content = self.controller.render_template(config_content) assert_in('# one\n# three\n# two\n', content) assert_in('# %-30s: %s' % ('zing', 'stars'), content) assert_in(config_content, content)
def test_run_testify_from_bin(self): output = test_call(['bin/testify', 'testing_suite', '-v']) assert_in(self.expected_tests, output)
def test_handle_termination_success(self): result = self.controller.handle_termination('kill') assert_in("Attempting to kill", result)
def test_handle_termination_not_implemented(self): self.action_run.stop.side_effect = NotImplementedError result = self.controller.handle_termination('stop') assert_in("Failed to stop", result)
def test_handle_command_mapped_command_failed(self): self.action_run.cancel.return_value = False result = self.controller.handle_command('cancel') self.action_run.cancel.assert_called_with() assert_in("Failed to cancel", result)
def test_handle_command_mapped_command(self): result = self.controller.handle_command('cancel') self.action_run.cancel.assert_called_with() assert_in("now in state", result)
def test_help(self): output = test_call( [sys.executable, '-m', 'testify.test_program', '--help']) assert_in('Usage:', output)
def test_handle_command_start_failure(self): self.instance.start.return_value = False response = self.controller.handle_command('start') self.instance.start.assert_called_with() assert_in("Failed to start", response)
def test_handle_mapped_command_failure(self): self.job_run.cancel.return_value = False result = self.controller.handle_command('cancel') self.job_run.cancel.assert_called_with() assert_in('Failed to cancel', result)
def test_handle_command_start(self): response = self.controller.handle_command('start') self.instance.start.assert_called_with() assert_in("starting", response)
def test_handle_mapped_command(self): result = self.controller.handle_command('start') self.job_run.start.assert_called_with() assert_in('now in state', result)
def test_run_testify_test_file_class_and_method(self): output = test_call(["python", "testing_suite/example_test.py", "-v", "ExampleTestCase.test_one"]) assert_in("PASSED. 1 test", output)
def test_run_testify_test_file_class(self): output = test_call(["python", "testing_suite/example_test.py", "-v", "ExampleTestCase"]) assert_in("PASSED. 2 tests", output)
def test_run_testify_test_module(self): output = test_call(['python', '-m', 'testing_suite.example_test', '-v']) assert_in(self.expected_tests, output)
def test_handle_command_start_failed(self): self.job_run.is_scheduled = True result = self.controller.handle_command('start') assert not self.action_run.start.mock_calls assert_in("can not be started", result)
def test_run_testify_test_file(self): output = test_call(['python', 'testing_suite/example_test.py', '-v']) assert_in(self.expected_tests, output)
def test_run_testify_test_file_class(self): output = test_call([ 'python', 'testing_suite/example_test.py', '-v', 'ExampleTestCase']) assert_in('PASSED. 2 tests', output)
def test_run_testify_test_file_class_and_method(self): output = test_call([ 'python', 'testing_suite/example_test.py', '-v', 'ExampleTestCase.test_one']) assert_in('PASSED. 1 test', output)
def test_run_testify_test_file(self): output = test_call(["python", "testing_suite/example_test.py", "-v"]) assert_in(self.expected_tests, output)