Ray Milkey | ff18b6e | 2017-07-13 08:34:19 -0700 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | import unittest |
| 4 | from httplib import OK, BAD_REQUEST |
| 5 | |
| 6 | import requests |
| 7 | import json |
| 8 | from subprocess import Popen |
| 9 | import time |
| 10 | |
| 11 | |
| 12 | class TestOnosDistributedManager(unittest.TestCase): |
| 13 | |
| 14 | base_url = 'http://localhost:5000/' |
| 15 | cluster_json_url = base_url + 'cluster.json' |
| 16 | |
| 17 | def setUp(self): |
| 18 | command = ["onos-distributed-manager"] |
| 19 | self.server_process = Popen(command) |
| 20 | time.sleep(1) |
| 21 | |
| 22 | def tearDown(self): |
| 23 | requests.put(self.base_url + 'exit') |
| 24 | |
| 25 | def test_operations(self): |
| 26 | node1_json = '{"id":"node1","ip":"10.128.11.161","port":9876}' |
| 27 | node2_json = '{"id":"node2","ip":"10.128.11.162","port":9876}' |
| 28 | node3_json = '{"id":"node3","ip":"10.128.11.163","port":9876}' |
| 29 | node4_json = '{"id":"node4","ip":"10.128.11.164","port":9876}' |
| 30 | node5_json = '{"id":"node5","ip":"10.128.11.165","port":9876}' |
| 31 | new_node5_json = '{"id":"node5","ip":"10.128.11.265","port":1234}' |
| 32 | |
| 33 | node1_dict = json.loads(node1_json) |
| 34 | new_node5_dict = json.loads(new_node5_json) |
| 35 | |
| 36 | post_headers = { 'Content-Type': 'application/json' } |
| 37 | |
| 38 | def check_list_lengths(expected_length): |
| 39 | json_request = requests.get(self.cluster_json_url) |
| 40 | self.assertEqual(json_request.status_code, OK) |
| 41 | json_object = json_request.json() |
| 42 | self.assertEqual(len(json_object["nodes"]), expected_length) |
| 43 | self.assertEqual(len(json_object["partitions"]), expected_length) |
| 44 | |
| 45 | def post_node(node_json): |
| 46 | request = requests.post(self.base_url, |
| 47 | data=node_json, |
| 48 | headers=post_headers) |
| 49 | self.assertEqual(request.status_code, OK) |
| 50 | |
| 51 | # Test initial configuration - node list should be empty |
| 52 | check_list_lengths(0) |
| 53 | |
| 54 | # Test post operation to add a node |
| 55 | post_node(node1_json) |
| 56 | |
| 57 | json_request = requests.get(self.cluster_json_url) |
| 58 | self.assertEqual(json_request.status_code, OK) |
| 59 | json_object = json_request.json() |
| 60 | self.assertEqual(len(json_object["nodes"]), 1) |
| 61 | self.assertEqual(len(json_object["partitions"]), 1) |
| 62 | self.assertIn(node1_dict, json_object["nodes"]) |
| 63 | self.assertIn(node1_dict["id"], json_object["partitions"][0]["members"]) |
| 64 | |
| 65 | # Re-posting the same node should cause an error |
| 66 | json_request = requests.post(self.base_url, |
| 67 | data=node1_json, |
| 68 | headers=post_headers) |
| 69 | self.assertEqual(json_request.status_code, BAD_REQUEST) |
| 70 | check_list_lengths(1) |
| 71 | |
| 72 | # Add 4 more nodes and check the partitions generated |
| 73 | post_node(node2_json) |
| 74 | post_node(node3_json) |
| 75 | post_node(node4_json) |
| 76 | post_node(node5_json) |
| 77 | |
| 78 | check_list_lengths(5) |
| 79 | json_request = requests.get(self.cluster_json_url) |
| 80 | self.assertEqual(json_request.status_code, OK) |
| 81 | json_object = json_request.json() |
| 82 | self.assertEqual(["node4", "node5", "node1"], |
| 83 | json_object["partitions"][3]["members"]) |
| 84 | |
| 85 | # modify a node |
| 86 | json_request = requests.put(self.base_url, |
| 87 | data=new_node5_json, |
| 88 | headers=post_headers) |
| 89 | self.assertEqual(json_request.status_code, OK) |
| 90 | json_request = requests.get(self.cluster_json_url) |
| 91 | self.assertEqual(json_request.status_code, OK) |
| 92 | json_object = json_request.json() |
| 93 | self.assertIn(new_node5_dict, json_object["nodes"]) |
| 94 | |
| 95 | # delete all the nodes |
| 96 | def delete_node(json_string): |
| 97 | request = requests.delete(self.base_url, |
| 98 | data=json_string, |
| 99 | headers=post_headers) |
| 100 | self.assertEqual(request.status_code, OK) |
| 101 | |
| 102 | delete_node(new_node5_json) |
| 103 | delete_node(node4_json) |
| 104 | delete_node(node3_json) |
| 105 | delete_node(node2_json) |
| 106 | delete_node(node1_json) |
| 107 | |
| 108 | # make sure that no nodes remain |
| 109 | check_list_lengths(0) |
| 110 | |
| 111 | if __name__ == '__main__': |
| 112 | unittest.main() |