1313#
1414# Portions Copyright Buildbot Team Members
1515# Portions Copyright 2014 Longaccess private company
16+
17+ from twisted .trial import unittest
1618try :
1719 from moto import mock_ec2
1820 assert mock_ec2
2224 boto = None
2325 ec2 = None
2426
25- from twisted .trial import unittest
26-
2727from buildbot .test .util .warnings import assertNotProducesWarnings
2828from buildbot .test .util .warnings import assertProducesWarning
2929from buildbot .test .util .warnings import assertProducesWarnings
@@ -106,6 +106,52 @@ def test_constructor_tags(self):
106106 )
107107 self .assertEqual (bs .tags , tags )
108108
109+ @mock_ec2
110+ def test_fail_mixing_classic_and_vpc_ec2_settings (self ):
111+ c = self .botoSetup ()
112+ amis = c .get_all_images ()
113+
114+ def create_worker ():
115+ ec2 .EC2LatentWorker ('bot1' , 'sekrit' , 'm1.large' ,
116+ keypair_name = "test_key" ,
117+ identifier = 'publickey' ,
118+ secret_identifier = 'privatekey' ,
119+ ami = amis [0 ].id ,
120+ security_name = "classic" ,
121+ subnet_id = "sn-1234"
122+ )
123+
124+ self .assertRaises (ValueError , create_worker )
125+
126+ @mock_ec2
127+ def test_start_vpc_instance (self ):
128+ c = self .botoSetup ()
129+
130+ vpc_conn = boto .connect_vpc ()
131+ vpc = vpc_conn .create_vpc ("192.168.0.0/24" )
132+ subnet = vpc_conn .create_subnet (vpc .id , "192.168.0.0/24" )
133+ amis = c .get_all_images ()
134+
135+ sg = c .create_security_group ("test_sg" , "test_sg" , vpc .id )
136+ bs = ec2 .EC2LatentWorker ('bot1' , 'sekrit' , 'm1.large' ,
137+ identifier = 'publickey' ,
138+ secret_identifier = 'privatekey' ,
139+ keypair_name = "test_key" ,
140+ security_group_ids = [sg .id ],
141+ subnet_id = subnet .id ,
142+ ami = amis [0 ].id
143+ )
144+
145+ instance_id , _ , _ = bs ._start_instance ()
146+ instances = [i for i in c .get_only_instances ()
147+ if i .state != "terminated" ]
148+
149+ self .assertEqual (len (instances ), 1 )
150+ self .assertEqual (instances [0 ].id , instance_id )
151+ self .assertEqual (instances [0 ].subnet_id , subnet .id )
152+ self .assertEqual (len (instances [0 ].groups ), 1 )
153+ self .assertEqual (instances [0 ].groups [0 ].id , sg .id )
154+
109155 @mock_ec2
110156 def test_start_instance (self ):
111157 c = self .botoSetup ()
@@ -133,6 +179,56 @@ def test_start_instance(self):
133179 self .assertEqual (instances [0 ].id , instance_id )
134180 self .assertEqual (instances [0 ].tags , {})
135181
182+ @mock_ec2
183+ def test_start_instance_volumes (self ):
184+ c = self .botoSetup ()
185+ amis = c .get_all_images ()
186+ with assertProducesWarnings (
187+ DeprecatedWorkerNameWarning ,
188+ messages_patterns = [
189+ r"Use of default value of 'keypair_name' of "
190+ r"EC2LatentWorker constructor is deprecated" ,
191+ r"Use of default value of 'security_name' of "
192+ r"EC2LatentWorker constructor is deprecated"
193+ ]):
194+ bs = ec2 .EC2LatentWorker ('bot1' , 'sekrit' , 'm1.large' ,
195+ identifier = 'publickey' ,
196+ secret_identifier = 'privatekey' ,
197+ ami = amis [0 ].id ,
198+ block_device_map = {
199+ "/dev/xvdb" : {
200+ "volume_type" : "io1" ,
201+ "iops" : 10 ,
202+ "size" : 20 ,
203+ },
204+ "/dev/xvdc" : {
205+ "volume_type" : "gp2" ,
206+ "size" : 30 ,
207+ "delete_on_termination" : False
208+ }
209+ }
210+ )
211+
212+ # moto does not currently map volumes properly. below ensures
213+ # that my conversion code properly composes it, including
214+ # delete_on_termination default.
215+ from boto .ec2 .blockdevicemapping import BlockDeviceType
216+ self .assertEqual (set (['/dev/xvdb' , '/dev/xvdc' ]), set (bs .block_device_map .keys ()))
217+
218+ def assertBlockDeviceEqual (a , b ):
219+ self .assertEqual (a .volume_type , b .volume_type )
220+ self .assertEqual (a .iops , b .iops )
221+ self .assertEqual (a .size , b .size )
222+ self .assertEqual (a .delete_on_termination , b .delete_on_termination )
223+
224+ assertBlockDeviceEqual (
225+ BlockDeviceType (volume_type = 'io1' , iops = 10 , size = 20 , delete_on_termination = True ),
226+ bs .block_device_map ['/dev/xvdb' ])
227+
228+ assertBlockDeviceEqual (
229+ BlockDeviceType (volume_type = 'gp2' , size = 30 , delete_on_termination = False ),
230+ bs .block_device_map ['/dev/xvdc' ])
231+
136232 @mock_ec2
137233 def test_start_instance_tags (self ):
138234 c = self .botoSetup ()
@@ -159,6 +255,38 @@ def test_start_instance_tags(self):
159255 self .assertEqual (instances [0 ].id , id )
160256 self .assertEqual (instances [0 ].tags , tags )
161257
258+ @mock_ec2
259+ def test_start_vpc_spot_instance (self ):
260+ c = self .botoSetup ()
261+
262+ vpc_conn = boto .connect_vpc ()
263+ vpc = vpc_conn .create_vpc ("192.168.0.0/24" )
264+ subnet = vpc_conn .create_subnet (vpc .id , "192.168.0.0/24" )
265+ amis = c .get_all_images ()
266+
267+ sg = c .create_security_group ("test_sg" , "test_sg" , vpc .id )
268+
269+ bs = ec2 .EC2LatentWorker ('bot1' , 'sekrit' , 'm1.large' ,
270+ identifier = 'publickey' ,
271+ secret_identifier = 'privatekey' ,
272+ keypair_name = "test_key" ,
273+ ami = amis [0 ].id , spot_instance = True ,
274+ max_spot_price = 1.5 ,
275+ security_group_ids = [sg .id ],
276+ subnet_id = subnet .id ,
277+ )
278+
279+ instance_id , _ , _ = bs ._start_instance ()
280+ instances = [i for i in c .get_only_instances ()
281+ if i .state != "terminated" ]
282+
283+ self .assertTrue (bs .spot_instance )
284+ self .assertEqual (len (instances ), 1 )
285+ self .assertEqual (instances [0 ].id , instance_id )
286+ self .assertEqual (instances [0 ].subnet_id , subnet .id )
287+ self .assertEqual (len (instances [0 ].groups ), 1 )
288+ self .assertEqual (instances [0 ].groups [0 ].id , sg .id )
289+
162290 @mock_ec2
163291 def test_start_spot_instance (self ):
164292 c = self .botoSetup ()
@@ -312,6 +440,20 @@ def test_use_of_default_security_warning(self):
312440 self .assertEqual (bs .keypair_name , 'test_keypair' )
313441 self .assertEqual (bs .security_name , 'latent_buildbot_slave' )
314442
443+ @mock_ec2
444+ def test_no_default_security_warning_when_security_group_ids (self ):
445+ c = self .botoSetup ()
446+ amis = c .get_all_images ()
447+
448+ bs = ec2 .EC2LatentWorker ('bot1' , 'sekrit' , 'm1.large' ,
449+ identifier = 'publickey' ,
450+ secret_identifier = 'privatekey' ,
451+ ami = amis [0 ].id ,
452+ keypair_name = 'test_keypair' ,
453+ subnet_id = ["sn-1" ]
454+ )
455+ self .assertEqual (bs .security_name , None )
456+
315457 @mock_ec2
316458 def test_use_non_default_keypair_security (self ):
317459 c = self .botoSetup ()
0 commit comments