test_os_init_write.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 = 0
  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. # 添加系统初始化操作
  87. def test_24_create(self):
  88. payload = {
  89. "os_init_id": TestOSInitWrite.os_init_id,
  90. "path": "/etc/hostname",
  91. "content": "{HOSTNAME}"
  92. }
  93. url = TestOSInitWrite.base_url + '/os_init_write'
  94. headers = {'content-type': 'application/json'}
  95. r = requests.post(url, data=json.dumps(payload), headers=headers)
  96. j_r = json.loads(r.content)
  97. print json.dumps(j_r, ensure_ascii=False)
  98. self.assertEqual('40901', j_r['state']['sub']['code'])
  99. def test_25_get(self):
  100. url = TestOSInitWrite.base_url + '/os_init_writes'
  101. headers = {'content-type': 'application/json'}
  102. r = requests.get(url, headers=headers)
  103. j_r = json.loads(r.content)
  104. print json.dumps(j_r, ensure_ascii=False)
  105. self.assertEqual('200', j_r['state']['code'])
  106. def test_26_update(self):
  107. payload = {
  108. "os_init_id": TestOSInitWrite.os_init_id,
  109. "path": "/etc/hostname",
  110. "content": "{HOSTNAME}"
  111. }
  112. url = TestOSInitWrite.base_url + '/os_init_write/' + TestOSInitWrite.os_init_write_id.__str__()
  113. headers = {'content-type': 'application/json'}
  114. r = requests.patch(url, data=json.dumps(payload), 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. @unittest.skip('skip delete os init write!')
  119. def test_27_delete(self):
  120. url = TestOSInitWrite.base_url + '/os_init_write/' + TestOSInitWrite.os_init_write_id.__str__()
  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. @unittest.skip('skip delete os init!')
  127. # 删除系统初始化组列表更新结果
  128. def test_31_delete(self):
  129. url = TestOSInitWrite.base_url + '/os_init/' + TestOSInitWrite.os_init_id.__str__()
  130. headers = {'content-type': 'application/json'}
  131. r = requests.delete(url, headers=headers)
  132. j_r = json.loads(r.content)
  133. print json.dumps(j_r, ensure_ascii=False)
  134. self.assertEqual('200', j_r['state']['code'])
  135. if __name__ == '__main__':
  136. unittest.main()