summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzhifeng.jiang <jiang.zhifeng@zte.com.cn>2016-07-08 11:03:22 +0800
committerzhifeng.jiang <jiang.zhifeng@zte.com.cn>2016-07-08 11:03:22 +0800
commit3a4f1304b228c559536f7c0d7fa052d7e61b6a56 (patch)
treee30d2887a467fcc33ca41d743b8dae2c91f7cbfa
parent5dbf3c4e2f6ef48676de10b4394de4a45456810d (diff)
Add some UT test cases and fix pep8 errors for fetchimg
JIRA:QTIP-89 Change-Id: I623cd6e48d90996b0199f9b31f86c8844b4d5ceb Signed-off-by: zhifeng.jiang <jiang.zhifeng@zte.com.cn>
-rw-r--r--func/fetchimg.py58
-rw-r--r--test-requirements.txt1
-rw-r--r--tests/fetchimg_test.py22
3 files changed, 51 insertions, 30 deletions
diff --git a/func/fetchimg.py b/func/fetchimg.py
index 1c621a3b..6cae426e 100644
--- a/func/fetchimg.py
+++ b/func/fetchimg.py
@@ -1,30 +1,28 @@
-##############################################################################
-# Copyright (c) 2015 Dell Inc and others.
-#
-# All rights reserved. This program and the accompanying materials
-# are made available under the terms of the Apache License, Version 2.0
-# which accompanies this distribution, and is available at
-# http://www.apache.org/licenses/LICENSE-2.0
-##############################################################################
-
-
-
-import os
-import time
-
-
-class FetchImg:
-
- def __init__(self):
- print 'Fetching Image!'
- print 'Fetching QTIP_VM Image'
-
- def download(self):
- time.sleep(2)
- os.system(
- 'mkdir -p Temp_Img && wget http://artifacts.opnfv.org/qtip/QTIP_CentOS.qcow2 -P Temp_Img')
-
- filepath = './Temp_Img/QTIP_CentOS.qcow2'
- while not os.path.isfile(filepath):
- time.sleep(10)
- print 'Download Completed!'
+##############################################################################
+# Copyright (c) 2015 Dell Inc and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+import os
+import time
+
+
+class FetchImg:
+
+ def __init__(self):
+ print 'Fetching Image!'
+ print 'Fetching QTIP_VM Image'
+
+ @staticmethod
+ def download():
+ time.sleep(2)
+ os.system(
+ 'mkdir -p Temp_Img && wget http://artifacts.opnfv.org/qtip/QTIP_CentOS.qcow2 -P Temp_Img')
+
+ filepath = './Temp_Img/QTIP_CentOS.qcow2'
+ while not os.path.isfile(filepath):
+ time.sleep(10)
+ print 'Download Completed!'
diff --git a/test-requirements.txt b/test-requirements.txt
index cdac2380..31581503 100644
--- a/test-requirements.txt
+++ b/test-requirements.txt
@@ -4,3 +4,4 @@
pytest
pykwalify
+mock
diff --git a/tests/fetchimg_test.py b/tests/fetchimg_test.py
new file mode 100644
index 00000000..683c9701
--- /dev/null
+++ b/tests/fetchimg_test.py
@@ -0,0 +1,22 @@
+import mock
+from func.fetchimg import FetchImg
+
+
+class TestClass:
+ @mock.patch('func.fetchimg.os')
+ @mock.patch('func.fetchimg.os.path')
+ def test_fetch_img_success(self, mock_path, mock_os):
+ mock_os.system.return_value = True
+ mock_path.isfile.return_value = True
+ img = FetchImg()
+ img.download()
+
+ @mock.patch('func.fetchimg.time')
+ @mock.patch('func.fetchimg.os.system')
+ @mock.patch('func.fetchimg.os.path')
+ def test_fetch_img_fail(self, mock_path, mock_system, mock_time):
+ img = FetchImg()
+ mock_system.return_value = True
+ mock_path.isfile.side_effect = [False, True]
+ img.download()
+ assert mock_time.sleep.call_count == 2