def test_update(self): with patch('sys.stderr', StringIO()): with self.assertRaises(SystemExit): cli.doit(('update', '--id', 'abcdefgh', '--slug', 'foo'), '/dev/null') self.assertRegexpMatches( sys.stderr.getvalue(), r'error: argument --slug: not allowed with argument --id') with self.assertRaisesRegexp(SystemExit, r'Must specify --name, --id, or --slug'): cli.doit(('update', '--periodicity', '120'), '/dev/null') add_response('update?id=abcdefgh&name=freedle') cli.doit(('update', '--id', 'abcdefgh', '--name', 'freedle'), '/dev/null') add_response('get?name=froodle&auth_key=arglebargle', body='{"status": "ok", "canary": {"id": "bcdefghi"}}') add_response('update?description=foodesc&id=bcdefghi&' 'auth_key=arglebargle') cli.doit(('update', '--name', 'froodle', '--description', 'foodesc', '--auth-key', 'arglebargle'), '/dev/null') # Same as above but without --auth-key, for code branch coverage add_response('get?name=froodle', body='{"status": "ok", "canary": {"id": "bcdefghi"}}') add_response('update?description=foodesc&id=bcdefghi') cli.doit(('update', '--name', 'froodle', '--description', 'foodesc'), '/dev/null')
def test_configure(self): host = 'hellohost' port = '12345' auth_key = 'arglebargle' with tempfile.NamedTemporaryFile(delete=False) as tf: cli.doit(('configure', '--host', host, '--port', port, '--auth-key', auth_key), tf.name) config = SafeConfigParser() config.read([tf.name]) self.assertEqual(host, config['coal-mine']['host']) self.assertEqual(port, config['coal-mine']['port']) self.assertEqual(auth_key, config['coal-mine']['auth-key']) add_response('pause?name=froodle&auth_key=arglebargle', host=host, port=port) cli.doit(('pause', '--name', 'froodle'), tf.name) cli.doit(('configure', '--no-auth-key'), tf.name) config = SafeConfigParser() config.read([tf.name]) with self.assertRaises(KeyError): self.assertEqual(auth_key, config['coal-mine']['auth-key']) add_response('pause?name=froodle', host=host, port=port) cli.doit(('pause', '--name', 'froodle'), tf.name) cli.doit(('configure', '--host', host + '2'), tf.name) config = SafeConfigParser() config.read([tf.name]) self.assertEqual(host + '2', config['coal-mine']['host'])
def test_list(self): add_response('list') cli.doit(('list', ), '/dev/null') add_response('list?search=foo&late=True&paused=True') cli.doit(('list', '--paused', '--late', '--search', 'foo'), '/dev/null')
def test_update_no_history(self): add_response('get?name=froodle', body='{"status": "ok", "canary": {"id": "bcdefghi"}}') add_response('update?description=foodesc&id=bcdefghi', body=self.update_body) with patch('sys.stdout', StringIO()): cli.doit(('update', '--name', 'froodle', '--description', 'foodesc', '--no-history'), '/dev/null') self.assertNotIn("{'history': []}", sys.stdout.getvalue())
def test_list_no_history(self): add_response('list') cli.doit(('list',), '/dev/null') add_response('list?search=foo&late=True&paused=True&verbose=True', body=self.list_body) with patch('sys.stdout', StringIO()): cli.doit(('list', '--verbose', '--no-history', '--paused', '--late', '--search', 'foo'), '/dev/null') self.assertNotIn("{'history': []}", sys.stdout.getvalue())
def test_create(self): with patch("sys.stderr", StringIO()): with self.assertRaises(SystemExit): cli.doit(("create", "--name", "froodle"), "/dev/null") self.assertRegexpMatches( sys.stderr.getvalue(), r"error: the following arguments are required: --periodicity" ) add_response("create?paused=False&name=froodle&periodicity=60.0") cli.doit(("create", "--name", "froodle", "--periodicity", "60"), "/dev/null")
def test_create(self): with patch('sys.stderr', StringIO()): with self.assertRaises(SystemExit): cli.doit(('create', '--name', 'froodle'), '/dev/null') self.assertRegexpMatches( sys.stderr.getvalue(), r'error: the following arguments are required: --periodicity') add_response('create?paused=False&name=froodle&periodicity=60.0') cli.doit(('create', '--name', 'froodle', '--periodicity', '60'), '/dev/null')
def test_update(self): with patch("sys.stderr", StringIO()): with self.assertRaises(SystemExit): cli.doit(("update", "--id", "abcdefgh", "--slug", "foo"), "/dev/null") self.assertRegexpMatches(sys.stderr.getvalue(), r"error: argument --slug: not allowed with argument --id") with self.assertRaisesRegexp(SystemExit, r"Must specify --name, --id, or --slug"): cli.doit(("update", "--periodicity", "120"), "/dev/null") add_response("update?id=abcdefgh&name=freedle") cli.doit(("update", "--id", "abcdefgh", "--name", "freedle"), "/dev/null") add_response("get?name=froodle&auth_key=arglebargle", body='{"status": "ok", "canary": {"id": "bcdefghi"}}') add_response("update?description=foodesc&id=bcdefghi&" "auth_key=arglebargle") cli.doit(("update", "--name", "froodle", "--description", "foodesc", "--auth-key", "arglebargle"), "/dev/null")
def test_configure(self): host = "hellohost" port = "12345" auth_key = "arglebargle" with tempfile.NamedTemporaryFile(delete=False) as tf: cli.doit(("configure", "--host", host, "--port", port, "--auth-key", auth_key), tf.name) config = SafeConfigParser() config.read([tf.name]) self.assertEqual(host, config["coal-mine"]["host"]) self.assertEqual(port, config["coal-mine"]["port"]) self.assertEqual(auth_key, config["coal-mine"]["auth-key"]) add_response("pause?name=froodle&auth_key=arglebargle", host=host, port=port) cli.doit(("pause", "--name", "froodle"), tf.name) cli.doit(("configure", "--no-auth-key"), tf.name) config = SafeConfigParser() config.read([tf.name]) with self.assertRaises(KeyError): self.assertEqual(auth_key, config["coal-mine"]["auth-key"]) add_response("pause?name=froodle", host=host, port=port) cli.doit(("pause", "--name", "froodle"), tf.name)
def test_get(self): add_response('get?name=froodle') cli.doit(('get', '--name', 'froodle'), '/dev/null')
def test_get(self): add_response('get?name=froodle', body=self.get_body) with patch('sys.stdout', StringIO()): cli.doit(('get', '--name', 'froodle'), '/dev/null') self.assertIn("{'history': []}", sys.stdout.getvalue())
def test_trigger(self): add_response("trigger?name=froodle") cli.doit(("trigger", "--name", "froodle"), "/dev/null")
def test_delete(self): add_response('delete?slug=froodle') cli.doit(('delete', '--slug', 'froodle'), '/dev/null')
def test_unpause(self): add_response("unpause?name=froodle") cli.doit(("unpause", "--name", "froodle"), "/dev/null")
def test_bad_response(self): with self.assertRaisesRegexp(SystemExit, r"^Not Found$"): add_response("get?name=froodle", status=404, body="Not Found") cli.doit(("get", "--name", "froodle"), "/dev/null")
def test_list(self): add_response("list") cli.doit(("list",), "/dev/null") add_response("list?search=foo&late=True&paused=True") cli.doit(("list", "--paused", "--late", "--search", "foo"), "/dev/null")
def test_bad_response(self): with self.assertRaisesRegexp(SystemExit, r'^Not Found$'): add_response('get?name=froodle', status=404, body='Not Found') cli.doit(('get', '--name', 'froodle'), '/dev/null')
def test_trigger(self): add_response('trigger?name=froodle') cli.doit(('trigger', '--name', 'froodle'), '/dev/null')
def test_delete(self): add_response("delete?slug=froodle") cli.doit(("delete", "--slug", "froodle"), "/dev/null")
def test_no_command(self): with patch("sys.stderr", StringIO()): with self.assertRaises(SystemExit): cli.doit((), "/dev/null") self.assertRegexpMatches(sys.stderr.getvalue(), r"No command specified")
def test_unpause_no_history(self): add_response('unpause?name=froodle', body=self.unpause_body) with patch('sys.stdout', StringIO()): cli.doit(('unpause', '--no-history', '--name', 'froodle'), '/dev/null') self.assertNotIn("'history': []", sys.stdout.getvalue())
def test_unpause(self): add_response('unpause?name=froodle') cli.doit(('unpause', '--name', 'froodle'), '/dev/null')
def test_no_command(self): with patch('sys.stderr', StringIO()): with self.assertRaises(SystemExit): cli.doit((), '/dev/null') self.assertRegexpMatches(sys.stderr.getvalue(), r'No command specified')
def test_get(self): add_response("get?name=froodle") cli.doit(("get", "--name", "froodle"), "/dev/null")