summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulia Kreger <juliaashleykreger@gmail.com>2017-09-10 16:23:00 +0000
committerJulia Kreger <juliaashleykreger@gmail.com>2017-11-19 01:34:33 -0500
commit395d927081eed65b0277628567b416145009fde9 (patch)
tree7d559f247fb2ce2768e1e5a6e65dfc5dc6ee1722
parentb5e905f9b85d60b8a8f6410cc3a771097f562089 (diff)
Complete move of baremetal machine tests
Migrating tests to wrap up machine actions. Ports will be migrated separately as there will also need to be portgroups, which could be wrapped into the same file/class depending on how that ends up being implemented. Change-Id: I239bbc134f42a47e8df75531dbd50916a97a6068
Notes
Notes (review): Code-Review+2: Monty Taylor <mordred@inaugust.com> Workflow+1: Monty Taylor <mordred@inaugust.com> Verified+2: Zuul Submitted-by: Zuul Submitted-at: Mon, 20 Nov 2017 20:56:48 +0000 Reviewed-on: https://review.openstack.org/502333 Project: openstack-infra/shade Branch: refs/heads/master
-rw-r--r--shade/operatorcloud.py13
-rw-r--r--shade/tests/fakes.py14
-rw-r--r--shade/tests/unit/test_baremetal_node.py542
-rw-r--r--shade/tests/unit/test_shade_operator.py232
4 files changed, 564 insertions, 237 deletions
diff --git a/shade/operatorcloud.py b/shade/operatorcloud.py
index 3270a8f..63b0c5e 100644
--- a/shade/operatorcloud.py
+++ b/shade/operatorcloud.py
@@ -207,7 +207,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
207 nic = self.manager.submit_task( 207 nic = self.manager.submit_task(
208 _tasks.MachinePortCreate(address=row['mac'], 208 _tasks.MachinePortCreate(address=row['mac'],
209 node_uuid=machine['uuid'])) 209 node_uuid=machine['uuid']))
210 created_nics.append(nic.uuid) 210 created_nics.append(nic['uuid'])
211 211
212 except Exception as e: 212 except Exception as e:
213 self.log.debug("ironic NIC registration failed", exc_info=True) 213 self.log.debug("ironic NIC registration failed", exc_info=True)
@@ -273,7 +273,10 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
273 machine = self.get_machine(machine['uuid']) 273 machine = self.get_machine(machine['uuid'])
274 if (machine['reservation'] is None and 274 if (machine['reservation'] is None and
275 machine['provision_state'] is not 'enroll'): 275 machine['provision_state'] is not 'enroll'):
276 276 # NOTE(TheJulia): In this case, the node has
277 # has moved on from the previous state and is
278 # likely not being verified, as no lock is
279 # present on the node.
277 self.node_set_provision_state( 280 self.node_set_provision_state(
278 machine['uuid'], 'provide') 281 machine['uuid'], 'provide')
279 machine = self.get_machine(machine['uuid']) 282 machine = self.get_machine(machine['uuid'])
@@ -288,8 +291,10 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
288 raise OpenStackCloudException( 291 raise OpenStackCloudException(
289 "Machine encountered a failure: %s" 292 "Machine encountered a failure: %s"
290 % machine['last_error']) 293 % machine['last_error'])
291 294 if not isinstance(machine, str):
292 return machine 295 return self._normalize_machine(machine)
296 else:
297 return machine
293 298
294 def unregister_machine(self, nics, uuid, wait=False, timeout=600): 299 def unregister_machine(self, nics, uuid, wait=False, timeout=600):
295 """Unregister Baremetal from Ironic 300 """Unregister Baremetal from Ironic
diff --git a/shade/tests/fakes.py b/shade/tests/fakes.py
index 9ff4c84..c449316 100644
--- a/shade/tests/fakes.py
+++ b/shade/tests/fakes.py
@@ -248,6 +248,16 @@ def make_fake_machine(machine_name, machine_id=None):
248 id=machine_id, 248 id=machine_id,
249 name=machine_name)) 249 name=machine_name))
250 250
251def make_fake_port(address, node_id=None, port_id=None):
252 if not node_id:
253 node_id = uuid.uuid4().hex
254 if not port_id:
255 port_id = uuid.uuid4().hex
256 return meta.obj_to_munch(FakeMachinePort(
257 id=port_id,
258 address=address,
259 node_id=node_id))
260
251 261
252class FakeFloatingIP(object): 262class FakeFloatingIP(object):
253 def __init__(self, id, pool, ip, fixed_ip, instance_id): 263 def __init__(self, id, pool, ip, fixed_ip, instance_id):
@@ -346,7 +356,7 @@ class FakeVolumeSnapshot(object):
346class FakeMachine(object): 356class FakeMachine(object):
347 def __init__(self, id, name=None, driver=None, driver_info=None, 357 def __init__(self, id, name=None, driver=None, driver_info=None,
348 chassis_uuid=None, instance_info=None, instance_uuid=None, 358 chassis_uuid=None, instance_info=None, instance_uuid=None,
349 properties=None): 359 properties=None, reservation=None, last_error=None):
350 self.uuid = id 360 self.uuid = id
351 self.name = name 361 self.name = name
352 self.driver = driver 362 self.driver = driver
@@ -355,6 +365,8 @@ class FakeMachine(object):
355 self.instance_info = instance_info 365 self.instance_info = instance_info
356 self.instance_uuid = instance_uuid 366 self.instance_uuid = instance_uuid
357 self.properties = properties 367 self.properties = properties
368 self.reservation = reservation
369 self.last_error = last_error
358 370
359 371
360class FakeMachinePort(object): 372class FakeMachinePort(object):
diff --git a/shade/tests/unit/test_baremetal_node.py b/shade/tests/unit/test_baremetal_node.py
index d76b375..d36e812 100644
--- a/shade/tests/unit/test_baremetal_node.py
+++ b/shade/tests/unit/test_baremetal_node.py
@@ -32,6 +32,12 @@ class TestBaremetalNode(base.IronicTestCase):
32 super(TestBaremetalNode, self).setUp() 32 super(TestBaremetalNode, self).setUp()
33 self.fake_baremetal_node = fakes.make_fake_machine( 33 self.fake_baremetal_node = fakes.make_fake_machine(
34 self.name, self.uuid) 34 self.name, self.uuid)
35 # TODO(TheJulia): Some tests below have fake ports,
36 # since they are required in some processes. Lets refactor
37 # them at some point to use self.fake_baremetal_port.
38 self.fake_baremetal_port = fakes.make_fake_port(
39 '00:01:02:03:04:05',
40 node_id=self.uuid)
35 41
36 def test_list_machines(self): 42 def test_list_machines(self):
37 fake_baremetal_two = fakes.make_fake_machine('two', str(uuid.uuid4())) 43 fake_baremetal_two = fakes.make_fake_machine('two', str(uuid.uuid4()))
@@ -820,6 +826,542 @@ class TestBaremetalNode(base.IronicTestCase):
820 self.assertIsNone(return_value) 826 self.assertIsNone(return_value)
821 self.assert_calls() 827 self.assert_calls()
822 828
829 def test_register_machine(self):
830 mac_address = '00:01:02:03:04:05'
831 nics = [{'mac': mac_address}]
832 node_uuid = self.fake_baremetal_node['uuid']
833 # TODO(TheJulia): There is a lot of duplication
834 # in testing creation. Surely this hsould be a helper
835 # or something. We should fix this, after we have
836 # ironicclient removed, as in the mean time visibility
837 # will be helpful.
838 node_to_post = {
839 'chassis_uuid': None,
840 'driver': None,
841 'driver_info': None,
842 'name': self.fake_baremetal_node['name'],
843 'properties': None,
844 'uuid': node_uuid}
845 self.fake_baremetal_node['provision_state'] = 'available'
846 if 'provision_state' in node_to_post:
847 node_to_post.pop('provision_state')
848 self.register_uris([
849 dict(
850 method='POST',
851 uri=self.get_mock_url(
852 resource='nodes'),
853 json=self.fake_baremetal_node,
854 validate=dict(json=node_to_post)),
855 dict(
856 method='POST',
857 uri=self.get_mock_url(
858 resource='ports'),
859 validate=dict(json={'address': mac_address,
860 'node_uuid': node_uuid}),
861 json=self.fake_baremetal_port),
862 ])
863 return_value = self.op_cloud.register_machine(nics, **node_to_post)
864
865 self.assertDictEqual(self.fake_baremetal_node, return_value)
866 self.assert_calls()
867
868 # TODO(TheJulia): After we remove ironicclient,
869 # we need to de-duplicate these tests. Possibly
870 # a dedicated class, although we should do it then
871 # as we may find differences that need to be accounted
872 # for newer microversions.
873 def test_register_machine_enroll(self):
874 mac_address = '00:01:02:03:04:05'
875 nics = [{'mac': mac_address}]
876 node_uuid = self.fake_baremetal_node['uuid']
877 node_to_post = {
878 'chassis_uuid': None,
879 'driver': None,
880 'driver_info': None,
881 'name': self.fake_baremetal_node['name'],
882 'properties': None,
883 'uuid': node_uuid}
884 self.fake_baremetal_node['provision_state'] = 'enroll'
885 manageable_node = self.fake_baremetal_node.copy()
886 manageable_node['provision_state'] = 'manageable'
887 available_node = self.fake_baremetal_node.copy()
888 available_node['provision_state'] = 'available'
889 self.register_uris([
890 dict(
891 method='POST',
892 uri=self.get_mock_url(
893 resource='nodes'),
894 validate=dict(json=node_to_post),
895 json=self.fake_baremetal_node),
896 dict(
897 method='POST',
898 uri=self.get_mock_url(
899 resource='ports'),
900 validate=dict(json={'address': mac_address,
901 'node_uuid': node_uuid}),
902 json=self.fake_baremetal_port),
903 dict(
904 method='PUT',
905 uri=self.get_mock_url(
906 resource='nodes',
907 append=[self.fake_baremetal_node['uuid'],
908 'states', 'provision']),
909 validate=dict(json={'target': 'manage'})),
910 dict(
911 method='GET',
912 uri=self.get_mock_url(
913 resource='nodes',
914 append=[self.fake_baremetal_node['uuid']]),
915 json=manageable_node),
916 dict(
917 method='GET',
918 uri=self.get_mock_url(
919 resource='nodes',
920 append=[self.fake_baremetal_node['uuid']]),
921 json=manageable_node),
922 dict(
923 method='PUT',
924 uri=self.get_mock_url(
925 resource='nodes',
926 append=[self.fake_baremetal_node['uuid'],
927 'states', 'provision']),
928 validate=dict(json={'target': 'provide'})),
929 dict(
930 method='GET',
931 uri=self.get_mock_url(
932 resource='nodes',
933 append=[self.fake_baremetal_node['uuid']]),
934 json=available_node),
935 dict(
936 method='GET',
937 uri=self.get_mock_url(
938 resource='nodes',
939 append=[self.fake_baremetal_node['uuid']]),
940 json=available_node),
941 ])
942 # NOTE(When we migrate to a newer microversion, this test
943 # may require revision. It was written for microversion
944 # ?1.13?, which accidently got reverted to 1.6 at one
945 # point during code being refactored soon after the
946 # change landed. Presently, with the lock at 1.6,
947 # this code is never used in the current code path.
948 return_value = self.op_cloud.register_machine(nics, **node_to_post)
949
950 self.assertDictEqual(available_node, return_value)
951 self.assert_calls()
952
953 def test_register_machine_enroll_wait(self):
954 mac_address = self.fake_baremetal_port
955 nics = [{'mac': mac_address}]
956 node_uuid = self.fake_baremetal_node['uuid']
957 node_to_post = {
958 'chassis_uuid': None,
959 'driver': None,
960 'driver_info': None,
961 'name': self.fake_baremetal_node['name'],
962 'properties': None,
963 'uuid': node_uuid}
964 self.fake_baremetal_node['provision_state'] = 'enroll'
965 manageable_node = self.fake_baremetal_node.copy()
966 manageable_node['provision_state'] = 'manageable'
967 available_node = self.fake_baremetal_node.copy()
968 available_node['provision_state'] = 'available'
969 self.register_uris([
970 dict(
971 method='POST',
972 uri=self.get_mock_url(
973 resource='nodes'),
974 validate=dict(json=node_to_post),
975 json=self.fake_baremetal_node),
976 dict(
977 method='POST',
978 uri=self.get_mock_url(
979 resource='ports'),
980 validate=dict(json={'address': mac_address,
981 'node_uuid': node_uuid}),
982 json=self.fake_baremetal_port),
983 dict(
984 method='GET',
985 uri=self.get_mock_url(
986 resource='nodes',
987 append=[self.fake_baremetal_node['uuid']]),
988 json=self.fake_baremetal_node),
989 dict(
990 method='PUT',
991 uri=self.get_mock_url(
992 resource='nodes',
993 append=[self.fake_baremetal_node['uuid'],
994 'states', 'provision']),
995 validate=dict(json={'target': 'manage'})),
996 dict(
997 method='GET',
998 uri=self.get_mock_url(
999 resource='nodes',
1000 append=[self.fake_baremetal_node['uuid']]),
1001 json=self.fake_baremetal_node),
1002 dict(
1003 method='GET',
1004 uri=self.get_mock_url(
1005 resource='nodes',
1006 append=[self.fake_baremetal_node['uuid']]),
1007 json=manageable_node),
1008 dict(
1009 method='PUT',
1010 uri=self.get_mock_url(
1011 resource='nodes',
1012 append=[self.fake_baremetal_node['uuid'],
1013 'states', 'provision']),
1014 validate=dict(json={'target': 'provide'})),
1015 dict(
1016 method='GET',
1017 uri=self.get_mock_url(
1018 resource='nodes',
1019 append=[self.fake_baremetal_node['uuid']]),
1020 json=available_node),
1021 dict(
1022 method='GET',
1023 uri=self.get_mock_url(
1024 resource='nodes',
1025 append=[self.fake_baremetal_node['uuid']]),
1026 json=available_node),
1027 ])
1028 return_value = self.op_cloud.register_machine(nics, wait=True,
1029 **node_to_post)
1030
1031 self.assertDictEqual(available_node, return_value)
1032 self.assert_calls()
1033
1034 def test_register_machine_enroll_failure(self):
1035 mac_address = '00:01:02:03:04:05'
1036 nics = [{'mac': mac_address}]
1037 node_uuid = self.fake_baremetal_node['uuid']
1038 node_to_post = {
1039 'chassis_uuid': None,
1040 'driver': None,
1041 'driver_info': None,
1042 'name': self.fake_baremetal_node['name'],
1043 'properties': None,
1044 'uuid': node_uuid}
1045 self.fake_baremetal_node['provision_state'] = 'enroll'
1046 failed_node = self.fake_baremetal_node.copy()
1047 failed_node['reservation'] = 'conductor0'
1048 failed_node['provision_state'] = 'verifying'
1049 failed_node['last_error'] = 'kaboom!'
1050 self.register_uris([
1051 dict(
1052 method='POST',
1053 uri=self.get_mock_url(
1054 resource='nodes'),
1055 json=self.fake_baremetal_node,
1056 validate=dict(json=node_to_post)),
1057 dict(
1058 method='POST',
1059 uri=self.get_mock_url(
1060 resource='ports'),
1061 validate=dict(json={'address': mac_address,
1062 'node_uuid': node_uuid}),
1063 json=self.fake_baremetal_port),
1064 dict(
1065 method='PUT',
1066 uri=self.get_mock_url(
1067 resource='nodes',
1068 append=[self.fake_baremetal_node['uuid'],
1069 'states', 'provision']),
1070 validate=dict(json={'target': 'manage'})),
1071 dict(
1072 method='GET',
1073 uri=self.get_mock_url(
1074 resource='nodes',
1075 append=[self.fake_baremetal_node['uuid']]),
1076 json=failed_node),
1077 dict(
1078 method='GET',
1079 uri=self.get_mock_url(
1080 resource='nodes',
1081 append=[self.fake_baremetal_node['uuid']]),
1082 json=failed_node),
1083 ])
1084
1085 self.assertRaises(
1086 exc.OpenStackCloudException,
1087 self.op_cloud.register_machine,
1088 nics,
1089 **node_to_post)
1090 self.assert_calls()
1091
1092 def test_register_machine_enroll_timeout(self):
1093 mac_address = '00:01:02:03:04:05'
1094 nics = [{'mac': mac_address}]
1095 node_uuid = self.fake_baremetal_node['uuid']
1096 node_to_post = {
1097 'chassis_uuid': None,
1098 'driver': None,
1099 'driver_info': None,
1100 'name': self.fake_baremetal_node['name'],
1101 'properties': None,
1102 'uuid': node_uuid}
1103 self.fake_baremetal_node['provision_state'] = 'enroll'
1104 busy_node = self.fake_baremetal_node.copy()
1105 busy_node['reservation'] = 'conductor0'
1106 busy_node['provision_state'] = 'verifying'
1107 self.register_uris([
1108 dict(
1109 method='POST',
1110 uri=self.get_mock_url(
1111 resource='nodes'),
1112 json=self.fake_baremetal_node,
1113 validate=dict(json=node_to_post)),
1114 dict(
1115 method='POST',
1116 uri=self.get_mock_url(
1117 resource='ports'),
1118 validate=dict(json={'address': mac_address,
1119 'node_uuid': node_uuid}),
1120 json=self.fake_baremetal_port),
1121 dict(
1122 method='PUT',
1123 uri=self.get_mock_url(
1124 resource='nodes',
1125 append=[self.fake_baremetal_node['uuid'],
1126 'states', 'provision']),
1127 validate=dict(json={'target': 'manage'})),
1128 dict(
1129 method='GET',
1130 uri=self.get_mock_url(
1131 resource='nodes',
1132 append=[self.fake_baremetal_node['uuid']]),
1133 json=self.fake_baremetal_node),
1134 dict(
1135 method='GET',
1136 uri=self.get_mock_url(
1137 resource='nodes',
1138 append=[self.fake_baremetal_node['uuid']]),
1139 json=busy_node),
1140 ])
1141 # NOTE(TheJulia): This test shortcircuits the timeout loop
1142 # such that it executes only once. The very last returned
1143 # state to the API is essentially a busy state that we
1144 # want to block on until it has cleared.
1145 self.assertRaises(
1146 exc.OpenStackCloudException,
1147 self.op_cloud.register_machine,
1148 nics,
1149 timeout=0.001,
1150 lock_timeout=0.001,
1151 **node_to_post)
1152 self.assert_calls()
1153
1154 def test_register_machine_enroll_timeout_wait(self):
1155 mac_address = '00:01:02:03:04:05'
1156 nics = [{'mac': mac_address}]
1157 node_uuid = self.fake_baremetal_node['uuid']
1158 node_to_post = {
1159 'chassis_uuid': None,
1160 'driver': None,
1161 'driver_info': None,
1162 'name': self.fake_baremetal_node['name'],
1163 'properties': None,
1164 'uuid': node_uuid}
1165 self.fake_baremetal_node['provision_state'] = 'enroll'
1166 self.register_uris([
1167 dict(
1168 method='POST',
1169 uri=self.get_mock_url(
1170 resource='nodes'),
1171 json=self.fake_baremetal_node,
1172 validate=dict(json=node_to_post)),
1173 dict(
1174 method='POST',
1175 uri=self.get_mock_url(
1176 resource='ports'),
1177 validate=dict(json={'address': mac_address,
1178 'node_uuid': node_uuid}),
1179 json=self.fake_baremetal_port),
1180 dict(
1181 method='GET',
1182 uri=self.get_mock_url(
1183 resource='nodes',
1184 append=[self.fake_baremetal_node['uuid']]),
1185 json=self.fake_baremetal_node),
1186 dict(
1187 method='PUT',
1188 uri=self.get_mock_url(
1189 resource='nodes',
1190 append=[self.fake_baremetal_node['uuid'],
1191 'states', 'provision']),
1192 validate=dict(json={'target': 'manage'})),
1193 dict(
1194 method='GET',
1195 uri=self.get_mock_url(
1196 resource='nodes',
1197 append=[self.fake_baremetal_node['uuid']]),
1198 json=self.fake_baremetal_node),
1199 ])
1200 self.assertRaises(
1201 exc.OpenStackCloudException,
1202 self.op_cloud.register_machine,
1203 nics,
1204 wait=True,
1205 timeout=0.001,
1206 **node_to_post)
1207 self.assert_calls()
1208
1209 def test_register_machine_port_create_failed(self):
1210 mac_address = '00:01:02:03:04:05'
1211 nics = [{'mac': mac_address}]
1212 node_uuid = self.fake_baremetal_node['uuid']
1213 node_to_post = {
1214 'chassis_uuid': None,
1215 'driver': None,
1216 'driver_info': None,
1217 'name': self.fake_baremetal_node['name'],
1218 'properties': None,
1219 'uuid': node_uuid}
1220 self.fake_baremetal_node['provision_state'] = 'available'
1221 self.register_uris([
1222 dict(
1223 method='POST',
1224 uri=self.get_mock_url(
1225 resource='nodes'),
1226 json=self.fake_baremetal_node,
1227 validate=dict(json=node_to_post)),
1228 dict(
1229 method='POST',
1230 uri=self.get_mock_url(
1231 resource='ports'),
1232 status_code=400,
1233 json={'error': 'invalid'},
1234 validate=dict(json={'address': mac_address,
1235 'node_uuid': node_uuid})),
1236 dict(
1237 method='DELETE',
1238 uri=self.get_mock_url(
1239 resource='nodes',
1240 append=[self.fake_baremetal_node['uuid']])),
1241 ])
1242 self.assertRaises(exc.OpenStackCloudException,
1243 self.op_cloud.register_machine,
1244 nics, **node_to_post)
1245
1246 self.assert_calls()
1247
1248 def test_unregister_machine(self):
1249 mac_address = self.fake_baremetal_port['address']
1250 nics = [{'mac': mac_address}]
1251 port_uuid = self.fake_baremetal_port['uuid']
1252 # NOTE(TheJulia): The two values below should be the same.
1253 port_node_uuid = self.fake_baremetal_port['node_uuid']
1254 port_url_address = 'detail?address=%s' % mac_address
1255 self.fake_baremetal_node['provision_state'] = 'available'
1256 self.register_uris([
1257 dict(
1258 method='GET',
1259 uri=self.get_mock_url(
1260 resource='nodes',
1261 append=[self.fake_baremetal_node['uuid']]),
1262 json=self.fake_baremetal_node),
1263 dict(
1264 method='GET',
1265 uri=self.get_mock_url(
1266 resource='ports',
1267 append=[port_url_address]),
1268 json={'ports': [{'address': mac_address,
1269 'node_uuid': port_node_uuid,
1270 'uuid': port_uuid}]}),
1271 dict(
1272 method='DELETE',
1273 uri=self.get_mock_url(
1274 resource='ports',
1275 append=[self.fake_baremetal_port['uuid']])),
1276 dict(
1277 method='DELETE',
1278 uri=self.get_mock_url(
1279 resource='nodes',
1280 append=[self.fake_baremetal_node['uuid']])),
1281 ])
1282
1283 self.op_cloud.unregister_machine(nics,
1284 self.fake_baremetal_node['uuid'])
1285
1286 self.assert_calls()
1287
1288 def test_unregister_machine_timeout(self):
1289 mac_address = self.fake_baremetal_port['address']
1290 nics = [{'mac': mac_address}]
1291 port_uuid = self.fake_baremetal_port['uuid']
1292 port_node_uuid = self.fake_baremetal_port['node_uuid']
1293 port_url_address = 'detail?address=%s' % mac_address
1294 self.fake_baremetal_node['provision_state'] = 'available'
1295 self.register_uris([
1296 dict(
1297 method='GET',
1298 uri=self.get_mock_url(
1299 resource='nodes',
1300 append=[self.fake_baremetal_node['uuid']]),
1301 json=self.fake_baremetal_node),
1302 dict(
1303 method='GET',
1304 uri=self.get_mock_url(
1305 resource='ports',
1306 append=[port_url_address]),
1307 json={'ports': [{'address': mac_address,
1308 'node_uuid': port_node_uuid,
1309 'uuid': port_uuid}]}),
1310 dict(
1311 method='DELETE',
1312 uri=self.get_mock_url(
1313 resource='ports',
1314 append=[self.fake_baremetal_port['uuid']])),
1315 dict(
1316 method='DELETE',
1317 uri=self.get_mock_url(
1318 resource='nodes',
1319 append=[self.fake_baremetal_node['uuid']])),
1320 dict(
1321 method='GET',
1322 uri=self.get_mock_url(
1323 resource='nodes',
1324 append=[self.fake_baremetal_node['uuid']]),
1325 json=self.fake_baremetal_node),
1326 ])
1327 self.assertRaises(
1328 exc.OpenStackCloudException,
1329 self.op_cloud.unregister_machine,
1330 nics,
1331 self.fake_baremetal_node['uuid'],
1332 wait=True,
1333 timeout=0.001)
1334
1335 self.assert_calls()
1336
1337 def test_unregister_machine_unavailable(self):
1338 # This is a list of invalid states that the method
1339 # should fail on.
1340 invalid_states = ['active', 'cleaning', 'clean wait', 'clean failed']
1341 mac_address = self.fake_baremetal_port['address']
1342 nics = [{'mac': mac_address}]
1343 url_list = []
1344 for state in invalid_states:
1345 self.fake_baremetal_node['provision_state'] = state
1346 url_list.append(
1347 dict(
1348 method='GET',
1349 uri=self.get_mock_url(
1350 resource='nodes',
1351 append=[self.fake_baremetal_node['uuid']]),
1352 json=self.fake_baremetal_node))
1353
1354 self.register_uris(url_list)
1355
1356 for state in invalid_states:
1357 self.assertRaises(
1358 exc.OpenStackCloudException,
1359 self.op_cloud.unregister_machine,
1360 nics,
1361 self.fake_baremetal_node['uuid'])
1362
1363 self.assert_calls()
1364
823 def test_update_machine_patch_no_action(self): 1365 def test_update_machine_patch_no_action(self):
824 self.register_uris([dict( 1366 self.register_uris([dict(
825 method='GET', 1367 method='GET',
diff --git a/shade/tests/unit/test_shade_operator.py b/shade/tests/unit/test_shade_operator.py
index 7528178..0fb2ffb 100644
--- a/shade/tests/unit/test_shade_operator.py
+++ b/shade/tests/unit/test_shade_operator.py
@@ -16,7 +16,6 @@ from distutils import version as du_version
16import mock 16import mock
17import munch 17import munch
18import testtools 18import testtools
19import uuid
20 19
21import os_client_config as occ 20import os_client_config as occ
22from os_client_config import cloud_config 21from os_client_config import cloud_config
@@ -31,11 +30,6 @@ class TestShadeOperator(base.RequestsMockTestCase):
31 30
32 def setUp(self): 31 def setUp(self):
33 super(TestShadeOperator, self).setUp() 32 super(TestShadeOperator, self).setUp()
34 self.machine_id = uuid.uuid4().hex
35 self.machine_name = self.getUniqueString('machine')
36 self.node = fakes.make_fake_machine(
37 machine_id=self.machine_id,
38 machine_name=self.machine_name)
39 33
40 def test_operator_cloud(self): 34 def test_operator_cloud(self):
41 self.assertIsInstance(self.op_cloud, shade.OperatorCloud) 35 self.assertIsInstance(self.op_cloud, shade.OperatorCloud)
@@ -71,232 +65,6 @@ class TestShadeOperator(base.RequestsMockTestCase):
71 self.assertRaises(exc.OpenStackCloudException, 65 self.assertRaises(exc.OpenStackCloudException,
72 self.op_cloud.list_nics_for_machine, None) 66 self.op_cloud.list_nics_for_machine, None)
73 67
74 @mock.patch.object(shade.OperatorCloud, 'ironic_client')
75 def test_register_machine(self, mock_client):
76 class fake_node(object):
77 uuid = "00000000-0000-0000-0000-000000000000"
78 provision_state = "available"
79 reservation = None
80 last_error = None
81
82 expected_return_value = dict(
83 uuid="00000000-0000-0000-0000-000000000000",
84 provision_state="available",
85 reservation=None,
86 last_error=None
87 )
88 mock_client.node.create.return_value = fake_node
89 mock_client.node.get.return_value = fake_node
90 nics = [{'mac': '00:00:00:00:00:00'}]
91 return_value = self.op_cloud.register_machine(nics)
92 self.assertDictEqual(expected_return_value, return_value)
93 self.assertTrue(mock_client.node.create.called)
94 self.assertTrue(mock_client.port.create.called)
95 self.assertFalse(mock_client.node.get.called)
96
97 @mock.patch.object(shade.OperatorCloud, 'ironic_client')
98 @mock.patch.object(shade.OperatorCloud, 'node_set_provision_state')
99 def test_register_machine_enroll(
100 self,
101 mock_set_state,
102 mock_client):
103 machine_uuid = "00000000-0000-0000-0000-000000000000"
104
105 class fake_node_init_state(object):
106 uuid = machine_uuid
107 provision_state = "enroll"
108 reservation = None
109 last_error = None
110
111 class fake_node_post_manage(object):
112 uuid = machine_uuid
113 provision_state = "enroll"
114 reservation = "do you have a flag?"
115 last_error = None
116
117 class fake_node_post_manage_done(object):
118 uuid = machine_uuid
119 provision_state = "manage"
120 reservation = None
121 last_error = None
122
123 class fake_node_post_provide(object):
124 uuid = machine_uuid
125 provision_state = "available"
126 reservation = None
127 last_error = None
128
129 class fake_node_post_enroll_failure(object):
130 uuid = machine_uuid
131 provision_state = "enroll"
132 reservation = None
133 last_error = "insufficent lolcats"
134
135 expected_return_value = dict(
136 uuid=machine_uuid,
137 provision_state="available",
138 reservation=None,
139 last_error=None
140 )
141
142 mock_client.node.get.side_effect = iter([
143 fake_node_init_state,
144 fake_node_post_manage,
145 fake_node_post_manage_done,
146 fake_node_post_provide])
147 mock_client.node.create.return_value = fake_node_init_state
148 nics = [{'mac': '00:00:00:00:00:00'}]
149 return_value = self.op_cloud.register_machine(nics)
150 self.assertDictEqual(expected_return_value, return_value)
151 self.assertTrue(mock_client.node.create.called)
152 self.assertTrue(mock_client.port.create.called)
153 self.assertTrue(mock_client.node.get.called)
154 mock_client.reset_mock()
155 mock_client.node.get.side_effect = iter([
156 fake_node_init_state,
157 fake_node_post_manage,
158 fake_node_post_manage_done,
159 fake_node_post_provide])
160 return_value = self.op_cloud.register_machine(nics, wait=True)
161 self.assertDictEqual(expected_return_value, return_value)
162 self.assertTrue(mock_client.node.create.called)
163 self.assertTrue(mock_client.port.create.called)
164 self.assertTrue(mock_client.node.get.called)
165 mock_client.reset_mock()
166 mock_client.node.get.side_effect = iter([
167 fake_node_init_state,
168 fake_node_post_manage,
169 fake_node_post_enroll_failure])
170 self.assertRaises(
171 shade.OpenStackCloudException,
172 self.op_cloud.register_machine,
173 nics)
174 self.assertRaises(
175 shade.OpenStackCloudException,
176 self.op_cloud.register_machine,
177 nics,
178 wait=True)
179
180 @mock.patch.object(shade.OperatorCloud, 'ironic_client')
181 @mock.patch.object(shade.OperatorCloud, 'node_set_provision_state')
182 def test_register_machine_enroll_timeout(
183 self,
184 mock_set_state,
185 mock_client):
186 machine_uuid = "00000000-0000-0000-0000-000000000000"
187
188 class fake_node_init_state(object):
189 uuid = machine_uuid
190 provision_state = "enroll"
191 reservation = "do you have a flag?"
192 last_error = None
193
194 mock_client.node.get.return_value = fake_node_init_state
195 mock_client.node.create.return_value = fake_node_init_state
196 nics = [{'mac': '00:00:00:00:00:00'}]
197 self.assertRaises(
198 shade.OpenStackCloudException,
199 self.op_cloud.register_machine,
200 nics,
201 lock_timeout=0.001)
202 self.assertTrue(mock_client.node.create.called)
203 self.assertTrue(mock_client.port.create.called)
204 self.assertTrue(mock_client.node.get.called)
205 mock_client.node.get.reset_mock()
206 mock_client.node.create.reset_mock()
207 self.assertRaises(
208 shade.OpenStackCloudException,
209 self.op_cloud.register_machine,
210 nics,
211 wait=True,
212 timeout=0.001)
213 self.assertTrue(mock_client.node.create.called)
214 self.assertTrue(mock_client.port.create.called)
215 self.assertTrue(mock_client.node.get.called)
216
217 @mock.patch.object(shade.OperatorCloud, 'ironic_client')
218 def test_register_machine_port_create_failed(self, mock_client):
219 class fake_node(object):
220 uuid = "00000000-0000-0000-0000-000000000000"
221 provision_state = "available"
222 resevation = None
223 last_error = None
224
225 nics = [{'mac': '00:00:00:00:00:00'}]
226 mock_client.node.create.return_value = fake_node
227 mock_client.port.create.side_effect = (
228 exc.OpenStackCloudException("Error"))
229 self.assertRaises(exc.OpenStackCloudException,
230 self.op_cloud.register_machine,
231 nics)
232 self.assertTrue(mock_client.node.create.called)
233 self.assertTrue(mock_client.port.create.called)
234 self.assertTrue(mock_client.node.delete.called)
235
236 @mock.patch.object(shade.OperatorCloud, 'ironic_client')
237 def test_unregister_machine(self, mock_client):
238 class fake_node(object):
239 provision_state = 'available'
240
241 class fake_port(object):
242 uuid = '00000000-0000-0000-0000-000000000001'
243
244 mock_client.port.get_by_address.return_value = fake_port
245 mock_client.node.get.return_value = fake_node
246 nics = [{'mac': '00:00:00:00:00:00'}]
247 uuid = "00000000-0000-0000-0000-000000000000"
248 self.op_cloud.unregister_machine(nics, uuid)
249 self.assertTrue(mock_client.node.delete.called)
250 self.assertTrue(mock_client.port.get_by_address.called)
251 self.assertTrue(mock_client.port.delete.called)
252 self.assertTrue(mock_client.port.get_by_address.called)
253 mock_client.port.get_by_address.assert_called_with(
254 address='00:00:00:00:00:00')
255 mock_client.port.delete.assert_called_with(
256 port_id='00000000-0000-0000-0000-000000000001')
257
258 @mock.patch.object(shade.OperatorCloud, 'ironic_client')
259 def test_unregister_machine_unavailable(self, mock_client):
260 invalid_states = ['active', 'cleaning', 'clean wait', 'clean failed']
261 nics = [{'mac': '00:00:00:00:00:00'}]
262 uuid = "00000000-0000-0000-0000-000000000000"
263 for state in invalid_states:
264 class fake_node(object):
265 provision_state = state
266
267 mock_client.node.get.return_value = fake_node
268 self.assertRaises(
269 exc.OpenStackCloudException,
270 self.op_cloud.unregister_machine,
271 nics,
272 uuid)
273 self.assertFalse(mock_client.node.delete.called)
274 self.assertFalse(mock_client.port.delete.called)
275 self.assertFalse(mock_client.port.get_by_address.called)
276 self.assertTrue(mock_client.node.get.called)
277 mock_client.node.reset_mock()
278 mock_client.node.reset_mock()
279
280 @mock.patch.object(shade.OperatorCloud, 'ironic_client')
281 def test_unregister_machine_timeout(self, mock_client):
282 class fake_node(object):
283 provision_state = 'available'
284
285 mock_client.node.get.return_value = fake_node
286 nics = [{'mac': '00:00:00:00:00:00'}]
287 uuid = "00000000-0000-0000-0000-000000000000"
288 self.assertRaises(
289 exc.OpenStackCloudException,
290 self.op_cloud.unregister_machine,
291 nics,
292 uuid,
293 wait=True,
294 timeout=0.001)
295 self.assertTrue(mock_client.node.delete.called)
296 self.assertTrue(mock_client.port.delete.called)
297 self.assertTrue(mock_client.port.get_by_address.called)
298 self.assertTrue(mock_client.node.get.called)
299
300 @mock.patch.object(shade.OpenStackCloud, '_image_client') 68 @mock.patch.object(shade.OpenStackCloud, '_image_client')
301 def test_get_image_name(self, mock_client): 69 def test_get_image_name(self, mock_client):
302 70