James Iter 9 лет назад
Родитель
Сommit
e91a319954
9 измененных файлов с 438 добавлено и 287 удалено
  1. 20 0
      api/base.py
  2. 4 20
      api/os_init.py
  3. 13 24
      api/os_init_write.py
  4. 2 20
      api/os_template.py
  5. 3 3
      api_route_table.py
  6. 1 1
      docs/todo.md
  7. 85 45
      tests/test_os_init.py
  8. 220 116
      tests/test_os_init_write.py
  9. 90 58
      tests/test_os_template.py

+ 20 - 0
api/base.py

@@ -199,3 +199,23 @@ class Base(object):
             return ret
         except ji.PreviewingError, e:
             return json.loads(e.message)
+
+    def delete(self, ids=None, ids_rule=None, by_field=None):
+        the_instance = self.the_class()
+
+        args_rules = [
+            ids_rule
+        ]
+
+        try:
+            ji.Check.previewing(args_rules, {ids_rule[1]: ids})
+
+            ret = dict()
+            ret['state'] = ji.Common.exchange_state(20000)
+
+            the_instance.delete_by_filter(filter_str=':'.join([by_field, 'in', ids]))
+
+            return ret
+        except ji.PreviewingError, e:
+            return json.loads(e.message)
+

+ 4 - 20
api/os_init.py

@@ -115,27 +115,11 @@ def r_update(_id):
 
 
 @Utils.dumps2response
-def r_delete(_id):
-    os_init = OSInit()
-
-    args_rules = [
-        Rules.ID.value
-    ]
-    os_init.id = _id
-
-    try:
-        ji.Check.previewing(args_rules, os_init.__dict__)
-
-        if os_init.exist():
-            os_init.delete()
-            OSInitWrite.delete_by_filter(filter_str='os_init_id:in:' + os_init.id.__str__())
-        else:
-            ret = dict()
-            ret['state'] = ji.Common.exchange_state(40401)
-            return ret
+def r_delete(ids):
+    os_init_write_base = Base(the_class=OSInitWrite)
+    os_init_write_base.delete(ids=ids, ids_rule=Rules.IDS.value, by_field='os_init_id')
 
-    except ji.PreviewingError, e:
-        return json.loads(e.message)
+    return os_init_base.delete(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
 
 
 @Utils.dumps2response

+ 13 - 24
api/os_init_write.py

@@ -64,14 +64,21 @@ def r_create():
             ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ': ', os_init.id.__str__()])
             return ret
 
-        if os_init_write.exist_by('path'):
+        data, total = os_init_write.get_by_filter(
+            filter_str=':'.join(['os_init_id','eq',os_init_write.os_init_id.__str__()]) + ';' +
+                       ':'.join(['path', 'eq', os_init_write.path]))
+
+        if data.__len__() > 0:
             ret['state'] = ji.Common.exchange_state(40901)
-            ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ': ', os_init_write.path])
+            ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ', path: ', os_init_write.path,
+                                                    ', os_init_id: ', os_init_write.os_init_id.__str__()])
             return ret
 
         os_init_write.create()
-        os_init_write.get_by('path')
-        ret['data'] = os_init_write.__dict__
+        data, total = os_init_write.get_by_filter(
+            filter_str=':'.join(['os_init_id','eq',os_init_write.os_init_id.__str__()]) + ';' +
+                       ':'.join(['path', 'eq', os_init_write.path]))
+        ret['data'] = data[0]
         return ret
     except ji.PreviewingError, e:
         return json.loads(e.message)
@@ -122,26 +129,8 @@ def r_update(_id):
 
 
 @Utils.dumps2response
-def r_delete(_id):
-    os_init_write = OSInitWrite()
-
-    args_rules = [
-        Rules.ID.value
-    ]
-    os_init_write.id = _id
-
-    try:
-        ji.Check.previewing(args_rules, os_init_write.__dict__)
-
-        if os_init_write.exist():
-            os_init_write.delete()
-        else:
-            ret = dict()
-            ret['state'] = ji.Common.exchange_state(40401)
-            return ret
-
-    except ji.PreviewingError, e:
-        return json.loads(e.message)
+def r_delete(ids):
+    return os_init_write_base.delete(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
 
 
 @Utils.dumps2response

+ 2 - 20
api/os_template.py

@@ -129,26 +129,8 @@ def r_update(_id):
 
 
 @Utils.dumps2response
-def r_delete(_id):
-    os_template = OSTemplate()
-
-    args_rules = [
-        Rules.ID.value
-    ]
-    os_template.id = _id
-
-    try:
-        ji.Check.previewing(args_rules, os_template.__dict__)
-
-        if os_template.exist():
-            os_template.delete()
-        else:
-            ret = dict()
-            ret['state'] = ji.Common.exchange_state(40401)
-            return ret
-
-    except ji.PreviewingError, e:
-        return json.loads(e.message)
+def r_delete(ids):
+    return os_template_base.delete(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
 
 
 @Utils.dumps2response

+ 3 - 3
api_route_table.py

@@ -27,14 +27,14 @@ add_rule_api(config.blueprint, '', api_func='config.r_get', methods=['GET'])
 # 系统初始化配置操作
 add_rule_api(os_init.blueprint, '', api_func='os_init.r_create', methods=['POST'])
 add_rule_api(os_init.blueprint, '/<_id>', api_func='os_init.r_update', methods=['PATCH'])
-add_rule_api(os_init.blueprint, '/<_id>', api_func='os_init.r_delete', methods=['DELETE'])
+add_rule_api(os_init.blueprints, '/<ids>', api_func='os_init.r_delete', methods=['DELETE'])
 add_rule_api(os_init.blueprints, '/<ids>', api_func='os_init.r_get', methods=['GET'])
 add_rule_api(os_init.blueprints, '', api_func='os_init.r_get_by_filter', methods=['GET'])
 add_rule_api(os_init.blueprints, '/_search', api_func='os_init.r_content_search', methods=['GET'])
 
 add_rule_api(os_init_write.blueprint, '', api_func='os_init_write.r_create', methods=['POST'])
 add_rule_api(os_init_write.blueprint, '/<_id>', api_func='os_init_write.r_update', methods=['PATCH'])
-add_rule_api(os_init_write.blueprint, '/<_id>', api_func='os_init_write.r_delete', methods=['DELETE'])
+add_rule_api(os_init_write.blueprints, '/<ids>', api_func='os_init_write.r_delete', methods=['DELETE'])
 add_rule_api(os_init_write.blueprints, '/<ids>', api_func='os_init_write.r_get', methods=['GET'])
 add_rule_api(os_init_write.blueprints, '', api_func='os_init_write.r_get_by_filter', methods=['GET'])
 add_rule_api(os_init_write.blueprints, '/_search', api_func='os_init_write.r_content_search', methods=['GET'])
@@ -42,7 +42,7 @@ add_rule_api(os_init_write.blueprints, '/_search', api_func='os_init_write.r_con
 # 系统模板操作
 add_rule_api(os_template.blueprint, '', api_func='os_template.r_create', methods=['POST'])
 add_rule_api(os_template.blueprint, '/<_id>', api_func='os_template.r_update', methods=['PATCH'])
-add_rule_api(os_template.blueprint, '/<_id>', api_func='os_template.r_delete', methods=['DELETE'])
+add_rule_api(os_template.blueprints, '/<ids>', api_func='os_template.r_delete', methods=['DELETE'])
 add_rule_api(os_template.blueprints, '/<ids>', api_func='os_template.r_get', methods=['GET'])
 add_rule_api(os_template.blueprints, '', api_func='os_template.r_get_by_filter', methods=['GET'])
 add_rule_api(os_template.blueprints, '/_search', api_func='os_template.r_content_search', methods=['GET'])

+ 1 - 1
docs/todo.md

@@ -8,7 +8,7 @@
 
 - [ ] os_init_write 的更新方法里,是否需要支持 os_init_id 参数
 - [ ] 增加模板镜像上传功能
-- [ ] 抽象出视图层的父类,尤其是 get、get_list、delete 这些方法基本相同
+- [x] 抽象出视图层的父类,尤其是 get、get_list、delete 这些方法基本相同
 - [x] guest_disk 表中加入 path 字段,具体磁盘位置由 path 指明
 - [x] guest 的系统磁盘,也需记录到 guest_disk 表中
 - [x] 将 Guest 的系统镜像合并到单独的磁盘镜像目录中。即不再为 Guest 的系统镜像单独分配目录

+ 85 - 45
tests/test_os_init.py

@@ -24,19 +24,19 @@ class TestOSInit(unittest.TestCase):
     def tearDown(self):
         pass
 
-    # 创建系统初始化组
-    def test_11_create(self):
-        payload = {
-            "name": 'CentOS-Systemd',
-            "remark": u'用作红帽 Systemd 系列的系统初始化。初始化操作依据 CentOS 7 来实现。'
-        }
-
-        url = TestOSInit.base_url + '/os_init'
-        headers = {'content-type': 'application/json'}
-        r = requests.post(url, data=json.dumps(payload), headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        self.assertEqual('200', j_r['state']['code'])
+    # # 创建系统初始化组
+    # def test_11_create(self):
+    #     payload = {
+    #         "name": 'CentOS-Systemd',
+    #         "remark": u'用作红帽 Systemd 系列的系统初始化。初始化操作依据 CentOS 7 来实现。'
+    #     }
+    #
+    #     url = TestOSInit.base_url + '/os_init'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
 
     # 获取系统初始化组列表
     def test_12_get(self):
@@ -48,38 +48,78 @@ class TestOSInit(unittest.TestCase):
         TestOSInit.os_init_id = j_r['data'][0]['id']
         self.assertEqual('200', j_r['state']['code'])
 
-    # 创建更新系统初始化组
-    def test_13_update(self):
-        payload = {
-            "name": 'RedHat-Systemd'
-        }
-
-        url = TestOSInit.base_url + '/os_init/' + TestOSInit.os_init_id.__str__()
-        headers = {'content-type': 'application/json'}
-        r = requests.patch(url, data=json.dumps(payload), headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        self.assertEqual('200', j_r['state']['code'])
-
-    # 校验系统初始化组列表更新结果
-    def test_14_get(self):
-        url = TestOSInit.base_url + '/os_inits'
-        headers = {'content-type': 'application/json'}
-        r = requests.get(url, headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        self.assertEqual('200', j_r['state']['code'])
-        self.assertEqual('RedHat-Systemd', j_r['data'][0]['name'])
-
-    # 删除系统初始化组列表更新结果
-    def test_15_delete(self):
-        url = TestOSInit.base_url + '/os_init/' + TestOSInit.os_init_id.__str__()
-        headers = {'content-type': 'application/json'}
-        r = requests.delete(url, headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        self.assertEqual('200', j_r['state']['code'])
-
+    # # 创建更新系统初始化组
+    # def test_13_update(self):
+    #     payload = {
+    #         "name": 'RedHat-Systemd'
+    #     }
+    #
+    #     url = TestOSInit.base_url + '/os_init/' + TestOSInit.os_init_id.__str__()
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.patch(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # # 校验系统初始化组列表更新结果
+    # def test_14_get(self):
+    #     url = TestOSInit.base_url + '/os_inits'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.get(url, headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #     self.assertEqual('RedHat-Systemd', j_r['data'][0]['name'])
+    #
+    # # 删除系统初始化组列表更新结果
+    # def test_15_delete(self):
+    #     url = TestOSInit.base_url + '/os_inits/' + TestOSInit.os_init_id.__str__()
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.delete(url, headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # def test_21_create(self):
+    #     payload = {
+    #         "name": 'Gentoo-OpenRC',
+    #         "remark": u'Gentoo startup process。'
+    #     }
+    #
+    #     url = TestOSInit.base_url + '/os_init'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # # 创建系统初始化组
+    # def test_22_create(self):
+    #     payload = {
+    #         "name": 'Ubuntu-Upstart',
+    #         "remark": u'Ubuntu startup process。'
+    #     }
+    #
+    #     url = TestOSInit.base_url + '/os_init'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # # 创建系统初始化组
+    # def test_23_create(self):
+    #     payload = {
+    #         "name": 'CentOS-SysV',
+    #         "remark": u'用作CentOS SysV 系列的系统初始化。初始化操作依据 CentOS 6.8 来实现。'
+    #     }
+    #
+    #     url = TestOSInit.base_url + '/os_init'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
 
 if __name__ == '__main__':
     unittest.main()

+ 220 - 116
tests/test_os_init_write.py

@@ -25,122 +25,226 @@ class TestOSInitWrite(unittest.TestCase):
     def tearDown(self):
         pass
 
-    # 创建系统初始化组
-    def test_11_create(self):
-        payload = {
-            "name": "CentOS-Systemd",
-            "remark": u"用作红帽 Systemd 系列的系统初始化。初始化操作依据 CentOS 7 来实现。"
-        }
-
-        url = TestOSInitWrite.base_url + '/os_init'
-        headers = {'content-type': 'application/json'}
-        r = requests.post(url, data=json.dumps(payload), headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        TestOSInitWrite.os_init_id = j_r['data']['id']
-        self.assertEqual('200', j_r['state']['code'])
-
-    # 添加系统初始化操作
-    def test_21_create(self):
-        payload = {
-            "os_init_id": TestOSInitWrite.os_init_id,
-            "path": "/etc/resolv.conf",
-            "content": "\n".join([
-                "nameserver {DNS1}",
-                "nameserver {DNS2}"
-            ])
-        }
-
-        url = TestOSInitWrite.base_url + '/os_init_write'
-        headers = {'content-type': 'application/json'}
-        r = requests.post(url, data=json.dumps(payload), headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        self.assertEqual('200', j_r['state']['code'])
-
-    # 添加系统初始化操作
-    def test_22_create(self):
-        payload = {
-            "os_init_id": TestOSInitWrite.os_init_id,
-            "path": "/etc/sysconfig/network-scripts/ifcfg-eth0",
-            "content": "\n".join([
-                "DEVICE=eth0",
-                "TYPE=Ethernet",
-                "ONBOOT=yes",
-                "BOOTPROTO=\"static\"",
-                "IPADDR={IP}",
-                "NETMASK={NETMASK}",
-                "GATEWAY={GATEWAY}",
-                "DNS1={DNS1}",
-                "DNS2={DNS2}",
-                "IPV6INIT=no",
-                "NAME=eth0"
-            ])
-        }
-
-        url = TestOSInitWrite.base_url + '/os_init_write'
-        headers = {'content-type': 'application/json'}
-        r = requests.post(url, data=json.dumps(payload), headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        self.assertEqual('200', j_r['state']['code'])
-
-    # 添加系统初始化操作
-    def test_23_create(self):
-        payload = {
-            "os_init_id": TestOSInitWrite.os_init_id,
-            "path": "/etc/hostname",
-            "content": "hostname"
-        }
-
-        url = TestOSInitWrite.base_url + '/os_init_write'
-        headers = {'content-type': 'application/json'}
-        r = requests.post(url, data=json.dumps(payload), headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        TestOSInitWrite.os_init_write_id = j_r['data']['id']
-        self.assertEqual('200', j_r['state']['code'])
-
-    def test_25_get_list(self):
-        url = TestOSInitWrite.base_url + '/os_init_writes'
-        headers = {'content-type': 'application/json'}
-        r = requests.get(url, headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        self.assertEqual('200', j_r['state']['code'])
-
-    def test_26_update(self):
-        payload = {
-            "path": "/etc/hostname",
-            "content": "{HOSTNAME}"
-        }
-
-        url = TestOSInitWrite.base_url + '/os_init_write/' + TestOSInitWrite.os_init_write_id.__str__()
-        headers = {'content-type': 'application/json'}
-        r = requests.patch(url, data=json.dumps(payload), headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        self.assertEqual('200', j_r['state']['code'])
-
-    @unittest.skip('skip delete os init write!')
-    def test_27_delete(self):
-        url = TestOSInitWrite.base_url + '/os_init_write/' + TestOSInitWrite.os_init_write_id.__str__()
-        headers = {'content-type': 'application/json'}
-        r = requests.delete(url, headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        self.assertEqual('200', j_r['state']['code'])
-
-    @unittest.skip('skip delete os init!')
-    # 删除系统初始化组列表更新结果
-    def test_31_delete(self):
-        url = TestOSInitWrite.base_url + '/os_init/' + TestOSInitWrite.os_init_id.__str__()
-        headers = {'content-type': 'application/json'}
-        r = requests.delete(url, headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        self.assertEqual('200', j_r['state']['code'])
-
+    # # 创建系统初始化组
+    # def test_11_create(self):
+    #     payload = {
+    #         "name": "CentOS-Systemd",
+    #         "remark": u"用作红帽 Systemd 系列的系统初始化。初始化操作依据 CentOS 7 来实现。"
+    #     }
+    #
+    #     url = TestOSInitWrite.base_url + '/os_init'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     TestOSInitWrite.os_init_id = j_r['data']['id']
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # # 添加系统初始化操作
+    # def test_21_create(self):
+    #     payload = {
+    #         "os_init_id": TestOSInitWrite.os_init_id,
+    #         "path": "/etc/resolv.conf",
+    #         "content": "\n".join([
+    #             "nameserver {DNS1}",
+    #             "nameserver {DNS2}"
+    #         ])
+    #     }
+    #
+    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # # 添加系统初始化操作
+    # def test_22_create(self):
+    #     payload = {
+    #         "os_init_id": TestOSInitWrite.os_init_id,
+    #         "path": "/etc/sysconfig/network-scripts/ifcfg-eth0",
+    #         "content": "\n".join([
+    #             "DEVICE=eth0",
+    #             "TYPE=Ethernet",
+    #             "ONBOOT=yes",
+    #             "BOOTPROTO=\"static\"",
+    #             "IPADDR={IP}",
+    #             "NETMASK={NETMASK}",
+    #             "GATEWAY={GATEWAY}",
+    #             "DNS1={DNS1}",
+    #             "DNS2={DNS2}",
+    #             "IPV6INIT=no",
+    #             "NAME=eth0"
+    #         ])
+    #     }
+    #
+    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # # 添加系统初始化操作
+    # def test_23_create(self):
+    #     payload = {
+    #         "os_init_id": TestOSInitWrite.os_init_id,
+    #         "path": "/etc/hostname",
+    #         "content": "hostname"
+    #     }
+    #
+    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     TestOSInitWrite.os_init_write_id = j_r['data']['id']
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # def test_25_get_list(self):
+    #     url = TestOSInitWrite.base_url + '/os_init_writes'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.get(url, headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # def test_26_update(self):
+    #     payload = {
+    #         "path": "/etc/hostname",
+    #         "content": "{HOSTNAME}"
+    #     }
+    #
+    #     url = TestOSInitWrite.base_url + '/os_init_write/' + TestOSInitWrite.os_init_write_id.__str__()
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.patch(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # @unittest.skip('skip delete os init write!')
+    # def test_27_delete(self):
+    #     url = TestOSInitWrite.base_url + '/os_init_writes/' + TestOSInitWrite.os_init_write_id.__str__()
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.delete(url, headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # @unittest.skip('skip delete os init!')
+    # # 删除系统初始化组列表更新结果
+    # def test_31_delete(self):
+    #     url = TestOSInitWrite.base_url + '/os_init/' + TestOSInitWrite.os_init_id.__str__()
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.delete(url, headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # def test_51_create(self):
+    #     payload = {
+    #         "os_init_id": 8,
+    #         "path": "/etc/resolv.conf",
+    #         "content": "\n".join([
+    #             "nameserver {DNS1}",
+    #             "nameserver {DNS2}"
+    #         ])
+    #     }
+    #
+    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # def test_52_create(self):
+    #     payload = {
+    #         "os_init_id": 8,
+    #         "path": "/etc/sysconfig/network-scripts/ifcfg-eth0",
+    #         "content": "\n".join([
+    #             "DEVICE=eth0",
+    #             "TYPE=Ethernet",
+    #             "ONBOOT=yes",
+    #             "BOOTPROTO=\"static\"",
+    #             "IPADDR={IP}",
+    #             "NETMASK={NETMASK}",
+    #             "GATEWAY={GATEWAY}",
+    #             "IPV6INIT=no",
+    #             "NAME=eth0"
+    #         ])
+    #     }
+    #
+    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # def test_53_create(self):
+    #     payload = {
+    #         "os_init_id": 8,
+    #         "path": "/etc/hostname",
+    #         "content": "{HOSTNAME}"
+    #     }
+    #
+    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     TestOSInitWrite.os_init_write_id = j_r['data']['id']
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # def test_61_create(self):
+    #     payload = {
+    #         "os_init_id": 7,
+    #         "path": "/etc/resolv.conf",
+    #         "content": "\n".join([
+    #             "nameserver {DNS1}",
+    #             "nameserver {DNS2}"
+    #         ])
+    #     }
+    #
+    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # def test_62_create(self):
+    #     payload = {
+    #         "os_init_id": 7,
+    #         "path": "/etc/conf.d/net",
+    #         "content": "\n".join([
+    #             "config_eth0=\"{IP}/{NETMASK}\"",
+    #             "routes_eth0=\"default via {GATEWAY}\""
+    #         ])
+    #     }
+    #
+    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # def test_63_create(self):
+    #     payload = {
+    #         "os_init_id": 7,
+    #         "path": "/etc/conf.d/hostname",
+    #         "content": "{HOSTNAME}"
+    #     }
+    #
+    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     TestOSInitWrite.os_init_write_id = j_r['data']['id']
+    #     self.assertEqual('200', j_r['state']['code'])
 
 if __name__ == '__main__':
     unittest.main()

+ 90 - 58
tests/test_os_template.py

@@ -24,64 +24,96 @@ class TestOSTemplate(unittest.TestCase):
     def tearDown(self):
         pass
 
-    # 创建系统模板
-    def test_11_create(self):
-        payload = {
-            "label": "CentOS-7.2",
-            "path": "template_pool/centos72_multi-user_2016-09-15_128G.qcow2",
-            "active": True,
-            "os_init_id": 3
-        }
-
-        url = TestOSTemplate.base_url + '/os_template'
-        headers = {'content-type': 'application/json'}
-        r = requests.post(url, data=json.dumps(payload), headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        TestOSTemplate.os_template_id = j_r['data']['id']
-        self.assertEqual('200', j_r['state']['code'])
-
-    # 获取系统模板列表
-    def test_12_get(self):
-        url = TestOSTemplate.base_url + '/os_templates'
-        headers = {'content-type': 'application/json'}
-        r = requests.get(url, headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        self.assertEqual('200', j_r['state']['code'])
-
-    # 更新系统模板
-    def test_13_update(self):
-        payload = {
-            "label": 'CentOS-72'
-        }
-
-        url = TestOSTemplate.base_url + '/os_template/' + TestOSTemplate.os_template_id.__str__()
-        headers = {'content-type': 'application/json'}
-        r = requests.patch(url, data=json.dumps(payload), headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        self.assertEqual('200', j_r['state']['code'])
-
-    # 校验系统模板更新结果
-    def test_14_get(self):
-        url = TestOSTemplate.base_url + '/os_templates'
-        headers = {'content-type': 'application/json'}
-        r = requests.get(url, headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        self.assertEqual('200', j_r['state']['code'])
-        self.assertEqual('CentOS-72', j_r['data'][0]['label'])
-
-    @unittest.skip('skip delete os template!')
-    # 删除系统模板
-    def test_15_delete(self):
-        url = TestOSTemplate.base_url + '/os_template/' + TestOSTemplate.os_template_id.__str__()
-        headers = {'content-type': 'application/json'}
-        r = requests.delete(url, headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        self.assertEqual('200', j_r['state']['code'])
+    # # 创建系统模板
+    # def test_11_create(self):
+    #     payload = {
+    #         "label": "CentOS-7.2",
+    #         "path": "template_pool/centos72_multi-user_2016-09-15_128G.qcow2",
+    #         "active": True,
+    #         "os_init_id": 3
+    #     }
+    #
+    #     url = TestOSTemplate.base_url + '/os_template'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     TestOSTemplate.os_template_id = j_r['data']['id']
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # # 获取系统模板列表
+    # def test_12_get(self):
+    #     url = TestOSTemplate.base_url + '/os_templates'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.get(url, headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # # 更新系统模板
+    # def test_13_update(self):
+    #     payload = {
+    #         "label": 'CentOS-72'
+    #     }
+    #
+    #     url = TestOSTemplate.base_url + '/os_template/' + TestOSTemplate.os_template_id.__str__()
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.patch(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # # 校验系统模板更新结果
+    # def test_14_get(self):
+    #     url = TestOSTemplate.base_url + '/os_templates'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.get(url, headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #     self.assertEqual('CentOS-72', j_r['data'][0]['label'])
+    #
+    # @unittest.skip('skip delete os template!')
+    # # 删除系统模板
+    # def test_15_delete(self):
+    #     url = TestOSTemplate.base_url + '/os_templates/' + TestOSTemplate.os_template_id.__str__()
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.delete(url, headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # def test_21_create(self):
+    #     payload = {
+    #         "label": "Gentoo",
+    #         "path": "template_pool/gentoo_2016-08-12_128G.qcow2",
+    #         "active": True,
+    #         "os_init_id": 7
+    #     }
+    #
+    #     url = TestOSTemplate.base_url + '/os_template'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     TestOSTemplate.os_template_id = j_r['data']['id']
+    #     self.assertEqual('200', j_r['state']['code'])
+    #
+    # def test_22_create(self):
+    #     payload = {
+    #         "label": "CentOS-6.8",
+    #         "path": "template_pool/P_centos68_multi-user_2016-09-28_128G.qcow2",
+    #         "active": True,
+    #         "os_init_id": 8
+    #     }
+    #
+    #     url = TestOSTemplate.base_url + '/os_template'
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     TestOSTemplate.os_template_id = j_r['data']['id']
+    #     self.assertEqual('200', j_r['state']['code'])
 
 
 if __name__ == '__main__':