test_os_template.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/3/31'
  8. __contact__ = 'james.iter.cn@gmail.com'
  9. __copyright__ = '(c) 2017 by James Iter.'
  10. class TestOSTemplate(unittest.TestCase):
  11. base_url = 'http://127.0.0.1:8008/api'
  12. os_template_id = 0
  13. def setUp(self):
  14. pass
  15. def tearDown(self):
  16. pass
  17. # 创建系统模板
  18. def test_11_create(self):
  19. payload = {
  20. "label": "CentOS-7.2",
  21. "name": "centos72_multi-user_2016-09-15_128G.qcow2",
  22. "active": True,
  23. "os_init_id": 3
  24. }
  25. url = TestOSTemplate.base_url + '/os_template'
  26. headers = {'content-type': 'application/json'}
  27. r = requests.post(url, data=json.dumps(payload), headers=headers)
  28. j_r = json.loads(r.content)
  29. print json.dumps(j_r, ensure_ascii=False)
  30. TestOSTemplate.os_template_id = j_r['data']['id']
  31. self.assertEqual('200', j_r['state']['code'])
  32. # 获取系统模板列表
  33. def test_12_get(self):
  34. url = TestOSTemplate.base_url + '/os_templates'
  35. headers = {'content-type': 'application/json'}
  36. r = requests.get(url, headers=headers)
  37. j_r = json.loads(r.content)
  38. print json.dumps(j_r, ensure_ascii=False)
  39. self.assertEqual('200', j_r['state']['code'])
  40. # 更新系统模板
  41. def test_13_update(self):
  42. payload = {
  43. "label": 'CentOS-72'
  44. }
  45. url = TestOSTemplate.base_url + '/os_template/' + TestOSTemplate.os_template_id.__str__()
  46. headers = {'content-type': 'application/json'}
  47. r = requests.patch(url, data=json.dumps(payload), headers=headers)
  48. j_r = json.loads(r.content)
  49. print json.dumps(j_r, ensure_ascii=False)
  50. self.assertEqual('200', j_r['state']['code'])
  51. # 校验系统模板更新结果
  52. def test_14_get(self):
  53. url = TestOSTemplate.base_url + '/os_templates'
  54. headers = {'content-type': 'application/json'}
  55. r = requests.get(url, 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. self.assertEqual('CentOS-72', j_r['data'][0]['label'])
  60. @unittest.skip('skip delete os template!')
  61. # 删除系统模板
  62. def test_15_delete(self):
  63. url = TestOSTemplate.base_url + '/os_template/' + TestOSTemplate.os_template_id.__str__()
  64. headers = {'content-type': 'application/json'}
  65. r = requests.delete(url, headers=headers)
  66. j_r = json.loads(r.content)
  67. print json.dumps(j_r, ensure_ascii=False)
  68. self.assertEqual('200', j_r['state']['code'])
  69. if __name__ == '__main__':
  70. unittest.main()