2018-05-15 01:31:21 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) 2018 Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2018-07-06 19:49:19 +02:00
|
|
|
from ansible.compat.tests.mock import patch, MagicMock
|
2018-05-15 01:31:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestAnsibleModuleTmpDir:
|
|
|
|
|
|
|
|
DATA = (
|
|
|
|
(
|
|
|
|
{
|
|
|
|
"_ansible_tmpdir": "/path/to/dir",
|
|
|
|
"_ansible_remote_tmp": "/path/tmpdir",
|
|
|
|
"_ansible_keep_remote_files": False,
|
|
|
|
},
|
2018-05-17 01:52:46 +02:00
|
|
|
True,
|
2018-05-15 01:31:21 +02:00
|
|
|
"/path/to/dir"
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
"_ansible_tmpdir": None,
|
|
|
|
"_ansible_remote_tmp": "/path/tmpdir",
|
|
|
|
"_ansible_keep_remote_files": False
|
|
|
|
},
|
2018-05-17 01:52:46 +02:00
|
|
|
False,
|
|
|
|
"/path/tmpdir/ansible-moduletmp-42-"
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
"_ansible_tmpdir": None,
|
|
|
|
"_ansible_remote_tmp": "/path/tmpdir",
|
|
|
|
"_ansible_keep_remote_files": False
|
|
|
|
},
|
|
|
|
True,
|
2018-05-15 01:31:21 +02:00
|
|
|
"/path/tmpdir/ansible-moduletmp-42-"
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
"_ansible_tmpdir": None,
|
|
|
|
"_ansible_remote_tmp": "$HOME/.test",
|
|
|
|
"_ansible_keep_remote_files": False
|
|
|
|
},
|
2018-05-17 01:52:46 +02:00
|
|
|
False,
|
2018-05-15 01:31:21 +02:00
|
|
|
os.path.join(os.environ['HOME'], ".test/ansible-moduletmp-42-")
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# pylint bug: https://github.com/PyCQA/pylint/issues/511
|
|
|
|
# pylint: disable=undefined-variable
|
2018-05-17 01:52:46 +02:00
|
|
|
@pytest.mark.parametrize('stdin, expected, stat_exists', ((s, e, t) for s, t, e in DATA),
|
2018-05-15 01:31:21 +02:00
|
|
|
indirect=['stdin'])
|
2018-05-17 01:52:46 +02:00
|
|
|
def test_tmpdir_property(self, am, monkeypatch, expected, stat_exists):
|
|
|
|
makedirs = {'called': False}
|
|
|
|
|
2018-05-15 01:31:21 +02:00
|
|
|
def mock_mkdtemp(prefix, dir):
|
|
|
|
return os.path.join(dir, prefix)
|
2018-05-17 01:52:46 +02:00
|
|
|
|
|
|
|
def mock_makedirs(path, mode):
|
|
|
|
makedirs['called'] = True
|
|
|
|
expected = os.path.expanduser(os.path.expandvars(am._remote_tmp))
|
|
|
|
assert path == expected
|
|
|
|
assert mode == 0o700
|
|
|
|
return
|
|
|
|
|
2018-05-15 01:31:21 +02:00
|
|
|
monkeypatch.setattr(tempfile, 'mkdtemp', mock_mkdtemp)
|
2018-05-17 01:52:46 +02:00
|
|
|
monkeypatch.setattr(os.path, 'exists', lambda x: stat_exists)
|
|
|
|
monkeypatch.setattr(os, 'makedirs', mock_makedirs)
|
2018-05-15 01:31:21 +02:00
|
|
|
monkeypatch.setattr(shutil, 'rmtree', lambda x: None)
|
|
|
|
|
|
|
|
with patch('time.time', return_value=42):
|
|
|
|
actual_tmpdir = am.tmpdir
|
|
|
|
assert actual_tmpdir == expected
|
|
|
|
|
|
|
|
# verify subsequent calls always produces the same tmpdir
|
|
|
|
assert am.tmpdir == actual_tmpdir
|
2018-05-17 01:52:46 +02:00
|
|
|
|
|
|
|
if not stat_exists:
|
|
|
|
assert makedirs['called']
|
2018-07-06 19:49:19 +02:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('stdin', ({"_ansible_tmpdir": None,
|
|
|
|
"_ansible_remote_tmp": "$HOME/.test",
|
|
|
|
"_ansible_keep_remote_files": True},),
|
|
|
|
indirect=['stdin'])
|
|
|
|
def test_tmpdir_makedirs_failure(self, am, monkeypatch):
|
|
|
|
|
|
|
|
mock_mkdtemp = MagicMock(return_value="/tmp/path")
|
|
|
|
mock_makedirs = MagicMock(side_effect=OSError("Some OS Error here"))
|
|
|
|
|
|
|
|
monkeypatch.setattr(tempfile, 'mkdtemp', mock_mkdtemp)
|
|
|
|
monkeypatch.setattr(os.path, 'exists', lambda x: False)
|
|
|
|
monkeypatch.setattr(os, 'makedirs', mock_makedirs)
|
|
|
|
|
|
|
|
actual = am.tmpdir
|
|
|
|
assert actual == "/tmp/path"
|
|
|
|
assert mock_makedirs.call_args[0] == (os.path.expanduser(os.path.expandvars("$HOME/.test")),)
|
|
|
|
assert mock_makedirs.call_args[1] == {"mode": 0o700}
|
|
|
|
|
|
|
|
# because makedirs failed the dir should be None so it uses the System tmp
|
|
|
|
assert mock_mkdtemp.call_args[1]['dir'] is None
|
|
|
|
assert mock_mkdtemp.call_args[1]['prefix'].startswith("ansible-moduletmp-")
|