test_config.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import requests
  4. import json
  5. import unittest
  6. __author__ = 'James Iter'
  7. __date__ = '2017/4/3'
  8. __contact__ = 'james.iter.cn@gmail.com'
  9. __copyright__ = '(c) 2017 by James Iter.'
  10. class TestConfig(unittest.TestCase):
  11. base_url = 'http://127.0.0.1:8008/api'
  12. config_id = 0
  13. def setUp(self):
  14. pass
  15. def tearDown(self):
  16. pass
  17. # 创建 JimV 配置
  18. def test_11_create(self):
  19. payload = {
  20. "glusterfs_volume": "gv0",
  21. "vm_network": "net-br0",
  22. "vm_manage_network": "net-br0",
  23. "start_ip": "10.10.3.1",
  24. "end_ip": "10.10.6.254",
  25. "start_vnc_port": 15900,
  26. "netmask": "255.255.240.0",
  27. "gateway": "10.10.15.254",
  28. "dns1": "10.1.17.1",
  29. "dns2": "223.5.5.5",
  30. "rsa_private": "",
  31. "rsa_public": ""
  32. }
  33. url = TestConfig.base_url + '/config'
  34. headers = {'content-type': 'application/json'}
  35. r = requests.post(url, data=json.dumps(payload), headers=headers)
  36. j_r = json.loads(r.content)
  37. print json.dumps(j_r, ensure_ascii=False)
  38. TestConfig.config_id = j_r['data']['id']
  39. self.assertEqual('200', j_r['state']['code'])
  40. # 获取配置
  41. def test_12_get(self):
  42. url = TestConfig.base_url + '/config'
  43. headers = {'content-type': 'application/json'}
  44. r = requests.get(url, headers=headers)
  45. j_r = json.loads(r.content)
  46. print json.dumps(j_r, ensure_ascii=False)
  47. self.assertEqual('200', j_r['state']['code'])
  48. # 更新配置
  49. def test_13_update(self):
  50. payload = {
  51. "rsa_public": "what?"
  52. }
  53. url = TestConfig.base_url + '/config'
  54. headers = {'content-type': 'application/json'}
  55. r = requests.patch(url, data=json.dumps(payload), headers=headers)
  56. j_r = json.loads(r.content)
  57. print json.dumps(j_r, ensure_ascii=False)
  58. self.assertEqual('200', j_r['state']['code'])
  59. # 校验更新结果
  60. def test_14_get(self):
  61. url = TestConfig.base_url + '/config'
  62. headers = {'content-type': 'application/json'}
  63. r = requests.get(url, headers=headers)
  64. j_r = json.loads(r.content)
  65. print json.dumps(j_r, ensure_ascii=False)
  66. self.assertEqual('200', j_r['state']['code'])
  67. self.assertEqual('what?', j_r['data']['rsa_public'])
  68. # 删除配置
  69. def test_15_delete(self):
  70. url = TestConfig.base_url + '/config'
  71. headers = {'content-type': 'application/json'}
  72. r = requests.delete(url, headers=headers)
  73. self.assertEqual(405, r.status_code)
  74. if __name__ == '__main__':
  75. unittest.main()