Unit Testcases for GCE Cinder Driver

Additional data files in gce/data:

1. Fake service key file(omni.json)
2. Data required to generate Gce Service(service_data.json)
3. Fake snapshot data for get_snapshot API(get_snapshot.json)

Change-Id: Ib286337218f0168e3817bd13387edc349de97afa
Signed-off-by: Sanket <sanket@infracloud.io>
This commit is contained in:
Sanket 2017-05-22 16:48:04 +05:30
parent 1de5c3778c
commit 1e54ec1abe
6 changed files with 19348 additions and 0 deletions

View File

@ -0,0 +1,12 @@
{
"type": "service_account",
"project_id": "omni-163105",
"private_key_id": "private_key_id",
"private_key": "-----BEGIN PRIVATE KEY-----\nstringofprivatekey2343\n-----END PRIVATE KEY-----\n",
"client_email": "random_email@developer.gserviceaccount.com",
"client_id": "12345678900987654321",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/674783144440-compute%40developer.gserviceaccount.com"
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,18 @@
{
"kind": "compute#snapshot",
"id": "6215990068200396094",
"creationTimestamp": "2017-03-30T13:34:25.760-07:00",
"name": "snap-253b2878-ec60-4793-ad19-e65496ec7aab",
"description": "Snapshot of VM with devstack installed and openstack omni cloned. \n\nAll passwords are mentioned in ~/devstack/local.conf\n\nTo start the show : cd ~/devstack; ./stack.sh",
"status": "READY",
"sourceDisk": "projects/omni-163105/zones/us-central1-a/disks/omni-dev",
"sourceDiskId": "2447162145770627697",
"diskSizeGb": "15",
"storageBytes": "1903741468",
"storageBytesStatus": "UP_TO_DATE",
"licenses": [
"https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/licenses/ubuntu-1604-xenial"
],
"selfLink": "projects/omni-163105/global/snapshots/snap-253b2878-ec60-4793-ad19-e65496ec7aab",
"labelFingerprint": "42WmSpB8rSM="
}

View File

@ -0,0 +1,50 @@
# Copyright (c) 2017 Platform9 Systems Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either expressed or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
from apiclient.http import HttpMock
from googleapiclient.discovery import build
DATA_DIR = os.path.dirname(os.path.abspath(__file__)) + '/data'
def fake_operation():
return {'name': 'fake_operation'}
def get_gce_service(service_key):
http = HttpMock(DATA_DIR + '/service/service_data.json', {'status': '200'})
service = build('compute', 'v1', http=http, developerKey=service_key)
return service
def get_snapshot(compute, project, name):
http = HttpMock(DATA_DIR + '/snapshot/get_snapshot.json',
{'status': '200'})
request = compute.snapshots().get(project=project, snapshot=name)
response = request.execute(http=http)
return response
def create_anything(*args, **kwargs):
return fake_operation()
def wait_for_operation(compute, project, operation, interval=1, timeout=60):
pass
def delete_anything(*args, **kwargs):
return fake_operation()

View File

@ -0,0 +1,114 @@
# Copyright (c) 2017 Platform9 Systems Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either expressed or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
import os
from cinder import context
from cinder import test
from cinder.volume.drivers.gce.driver import GceDriver
from cinder.tests.unit.volume.drivers.gce import gce_mock
from cinder.tests.unit.fake_volume import fake_volume_obj
from cinder.tests.unit.fake_snapshot import fake_snapshot_obj
from cinder.volume.drivers.gce.gceutils import GceOperationError
DATA_DIR = os.path.dirname(os.path.abspath(__file__)) + '/data'
class GCEVolumeTestCase(test.TestCase):
@mock.patch('cinder.volume.drivers.gce.driver.gceutils.get_gce_service')
def setUp(self, mock_service):
mock_service.side_effect = gce_mock.get_gce_service
super(GCEVolumeTestCase, self).setUp()
self._driver = GceDriver()
self._driver.gce_zone = 'us-central1-c'
self._driver.gce_region = 'us-central1'
self._driver.gce_project = 'omni-163105'
self._driver.gce_svc_key = "{0}/omni.json".format(DATA_DIR)
self.test_context = context.get_admin_context()
self.fake_volume = fake_volume_obj(self.test_context)
self.fake_snapshot = fake_snapshot_obj(self.test_context)
self.fake_snapshot.volume = self.fake_volume
self._driver.do_setup(self.test_context)
@mock.patch('cinder.volume.drivers.gce.driver.gceutils.wait_for_operation')
@mock.patch('cinder.volume.drivers.gce.driver.gceutils.create_disk')
def _create_volume(self, mock_disk, mock_wait):
mock_disk.side_effect = gce_mock.create_anything
mock_wait.side_effect = gce_mock.wait_for_operation
self._driver.create_volume(self.fake_volume)
mock_wait.assert_called_once_with(self._driver.gce_svc,
self._driver.gce_project,
gce_mock.fake_operation())
@mock.patch('cinder.volume.drivers.gce.driver.gceutils.wait_for_operation')
@mock.patch('cinder.volume.drivers.gce.driver.gceutils.snapshot_disk')
def _create_snapshot(self, mock_snapshot, mock_wait):
mock_snapshot.side_effect = gce_mock.create_anything
mock_wait.side_effect = gce_mock.wait_for_operation
self._driver.create_snapshot(self.fake_snapshot)
mock_wait.assert_called_once_with(self._driver.gce_svc,
self._driver.gce_project,
gce_mock.fake_operation())
def test_volume_create_success(self):
self.assertIsNone(self._create_volume())
@mock.patch('cinder.volume.drivers.gce.driver.gceutils.wait_for_operation')
@mock.patch('cinder.volume.drivers.gce.driver.gceutils.delete_disk')
def test_volume_deletion_success(self, mock_disk, mock_wait):
mock_disk.side_effect = gce_mock.delete_anything
mock_wait.side_effect = gce_mock.wait_for_operation
self.assertIsNone(self._driver.delete_volume(self.fake_volume))
mock_wait.assert_called_once_with(self._driver.gce_svc,
self._driver.gce_project,
gce_mock.fake_operation())
@mock.patch('cinder.volume.drivers.gce.driver.gceutils.wait_for_operation')
@mock.patch('cinder.volume.drivers.gce.driver.gceutils.create_disk')
def test_volume_deletion_failure(self, mock_disk, mock_wait):
mock_disk.side_effect = gce_mock.create_anything
mock_wait.side_effect = GceOperationError
self.assertRaises(GceOperationError, self._driver.delete_volume,
self.fake_volume)
def test_create_snapshot(self):
self._create_volume()
self.assertIsNone(self._create_snapshot())
@mock.patch('cinder.volume.drivers.gce.driver.gceutils.wait_for_operation')
@mock.patch('cinder.volume.drivers.gce.driver.gceutils.delete_snapshot')
def test_delete_snapshot(self, mock_snapshot, mock_wait):
mock_snapshot.side_effect = gce_mock.delete_anything
mock_wait.side_effect = gce_mock.wait_for_operation
self.assertIsNone(self._driver.delete_snapshot(self.fake_snapshot))
mock_wait.assert_called_once_with(self._driver.gce_svc,
self._driver.gce_project,
gce_mock.fake_operation())
@mock.patch('cinder.volume.drivers.gce.driver.gceutils.wait_for_operation')
@mock.patch(
'cinder.volume.drivers.gce.driver.gceutils.create_disk_from_snapshot')
@mock.patch('cinder.volume.drivers.gce.driver.gceutils.get_snapshot')
def test_create_volume_from_snapshot(self, mock_get_snapshot,
mock_snapshot, mock_wait):
mock_get_snapshot.side_effect = gce_mock.get_snapshot
mock_snapshot.side_effect = gce_mock.create_anything
mock_wait.side_effect = gce_mock.wait_for_operation
self.assertIsNone(
self._driver.create_volume_from_snapshot(self.fake_volume,
self.fake_snapshot))
mock_wait.assert_called_once_with(self._driver.gce_svc,
self._driver.gce_project,
gce_mock.fake_operation())