Coverage for test_ali_vswtich.py: 75%
59 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-10 20:52 +0800
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-10 20:52 +0800
1# encoding: utf-8
2'''
3Created on 2025年3月7日
5@author: jingyu.wy
6'''
7import unittest
8import uuid
10from dotenv import load_dotenv
12from ansible_collections.alibaba.apsarastack.plugins.modules.ali_vpc import main as vpc_main
13from ansible_collections.alibaba.apsarastack.plugins.modules.ali_vswitch import main as vswitch_main
14from ansible_collections.alibaba.apsarastack.tests.test_utils import run_module, run_unittest_with_coverage
17class Test(unittest.TestCase):
19 def __init__(self, methodName:str="runTest") -> None:
20 unittest.TestCase.__init__(self, methodName=methodName)
21 self._vpc_info = {
22 "cidr_block": "172.16.0.0/16",
23 "vpc_name": "ansible_test_vswtich",
24 "description": "create by ansible unit test",
25 "tags": {"key1":"value1"}
26 }
28 def setUp(self)->None:
29 unittest.TestCase.setUp(self)
30 load_dotenv()
31 vpc_args = self._vpc_info
32 result = run_module(vpc_main, vpc_args)
33 self._vpc_info['id'] = result['vpc']['vpc_id']
35 def tearDown(self) -> None:
36 unittest.TestCase.tearDown(self)
37 vpc_args = {
38 "state": "absent",
39 } | self._vpc_info
40 try:
41 run_module(vpc_main, vpc_args)
42 except:
43 pass
45 def testDescribeVpcInfo(self):
47 vswtich_args = {
48 "state": "absent",
49 "cidr_block": "192.168.0.0/24",
50 "vpc_id": self._vpc_info['id'],
51 "id": 'not_exist_test',
52 }
53 result = run_module(vswitch_main, vswtich_args)
54 self.assertNotIn('failed', result)
55 self.assertFalse(result['changed'])
56 self.assertEqual(result['vswitch'], {})
58 vswtich_args = {
59 "vpc_id": self._vpc_info['id'],
60 "cidr_block": "172.16.1.0/24",
61 "name": 'ansible_test_vswtich',
62 }
63 result = run_module(vswitch_main, vswtich_args)
64 self.assertEqual(result['vswitch']['vpc_id'], self._vpc_info['id'])
65 self.assertEqual(result['vswitch']['vswitch_name'], vswtich_args['name'])
67 vswtich_args = {
68 "vpc_id": self._vpc_info['id'],
69 "id": result['vswitch']['id'],
70 "cidr_block": "172.16.1.0/24",
71 "name": 'modify_ansible_test_vswtich',
72 }
73 result = run_module(vswitch_main, vswtich_args)
74 self.assertNotIn('failed', result, result.get('msg', ''))
75 self.assertTrue(result['changed'])
76 self.assertEqual(result['vswitch']['vswitch_name'], vswtich_args['name'])
78 vswtich_args = {
79 "vpc_id": self._vpc_info['id'],
80 "id": result['vswitch']['id'],
81 "cidr_block": "172.16.1.0/24",
82 "tags": {"key1":"value1"}
83 }
84 result = run_module(vswitch_main, vswtich_args)
85 self.assertNotIn('failed', result, result.get('msg', ''))
86 self.assertTrue(result['changed'])
87 # TODO: 无法读取到tag信息
89 vswtich_args = {
90 "vpc_id": self._vpc_info['id'],
91 "id": result['vswitch']['id'],
92 "cidr_block": "172.16.1.0/24",
93 "purge_tags": True
94 }
95 result = run_module(vswitch_main, vswtich_args)
96 self.assertNotIn('failed', result, result.get('msg', ''))
97 self.assertTrue(result['changed'])
99 vswtich_args = {
100 "state": "absent",
101 "id": result['vswitch']['id'],
102 "vpc_id": self._vpc_info['id'],
103 "cidr_block": "172.16.1.0/24",
104 "name": 'ansible_test_vswtich',
105 }
106 result = run_module(vswitch_main, vswtich_args)
107 self.assertNotIn('failed', result, result.get('msg', ''))
109 vswtich_args = {
110 "vpc_id": self._vpc_info['id'],
111 "cidr_block": "172.16.1.0/24",
112 "name": 'http://ansible_test_vswtich',
113 }
114 result = run_module(vswitch_main, vswtich_args)
115 self.assertIn('failed', result)
116 self.assertEqual(result['msg'], 'vswitch_name can not start with http:// or https://')
118 vswtich_args = {
119 "vpc_id": self._vpc_info['id'],
120 "cidr_block": "172.16.1.0/24",
121 "name": 'ansible_test_vswtich',
122 "description": "http://modify by ansible unit test",
123 }
124 result = run_module(vswitch_main, vswtich_args)
125 self.assertIn('failed', result)
126 self.assertEqual(result['msg'], 'description can not start with http:// or https://')
129if __name__ == "__main__":
130 # import sys;sys.argv = ['', 'Test.testCreateVpc']
131 run_unittest_with_coverage()