test_common_utils.py 1.38 KB
Newer Older
SparkSnail's avatar
SparkSnail committed
1
2
3
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

4
5
from pathlib import Path
from subprocess import Popen, PIPE, STDOUT
liuzhe-lz's avatar
liuzhe-lz committed
6
7
import sys
from unittest import TestCase, main, skipIf
8

9
sys.path.append(str(Path(__file__).parent))
SparkSnail's avatar
SparkSnail committed
10
from mock.restful_server import init_response
11
12
13
14
15

from nni.tools.nnictl.command_utils import kill_command
from nni.tools.nnictl.common_utils import get_yml_content, get_json_content, detect_process

cwd = Path(__file__).parent
SparkSnail's avatar
SparkSnail committed
16
17
18
19
20
21
22
23

class CommonUtilsTestCase(TestCase):

    @classmethod
    def setUpClass(cls):
        init_response()

    def test_get_yml(self):
24
25
        yml_path = cwd / 'config_files/test_files/test_yaml.yml'
        content = get_yml_content(str(yml_path))
SparkSnail's avatar
SparkSnail committed
26
27
28
        self.assertEqual(content, {'field':'test'})

    def test_get_json(self):
29
30
        json_path = cwd / 'config_files/test_files/test_json.json'
        content = get_json_content(str(json_path))
SparkSnail's avatar
SparkSnail committed
31
32
        self.assertEqual(content, {'field':'test'})

liuzhe-lz's avatar
liuzhe-lz committed
33
    @skipIf(sys.platform == 'win32', 'FIXME: Fails randomly on Windows, cannot reproduce locally')
SparkSnail's avatar
SparkSnail committed
34
    def test_detect_process(self):
liuzhe-lz's avatar
liuzhe-lz committed
35
36
37
38
        if sys.platform == 'win32':
            cmds = ['timeout', '360000']
        else:
            cmds = ['sleep', '360000']
SparkSnail's avatar
SparkSnail committed
39
40
41
42
43
44
        process = Popen(cmds, stdout=PIPE, stderr=STDOUT)
        self.assertTrue(detect_process(process.pid))
        kill_command(process.pid)

if __name__ == '__main__':
    main()