Coverage for test_ali_ram_user.py: 11%

44 statements  

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

1# encoding: utf-8 

2''' 

3Created on 2025年3月7日 

4 

5@author: jingyu.wy 

6''' 

7import unittest 

8 

9 

10from dotenv import load_dotenv 

11 

12from ansible_collections.alibaba.apsarastack.plugins.modules.ali_ram_user import main as ram_user_main 

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

14 

15 

16class Test(unittest.TestCase): 

17 

18 def setUp(self) -> None: 

19 unittest.TestCase.setUp(self) 

20 load_dotenv() 

21 

22 def testCreateRamUser(self): 

23 # state=dict(default='present', choices=['present', 'absent']), 

24 # user_name=dict(type='str', aliases=['name']), 

25 # user_id=dict(type='str', aliases=['id']), 

26 # display_name=dict(type='str'), 

27 # mobile_phone=dict(type='str', aliases=['phone']), 

28 # email=dict(type='str'), 

29 # comments=dict(type='str'), 

30 # new_user_name=dict(type='str') 

31 ram_user_args = { 

32 "user_name": "ansiable_test", 

33 "display_name": "ansiable_test", 

34 "mobile_phone": "15129022361", 

35 "email": "12345@qq.com", 

36 "comments": "ansible_test" 

37 } 

38 result = run_module(ram_user_main, ram_user_args) 

39 self.assertNotIn('failed', result) 

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

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

42 

43 vpc_args = { 

44 "state": "present", 

45 "cidr_block": "192.168.0.0/24", 

46 "vpc_name": "ansible_test_vpc", 

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

48 } 

49 result = run_module(vpc_main, vpc_args) 

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

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

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 } 

59 result = run_module(vpc_main, vpc_args) 

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

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

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

63 

64 vpc_args = { 

65 "state": "present", 

66 "cidr_block": "192.168.0.0/24", 

67 "vpc_name": "ansible_test_vpc", 

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

69 "tags": {"key1":"value1"} 

70 } 

71 result = run_module(vpc_main, vpc_args) 

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

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

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

75 

76 vpc_args = { 

77 "state": "present", 

78 "cidr_block": "192.168.0.0/24", 

79 "vpc_name": "ansible_test_vpc", 

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

81 "purge_tags": True 

82 } 

83 result = run_module(vpc_main, vpc_args) 

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

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

86 

87 vpc_args = { 

88 "state": "absent", 

89 "cidr_block": "192.168.0.0/24", 

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

91 } 

92 result = run_module(vpc_main, vpc_args) 

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

94 

95 vpc_args = { 

96 "cidr_block": "192.168.0.0/24", 

97 "vpc_name": "http://ansible_test_vpc", 

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

99 } 

100 result = run_module(vpc_main, vpc_args) 

101 self.assertIn('failed', result) 

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

103 

104 vpc_args = { 

105 "cidr_block": "192.168.0.0/24", 

106 "vpc_name": "ansible_test_vpc", 

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

108 } 

109 result = run_module(vpc_main, vpc_args) 

110 self.assertIn('failed', result) 

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

112 

113 

114if __name__ == "__main__": 

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

116 run_unittest_with_coverage()