summaryrefslogtreecommitdiffstats
path: root/tests/unit/test_utils.py
blob: 4cfe741d35fe3205d41286d170ce1093fe1cbcfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
##############################################################################
# Copyright (c) 2017 ZTE Corp and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
import os
import pytest
import mock

from deploy import utils
from deploy.utils import (
    err_exit,
    check_sudo_privilege,
    check_file_exists,
    make_file_executable,
    confirm_dir_exists,
    check_scenario_valid
)


def test_err_exit():
    message = 'test error msg!'
    with pytest.raises(SystemExit):
        err_exit(message)


@mock.patch('deploy.utils.err_exit')
@mock.patch('deploy.utils.os.getuid')
def test_check_sudo_privilege(mock_getuid, mock_err_exit):
    mock_getuid.return_value = 1
    check_sudo_privilege()
    utils.err_exit.assert_called_once_with('You need run this script with sudo privilege')


@pytest.mark.parametrize('test_file_name', [
    ('no_exist_file'),
    ('exist_file')])
def test_check_file_exists(tmpdir, test_file_name):
    try:
        file_path = os.path.join(tmpdir.dirname, tmpdir.basename, test_file_name)
        if test_file_name == 'exist_file':
            os.mknod(file_path)
        check_file_exists(file_path)
    except SystemExit:
        if test_file_name == 'exist_file':
            assert 0
    else:
        if test_file_name == 'no_exist_file':
            assert 0
    finally:
        tmpdir.remove()


@pytest.mark.parametrize('test_file_name', [
    ('no_exist_file'),
    ('no_exe_file'),
    ('exe_file')])
def test_make_file_executable(tmpdir, test_file_name):
    try:
        file_path = os.path.join(tmpdir.dirname, tmpdir.basename, test_file_name)
        if test_file_name == 'no_exe_file':
            os.mknod(file_path)
        if test_file_name == 'exe_file':
            os.mknod(file_path, 0700)
        make_file_executable(file_path)
    except SystemExit:
        if test_file_name == 'no_exe_file' or test_file_name == 'exe_file':
            assert 0
    else:
        if test_file_name == 'no_exist_file':
            assert 0
    finally:
        if test_file_name == 'no_exe_file' or test_file_name == 'exe_file':
            assert os.access(file_path, os.X_OK)
        tmpdir.remove()


@pytest.mark.parametrize('test_dir_name', [
    ('no_exist_dir'),
    ('exist_dir')])
def test_confirm_dir_exists(tmpdir, test_dir_name):
    if test_dir_name == 'no_exist_dir':
        dir_path = os.path.join(tmpdir.dirname, tmpdir.basename, 'no_exist_dir')
    if test_dir_name == 'exist_dir':
        tmpsubdir = tmpdir.mkdir('exist_dir')
        dir_path = os.path.join(tmpsubdir.dirname, tmpsubdir.basename)
    confirm_dir_exists(dir_path)
    assert os.path.isdir(dir_path)
    tmpdir.remove()


@pytest.mark.parametrize('scenario', [
    ('os-nosdn-nofeature-ha')])
@mock.patch("deploy.utils.err_exit")
def test_check_scenario_supported(mock_err_exit, scenario):
    check_scenario_valid(scenario)
    mock_err_exit.assert_not_called()


@pytest.mark.parametrize('scenario', [
    ('os-odl-kvm-ha')])
@mock.patch("deploy.utils.err_exit")
def test_check_scenario_unsupported(mock_err_exit, scenario):
    check_scenario_valid(scenario)
    mock_err_exit.assert_called_once()