Add unit test for jobutil
[vfc/nfvo/lcm.git] / lcm / pub / utils / tests.py
1 # Copyright 2018 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import unittest
16 import mock
17 import enumutil
18 import fileutil
19 import urllib2
20
21 from lcm.pub.database.models import JobStatusModel, JobModel
22 from lcm.pub.utils.jobutil import JobUtil
23
24
25 class MockReq():
26     def read(self):
27         return "1"
28
29     def close(self):
30         pass
31
32
33 class UtilsTest(unittest.TestCase):
34     def setUp(self):
35         pass
36
37     def tearDown(self):
38         pass
39
40     def test_enum(self):
41         MY_TYPE = enumutil.enum(SAMLL=0, LARGE=1)
42         self.assertEqual(0, MY_TYPE.SAMLL)
43         self.assertEqual(1, MY_TYPE.LARGE)
44
45     def test_create_and_delete_dir(self):
46         dirs = "abc/def/hij"
47         fileutil.make_dirs(dirs)
48         fileutil.make_dirs(dirs)
49         fileutil.delete_dirs(dirs)
50
51     @mock.patch.object(urllib2, 'urlopen')
52     def test_download_file_from_http(self, mock_urlopen):
53         mock_urlopen.return_value = MockReq()
54         fileutil.delete_dirs("abc")
55         is_ok, f_name = fileutil.download_file_from_http("1", "abc", "1.txt")
56         self.assertTrue(is_ok)
57         self.assertTrue(f_name.endswith("abc/1.txt"))
58         fileutil.delete_dirs("abc")
59
60     def test_query_job_status(self):
61         job_id = "1"
62         JobStatusModel.objects.filter().delete()
63         JobStatusModel(
64             indexid=1,
65             jobid=job_id,
66             status="success",
67             progress=10
68         ).save()
69         JobStatusModel(
70             indexid=2,
71             jobid=job_id,
72             status="success",
73             progress=50
74         ).save()
75         JobStatusModel(
76             indexid=3,
77             jobid=job_id,
78             status="success",
79             progress=100
80         ).save()
81         jobs = JobUtil.query_job_status(job_id)
82         self.assertEqual(1, len(jobs))
83         self.assertEqual(3, jobs[0].indexid)
84         jobs = JobUtil.query_job_status(job_id, 1)
85         self.assertEqual(2, len(jobs))
86         self.assertEqual(3, jobs[0].indexid)
87         self.assertEqual(2, jobs[1].indexid)
88         JobStatusModel.objects.filter().delete()
89
90     def test_is_job_exists(self):
91         job_id = "1"
92         JobModel.objects.filter().delete()
93         JobModel(
94             jobid=job_id,
95             jobtype="1",
96             jobaction="2",
97             resid="3",
98             status=0
99         ).save()
100         self.assertTrue(JobUtil.is_job_exists(job_id))
101         JobModel.objects.filter().delete()