|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# Copyright (C) 2017-2018 Vanessa Sochat. |
| 4 | +# Copyright (C) 2018 The Board of Trustees of the Leland Stanford Junior |
| 5 | +# University. |
| 6 | + |
| 7 | +# This program is free software: you can redistribute it and/or modify it |
| 8 | +# under the terms of the GNU Affero General Public License as published by |
| 9 | +# the Free Software Foundation, either version 3 of the License, or (at your |
| 10 | +# option) any later version. |
| 11 | + |
| 12 | +# This program is distributed in the hope that it will be useful, but WITHOUT |
| 13 | +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public |
| 15 | +# License for more details. |
| 16 | + |
| 17 | +# You should have received a copy of the GNU Affero General Public License |
| 18 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +from spython.utils import get_installdir |
| 21 | +import unittest |
| 22 | +import tempfile |
| 23 | +import shutil |
| 24 | +import json |
| 25 | +import os |
| 26 | + |
| 27 | +print("############################################################ test_utils") |
| 28 | + |
| 29 | +class TestUtils(unittest.TestCase): |
| 30 | + |
| 31 | + def setUp(self): |
| 32 | + self.pwd = get_installdir() |
| 33 | + self.tmpdir = tempfile.mkdtemp() |
| 34 | + |
| 35 | + def tearDown(self): |
| 36 | + shutil.rmtree(self.tmpdir) |
| 37 | + |
| 38 | + |
| 39 | + def test_write_read_files(self): |
| 40 | + '''test_write_read_files will test the functions write_file and read_file |
| 41 | + ''' |
| 42 | + print("Testing utils.write_file...") |
| 43 | + from spython.utils import write_file |
| 44 | + import json |
| 45 | + tmpfile = tempfile.mkstemp()[1] |
| 46 | + os.remove(tmpfile) |
| 47 | + write_file(tmpfile,"hello!") |
| 48 | + self.assertTrue(os.path.exists(tmpfile)) |
| 49 | + |
| 50 | + print("Testing utils.read_file...") |
| 51 | + from spython.utils import read_file |
| 52 | + content = read_file(tmpfile)[0] |
| 53 | + self.assertEqual("hello!",content) |
| 54 | + |
| 55 | + from spython.utils import write_json |
| 56 | + print("Testing utils.write_json...") |
| 57 | + print("...Case 1: Providing bad json") |
| 58 | + bad_json = {"Wakkawakkawakka'}":[{True},"2",3]} |
| 59 | + tmpfile = tempfile.mkstemp()[1] |
| 60 | + os.remove(tmpfile) |
| 61 | + with self.assertRaises(TypeError) as cm: |
| 62 | + write_json(bad_json,tmpfile) |
| 63 | + |
| 64 | + print("...Case 2: Providing good json") |
| 65 | + good_json = {"Wakkawakkawakka":[True,"2",3]} |
| 66 | + tmpfile = tempfile.mkstemp()[1] |
| 67 | + os.remove(tmpfile) |
| 68 | + write_json(good_json,tmpfile) |
| 69 | + with open(tmpfile,'r') as filey: |
| 70 | + content = json.loads(filey.read()) |
| 71 | + self.assertTrue(isinstance(content,dict)) |
| 72 | + self.assertTrue("Wakkawakkawakka" in content) |
| 73 | + |
| 74 | + |
| 75 | + def test_check_install(self): |
| 76 | + '''check install is used to check if a particular software is installed. |
| 77 | + If no command is provided, singularity is assumed to be the test case''' |
| 78 | + print("Testing utils.check_install") |
| 79 | + from spython.utils import check_install |
| 80 | + is_installed = check_install() |
| 81 | + self.assertTrue(is_installed) |
| 82 | + is_not_installed = check_install('fakesoftwarename') |
| 83 | + self.assertTrue(not is_not_installed) |
| 84 | + |
| 85 | + |
| 86 | + def test_get_installdir(self): |
| 87 | + '''get install directory should return the base of where singularity |
| 88 | + is installed |
| 89 | + ''' |
| 90 | + print("Testing utils.get_installdir") |
| 91 | + from spython.utils import get_installdir |
| 92 | + whereami = get_installdir() |
| 93 | + print(whereami) |
| 94 | + self.assertTrue('spython' in whereami) |
| 95 | + |
| 96 | + |
| 97 | + def test_remove_uri(self): |
| 98 | + print("Testing utils.remove_uri") |
| 99 | + from spython.utils import remove_uri |
| 100 | + self.assertEqual(remove_uri('docker://ubuntu'),'ubuntu') |
| 101 | + self.assertEqual(remove_uri('shub://vanessa/singularity-images'),'vanessa/singularity-images') |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == '__main__': |
| 106 | + unittest.main() |
0 commit comments