def test_normalize_cmd(self): """Exercise the normalize-m3u sub-command""" args = ['normalize-m3u', '-c', 'cp1252', '-o', self._tmp, '-r', 'l-', '-p' '\\\\\\\\=>/', '-p', 'M:/=>/pub/mp3/', '-u', '-b'] with captured_output() as (out, err): status = rubepl.main(args) output = out.getvalue().strip() assert '' == output errors = err.getvalue().strip() assert 'usage:' == errors[0:6] args.append(self._pls1B2B) with captured_output() as (out, err): status = rubepl.main(args) output = out.getvalue().strip() assert '"plf1B2B"=>"plf1b2b"' == output errors = err.getvalue().strip() assert '' == errors with open(os.path.join(self._tmp, 'plf1b2b.m3u8'), 'rb') as fh: text = fh.read() assert text == self._JIN_1B2B
def test_verbose_output(self): with mock_verbosity(True): with captured_output() as (out, err): verbose(TEST_MESSAGE) self.assertEqual( out.getvalue().strip(), "\x1b[33m==> hokusai exec `%s`\n\x1b[0m" % TEST_MESSAGE)
def test_01_k8s_update(self, mocked_sys_exit): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=open( os.path.join(os.getcwd(), 'test', 'fixtures', 'sts-get-caller-identity-response.xml')).read(), content_type="application/xml") httpretty.register_uri( httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=open( os.path.join(os.getcwd(), 'test', 'fixtures', 'ecr-repositories-response.json')).read(), content_type="application/x-amz-json-1.1") with captured_output() as (out, err): kubernetes.k8s_update(TEST_KUBE_CONTEXT, filename=self.__class__.kubernetes_yml, skip_checks=True) mocked_sys_exit.assert_called_once_with(0) # self.assertIn('deployment.apps "hello-web" unchanged', out.getvalue().strip()) # self.assertIn('service "hello-web" unchanged', out.getvalue().strip()) self.assertIn( 'Updated Kubernetes environment %s' % self.__class__.kubernetes_yml, out.getvalue().strip())
def test_masked_verbose_output(self): with mock_verbosity(True): with captured_output() as (out, err): verbose(TEST_MESSAGE, mask=(r'^(\w*).*$', r'\1 ***')) self.assertEqual( out.getvalue().strip(), "\x1b[33m==> hokusai exec `%s`\n\x1b[0m" % 'Ohai ***')
def test_get_arg_parser_argument_type(self): parser = _get_arg_parser(parse_me_full_docstring, [str, int], [("one", NoDefault), ("two", NoDefault), ("three", 12)], ":") with captured_output(): self.assertRaises(SystemExit, parser.parse_args, "yes i_should_be_an_int".split())
def test_get_arg_parser_required_arguments(self): parser = _get_arg_parser(parse_me_full_docstring, [str, int], [("one", NoDefault), ("two", NoDefault), ("three", 12)], ":") with captured_output(): self.assertRaises(SystemExit, parser.parse_args, "we_are_missing_two".split())
def test_shout(self, mocked_check_output): with captured_output() as (out, err): retval = shout('whoami') self.assertEqual(retval, 'hokusai') self.assertEqual(type(retval), str) mocked_check_output.assert_called_once_with('whoami', shell=True, stderr=-2)
def test_help_is_complete(self): with captured_output() as (out, _): self.assertRaises(SystemExit, Parseable.parser.parse_args, ["-h"]) help_message = out.getvalue() self.assertIn("parseable", help_message) # Private methods and classmethods are not exposed by default self.assertNotIn("private_method", help_message) self.assertNotIn("cls_method", help_message)
def test_03_k8s_delete(self, mocked_sys_exit): with captured_output() as (out, err): kubernetes.k8s_delete(TEST_KUBE_CONTEXT, yaml_file_name='minikube') mocked_sys_exit.assert_called_once_with(0) self.assertIn( 'Deleted Kubernetes environment %s' % self.__class__.kubernetes_yml, out.getvalue().strip())
def test_03_k8s_status(self, mocked_sys_exit): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=self.fixture('sts-get-caller-identity-response.xml'), content_type="application/xml") httpretty.register_uri( httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=self.fixture('ecr-repositories-response.json'), content_type="application/x-amz-json-1.1") with captured_output() as (out, err): kubernetes.k8s_status(TEST_KUBE_CONTEXT, True, False, False, False, filename=self.__class__.kubernetes_yml) mocked_sys_exit.assert_called_once_with(0) self.assertIn('Resources', out.getvalue().strip()) # self.assertIn('deployment.apps/hello-web', out.getvalue().strip()) # self.assertIn('service/hello-web', out.getvalue().strip()) mocked_sys_exit.reset_mock() kubernetes.k8s_status(TEST_KUBE_CONTEXT, True, False, True, False, filename=self.__class__.kubernetes_yml) mocked_sys_exit.assert_called_once_with(0) self.assertIn('Resources', out.getvalue().strip()) # self.assertIn('Name: hello-web', out.getvalue().strip()) # self.assertIn('Selector: app=hello', out.getvalue().strip()) mocked_sys_exit.reset_mock() kubernetes.k8s_status(TEST_KUBE_CONTEXT, False, True, False, False, filename=self.__class__.kubernetes_yml) mocked_sys_exit.assert_called_once_with(0) self.assertIn('Pods', out.getvalue().strip()) # self.assertIn('hello-web', out.getvalue().strip()) # self.assertTrue('ContainerCreating' in out.getvalue().strip() or 'Running' in out.getvalue().strip()) mocked_sys_exit.reset_mock() kubernetes.k8s_status(TEST_KUBE_CONTEXT, False, True, True, False, filename=self.__class__.kubernetes_yml) mocked_sys_exit.assert_called_once_with(0) # self.assertIn('Name: hello-web', out.getvalue().strip()) mocked_sys_exit.reset_mock()
def test_get_tracks_cmd(self): """Exercise the get-tracks sub-command""" args = ['get-tracks', '-c', 'cp1252', self._pls1B2B] with captured_output() as (out, err): status = rubepl.main(args) output = set(filter(lambda x: 0 != len(x), out.getvalue().split('\n'))) assert output == set(map(lambda x: x[0], self._TRACKS)) errors = err.getvalue().strip() assert '' == errors
def test_01_k8s_update(self, mocked_sys_exit): with captured_output() as (out, err): kubernetes.k8s_update(TEST_KUBE_CONTEXT, yaml_file_name='minikube') mocked_sys_exit.assert_called_once_with(0) # self.assertIn('deployment.apps "hello-web" unchanged', out.getvalue().strip()) # self.assertIn('service "hello-web" unchanged', out.getvalue().strip()) self.assertIn( 'Updated Kubernetes environment %s' % self.__class__.kubernetes_yml, out.getvalue().strip())
def test_00_k8s_create(self, mocked_sys_exit): with captured_output() as (out, err): kubernetes.k8s_create(TEST_KUBE_CONTEXT, filename=self.__class__.kubernetes_yml) mocked_sys_exit.assert_called_once_with(0) # self.assertIn('deployment.apps "hello-web" created', out.getvalue().strip()) # self.assertIn('service "hello-web" created', out.getvalue().strip()) self.assertIn( 'Created Kubernetes environment %s' % self.__class__.kubernetes_yml, out.getvalue().strip())
def test_playlists_xml_to_m3u_cmd(self): """Exercise the get-playlists-xml sub-command""" args = ['get-playlists-xml', '-o', self._tmp, '-y', 'Athens 2002', self._db, self._pl] with captured_output() as (out, err): status = rubepl.main(args) print(out.getvalue().strip()) print(err.getvalue().strip()) with open(os.path.join(self._tmp, 'Athens 2002.m3u'), 'r') as fh: text = fh.read() assert text == self._ATHENS
def test_02_k8s_status(self, mocked_sys_exit): with captured_output() as (out, err): kubernetes.k8s_status(TEST_KUBE_CONTEXT, True, False, False, False, yaml_file_name='minikube') mocked_sys_exit.assert_called_once_with(0) self.assertIn('Resources', out.getvalue().strip()) # self.assertIn('deployment.apps/hello-web', out.getvalue().strip()) # self.assertIn('service/hello-web', out.getvalue().strip()) mocked_sys_exit.reset_mock() kubernetes.k8s_status(TEST_KUBE_CONTEXT, True, False, True, False, yaml_file_name='minikube') mocked_sys_exit.assert_called_once_with(0) self.assertIn('Resources', out.getvalue().strip()) # self.assertIn('Name: hello-web', out.getvalue().strip()) # self.assertIn('Selector: app=hello', out.getvalue().strip()) mocked_sys_exit.reset_mock() kubernetes.k8s_status(TEST_KUBE_CONTEXT, False, True, False, False, yaml_file_name='minikube') mocked_sys_exit.assert_called_once_with(0) self.assertIn('Pods', out.getvalue().strip()) # self.assertIn('hello-web', out.getvalue().strip()) # self.assertTrue('ContainerCreating' in out.getvalue().strip() or 'Running' in out.getvalue().strip()) mocked_sys_exit.reset_mock() kubernetes.k8s_status(TEST_KUBE_CONTEXT, False, True, True, False, yaml_file_name='minikube') mocked_sys_exit.assert_called_once_with(0) # self.assertIn('Name: hello-web', out.getvalue().strip()) mocked_sys_exit.reset_mock()
def test_05_k8s_delete(self, mocked_sys_exit): httpretty.register_uri( httpretty.POST, "https://sts.amazonaws.com/", body=self.fixture('sts-get-caller-identity-response.xml'), content_type="application/xml") httpretty.register_uri( httpretty.POST, "https://api.ecr.us-east-1.amazonaws.com/", body=self.fixture('ecr-repositories-response.json'), content_type="application/x-amz-json-1.1") with captured_output() as (out, err): kubernetes.k8s_delete(TEST_KUBE_CONTEXT, filename=self.__class__.kubernetes_yml) mocked_sys_exit.assert_called_once_with(0) self.assertIn( 'Deleted Kubernetes environment %s' % self.__class__.kubernetes_yml, out.getvalue().strip())
def test_invalid_commands(self): invalid_commands = [ 'rockblock send imei user pass', 'rockblock send imei user pass "Hello, World" --data-format=foo', 'rockblock send imei user pass 48656c6c6f2c20576f726c6421y --data-format=hex', 'rockblock send imei user pass SGVsbG8sIFdvcmxkIQ= --data-format=base64', 'rockblock listen console 0.0.0.0', 'rockblock listen console 0.0.0.0 animal', 'rockblock listen csv 0.0.0.0 80', 'rockblock listen mqtt 0.0.0.0 80 localhost 1883 my/mqtt/topic --mqtt-qos=9', 'rockblock listen mqtt 0.0.0.0 80 localhost 1883', ] for command in invalid_commands: try: with captured_output(): parser.parse_args(split(command)[1:]) self.fail( 'No exception parsing invalid command: {}'.format(command)) except: pass
def test_parse_class_classmethod_are_not_sub_command(self): with captured_output(): with self.assertRaises(SystemExit): NeedParsing.parser.call("12 parse-me-if-you-can one 2")
def test_parse_class_do_not_expose_private_methods(self): with captured_output(): with self.assertRaises(SystemExit): ShowMyDocstring.parser.parse_args("will-not-appear 12".split()) with self.assertRaises(SystemExit): ShowMyDocstring.parser.parse_args("str".split())
def test_returncode(self, mocked_call): with captured_output() as (out, err): self.assertEqual(returncode('whoami'), 0) mocked_call.assert_called_once_with('whoami', shell=True, stderr=-2)
def test_shout(self, mocked_check_output): with captured_output() as (out, err): self.assertEqual(shout('whoami'), 'hokusai') mocked_check_output.assert_called_once_with('whoami', shell=True, stderr=-2)
def test_non_verbose_output(self): with mock_verbosity(False): with captured_output() as (out, err): verbose(TEST_MESSAGE) self.assertEqual(out.getvalue().strip(), '')
def test_verbose_returns_input(self): with mock_verbosity(True): with captured_output() as (out, err): msg = verbose(TEST_MESSAGE) self.assertEqual(msg, TEST_MESSAGE)
def test_print_red(self): with captured_output() as (out, err): print_red(TEST_MESSAGE) self.assertEqual(out.getvalue().strip(), "\x1b[31m%s\x1b[0m" % TEST_MESSAGE)
def test_command_catches_exceptions(self, mocked_sys_exit): with captured_output() as (out, err): foo_command(False) mocked_sys_exit.assert_called_once_with(-1) self.assertIn('ERROR: Bad command', out.getvalue().strip())
def tearDownClass(cls): with captured_output() as (out, err): shout(cls.kctl.command("delete all --selector testFixture=true"), print_output=True)