Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for provision volume workflow #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ python:
- "2.7"
- "3.5"

env:
- ST2_REPO_PATH=/tmp/st2

before_install:
- sudo mkdir -p /var/log/opensds
- sudo touch /var/log/opensds/orchestration.log
- sudo chmod 666 /var/log/opensds/orchestration.log
- git clone https://github.com/StackStorm/st2.git /tmp/st2

install:
- pip install tox codecov

script:
- tox
- $ST2_REPO_PATH/st2common/bin/st2-run-pack-tests -p contrib/st2/opensds

Copy link
Collaborator

@wisererik wisererik May 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please andd script into tox, otherwise the codecov will not caculate the coverage of unittest

after_success:
- codecov
52 changes: 52 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,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 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",
auth_token="12345")

self.assertEqual(response, None)

mock_post.assert_called_once_with(
url='http://127.0.0.1:5000/v1beta/123/block/attachments',
data=mock.ANY, # JSON data to be handled externally
headers={
'content-type': 'application/json',
'x-auth-token': '12345'}
)
48 changes: 48 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,48 @@
# 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, auth_token="12345")

self.assertEqual(response, "12345")

mock_post.assert_called_once_with(
url='http://127.0.0.1:5000/v1beta/123/block/volumes',
data=mock.ANY, # JSON data to be handled externally
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", auth_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", auth_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", auth_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'}
)
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[pytest]
usefixtures = cache
python_files = test_*.py __init__.py
norecursedirs = st2