22from __future__ import division
33from __future__ import absolute_import
44from builtins import object
5- from mock import MagicMock
5+
6+ import mock
67
78from stacker .context import Context
89from stacker .config import Config , Stack
9- from stacker .lookups import Lookup
10+ from stacker .exceptions import StackDoesNotExist , StackUpdateBadStatus
11+ from stacker .providers .base import BaseProvider
1012
1113
1214class MockThreadingEvent (object ):
@@ -23,23 +25,72 @@ def build(self, region=None, profile=None):
2325 return self .provider
2426
2527
26- def mock_provider (** kwargs ):
27- return MagicMock (** kwargs )
28+ class MockProvider (BaseProvider ):
29+ def __init__ (self , outputs = None ):
30+ self ._stacks = {}
31+ for stack_name , stack_outputs in (outputs or {}).items ():
32+ self ._stacks [stack_name ] = {
33+ "StackName" : stack_name ,
34+ "Outputs" : stack_outputs ,
35+ "StackStatus" : "CREATED"
36+ }
37+
38+ def get_stack (self , stack_name , ** kwargs ):
39+ try :
40+ return self ._stacks [stack_name ]
41+ except KeyError :
42+ raise StackDoesNotExist (stack_name )
43+
44+ def get_outputs (self , stack_name , * args , ** kwargs ):
45+ return self .get_stack (stack_name )["Outputs" ]
46+
47+ def get_stack_status (self , stack_name , * args , ** kwargs ):
48+ return self .get_stack (stack_name )["StackStatus" ]
49+
50+ def create_stack (self , stack_name , * args , ** kwargs ):
51+ try :
52+ stack = self .get_stack (stack_name )
53+ status = self .get_stack_status (stack )
54+ if status != "DELETED" :
55+ raise StackUpdateBadStatus (stack_name , status , "can't create" )
56+ except StackDoesNotExist :
57+ pass
58+
59+ return None
60+
61+ def update_stack (self , stack_name , * args , ** kwargs ):
62+ stack = self .get_stack (stack_name )
63+ status = self .get_stack_status (stack )
64+ if status == "DELETED" :
65+ raise StackUpdateBadStatus (stack_name , status , "can't update" )
66+
67+ stack ["StackStatus" ] = "UPDATED"
68+ return None
2869
70+ def destroy_stack (self , stack_name , * args , ** kwargs ):
71+ stack = self .get_stack (stack_name )
72+ status = self .get_stack_status (stack )
73+ if status == "DELETED" :
74+ raise StackUpdateBadStatus (stack_name , status , "can't destroy" )
2975
30- def mock_context (namespace = "default" , extra_config_args = None , ** kwargs ):
76+ stack ["StackStatus" ] = "DELETED"
77+ return None
78+
79+
80+ def mock_provider (outputs = None , ** kwargs ):
81+ provider = mock .MagicMock (wraps = MockProvider (outputs ), ** kwargs )
82+ return provider
83+
84+
85+ def mock_context (namespace = "default" , extra_config_args = None ,
86+ environment = None , ** kwargs ):
3187 config_args = {"namespace" : namespace }
3288 if extra_config_args :
3389 config_args .update (extra_config_args )
90+
3491 config = Config (config_args )
35- if kwargs .get ("environment" ):
36- return Context (
37- config = config ,
38- ** kwargs )
39- return Context (
40- config = config ,
41- environment = {},
42- ** kwargs )
92+ environment = environment or {}
93+ return Context (config = config , environment = environment , ** kwargs )
4394
4495
4596def generate_definition (base_name , stack_id , ** overrides ):
@@ -53,12 +104,6 @@ def generate_definition(base_name, stack_id, **overrides):
53104 return Stack (definition )
54105
55106
56- def mock_lookup (lookup_input , lookup_type , raw = None ):
57- if raw is None :
58- raw = "%s %s" % (lookup_type , lookup_input )
59- return Lookup (type = lookup_type , input = lookup_input , raw = raw )
60-
61-
62107class SessionStub (object ):
63108
64109 """Stubber class for boto3 sessions made with session_cache.get_session()
0 commit comments