def test_create_nested_snapshots_calls_order(self): fake_volume1 = mock.MagicMock() fake_volume2 = mock.MagicMock() fake_snapshot1 = mock.MagicMock() fake_snapshot2 = mock.MagicMock() scenario = volumes.CreateNestedSnapshotsAndAttachVolume( self._get_context()) scenario._attach_volume = mock.MagicMock(return_value=mock.MagicMock()) scenario._detach_volume = mock.MagicMock() scenario._delete_server = mock.MagicMock() scenario._create_volume = mock.MagicMock( side_effect=[fake_volume1, fake_volume2]) scenario._delete_volume = mock.MagicMock() scenario._create_snapshot = mock.MagicMock( side_effect=[fake_snapshot1, fake_snapshot2]) scenario._delete_snapshot = mock.MagicMock() scenario.run(nested_level=2) vol_delete_calls = [mock.call(fake_volume2), mock.call(fake_volume1)] snap_delete_calls = [ mock.call(fake_snapshot2), mock.call(fake_snapshot1) ] scenario._delete_volume.assert_has_calls(vol_delete_calls) scenario._delete_snapshot.assert_has_calls(snap_delete_calls)
def test_create_nested_snapshots_check_resources_size(self, mock_random): mock_random.randint.return_value = 3 fake_volume = mock.MagicMock() fake_snapshot = mock.MagicMock() fake_server = mock.MagicMock() scenario = volumes.CreateNestedSnapshotsAndAttachVolume( self._get_context()) scenario.get_random_server = mock.MagicMock(return_value=fake_server) scenario._attach_volume = mock.MagicMock(return_value=mock.MagicMock()) scenario._detach_volume = mock.MagicMock() scenario._delete_server = mock.MagicMock() scenario._create_volume = mock.MagicMock(return_value=fake_volume) scenario._delete_volume = mock.MagicMock() scenario._create_snapshot = mock.MagicMock(return_value=fake_snapshot) scenario._delete_snapshot = mock.MagicMock() scenario.run(nested_level=2) # NOTE: One call for random size random_call_count = mock_random.randint.call_count self.assertEqual(1, random_call_count) calls = scenario._create_volume.mock_calls expected_calls = [mock.call(3)] expected_calls.extend([mock.call(3, snapshot_id=fake_snapshot.id)]) self.assertEqual(expected_calls, calls)
def test_create_nested_snapshots_and_attach_volume_deprecate_kwargs(self): fake_volume = mock.MagicMock() fake_volume.id = "FAKE_ID" fake_snapshot = mock.MagicMock() fake_attach = mock.MagicMock() scenario = volumes.CreateNestedSnapshotsAndAttachVolume( self._get_context()) scenario._attach_volume = mock.MagicMock(return_value=fake_attach) scenario._detach_volume = mock.MagicMock() scenario._delete_server = mock.MagicMock() scenario._create_volume = mock.MagicMock(return_value=fake_volume) scenario._delete_volume = mock.MagicMock() scenario._create_snapshot = mock.MagicMock(return_value=fake_snapshot) scenario._delete_snapshot = mock.MagicMock() volume_kwargs = {"volume_type": "type1"} snapshot_kwargs = {"name": "snapshot1", "description": "snaphot one"} scenario.run(size={ "min": 1, "max": 1 }, create_volume_kwargs=volume_kwargs, **snapshot_kwargs) scenario._create_snapshot.assert_called_once_with( fake_volume.id, False, **snapshot_kwargs) self.assertEqual(fake_snapshot, scenario._create_snapshot.return_value)
def test_create_nested_snapshots_and_attach_volume_kwargs(self): fake_volume = mock.MagicMock() fake_snapshot = mock.MagicMock() fake_attach = mock.MagicMock() scenario = volumes.CreateNestedSnapshotsAndAttachVolume( context=self._get_context()) scenario._attach_volume = mock.MagicMock(return_value=fake_attach) scenario._detach_volume = mock.MagicMock() scenario._delete_server = mock.MagicMock() scenario._create_volume = mock.MagicMock(return_value=fake_volume) scenario._delete_volume = mock.MagicMock() scenario._create_snapshot = mock.MagicMock(return_value=fake_snapshot) scenario._delete_snapshot = mock.MagicMock() volume_kwargs = {"volume_type": "type1"} scenario.run(size={ "min": 1, "max": 1 }, create_volume_kwargs=volume_kwargs) scenario._create_volume.assert_called_once_with(1, **volume_kwargs) self.assertEqual(fake_volume, scenario._create_volume.return_value)
def test_create_nested_snapshots_and_attach_volume(self): fake_volume = mock.MagicMock() fake_snapshot = mock.MagicMock() fake_attach = mock.MagicMock() scenario = volumes.CreateNestedSnapshotsAndAttachVolume( context=self._get_context()) scenario._attach_volume = mock.MagicMock(return_value=fake_attach) scenario._detach_volume = mock.MagicMock() scenario._delete_server = mock.MagicMock() scenario._create_volume = mock.MagicMock(return_value=fake_volume) scenario._delete_volume = mock.MagicMock() scenario._create_snapshot = mock.MagicMock(return_value=fake_snapshot) scenario._delete_snapshot = mock.MagicMock() scenario.run() volume_count = scenario._create_volume.call_count snapshots_count = scenario._create_snapshot.call_count attached_count = scenario._attach_volume.call_count self.assertEqual(scenario._delete_volume.call_count, volume_count) self.assertEqual(scenario._delete_snapshot.call_count, snapshots_count) self.assertEqual(scenario._detach_volume.call_count, attached_count)
def test_create_nested_snapshots_and_attach_volume(self, mock_randint): mock_service = self.mock_cinder.return_value mock_randint.return_value = 2 volume_kwargs = {"volume_type": "type1"} snapshot_kwargs = {"name": "snapshot1", "description": "snaphot one"} scenario = volumes.CreateNestedSnapshotsAndAttachVolume( context=self._get_context()) scenario.get_random_server = mock.MagicMock() scenario._attach_volume = mock.MagicMock() scenario._detach_volume = mock.MagicMock() scenario.run(create_volume_kwargs=volume_kwargs, create_snapshot_kwargs=snapshot_kwargs) mock_service.create_volume.assert_called_once_with( mock_randint.return_value, **volume_kwargs) mock_service.create_snapshot.assert_called_once_with( mock_service.create_volume.return_value.id, force=False, **snapshot_kwargs) scenario._attach_volume(scenario.get_random_server.return_value, mock_service.create_volume.return_value) mock_service.delete_volume.assert_called_once_with( mock_service.create_volume.return_value) mock_service.delete_snapshot.assert_called_once_with( mock_service.create_snapshot.return_value) scenario._detach_volume.assert_called_once_with( scenario.get_random_server.return_value, mock_service.create_volume.return_value, scenario._attach_volume.return_value)
def test_create_nested_snapshots_and_attach_volume_2(self, mock_randint): mock_service = self.mock_cinder.return_value mock_randint.return_value = 2 nested_level = 3 volume_size = mock_randint.return_value fake_volumes = [ mock.Mock(size=volume_size) for i in range(nested_level) ] fake_snapshots = [mock.Mock() for i in range(nested_level)] fake_attachs = [ mock.Mock(size=volume_size) for i in range(nested_level) ] mock_service.create_volume.side_effect = fake_volumes mock_service.create_snapshot.side_effect = fake_snapshots scenario = volumes.CreateNestedSnapshotsAndAttachVolume( context=self._get_context()) scenario.get_random_server = mock.MagicMock() scenario._attach_volume = mock.MagicMock(side_effect=fake_attachs) scenario._detach_volume = mock.MagicMock() scenario.run(nested_level=nested_level) expected_volumes = [mock.call(volume_size)] expected_snapshots = [mock.call(fake_volumes[0].id, force=False)] expected_attachs = [ mock.call(scenario.get_random_server.return_value, fake_volumes[0]) ] for i in range(nested_level - 1): expected_volumes.append( mock.call(volume_size, snapshot_id=fake_snapshots[i].id)) expected_snapshots.append( mock.call(fake_volumes[i + 1].id, force=False)) expected_attachs.append( mock.call(scenario.get_random_server.return_value, fake_volumes[i + 1])) mock_service.create_volume.assert_has_calls(expected_volumes) mock_service.create_snapshot.assert_has_calls(expected_snapshots) scenario._attach_volume.assert_has_calls(expected_attachs) fake_volumes.reverse() fake_snapshots.reverse() fake_attachs.reverse() mock_service.delete_volume.assert_has_calls( [mock.call(volume) for volume in fake_volumes]) mock_service.delete_snapshot.assert_has_calls( [mock.call(snapshot) for snapshot in fake_snapshots]) scenario._detach_volume.assert_has_calls([ mock.call(scenario.get_random_server.return_value, fake_volumes[i], fake_attachs[i]) for i in range(len(fake_volumes)) ])