Add some UT test cases and fix pep8 errors for fetchimg 77/16577/1
authorzhifeng.jiang <jiang.zhifeng@zte.com.cn>
Fri, 8 Jul 2016 03:03:22 +0000 (11:03 +0800)
committerzhifeng.jiang <jiang.zhifeng@zte.com.cn>
Fri, 8 Jul 2016 03:03:22 +0000 (11:03 +0800)
JIRA:QTIP-89

Change-Id: I623cd6e48d90996b0199f9b31f86c8844b4d5ceb
Signed-off-by: zhifeng.jiang <jiang.zhifeng@zte.com.cn>
func/fetchimg.py
test-requirements.txt
tests/fetchimg_test.py [new file with mode: 0644]

index 1c621a3..6cae426 100644 (file)
@@ -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!'
+##############################################################################\r
+# Copyright (c) 2015 Dell Inc  and others.\r
+#\r
+# All rights reserved. This program and the accompanying materials\r
+# are made available under the terms of the Apache License, Version 2.0\r
+# which accompanies this distribution, and is available at\r
+# http://www.apache.org/licenses/LICENSE-2.0\r
+##############################################################################\r
+import os\r
+import time\r
+\r
+\r
+class FetchImg:\r
+\r
+    def __init__(self):\r
+        print 'Fetching Image!'\r
+        print 'Fetching QTIP_VM Image'\r
+\r
+    @staticmethod\r
+    def download():\r
+        time.sleep(2)\r
+        os.system(\r
+            'mkdir -p Temp_Img && wget http://artifacts.opnfv.org/qtip/QTIP_CentOS.qcow2 -P Temp_Img')\r
+\r
+        filepath = './Temp_Img/QTIP_CentOS.qcow2'\r
+        while not os.path.isfile(filepath):\r
+            time.sleep(10)\r
+        print 'Download Completed!'\r
index cdac238..3158150 100644 (file)
@@ -4,3 +4,4 @@
 
 pytest
 pykwalify
+mock
diff --git a/tests/fetchimg_test.py b/tests/fetchimg_test.py
new file mode 100644 (file)
index 0000000..683c970
--- /dev/null
@@ -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