class VSWaitReadyGoTests(unittest.TestCase): def setUp(self): self.client = MagicMock() self.vs = VSManager(self.client) self.guestObject = self.client['Virtual_Guest'].getObject @patch('SoftLayer.managers.vs.VSManager.wait_for_ready') def test_wait_interface(self, ready): # verify interface to wait_for_ready is intact self.vs.wait_for_transaction(1, 1) ready.assert_called_once_with(1, 1, delay=1, pending=True) def test_active_not_provisioned(self): # active transaction and no provision date should be false self.guestObject.side_effect = [ {'activeTransaction': {'id': 1}}, ] value = self.vs.wait_for_ready(1, 1) self.assertFalse(value) def test_active_and_provisiondate(self): # active transaction and provision date should be True self.guestObject.side_effect = [ {'activeTransaction': {'id': 1}, 'provisionDate': 'aaa'}, ] value = self.vs.wait_for_ready(1, 1) self.assertTrue(value) def test_active_provision_pending(self): # active transaction and provision date # and pending should be false self.guestObject.side_effect = [ {'activeTransaction': {'id': 1}, 'provisionDate': 'aaa'}, ] value = self.vs.wait_for_ready(1, 1, pending=True) self.assertFalse(value) def test_active_reload(self): # actively running reload self.guestObject.side_effect = [ { 'activeTransaction': {'id': 1}, 'provisionDate': 'aaa', 'lastOperatingSystemReload': {'id': 1}, }, ] value = self.vs.wait_for_ready(1, 1) self.assertFalse(value) def test_reload_no_pending(self): # reload complete, maintance transactions self.guestObject.side_effect = [ { 'activeTransaction': {'id': 2}, 'provisionDate': 'aaa', 'lastOperatingSystemReload': {'id': 1}, }, ] value = self.vs.wait_for_ready(1, 1) self.assertTrue(value) def test_reload_pending(self): # reload complete, pending maintance transactions self.guestObject.side_effect = [ { 'activeTransaction': {'id': 2}, 'provisionDate': 'aaa', 'lastOperatingSystemReload': {'id': 1}, }, ] value = self.vs.wait_for_ready(1, 1, pending=True) self.assertFalse(value) @patch('SoftLayer.managers.vs.sleep') def test_ready_iter_once_incomplete(self, _sleep): self.guestObject = self.client['Virtual_Guest'].getObject # no iteration, false self.guestObject.side_effect = [ {'activeTransaction': {'id': 1}}, ] value = self.vs.wait_for_ready(1, 1) self.assertFalse(value) self.assertFalse(_sleep.called) @patch('SoftLayer.managers.vs.sleep') def test_iter_once_complete(self, _sleep): # no iteration, true self.guestObject.side_effect = [ {'provisionDate': 'aaa'}, ] value = self.vs.wait_for_ready(1, 1) self.assertTrue(value) self.assertFalse(_sleep.called) @patch('SoftLayer.managers.vs.sleep') def test_iter_four_complete(self, _sleep): # test 4 iterations with positive match self.guestObject.side_effect = [ {'activeTransaction': {'id': 1}}, {'activeTransaction': {'id': 1}}, {'activeTransaction': {'id': 1}}, {'provisionDate': 'aaa'}, ] value = self.vs.wait_for_ready(1, 4) self.assertTrue(value) _sleep.assert_has_calls([call(1), call(1), call(1)]) self.guestObject.assert_has_calls([ call(id=1, mask=ANY), call(id=1, mask=ANY), call(id=1, mask=ANY), call(id=1, mask=ANY), ]) @patch('SoftLayer.managers.vs.sleep') def test_iter_two_incomplete(self, _sleep): # test 2 iterations, with no matches self.guestObject.side_effect = [ {'activeTransaction': {'id': 1}}, {'activeTransaction': {'id': 1}}, {'provisionDate': 'aaa'} ] value = self.vs.wait_for_ready(1, 2) self.assertFalse(value) _sleep.assert_called_once_with(1) self.guestObject.assert_has_calls([ call(id=1, mask=ANY), call(id=1, mask=ANY), ]) @patch('SoftLayer.managers.vs.sleep') def test_iter_ten_incomplete(self, _sleep): # 10 iterations at 10 second sleeps with no # matching values. self.guestObject.side_effect = [ {}, {'activeTransaction': {'id': 1}}, {'activeTransaction': {'id': 1}}, {'activeTransaction': {'id': 1}}, {'activeTransaction': {'id': 1}}, {'activeTransaction': {'id': 1}}, {'activeTransaction': {'id': 1}}, {'activeTransaction': {'id': 1}}, {'activeTransaction': {'id': 1}}, {'activeTransaction': {'id': 1}}, ] value = self.vs.wait_for_ready(1, 10, delay=10) self.assertFalse(value) self.guestObject.assert_has_calls([ call(id=1, mask=ANY), call(id=1, mask=ANY), call(id=1, mask=ANY), call(id=1, mask=ANY), call(id=1, mask=ANY), call(id=1, mask=ANY), call(id=1, mask=ANY), call(id=1, mask=ANY), call(id=1, mask=ANY), call(id=1, mask=ANY), ]) # should only be 9 calls to sleep, last iteration # should return a value and skip the sleep _sleep.assert_has_calls([ call(10), call(10), call(10), call(10), call(10), call(10), call(10), call(10), call(10)])