Skip to content

Commit

Permalink
Add unit test for provision volume workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Vazhappilly committed May 17, 2019
1 parent 5fa0023 commit a320c9e
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 0 deletions.
59 changes: 59 additions & 0 deletions contrib/st2/opensds/tests/test_action_attach_volume.py
Original file line number Diff line number Diff line change
@@ -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'}
)
52 changes: 52 additions & 0 deletions contrib/st2/opensds/tests/test_action_create_volume.py
Original file line number Diff line number Diff line change
@@ -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)
40 changes: 40 additions & 0 deletions contrib/st2/opensds/tests/test_action_delete_attachment.py
Original file line number Diff line number Diff line change
@@ -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'}
)
40 changes: 40 additions & 0 deletions contrib/st2/opensds/tests/test_action_delete_volume.py
Original file line number Diff line number Diff line change
@@ -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'}
)
41 changes: 41 additions & 0 deletions contrib/st2/opensds/tests/test_action_list_volume.py
Original file line number Diff line number Diff line change
@@ -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'}
)

0 comments on commit a320c9e

Please sign in to comment.