From f855e21b4433baa553810a89d885e868608ae500 Mon Sep 17 00:00:00 2001 From: Joseph Vazhappilly Date: Tue, 7 May 2019 17:01:02 +0530 Subject: [PATCH] Add unit test for provision volume workflow --- .../tests/test_action_attach_volume.py | 59 +++++++++++++++++++ .../tests/test_action_create_volume.py | 52 ++++++++++++++++ .../tests/test_action_delete_attachment.py | 40 +++++++++++++ .../tests/test_action_delete_volume.py | 40 +++++++++++++ .../opensds/tests/test_action_list_volume.py | 41 +++++++++++++ 5 files changed, 232 insertions(+) create mode 100644 contrib/st2/opensds/tests/test_action_attach_volume.py create mode 100644 contrib/st2/opensds/tests/test_action_create_volume.py create mode 100644 contrib/st2/opensds/tests/test_action_delete_attachment.py create mode 100644 contrib/st2/opensds/tests/test_action_delete_volume.py create mode 100644 contrib/st2/opensds/tests/test_action_list_volume.py diff --git a/contrib/st2/opensds/tests/test_action_attach_volume.py b/contrib/st2/opensds/tests/test_action_attach_volume.py new file mode 100644 index 0000000..d645207 --- /dev/null +++ b/contrib/st2/opensds/tests/test_action_attach_volume.py @@ -0,0 +1,59 @@ +# Copyright 2019 The OpenSDS Authors. +# +# 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 express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from st2tests.base import BaseActionTestCase +from attach_volume import AttachVolumeAction +import mock + + +class AttachVolumeActionTestCase(BaseActionTestCase): + action_cls = AttachVolumeAction + + def test_run(self): + with mock.patch('requests.post') as mock_post: + # Configure the mock to return a response + mock_post.return_value.status_code = 200 + mock_post.return_value.json.return_value = {} + + action = self.get_action_instance() + + response = action.run( + ip_addr="127.0.0.1", + port="5000", + tenant_id="123", + volume_id="vol_name", + host_info={ + "host": "ubuntu", + "initiator": "iqn.1993-08.org.debian:01:437bac3717c8", + "ip": "100.64.41.133" + }, + access_protocol="iscsi", + token="12345") + + self.assertEqual(response, None) + + expected_data = '{"ConnectionInfo": "", ' + \ + '"HostInfo": {"ip": "100.64.41.133", ' + \ + '"host": "ubuntu", ' + \ + '"initiator": "iqn.1993-08.org.debian:01:437bac3717c8"}, ' + \ + '"AccessProtocol": "iscsi", "VolumeId": "vol_name", ' + \ + '"TenantId": "123", "Mountpoint": ""}' + + mock_post.assert_called_once_with( + url='http://127.0.0.1:5000/v1beta/123/block/attachments', + data=expected_data, + headers={ + 'content-type': 'application/json', + 'x-auth-token': '12345'} + ) diff --git a/contrib/st2/opensds/tests/test_action_create_volume.py b/contrib/st2/opensds/tests/test_action_create_volume.py new file mode 100644 index 0000000..a81deda --- /dev/null +++ b/contrib/st2/opensds/tests/test_action_create_volume.py @@ -0,0 +1,52 @@ +# Copyright 2019 The OpenSDS Authors. +# +# 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 express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from st2tests.base import BaseActionTestCase +from create_volume import CreateVolumeAction +from nose.tools import assert_is_not_none +import mock + + +class CreateVolumeActionTestCase(BaseActionTestCase): + action_cls = CreateVolumeAction + + def test_run(self): + with mock.patch('requests.post') as mock_post: + # Configure the mock to return a response + mock_post.return_value.status_code = 200 + res = {"id": "12345"} + mock_post.return_value.json.return_value = res + + action = self.get_action_instance() + + response = action.run( + ip_addr="127.0.0.1", port="5000", + tenant_id="123", name="vol_name", size=1, token="12345") + + self.assertEqual(response, "12345") + expected_data = '{"AvailabilityZone": "default", ' + \ + '"Name": "vol_name", "SnapshotFromCloud": "", ' + \ + '"profile_id": "", "SnapshotId": "", "Size": 1, ' + \ + '"Description": "Volume"}' + + mock_post.assert_called_once_with( + url='http://127.0.0.1:5000/v1beta/123/block/volumes', + data=expected_data, + headers={ + 'content-type': 'application/json', + 'x-auth-token': '12345'} + ) + # If the request is sent successfully, + # then a response to be returned. + assert_is_not_none(response) diff --git a/contrib/st2/opensds/tests/test_action_delete_attachment.py b/contrib/st2/opensds/tests/test_action_delete_attachment.py new file mode 100644 index 0000000..f5253b8 --- /dev/null +++ b/contrib/st2/opensds/tests/test_action_delete_attachment.py @@ -0,0 +1,40 @@ +# Copyright 2019 The OpenSDS Authors. +# +# 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 express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from st2tests.base import BaseActionTestCase +from delete_attachment import DeleteAttachmentAction +import mock + + +class DeleteAttachmentActionTestCase(BaseActionTestCase): + action_cls = DeleteAttachmentAction + + def test_run(self): + with mock.patch('requests.delete') as mock_delete: + # Configure the mock to return a response + mock_delete.return_value.status_code = 200 + mock_delete.return_value.json.return_value = {} + + action = self.get_action_instance() + + response = action.run( + ip_addr="127.0.0.1", port="5000", + tenant_id="123", attachment_id="789", token="12345") + + self.assertEqual(response, None) + + mock_delete.assert_called_once_with( + url='http://127.0.0.1:5000/v1beta/123/block/attachments/789', + headers={'x-auth-token': '12345'} + ) diff --git a/contrib/st2/opensds/tests/test_action_delete_volume.py b/contrib/st2/opensds/tests/test_action_delete_volume.py new file mode 100644 index 0000000..3f808d5 --- /dev/null +++ b/contrib/st2/opensds/tests/test_action_delete_volume.py @@ -0,0 +1,40 @@ +# Copyright 2019 The OpenSDS Authors. +# +# 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 express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from st2tests.base import BaseActionTestCase +from delete_volume import DeleteVolumeAction +import mock + + +class DeleteVolumeActionTestCase(BaseActionTestCase): + action_cls = DeleteVolumeAction + + def test_run(self): + with mock.patch('requests.delete') as mock_delete: + # Configure the mock to return a response + mock_delete.return_value.status_code = 202 + mock_delete.return_value.json.return_value = {} + + action = self.get_action_instance() + + response = action.run( + ip_addr="127.0.0.1", port="5000", + tenant_id="123", volume_id="987", token="12345") + + self.assertEqual(response, None) + + mock_delete.assert_called_once_with( + url='http://127.0.0.1:5000/v1beta/123/block/volumes/987', + headers={'x-auth-token': '12345'} + ) diff --git a/contrib/st2/opensds/tests/test_action_list_volume.py b/contrib/st2/opensds/tests/test_action_list_volume.py new file mode 100644 index 0000000..af26b14 --- /dev/null +++ b/contrib/st2/opensds/tests/test_action_list_volume.py @@ -0,0 +1,41 @@ +# Copyright 2019 The OpenSDS Authors. +# +# 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 express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from st2tests.base import BaseActionTestCase +from list_volume import ListVolumeAction +import mock + + +class ListVolumeActionTestCase(BaseActionTestCase): + action_cls = ListVolumeAction + + def test_run(self): + with mock.patch('requests.get') as mock_get: + # Configure the mock to return a response + mock_get.return_value.status_code = 200 + res = {"id": "12345"} + mock_get.return_value.json.return_value = res + + action = self.get_action_instance() + + response = action.run( + ip_addr="127.0.0.1", + port="5000", tenant_id="123", token="12345") + + self.assertEqual(response, None) + + mock_get.assert_called_once_with( + url='http://127.0.0.1:5000/v1beta/123/block/volumes', + headers={'x-auth-token': '12345'} + )