1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import sys
16+ import logging
1517import unittest
16- from datetime import datetime , timedelta
17-
1818import pytz
19- from dateutil import parser as date_parser
19+
20+ from copy import copy
21+ from datetime import datetime , timedelta
22+ from dateutil .parser import parse
23+ from hamcrest import raises , calling , assert_that
2024from flexmock import flexmock , flexmock_teardown
2125from nose .tools import assert_raises
26+ from tests .builder import a , instance , volume , volume_type
27+
2228from almanach import config
2329from almanach .common .almanach_entity_not_found_exception import AlmanachEntityNotFoundException
2430from almanach .common .date_format_exception import DateFormatException
31+ from almanach .common .validation_exception import InvalidAttributeException
2532from almanach .core .controller import Controller
2633from almanach .core .model import Instance , Volume
27- from tests .builder import a , instance , volume , volume_type
34+
35+ logging .basicConfig (stream = sys .stdout , level = logging .DEBUG )
2836
2937
3038class ControllerTest (unittest .TestCase ):
@@ -70,6 +78,9 @@ def test_resize_instance(self):
7078 fake_instance = a (instance ())
7179
7280 dates_str = "2015-10-21T16:25:00.000000Z"
81+ fake_instance .start = parse (dates_str )
82+ fake_instance .end = None
83+ fake_instance .last_event = parse (dates_str )
7384
7485 (flexmock (self .database_adapter )
7586 .should_receive ("get_active_entity" )
@@ -78,39 +89,100 @@ def test_resize_instance(self):
7889 .once ())
7990 (flexmock (self .database_adapter )
8091 .should_receive ("close_active_entity" )
81- .with_args (fake_instance .entity_id , date_parser . parse (dates_str ))
92+ .with_args (fake_instance .entity_id , parse (dates_str ))
8293 .once ())
83- fake_instance .start = dates_str
84- fake_instance .end = None
85- fake_instance .last_event = dates_str
94+
8695 (flexmock (self .database_adapter )
8796 .should_receive ("insert_entity" )
8897 .with_args (fake_instance )
8998 .once ())
9099
91100 self .controller .resize_instance (fake_instance .entity_id , "newly_flavor" , dates_str )
92101
93- def test_update_active_instance_entity_with_a_new_start_date (self ):
102+ def test_update_active_instance_entity_with_a_new_flavor (self ):
103+ flavor = u"my flavor name"
94104 fake_instance1 = a (instance ())
95- fake_instance2 = fake_instance1
96- fake_instance2 . start = "2015-10-21T16:25:00.000000Z"
105+ fake_instance2 = copy ( fake_instance1 )
106+ self . _expect_get_active_entity_and_update ( fake_instance1 , fake_instance2 , flavor = flavor )
97107
98- (flexmock (self .database_adapter )
99- .should_receive ("get_active_entity" )
100- .with_args (fake_instance1 .entity_id )
101- .and_return (fake_instance1 )
102- .once ())
108+ self .controller .update_active_instance_entity (
109+ instance_id = fake_instance1 .entity_id ,
110+ flavor = flavor ,
111+ )
103112
104- (flexmock (self .database_adapter )
105- .should_receive ("update_active_entity" )
106- .with_args (fake_instance2 )
107- .once ())
113+ def test_update_active_instance_entity_with_a_new_name (self ):
114+ name = u"my instance name"
115+ fake_instance1 = a (instance ())
116+ fake_instance2 = copy (fake_instance1 )
117+ self ._expect_get_active_entity_and_update (fake_instance1 , fake_instance2 , name = name )
118+
119+ self .controller .update_active_instance_entity (
120+ instance_id = fake_instance1 .entity_id ,
121+ name = name ,
122+ )
123+
124+ def test_update_active_instance_entity_with_a_new_os (self ):
125+ os = {
126+ "os_type" : u"linux" ,
127+ "version" : u"7" ,
128+ "distro" : u"centos"
129+ }
130+ fake_instance1 = a (instance ())
131+ fake_instance2 = copy (fake_instance1 )
132+ self ._expect_get_active_entity_and_update (fake_instance1 , fake_instance2 , os = os )
133+
134+ self .controller .update_active_instance_entity (
135+ instance_id = fake_instance1 .entity_id ,
136+ os = os ,
137+ )
138+
139+ def test_update_active_instance_entity_with_a_new_metadata (self ):
140+ metadata = {
141+ "key" : "value"
142+ }
143+ fake_instance1 = a (instance ())
144+ fake_instance2 = copy (fake_instance1 )
145+ self ._expect_get_active_entity_and_update (fake_instance1 , fake_instance2 , metadata = metadata )
146+
147+ self .controller .update_active_instance_entity (
148+ instance_id = fake_instance1 .entity_id ,
149+ metadata = metadata ,
150+ )
151+
152+ def test_update_active_instance_entity_with_a_new_start_date (self ):
153+ fake_instance1 = a (instance ())
154+ fake_instance2 = copy (fake_instance1 )
155+ self ._expect_get_active_entity_and_update (fake_instance1 , fake_instance2 , start = "2015-10-21T16:25:00.000000Z" )
108156
109157 self .controller .update_active_instance_entity (
110158 instance_id = fake_instance1 .entity_id ,
111159 start_date = "2015-10-21T16:25:00.000000Z" ,
112160 )
113161
162+ def test_update_active_instance_entity_with_a_new_end_date (self ):
163+ fake_instance1 = a (instance ())
164+ fake_instance2 = copy (fake_instance1 )
165+ self ._expect_get_active_entity_and_update (fake_instance1 , fake_instance2 , end = "2015-10-21T16:25:00.000000Z" )
166+
167+ self .controller .update_active_instance_entity (
168+ instance_id = fake_instance1 .entity_id ,
169+ end_date = "2015-10-21T16:25:00.000000Z" ,
170+ )
171+
172+ def test_instance_updated_wrong_attributes_raises_exception (self ):
173+ fake_instance1 = a (instance ())
174+
175+ (flexmock (self .database_adapter )
176+ .should_receive ("get_active_entity" )
177+ .with_args (fake_instance1 .entity_id )
178+ .and_return (fake_instance1 )
179+ .never ())
180+
181+ assert_that (
182+ calling (self .controller .update_active_instance_entity ).with_args (instance_id = fake_instance1 .entity_id ,
183+ wrong_attribute = "this is wrong" ),
184+ raises (InvalidAttributeException ))
185+
114186 def test_instance_created_but_its_an_old_event (self ):
115187 fake_instance = a (instance ()
116188 .with_last_event (pytz .utc .localize (datetime (2015 , 10 , 21 , 16 , 29 , 0 ))))
@@ -158,7 +230,7 @@ def test_instance_deleted(self):
158230
159231 (flexmock (self .database_adapter )
160232 .should_receive ("close_active_entity" )
161- .with_args ("id1" , date_parser . parse ("2015-10-21T16:25:00.000000Z" ))
233+ .with_args ("id1" , parse ("2015-10-21T16:25:00.000000Z" ))
162234 .once ())
163235
164236 self .controller .delete_instance ("id1" , "2015-10-21T16:25:00.000000Z" )
@@ -384,6 +456,10 @@ def test_create_volume_but_its_an_old_event(self):
384456 def test_volume_updated (self ):
385457 fake_volume = a (volume ())
386458 dates_str = "2015-10-21T16:25:00.000000Z"
459+ fake_volume .size = "new_size"
460+ fake_volume .start = parse (dates_str )
461+ fake_volume .end = None
462+ fake_volume .last_event = parse (dates_str )
387463
388464 (flexmock (self .database_adapter )
389465 .should_receive ("get_active_entity" )
@@ -392,12 +468,9 @@ def test_volume_updated(self):
392468 .once ())
393469 (flexmock (self .database_adapter )
394470 .should_receive ("close_active_entity" )
395- .with_args (fake_volume .entity_id , date_parser . parse (dates_str ))
471+ .with_args (fake_volume .entity_id , parse (dates_str ))
396472 .once ())
397- fake_volume .size = "new_size"
398- fake_volume .start = dates_str
399- fake_volume .end = None
400- fake_volume .last_event = dates_str
473+
401474 (flexmock (self .database_adapter )
402475 .should_receive ("insert_entity" )
403476 .with_args (fake_volume )
@@ -648,3 +721,20 @@ def test_list_volume_types(self):
648721 .once ())
649722
650723 self .assertEqual (len (self .controller .list_volume_types ()), 2 )
724+
725+ def _expect_get_active_entity_and_update (self , fake_instance1 , fake_instance2 , ** kwargs ):
726+ for key , value in kwargs .items ():
727+ if key in ['start' , 'end' ]:
728+ value = parse (value )
729+
730+ setattr (fake_instance2 , key , value )
731+
732+ (flexmock (self .database_adapter )
733+ .should_receive ("get_active_entity" )
734+ .with_args (fake_instance1 .entity_id )
735+ .and_return (fake_instance1 )
736+ .once ())
737+ (flexmock (self .database_adapter )
738+ .should_receive ("update_active_entity" )
739+ .with_args (fake_instance2 )
740+ .once ())
0 commit comments