test_guest.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. disk_uuid = ''
  14. def setUp(self):
  15. pass
  16. def tearDown(self):
  17. pass
  18. # 创建Guest
  19. # @unittest.skip('skip create guest')
  20. def test_11_create(self):
  21. payload = {
  22. "cpu": 4,
  23. "memory": 4,
  24. "os_template_id": 1,
  25. "quantity": 1,
  26. "name": "",
  27. "password": "pswd.com",
  28. "lease_term": 100
  29. }
  30. url = TestGuest.base_url + '/guest'
  31. headers = {'content-type': 'application/json'}
  32. r = requests.post(url, data=json.dumps(payload), headers=headers)
  33. j_r = json.loads(r.content)
  34. print json.dumps(j_r, ensure_ascii=False)
  35. self.assertEqual('200', j_r['state']['code'])
  36. # 获取Guest列表
  37. def test_12_get(self):
  38. url = TestGuest.base_url + '/guests'
  39. headers = {'content-type': 'application/json'}
  40. r = requests.get(url, headers=headers)
  41. j_r = json.loads(r.content)
  42. print json.dumps(j_r, ensure_ascii=False)
  43. TestGuest.uuid = j_r['data'][-1]['uuid']
  44. self.assertEqual('200', j_r['state']['code'])
  45. # 更新Guest属性
  46. def test_13_update(self):
  47. payload = {
  48. "remark": "zabbix",
  49. }
  50. url = TestGuest.base_url + '/guest/' + TestGuest.uuid
  51. headers = {'content-type': 'application/json'}
  52. r = requests.patch(url, data=json.dumps(payload), headers=headers)
  53. j_r = json.loads(r.content)
  54. print json.dumps(j_r, ensure_ascii=False)
  55. self.assertEqual('200', j_r['state']['code'])
  56. # 校验更新结果
  57. def test_14_get(self):
  58. url = TestGuest.base_url + '/guests'
  59. headers = {'content-type': 'application/json'}
  60. r = requests.get(url, headers=headers)
  61. j_r = json.loads(r.content)
  62. print json.dumps(j_r, ensure_ascii=False)
  63. self.assertEqual('200', j_r['state']['code'])
  64. self.assertEqual('zabbix', j_r['data'][-1]['remark'])
  65. # 创建 Disk
  66. def test_21_create_disk(self):
  67. payload = {
  68. "size": 10
  69. }
  70. url = TestGuest.base_url + '/disk'
  71. headers = {'content-type': 'application/json'}
  72. r = requests.post(url, data=json.dumps(payload), headers=headers)
  73. j_r = json.loads(r.content)
  74. print json.dumps(j_r, ensure_ascii=False)
  75. self.assertEqual('200', j_r['state']['code'])
  76. def test_22_get_disks(self):
  77. url = TestGuest.base_url + '/disks'
  78. headers = {'content-type': 'application/json'}
  79. r = requests.get(url, headers=headers)
  80. j_r = json.loads(r.content)
  81. print json.dumps(j_r, ensure_ascii=False)
  82. TestGuest.disk_uuid = j_r['data'][-1]['uuid']
  83. self.assertEqual('200', j_r['state']['code'])
  84. # @unittest.skip('skip resize disk')
  85. def test_23_disk_resize(self):
  86. url = TestGuest.base_url + '/disk/_disk_resize/' + TestGuest.disk_uuid + '/2000'
  87. headers = {'content-type': 'application/json'}
  88. r = requests.put(url, headers=headers)
  89. j_r = json.loads(r.content)
  90. print json.dumps(j_r, ensure_ascii=False)
  91. self.assertEqual('200', j_r['state']['code'])
  92. def test_31_attach_disk(self):
  93. # TestGuest.uuid = '97eb8d05-0939-4b0e-afa9-21dc77a9ba89'
  94. # TestGuest.disk_uuid = '88b1dd46-621a-4b29-8eab-1024a94c4653'
  95. url = TestGuest.base_url + '/guest/_attach_disk/' + TestGuest.uuid + '/' + TestGuest.disk_uuid
  96. headers = {'content-type': 'application/json'}
  97. r = requests.put(url, headers=headers)
  98. j_r = json.loads(r.content)
  99. print json.dumps(j_r, ensure_ascii=False)
  100. self.assertEqual('200', j_r['state']['code'])
  101. # def test_32_detach_disk(self):
  102. # url = TestGuest.base_url + '/guest/_detach_disk/' + TestGuest.disk_uuid
  103. # headers = {'content-type': 'application/json'}
  104. # r = requests.put(url, headers=headers)
  105. # j_r = json.loads(r.content)
  106. # print json.dumps(j_r, ensure_ascii=False)
  107. # self.assertEqual('200', j_r['state']['code'])
  108. #
  109. # # 删除Guest
  110. # # @unittest.skip('skip delete guest')
  111. # def test_41_delete(self):
  112. # url = TestGuest.base_url + '/guests/' + TestGuest.uuid
  113. # headers = {'content-type': 'application/json'}
  114. # r = requests.delete(url, headers=headers)
  115. # j_r = json.loads(r.content)
  116. # print json.dumps(j_r, ensure_ascii=False)
  117. # self.assertEqual('200', j_r['state']['code'])
  118. #
  119. # def test_42_delete_disk(self):
  120. # url = TestGuest.base_url + '/disk/' + TestGuest.disk_uuid
  121. # headers = {'content-type': 'application/json'}
  122. # r = requests.delete(url, headers=headers)
  123. # j_r = json.loads(r.content)
  124. # print json.dumps(j_r, ensure_ascii=False)
  125. # self.assertEqual('200', j_r['state']['code'])
  126. if __name__ == '__main__':
  127. unittest.main()