test_guest.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/4'
  8. __contact__ = 'james.iter.cn@gmail.com'
  9. __copyright__ = '(c) 2017 by James Iter.'
  10. class TestGuest(unittest.TestCase):
  11. base_url = 'http://127.0.0.1:8008/api'
  12. uuid = ''
  13. def setUp(self):
  14. pass
  15. def tearDown(self):
  16. pass
  17. # 创建Guest
  18. @unittest.skip('skip create guest')
  19. def test_11_create(self):
  20. payload = {
  21. "cpu": 4,
  22. "memory": 4,
  23. "os_template_id": 1,
  24. "quantity": 2,
  25. "name": "",
  26. "password": "pswd.com",
  27. "lease_term": 100
  28. }
  29. url = TestGuest.base_url + '/guest'
  30. headers = {'content-type': 'application/json'}
  31. r = requests.post(url, data=json.dumps(payload), headers=headers)
  32. j_r = json.loads(r.content)
  33. print json.dumps(j_r, ensure_ascii=False)
  34. self.assertEqual('200', j_r['state']['code'])
  35. # 获取Guest列表
  36. def test_12_get(self):
  37. url = TestGuest.base_url + '/guests'
  38. headers = {'content-type': 'application/json'}
  39. r = requests.get(url, headers=headers)
  40. j_r = json.loads(r.content)
  41. print json.dumps(j_r, ensure_ascii=False)
  42. TestGuest.uuid = j_r['data'][0]['uuid']
  43. self.assertEqual('200', j_r['state']['code'])
  44. # 更新Guest属性
  45. def test_13_update(self):
  46. payload = {
  47. "remark": "zabbix",
  48. }
  49. url = TestGuest.base_url + '/guest/' + TestGuest.uuid
  50. headers = {'content-type': 'application/json'}
  51. r = requests.patch(url, data=json.dumps(payload), headers=headers)
  52. j_r = json.loads(r.content)
  53. print json.dumps(j_r, ensure_ascii=False)
  54. self.assertEqual('200', j_r['state']['code'])
  55. # 校验更新结果
  56. def test_14_get(self):
  57. url = TestGuest.base_url + '/guests'
  58. headers = {'content-type': 'application/json'}
  59. r = requests.get(url, headers=headers)
  60. j_r = json.loads(r.content)
  61. print json.dumps(j_r, ensure_ascii=False)
  62. self.assertEqual('200', j_r['state']['code'])
  63. self.assertEqual('zabbix', j_r['data'][0]['remark'])
  64. # 删除Guest
  65. # @unittest.skip('skip delete guest')
  66. def test_15_delete(self):
  67. url = TestGuest.base_url + '/guests/' + TestGuest.uuid
  68. headers = {'content-type': 'application/json'}
  69. r = requests.delete(url, headers=headers)
  70. j_r = json.loads(r.content)
  71. print json.dumps(j_r, ensure_ascii=False)
  72. self.assertEqual('200', j_r['state']['code'])
  73. if __name__ == '__main__':
  74. unittest.main()