1
1
import unittest
2
- import boto
3
2
import boto3
3
+ import json
4
4
5
5
from moto import mock_ec2 , mock_sns
6
6
from backuplambda import *
7
7
8
8
9
- def add_servers (ami_id , count ):
10
- conn = boto .connect_ec2 ('the_key' , 'the_secret' )
11
- for index in range (count ):
12
- instance = conn .run_instances (ami_id )
13
- instance .instances [0 ].add_tag ('Snapshot' , 'True' )
14
-
15
-
16
9
def add_volume (tag_name , tag_value , region_name ):
17
10
18
11
ec2_boto = boto3 .client ('ec2' , region_name = region_name )
@@ -25,11 +18,21 @@ def add_volume(tag_name, tag_value, region_name):
25
18
ec2_boto .create_tags (Resources = [resource_id ],
26
19
Tags = [{"Key" : tag_name , "Value" : tag_value }])
27
20
21
+ return resource_id
22
+
23
+
24
+ def add_volume_snapshot (resource_id , description , region_name ):
25
+
26
+ ec2_boto = boto3 .client ('ec2' , region_name = region_name )
27
+ current_snap = ec2_boto .create_snapshot (VolumeId = resource_id ,
28
+ Description = description )
29
+
28
30
29
31
class EC2BackupManagerTest (unittest .TestCase ):
30
32
31
33
@mock_ec2
32
- def test_init (self ):
34
+ def test_resolve_resource_bytag (self ):
35
+
33
36
add_volume ("Snapshot" , "True" , "ap-southeast-1" )
34
37
add_volume ("Name" , "Anotherone" , "ap-southeast-1" )
35
38
@@ -73,9 +76,52 @@ def test_ec2_one_volume(self):
73
76
74
77
"arn" : arn ,
75
78
76
- "keep_count" : 12
79
+ "keep_count" : 2
80
+ }
81
+
82
+ result = lambda_handler (event )
83
+ dajson = json .loads (result )
84
+
85
+ self .assertEqual (dajson ["metrics" ]["total_resources" ], 1 )
86
+ self .assertEqual (dajson ["metrics" ]["total_creates" ], 1 )
87
+ self .assertEqual (dajson ["metrics" ]["total_deletes" ], 0 )
88
+ self .assertEqual (dajson ["metrics" ]["total_errors" ], 0 )
89
+
90
+ @mock_ec2
91
+ @mock_sns
92
+ def test_ec2_image_rotation (self ):
93
+ region_name = "ap-southeast-2"
94
+
95
+ volume = add_volume ("MakeSnapshot" , "True" , region_name )
96
+ add_volume ("Name" , "Anotherone" , region_name )
97
+
98
+ add_volume_snapshot (volume , description = "day_snapshot-1" , region_name = region_name )
99
+ add_volume_snapshot (volume , description = "day_snapshot-2" , region_name = region_name )
100
+
101
+ sns_boto = boto3 .client ('sns' , region_name = region_name )
102
+
103
+ response = sns_boto .create_topic (Name = "datopic" )
104
+ arn = response ["TopicArn" ]
105
+
106
+ event = {
107
+ "period_label" : "day" ,
108
+ "period_format" : "%a%H" ,
109
+
110
+ "ec2_region_name" : region_name ,
111
+ # "rds_region_name": region_name,
112
+
113
+ "tag_name" : "MakeSnapshot" ,
114
+ "tag_value" : "True" ,
115
+
116
+ "arn" : arn ,
117
+
118
+ "keep_count" : 1
77
119
}
78
120
79
121
result = lambda_handler (event )
122
+ dajson = json .loads (result )
80
123
81
- print result
124
+ self .assertEqual (dajson ["metrics" ]["total_resources" ], 1 )
125
+ self .assertEqual (dajson ["metrics" ]["total_creates" ], 1 )
126
+ self .assertEqual (dajson ["metrics" ]["total_deletes" ], 2 )
127
+ self .assertEqual (dajson ["metrics" ]["total_errors" ], 0 )
0 commit comments