Coverage for test_ali_security_group.py: 50%

26 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-11 16:30 +0800

1# encoding: utf-8 

2''' 

3Created on 2025年3月7日 

4 

5@author: jingyu.wy 

6''' 

7import unittest 

8import sys 

9import uuid 

10 

11from dotenv import load_dotenv 

12 

13from ansible_collections.alibaba.apsarastack.plugins.modules.ali_security_group import main as security_group_main 

14from ansible_collections.alibaba.apsarastack.plugins.modules.ali_vpc import main as vpc_main 

15from ansible_collections.alibaba.apsarastack.tests.test_utils import run_module, run_unittest_with_coverage 

16 

17 

18class Test(unittest.TestCase): 

19 

20 

21 def __init__(self, methodName:str="runTest") -> None: 

22 unittest.TestCase.__init__(self, methodName=methodName) 

23 self.name = "ansible_test_security_group_info_%s" % uuid.uuid1() 

24 self._vpc_info = { 

25 "cidr_block": "172.16.0.0/16", 

26 "vpc_name": self.name, 

27 "description": "create by ansible unit test", 

28 "tags": {"key1":"value1"} 

29 } 

30 

31 def setUp(self) -> None: 

32 unittest.TestCase.setUp(self) 

33 load_dotenv() 

34 vpc_args = self._vpc_info 

35 result = run_module(vpc_main, vpc_args) 

36 self._vpc_info['id'] = result['vpc']['vpc_id'] 

37 

38 def testCreateSecurityGroup(self): 

39 security_group_args = { 

40 "state": "absent", 

41 "name": self.name, 

42 "vpc_id": self._vpc_info['id'] 

43 } 

44 result = run_module(security_group_main, security_group_args) 

45 self.assertNotIn('failed', result) 

46 self.assertEqual(result['changed'], False) 

47 self.assertEqual(result['group']['group_name'], security_group_args['name']) 

48 

49 # vpc_args = { 

50 # "state": "present", 

51 # "cidr_block": "192.168.0.0/24", 

52 # "vpc_name": "ansible_test_vpc", 

53 # "description": "create by ansible unit test", 

54 # } 

55 # result = run_module(vpc_main, vpc_args) 

56 # self.assertNotIn('failed', result, result.get('msg', '')) 

57 # self.assertEqual(result['vpc']['vpc_name'], vpc_args['vpc_name']) 

58 

59 # vpc_args = { 

60 # "state": "present", 

61 # "cidr_block": "192.168.0.0/24", 

62 # "vpc_name": "ansible_test_vpc", 

63 # "description": "modify by ansible unit test", 

64 # } 

65 # result = run_module(vpc_main, vpc_args) 

66 # self.assertNotIn('failed', result, result.get('msg', '')) 

67 # self.assertTrue(result['changed']) 

68 # self.assertEqual(result['vpc']['description'], vpc_args['description']) 

69 

70 # vpc_args = { 

71 # "state": "present", 

72 # "cidr_block": "192.168.0.0/24", 

73 # "vpc_name": "ansible_test_vpc", 

74 # "description": "modify by ansible unit test", 

75 # "tags": {"key1":"value1"} 

76 # } 

77 # result = run_module(vpc_main, vpc_args) 

78 # self.assertNotIn('failed', result, result.get('msg', '')) 

79 # self.assertTrue(result['changed']) 

80 # # TODO: 无法读取到tag信息 

81 

82 # vpc_args = { 

83 # "state": "present", 

84 # "cidr_block": "192.168.0.0/24", 

85 # "vpc_name": "ansible_test_vpc", 

86 # "description": "modify by ansible unit test", 

87 # "purge_tags": True 

88 # } 

89 # result = run_module(vpc_main, vpc_args) 

90 # self.assertNotIn('failed', result, result.get('msg', '')) 

91 # self.assertTrue(result['changed']) 

92 

93 # vpc_args = { 

94 # "state": "absent", 

95 # "cidr_block": "192.168.0.0/24", 

96 # "vpc_id": result['vpc']['vpc_id'], 

97 # } 

98 # result = run_module(vpc_main, vpc_args) 

99 # self.assertNotIn('failed', result, result.get('msg', '')) 

100 

101 # vpc_args = { 

102 # "cidr_block": "192.168.0.0/24", 

103 # "vpc_name": "http://ansible_test_vpc", 

104 # "description": "modify by ansible unit test", 

105 # } 

106 # result = run_module(vpc_main, vpc_args) 

107 # self.assertIn('failed', result) 

108 # self.assertEqual(result['msg'], 'vpc_name can not start with http:// or https://') 

109 

110 # vpc_args = { 

111 # "cidr_block": "192.168.0.0/24", 

112 # "vpc_name": "ansible_test_vpc", 

113 # "description": "http://modify by ansible unit test", 

114 # } 

115 # result = run_module(vpc_main, vpc_args) 

116 # self.assertIn('failed', result) 

117 # self.assertEqual(result['msg'], 'description can not start with http:// or https://') 

118 

119 

120if __name__ == "__main__": 

121 # import sys;sys.argv = ['', 'Test.testCreateVpc'] 

122 run_unittest_with_coverage()