1212#
1313
1414import argparse
15+ import copy
1516import mock
17+ import uuid
1618
1719from openstackclient .api import network_v2
1820from openstackclient .tests import fakes
@@ -46,6 +48,8 @@ def setUp(self):
4648
4749 self .namespace = argparse .Namespace ()
4850
51+ self .app .client_manager .session = mock .Mock ()
52+
4953 self .app .client_manager .network = FakeNetworkV2Client (
5054 endpoint = fakes .AUTH_URL ,
5155 token = fakes .AUTH_TOKEN ,
@@ -55,3 +59,88 @@ def setUp(self):
5559 session = mock .Mock (),
5660 service_type = "network" ,
5761 )
62+
63+
64+ class FakeNetwork (object ):
65+ """Fake one or more networks."""
66+
67+ @staticmethod
68+ def create_one_network (attrs = {}, methods = {}):
69+ """Create a fake network.
70+
71+ :param Dictionary attrs:
72+ A dictionary with all attributes
73+ :param Dictionary methods:
74+ A dictionary with all methods
75+ :return:
76+ A FakeResource object, with id, name, admin_state_up,
77+ router_external, status, subnets, tenant_id
78+ """
79+ # Set default attributes.
80+ network_attrs = {
81+ 'id' : 'network-id-' + uuid .uuid4 ().hex ,
82+ 'name' : 'network-name-' + uuid .uuid4 ().hex ,
83+ 'status' : 'ACTIVE' ,
84+ 'tenant_id' : 'project-id-' + uuid .uuid4 ().hex ,
85+ 'admin_state_up' : True ,
86+ 'shared' : False ,
87+ 'subnets' : ['a' , 'b' ],
88+ 'provider_network_type' : 'vlan' ,
89+ 'router_external' : True ,
90+ 'is_dirty' : True ,
91+ }
92+
93+ # Overwrite default attributes.
94+ network_attrs .update (attrs )
95+
96+ # Set default methods.
97+ network_methods = {
98+ 'keys' : ['id' , 'name' , 'admin_state_up' , 'router_external' ,
99+ 'status' , 'subnets' , 'tenant_id' ],
100+ }
101+
102+ # Overwrite default methods.
103+ network_methods .update (methods )
104+
105+ network = fakes .FakeResource (info = copy .deepcopy (network_attrs ),
106+ methods = copy .deepcopy (network_methods ),
107+ loaded = True )
108+ return network
109+
110+ @staticmethod
111+ def create_networks (attrs = {}, methods = {}, count = 2 ):
112+ """Create multiple fake networks.
113+
114+ :param Dictionary attrs:
115+ A dictionary with all attributes
116+ :param Dictionary methods:
117+ A dictionary with all methods
118+ :param int count:
119+ The number of networks to fake
120+ :return:
121+ A list of FakeResource objects faking the networks
122+ """
123+ networks = []
124+ for i in range (0 , count ):
125+ networks .append (FakeNetwork .create_one_network (attrs , methods ))
126+
127+ return networks
128+
129+ @staticmethod
130+ def get_networks (networks = None , count = 2 ):
131+ """Get an iterable MagicMock object with a list of faked networks.
132+
133+ If networks list is provided, then initialize the Mock object with the
134+ list. Otherwise create one.
135+
136+ :param List networks:
137+ A list of FakeResource objects faking networks
138+ :param int count:
139+ The number of networks to fake
140+ :return:
141+ An iterable Mock object with side_effect set to a list of faked
142+ networks
143+ """
144+ if networks is None :
145+ networks = FakeNetwork .create_networks (count )
146+ return mock .MagicMock (side_effect = networks )
0 commit comments