test_os_init_write.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 TestOSInitWrite(unittest.TestCase):
  11. base_url = 'http://127.0.0.1:8008/api'
  12. os_init_id = 3
  13. os_init_write_id = 0
  14. def setUp(self):
  15. pass
  16. def tearDown(self):
  17. pass
  18. # 创建系统初始化组
  19. def test_11_create(self):
  20. payload = {
  21. "name": "CentOS-Systemd",
  22. "remark": u"用作红帽 Systemd 系列的系统初始化。初始化操作依据 CentOS 7 来实现。"
  23. }
  24. url = TestOSInitWrite.base_url + '/os_init'
  25. headers = {'content-type': 'application/json'}
  26. r = requests.post(url, data=json.dumps(payload), headers=headers)
  27. j_r = json.loads(r.content)
  28. print json.dumps(j_r, ensure_ascii=False)
  29. TestOSInitWrite.os_init_id = j_r['data']['id']
  30. self.assertEqual('200', j_r['state']['code'])
  31. # 添加系统初始化操作
  32. def test_21_create(self):
  33. payload = {
  34. "os_init_id": TestOSInitWrite.os_init_id,
  35. "path": "/etc/resolv.conf",
  36. "content": "\n".join([
  37. "nameserver {DNS1}",
  38. "nameserver {DNS2}"
  39. ])
  40. }
  41. url = TestOSInitWrite.base_url + '/os_init_write'
  42. headers = {'content-type': 'application/json'}
  43. r = requests.post(url, data=json.dumps(payload), headers=headers)
  44. j_r = json.loads(r.content)
  45. print json.dumps(j_r, ensure_ascii=False)
  46. self.assertEqual('200', j_r['state']['code'])
  47. # 添加系统初始化操作
  48. def test_22_create(self):
  49. payload = {
  50. "os_init_id": TestOSInitWrite.os_init_id,
  51. "path": "/etc/sysconfig/network-scripts/ifcfg-eth0",
  52. "content": "\n".join([
  53. "DEVICE=eth0",
  54. "TYPE=Ethernet",
  55. "ONBOOT=yes",
  56. "BOOTPROTO=\"static\"",
  57. "IPADDR={IP}",
  58. "NETMASK={NETMASK}",
  59. "GATEWAY={GATEWAY}",
  60. "DNS1={DNS1}",
  61. "DNS2={DNS2}",
  62. "IPV6INIT=no",
  63. "NAME=eth0"
  64. ])
  65. }
  66. url = TestOSInitWrite.base_url + '/os_init_write'
  67. headers = {'content-type': 'application/json'}
  68. r = requests.post(url, data=json.dumps(payload), headers=headers)
  69. j_r = json.loads(r.content)
  70. print json.dumps(j_r, ensure_ascii=False)
  71. self.assertEqual('200', j_r['state']['code'])
  72. # 添加系统初始化操作
  73. def test_23_create(self):
  74. payload = {
  75. "os_init_id": TestOSInitWrite.os_init_id,
  76. "path": "/etc/hostname",
  77. "content": "hostname"
  78. }
  79. url = TestOSInitWrite.base_url + '/os_init_write'
  80. headers = {'content-type': 'application/json'}
  81. r = requests.post(url, data=json.dumps(payload), headers=headers)
  82. j_r = json.loads(r.content)
  83. print json.dumps(j_r, ensure_ascii=False)
  84. TestOSInitWrite.os_init_write_id = j_r['data']['id']
  85. self.assertEqual('200', j_r['state']['code'])
  86. def test_25_get_list(self):
  87. url = TestOSInitWrite.base_url + '/os_init_writes'
  88. headers = {'content-type': 'application/json'}
  89. r = requests.get(url, headers=headers)
  90. j_r = json.loads(r.content)
  91. print json.dumps(j_r, ensure_ascii=False)
  92. self.assertEqual('200', j_r['state']['code'])
  93. def test_26_update(self):
  94. payload = {
  95. "path": "/etc/hostname",
  96. "content": "{HOSTNAME}"
  97. }
  98. url = TestOSInitWrite.base_url + '/os_init_write/' + TestOSInitWrite.os_init_write_id.__str__()
  99. headers = {'content-type': 'application/json'}
  100. r = requests.patch(url, data=json.dumps(payload), headers=headers)
  101. j_r = json.loads(r.content)
  102. print json.dumps(j_r, ensure_ascii=False)
  103. self.assertEqual('200', j_r['state']['code'])
  104. @unittest.skip('skip delete os init write!')
  105. def test_27_delete(self):
  106. url = TestOSInitWrite.base_url + '/os_init_write/' + TestOSInitWrite.os_init_write_id.__str__()
  107. headers = {'content-type': 'application/json'}
  108. r = requests.delete(url, headers=headers)
  109. j_r = json.loads(r.content)
  110. print json.dumps(j_r, ensure_ascii=False)
  111. self.assertEqual('200', j_r['state']['code'])
  112. @unittest.skip('skip delete os init!')
  113. # 删除系统初始化组列表更新结果
  114. def test_31_delete(self):
  115. url = TestOSInitWrite.base_url + '/os_init/' + TestOSInitWrite.os_init_id.__str__()
  116. headers = {'content-type': 'application/json'}
  117. r = requests.delete(url, headers=headers)
  118. j_r = json.loads(r.content)
  119. print json.dumps(j_r, ensure_ascii=False)
  120. self.assertEqual('200', j_r['state']['code'])
  121. if __name__ == '__main__':
  122. unittest.main()