def test_create_app(self): app_data = {} url = "http://target/apps" httpretty.register_uri(httpretty.POST, url, body=json.dumps(app_data), status=200) cl = client.Client("http://target", "abc123") data = { "name": "myapp", "platform": "python", "pool": "deadpool", "tag": ["tag 1", "tag 2"], } cl.apps.create(**data) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"]) self.assertEqual(data["name"], httpretty.last_request().parsed_body["name"][0]) self.assertEqual(data["platform"], httpretty.last_request().parsed_body["platform"][0]) self.assertEqual(data["pool"], httpretty.last_request().parsed_body["pool"][0]) self.assertEqual(data["tag"], httpretty.last_request().parsed_body["tag"])
class MachinesTestCase(unittest.TestCase): cl = client.Client("http://target", "abc123") def setUp(self): httpretty.enable() def tearDown(self): httpretty.disable() httpretty.reset() def test_list(self): url = "http://target/iaas/machines" data = [{}] httpretty.register_uri(httpretty.GET, url, body=json.dumps(data), status=200) self.cl.machines.list() self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"]) def test_delete(self): url = "http://target/iaas/machines/12345" httpretty.register_uri(httpretty.DELETE, url, status=200) self.cl.machines.delete(machine_id=12345) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"])
def test_deploys_list_with_querystring_param(self): all_deploys = [{ "ID": "791", "App": "critical-app", "Image": "v1", }, { "ID": "1000", "App": "critical-app", "Image": "v2", }] httpretty.register_uri( httpretty.GET, "http://api.tsuru.target/deploys?app=critical-app", status=200, content_type='application/json', body=json.dumps(all_deploys)) params = {"app": "critical-app"} client_instance = client.Client("http://api.tsuru.target", "tsuru_token") result = client_instance.deploys.list(**params) self.assertEqual(all_deploys, result) request = httpretty.last_request() expected = {"app": ["critical-app"]} self.assertEqual(expected, request.querystring)
def test_create_plan(self): plan_data = {} url = "http://target/plans" httpretty.register_uri( httpretty.POST, url, body=json.dumps(plan_data), status=200 ) cl = client.Client("http://target", "abc123") data = { "name": "myplan", "memory": "123", "swap": "456", "cpu": "789", "router": "myrouter" } cl.plans.create(**data) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"]) self.assertEqual(data["name"], httpretty.last_request().parsed_body["name"][0]) self.assertEqual(data["memory"], httpretty.last_request().parsed_body["memory"][0]) self.assertEqual(data["swap"], httpretty.last_request().parsed_body["swap"][0]) self.assertEqual(data["cpu"], httpretty.last_request().parsed_body["cpu"][0]) self.assertEqual(data["router"], httpretty.last_request().parsed_body["router"][0])
class HealingsTestCase(unittest.TestCase): cl = client.Client("http://target", "abc123") def setUp(self): httpretty.enable() def tearDown(self): httpretty.disable() httpretty.reset() def test_list(self): url = "http://target/1.2/healing/node" data = [{}] httpretty.register_uri(httpretty.GET, url, body=json.dumps(data), status=200) self.cl.healings.list() self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"]) def test_remove(self): url = "http://target/1.2/healing/node" httpretty.register_uri(httpretty.DELETE, url, status=200) self.cl.healings.remove(pool="abc") self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"]) def test_update(self): url = "http://target/1.2/healing/node" httpretty.register_uri(httpretty.POST, url, status=200) self.cl.healings.update(**{"pool": "abc"}) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"])
def test_remove_app(self): url = "http://target/apps/appname" httpretty.register_uri(httpretty.DELETE, url, status=200) cl = client.Client("http://target", "abc123") cl.apps.remove("appname") self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"])
def test_remove(self): url = "http://target/iaas/templates/mytemplate" httpretty.register_uri(httpretty.DELETE, url, status=200) cl = client.Client("http://target", "token") cl.templates.remove("mytemplate") self.assertEqual("bearer token", httpretty.last_request().headers["authorization"])
def test_rebalance(self): url = "http://target/node/rebalance" httpretty.register_uri(httpretty.POST, url, body="", status=200) cl = client.Client("http://target", "abc123") cl.pools.rebalance("dev") result = httpretty.last_request().body.decode('utf-8') expected = "MetadataFilter.pool=dev" self.assertEqual(expected, result)
def __init__(self, pool): try: self.tsuru_target = os.environ['TSURU_TARGET'].rstrip("/") self.tsuru_token = os.environ['TSURU_TOKEN'] self.client = client.Client(self.tsuru_target, self.tsuru_token) except KeyError: raise KeyError("TSURU_TARGET or TSURU_TOKEN envs not set") try: self.user = self.client.users.info() except Exception as ex: raise Exception("Failed to get current user info: {}".format(ex)) self.pool = pool
def test_create(self): url = "http://target/iaas/templates" httpretty.register_uri(httpretty.POST, url, status=201) cl = client.Client("http://target", "token") cl.templates.create("mytemplate", "myiaas", key="value", another_key="val") self.assertEqual("bearer token", httpretty.last_request().headers["authorization"])
def test_rebalance(self): url = "http://target/docker/containers/rebalance" httpretty.register_uri(httpretty.POST, url, body=json.dumps("{}"), status=200) cl = client.Client("http://target", "abc123") cl.pools.rebalance("dev") result = json.loads(httpretty.last_request().body) expected = {"metadataFilter": {"pool": "dev"}} self.assertDictEqual(expected, result)
def test_deploys_list_when_has_no_deploys(self): httpretty.register_uri(httpretty.GET, "http://api.tsuru.target/deploys", status=204, content_type='application/json', body={}) client_instance = client.Client("http://api.tsuru.target", "tsuru_token") result = client_instance.deploys.list() self.assertEqual([], result)
def test_get_app(self): app_data = {} url = "http://target/apps/appname" httpretty.register_uri(httpretty.GET, url, body=json.dumps(app_data), status=200) cl = client.Client("http://target", "abc123") result = cl.apps.get("appname") self.assertDictEqual({}, result) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"])
def test_list_apps_handle_no_content(self): apps_data = [] url = "http://target/apps" httpretty.register_uri(httpretty.GET, url, body=json.dumps(apps_data), status=204) cl = client.Client("http://target", "abc123") response = cl.apps.list() self.assertListEqual([], response) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"])
def test_list(self): template_data = [] url = "http://target/iaas/templates" httpretty.register_uri(httpretty.GET, url, body=json.dumps(template_data), status=200) cl = client.Client("http://target", "token") result = cl.templates.list() self.assertListEqual(result, template_data) self.assertEqual("bearer token", httpretty.last_request().headers["authorization"])
def test_move(self): url = "http://target/docker/containers/move" httpretty.register_uri(httpretty.POST, url, body=json.dumps("{}"), status=200, content_type='application/x-json-stream') cl = client.Client("http://target", "abc123") cl.containers.move("node1", "node2") request = httpretty.last_request() expected = {"from": ["node1"], "to": ["node2"]} self.assertDictEqual(expected, request.querystring)
def test_deploys_list_with_deploy_id(self): deploy_data = {"ID": "1001", "App": "critical-app"} httpretty.register_uri(httpretty.GET, "http://api.tsuru.target/deploys/1001", status=200, content_type='application/json', body=json.dumps(deploy_data)) client_instance = client.Client("http://api.tsuru.target", "tsuru_token") result = client_instance.deploys.list(deploy_id="1001") self.assertEqual(deploy_data, result)
def test_info(self): data = {} url = "http://target/users/info" httpretty.register_uri( httpretty.GET, url, body=json.dumps(data), status=200 ) cl = client.Client("http://target", "token") result = cl.users.info() self.assertDictEqual(result, data) self.assertEqual("bearer token", httpretty.last_request().headers["authorization"])
def runTest(): runner = TsuruRunner() tsuruClient = client.Client(runner.endpoint, runner.token) threads = [] for conf in runner.config["envs"]: app = TsuruApp(runner, conf["pool"], conf["plan"], tsuruClient) threads.append(app) app.start() for thread in threads: thread.join(MAX_DEPLOY_TIME) runner.buffer.printMessage() runner.resetTarget() exit(0)
def test_list_plans(self): plans_data = [] url = "http://target/plans" httpretty.register_uri( httpretty.GET, url, body=json.dumps(plans_data), status=200 ) cl = client.Client("http://target", "abc123") result = cl.plans.list() self.assertListEqual([], result) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"])
def test_create(self): url = "http://target/iaas/templates" httpretty.register_uri( httpretty.POST, url, status=201 ) cl = client.Client("http://target", "token") cl.templates.create("mytemplate", "myiaas", key="value", another_key="val") self.assertEqual("bearer token", httpretty.last_request().headers["authorization"]) result = httpretty.last_request().body.decode('utf-8') expected = "another_key=val&IaaSName=myiaas&Name=mytemplate&key=value" self.assertEqual(expected, result)
def test_restart_app(self): app_data = {} url = "http://target/apps/myapp/restart" httpretty.register_uri(httpretty.POST, url, body=json.dumps(app_data), status=200) cl = client.Client("http://target", "abc123") data = {"appname": "myapp", "process": "myprocess"} cl.apps.restart(**data) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"]) self.assertEqual(data["process"], httpretty.last_request().parsed_body["process"][0])
def test_create_app(self): app_data = {} url = "http://target/apps" httpretty.register_uri(httpretty.POST, url, body=json.dumps(app_data), status=200) cl = client.Client("http://target", "abc123") data = { "name": "appname", "framework": "framework", } cl.apps.create(**data) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"])
def test_list_apps_by_query_string(self): apps_data = [] url = "http://target/apps" httpretty.register_uri(httpretty.GET, url, body=json.dumps(apps_data), status=200) cl = client.Client("http://target", "abc123") cl.apps.list(name="appname", pool="testpool") self.assertEqual({ 'name': ['appname'], 'pool': ['testpool'] }, httpretty.last_request().querystring) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"])
def test_update_app(self): app_data = {} url = "http://target/apps/appname" httpretty.register_uri(httpretty.PUT, url, body=json.dumps(app_data), status=200) cl = client.Client("http://target", "abc123") data = {"pool": "mypool", "plan": "myplan", "router": "myrouter"} cl.apps.update("appname", **data) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"]) self.assertEqual(data["pool"], httpretty.last_request().parsed_body["pool"][0]) self.assertEqual(data["plan"], httpretty.last_request().parsed_body["plan"][0]) self.assertEqual(data["router"], httpretty.last_request().parsed_body["router"][0])
def test_deploys_list(self): all_deploys = [{ "ID": "1", "App": "just-another-app", "Image": "v1" }, { "ID": "2", "App": "critical-app", "Image": "v1" }] httpretty.register_uri(httpretty.GET, "http://api.tsuru.target/deploys", status=200, content_type='application/json', body=json.dumps(all_deploys)) client_instance = client.Client("http://api.tsuru.target", "tsuru_token") result = client_instance.deploys.list() self.assertEqual(all_deploys, result)
class EventsTestCase(unittest.TestCase): cl = client.Client("http://target", "abc123") def setUp(self): httpretty.enable() def tearDown(self): httpretty.disable() httpretty.reset() def test_list(self): url = "http://target/1.1/events" httpretty.register_uri(httpretty.GET, url, status=200) self.cl.events.list(kindname="node.create") self.assertEqual("/1.1/events?kindname=node.create", httpretty.last_request().path) def test_list_kinds(self): url = "http://target/1.1/events/kinds" httpretty.register_uri(httpretty.GET, url, status=200) self.cl.events.list_kinds() self.assertEqual("/1.1/events/kinds", httpretty.last_request().path) def test_get(self): url = "http://target/1.1/events/123" httpretty.register_uri(httpretty.GET, url, status=200) self.cl.events.get(123) self.assertEqual("/1.1/events/123", httpretty.last_request().path) def test_cancel(self): url = "http://target/1.1/events/123/cancel" httpretty.register_uri(httpretty.POST, url, status=200) self.cl.events.cancel(123) self.assertEqual("/1.1/events/123/cancel", httpretty.last_request().path)
def test_create_node(self): node_data = { "Status": "ready", "Metadata": { "pool": "tsuru2", "iaas": "ec2", "LastSuccess": "2015-11-16T18:44:36-02:00", }, "Address": "http://127.0.0.3:4243" } url = "http://target/docker/node" httpretty.register_uri(httpretty.POST, url, body=json.dumps(node_data), status=200) cl = client.Client("http://target", "abc123") data = { "address": "127.0.0.3:4243", "pool": "tsuru2", "register": "false" } result = cl.nodes.create(**data) self.assertDictEqual(result.json(), node_data) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"]) self.assertIn("register", httpretty.last_request().body) self.assertIn("false", httpretty.last_request().body) result = cl.nodes.create(**data) self.assertDictEqual(result.json(), node_data) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"]) self.assertIn("register", httpretty.last_request().body) self.assertIn("false", httpretty.last_request().body) data = { "address": "127.0.0.3:4243", "pool": "tsuru2", "register": "true" } result = cl.nodes.create(**data) self.assertDictEqual(result.json(), node_data) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"]) self.assertIn("register", httpretty.last_request().body) self.assertIn("true", httpretty.last_request().body) data = { "address": "127.0.0.3:4243", "pool": "tsuru2", "register": "invalid" } result = cl.nodes.create(**data) self.assertDictEqual(result.json(), node_data) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"]) self.assertIn("register", httpretty.last_request().body) self.assertIn("invalid", httpretty.last_request().body)
class NodesTestCase(unittest.TestCase): cl = client.Client("http://target", "abc123") def setUp(self): httpretty.enable() def tearDown(self): httpretty.disable() httpretty.reset() def test_create_node(self): node_data = { "Status": "ready", "Metadata": { "pool": "tsuru2", "iaas": "ec2", "LastSuccess": "2015-11-16T18:44:36-02:00", }, "Address": "http://127.0.0.3:4243" } url = "http://target/1.2/node" httpretty.register_uri( httpretty.POST, url, body=json.dumps(node_data), status=200, content_type='application/x-json-stream', ) data = { "address": "127.0.0.3:4243", "pool": "tsuru2", "register": "false" } result = self.cl.nodes.create(**data) self.assertListEqual(list(result), [node_data]) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"]) self.assertIn("register", httpretty.last_request().body.decode('utf-8')) self.assertIn("false", httpretty.last_request().body.decode('utf-8')) result = self.cl.nodes.create(**data) self.assertListEqual(list(result), [node_data]) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"]) self.assertIn("register", httpretty.last_request().body.decode('utf-8')) self.assertIn("false", httpretty.last_request().body.decode('utf-8')) data = { "address": "127.0.0.3:4243", "pool": "tsuru2", "register": "true" } result = self.cl.nodes.create(**data) self.assertListEqual(list(result), [node_data]) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"]) self.assertIn("register", httpretty.last_request().body.decode('utf-8')) self.assertIn("true", httpretty.last_request().body.decode('utf-8')) data = { "address": "127.0.0.3:4243", "pool": "tsuru2", "register": "invalid" } result = self.cl.nodes.create(**data) self.assertListEqual(list(result), [node_data]) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"]) self.assertIn("register", httpretty.last_request().body.decode('utf-8')) self.assertIn("invalid", httpretty.last_request().body.decode('utf-8')) def test_list(self): data = { "nodes": [{ "Status": "ready", "Metadata": { "pool": "tsuru2", "iaas": "ec2", "LastSuccess": "2015-11-16T18:44:36-02:00", }, "Address": "http://127.0.0.3:4243" }] } url = "http://target/1.2/node" httpretty.register_uri(httpretty.GET, url, body=json.dumps(data), status=200) result = self.cl.nodes.list() self.assertDictEqual(result, data) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"]) def test_remove(self): url = "http://target/1.2/node/127.0.0.1:4243" httpretty.register_uri(httpretty.DELETE, url, status=200) data = {"no-rebalance": "true"} self.cl.nodes.remove(address="127.0.0.1:4243", **data) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"]) def test_update(self): url = "http://target/1.2/node" httpretty.register_uri(httpretty.PUT, url, status=200) data = {"enable": "true"} self.cl.nodes.update(address="127.0.0.1:4243", **data) self.assertEqual("bearer abc123", httpretty.last_request().headers["authorization"])
def client(self): target = settings.TSURU_HOST token = self.request.session.get('tsuru_token').split(" ")[-1] return client.Client(target, token)