"Fossies" - the Fresh Open Source Software Archive

Member "freezer-10.0.0/freezer/tests/unit/scheduler/test_utils.py" (14 Apr 2021, 4549 Bytes) of package /linux/misc/openstack/freezer-10.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_utils.py": 9.0.0_vs_10.0.0.

    1 # (c) Copyright 2019 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 os
   16 import shutil
   17 import tempfile
   18 import unittest
   19 from unittest import mock
   20 from unittest.mock import patch
   21 
   22 from freezer.scheduler import utils
   23 
   24 job_list = [{"job_id": "test"}]
   25 
   26 
   27 class TestUtils(unittest.TestCase):
   28     def setUp(self):
   29         self.client = mock.Mock()
   30         self.client.clients.create = mock.Mock(return_value='test')
   31         self.client.jobs.list = mock.Mock(return_value=job_list)
   32         self.client.client_id = "test"
   33 
   34     def test_do_register(self):
   35         ret = utils.do_register(self.client, args=None)
   36         self.assertEqual(0, ret)
   37 
   38     def test_del_register_error(self):
   39         self.client.clients.delete = mock.Mock(side_effect=Exception(
   40             'delete client error: bad request'))
   41         with self.assertRaises(Exception) as cm:  # noqa
   42             utils.del_register(self.client)
   43             the_exception = cm.exception
   44             self.assertIn('delete client error', str(the_exception))
   45 
   46     def test_find_config_files(self):
   47         temp = tempfile.NamedTemporaryFile('wb', delete=True,
   48                                            suffix='.conf')
   49         ret = utils.find_config_files(temp.name)
   50         self.assertEqual([temp.name], ret)
   51         temp.close()
   52         self.assertFalse(os.path.exists(temp.name))
   53 
   54     def test_find_config_files_path(self):
   55         temp = tempfile.NamedTemporaryFile('wb', delete=True,
   56                                            suffix='.conf')
   57         temp_path = os.path.dirname(temp.name)
   58         ret = utils.find_config_files(temp_path)
   59         self.assertEqual([temp.name], ret)
   60         temp.close()
   61         self.assertFalse(os.path.exists(temp.name))
   62 
   63     @patch('oslo_serialization.jsonutils.load')
   64     def test_load_doc_from_json_file(self, mock_load):
   65         os.mknod("/tmp/test_freezer.conf")
   66         mock_load.side_effect = Exception('error')
   67         try:
   68             utils.load_doc_from_json_file("/tmp/test_freezer.conf")
   69         except Exception as e:
   70             self.assertIn("Unable to load conf file", str(e))
   71         os.remove("/tmp/test_freezer.conf")
   72 
   73     def test_get_jobs_from_disk(self):
   74         temp = tempfile.mkdtemp()
   75         file = '/'.join([temp, "test.conf"])
   76         data = b'{"job_id": "test"}'
   77         with open(file, 'wb') as f:
   78             f.write(data)
   79         ret = utils.get_jobs_from_disk(temp)
   80         self.assertEqual(job_list, ret)
   81         shutil.rmtree(temp)
   82         self.assertFalse(os.path.exists(file))
   83 
   84     def test_save_jobs_to_disk(self):
   85         job_doc_list = job_list
   86         tmpdir = tempfile.mkdtemp()
   87         utils.save_jobs_to_disk(job_doc_list, tmpdir)
   88         file = '/'.join([tmpdir, "job_test.conf"])
   89         self.assertTrue(os.path.exists(file))
   90         shutil.rmtree(tmpdir)
   91 
   92     def test_get_active_jobs_from_api(self):
   93         ret = utils.get_active_jobs_from_api(self.client)
   94         self.assertEqual(job_list, ret)
   95 
   96     @patch('os.kill')
   97     @patch('psutil.Process')
   98     def test_terminate_subprocess1(self, mock_process, mock_oskill):
   99         mock_pro = mock.MagicMock()
  100         mock_pro.name.startswith.return_value = False
  101         mock_process.return_value = mock_pro
  102         result = utils.terminate_subprocess(35, 'test')
  103         self.assertIsNone(result)
  104         mock_pro.name.startswith.return_value = True
  105         mock_oskill.side_effect = Exception("error")
  106         result = utils.terminate_subprocess(35, 'test')
  107         self.assertIsNone(result)
  108 
  109     @patch('psutil.Process')
  110     def test_terminate_subprocess(self, mock_process_constructor):
  111         mock_pro = mock_process_constructor.return_value
  112         seffect = mock.Mock(
  113             side_effect=Exception('Process 35 does not exists anymore'))
  114         mock_pro.raiseError.side_effect = seffect
  115         with self.assertRaises(Exception) as cm:  # noqa
  116             utils.terminate_subprocess(35, "test")
  117             the_exception = cm.exception
  118             self.assertIn('does not exists anymore',
  119                           str(the_exception))