test_common_utils.py 1.34 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

SparkSnail's avatar
SparkSnail committed
9
from mock.restful_server import init_response
10
11
12
13
14

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
15
16
17
18
19
20
21
22

class CommonUtilsTestCase(TestCase):

    @classmethod
    def setUpClass(cls):
        init_response()

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

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

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

if __name__ == '__main__':
    main()