1
+ import unittest
2
+ import boto
3
+ import boto3
4
+
5
+ from moto import mock_ec2 , mock_sns
6
+ from backuplambda import *
7
+
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
+ def add_volume (tag_name , tag_value , region_name ):
17
+
18
+ ec2_boto = boto3 .client ('ec2' , region_name = region_name )
19
+
20
+ ec2_boto .create_volume (Size = 200 , AvailabilityZone = region_name + "a" )
21
+
22
+ vols = ec2_boto .describe_volumes ()
23
+
24
+ resource_id = vols ["Volumes" ][0 ]["VolumeId" ]
25
+ ec2_boto .create_tags (Resources = [resource_id ],
26
+ Tags = [{"Key" : tag_name , "Value" : tag_value }])
27
+
28
+
29
+ class EC2BackupManagerTest (unittest .TestCase ):
30
+
31
+ @mock_ec2
32
+ def test_init (self ):
33
+ add_volume ("Snapshot" , "True" , "ap-southeast-1" )
34
+ add_volume ("Name" , "Anotherone" , "ap-southeast-1" )
35
+
36
+ mgr = EC2BackupManager (ec2_region_name = "ap-southeast-1" ,
37
+ period = "day" ,
38
+ tag_name = "Snapshot" ,
39
+ tag_value = "True" ,
40
+ date_suffix = "dd" ,
41
+ keep_count = "2" )
42
+
43
+ volumes = mgr .get_backable_resources ()
44
+
45
+ assert len (volumes ) == 1
46
+
47
+
48
+ class LambdaHandlerTest (unittest .TestCase ):
49
+
50
+ @mock_ec2
51
+ @mock_sns
52
+ def test_ec2_one_volume (self ):
53
+
54
+ region_name = "ap-southeast-2"
55
+
56
+ add_volume ("MakeSnapshot" , "True" , region_name )
57
+ add_volume ("Name" , "Anotherone" , region_name )
58
+
59
+ sns_boto = boto3 .client ('sns' , region_name = region_name )
60
+
61
+ response = sns_boto .create_topic (Name = "datopic" )
62
+ arn = response ["TopicArn" ]
63
+
64
+ event = {
65
+ "period_label" : "day" ,
66
+ "period_format" : "%a%H" ,
67
+
68
+ "ec2_region_name" : region_name ,
69
+ # "rds_region_name": region_name,
70
+
71
+ "tag_name" : "MakeSnapshot" ,
72
+ "tag_value" : "True" ,
73
+
74
+ "arn" : arn ,
75
+
76
+ "keep_count" : 12
77
+ }
78
+
79
+ result = lambda_handler (event )
80
+
81
+ print result
0 commit comments