Coverage for test_ali_vpc.py: 20%

44 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-22 16:05 +0800

1# encoding: utf-8 

2''' 

3Created on 2025年3月7日 

4 

5@author: jingyu.wy 

6''' 

7import unittest 

8 

9from dotenv import load_dotenv 

10 

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

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

13 

14 

15class Test(unittest.TestCase): 

16 

17 def setUp(self) -> None: 

18 unittest.TestCase.setUp(self) 

19 load_dotenv() 

20 

21 def testCreateVpc(self): 

22 vpc_args = { 

23 "state": "absent", 

24 "cidr_block": "192.168.0.0/24", 

25 "vpc_id": 'not_exist_test', 

26 } 

27 result = run_module(vpc_main, vpc_args) 

28 self.assertNotIn('failed', result) 

29 self.assertFalse(result['changed']) 

30 self.assertEqual(result['vpc'], {}) 

31 

32 vpc_args = { 

33 "state": "present", 

34 "cidr_block": "192.168.0.0/24", 

35 "vpc_name": "ansible_test_vpc", 

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

37 } 

38 result = run_module(vpc_main, vpc_args) 

39 self.assertNotIn('failed', result, result.get('msg', '')) 

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

41 

42 vpc_args = { 

43 "state": "present", 

44 "cidr_block": "192.168.0.0/24", 

45 "vpc_name": "ansible_test_vpc", 

46 "description": "modify by ansible unit test", 

47 } 

48 result = run_module(vpc_main, vpc_args) 

49 self.assertNotIn('failed', result, result.get('msg', '')) 

50 self.assertTrue(result['changed']) 

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

52 

53 vpc_args = { 

54 "state": "present", 

55 "cidr_block": "192.168.0.0/24", 

56 "vpc_name": "ansible_test_vpc", 

57 "description": "modify by ansible unit test", 

58 "tags": {"key1":"value1"} 

59 } 

60 result = run_module(vpc_main, vpc_args) 

61 self.assertNotIn('failed', result, result.get('msg', '')) 

62 self.assertTrue(result['changed']) 

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

64 

65 vpc_args = { 

66 "state": "present", 

67 "cidr_block": "192.168.0.0/24", 

68 "vpc_name": "ansible_test_vpc", 

69 "description": "modify by ansible unit test", 

70 "purge_tags": True 

71 } 

72 result = run_module(vpc_main, vpc_args) 

73 self.assertNotIn('failed', result, result.get('msg', '')) 

74 self.assertTrue(result['changed']) 

75 

76 vpc_args = { 

77 "state": "absent", 

78 "cidr_block": "192.168.0.0/24", 

79 "vpc_id": result['vpc']['vpc_id'], 

80 } 

81 result = run_module(vpc_main, vpc_args) 

82 self.assertNotIn('failed', result, result.get('msg', '')) 

83 

84 vpc_args = { 

85 "cidr_block": "192.168.0.0/24", 

86 "vpc_name": "http://ansible_test_vpc", 

87 "description": "modify by ansible unit test", 

88 } 

89 result = run_module(vpc_main, vpc_args) 

90 self.assertIn('failed', result) 

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

92 

93 vpc_args = { 

94 "cidr_block": "192.168.0.0/24", 

95 "vpc_name": "ansible_test_vpc", 

96 "description": "http://modify by ansible unit test", 

97 } 

98 result = run_module(vpc_main, vpc_args) 

99 self.assertIn('failed', result) 

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

101 

102 

103if __name__ == "__main__": 

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

105 run_unittest_with_coverage()