<commit_before>import cStringIO
import mock
import os
import pytest
import re
import shutil
import stat
import tempfile
import time
from collections import OrderedDict

from Crypto.PublicKey import RSA
from Crypto import Random

from awx.main.isolated import run, isolated_manager

from django.conf import settings

HERE, FILENAME = os.path.split(__file__)


@pytest.fixture(scope='function')
def rsa_key(request):
    passphrase = '<PASSWORD>'
    key = RSA.generate(1024, Random.new().read)
    return (key.exportKey('PEM', passphrase, pkcs=1), passphrase)


@pytest.fixture(scope='function')
def private_data_dir(request):
    path = tempfile.mkdtemp(prefix='ansible_tower_unit_test')
    request.addfinalizer(lambda: shutil.rmtree(path))
    return path


@pytest.fixture(autouse=True)
def mock_sleep(request):
    # the process teardown mechanism uses `time.sleep` to wait on processes to
    # respond to SIGTERM; these are tests and don't care about being nice
    m = mock.patch('time.sleep')
    m.start()
    request.addfinalizer(m.stop)


def test_simple_spawn():
    stdout = cStringIO.StringIO()
    status, rc = run.run_pexpect(
        ['ls', '-la'],
        HERE,
        {},
        stdout,
        cancelled_callback=lambda: False,
    )
    assert status == 'successful'
    assert rc == 0
    assert FILENAME in stdout.getvalue()


def test_error_rc():
    stdout = cStringIO.StringIO()
    status, rc = run.run_pexpect(
        ['ls', '-nonsense'],
        HERE,
        {},
        stdout,
        cancelled_callback=lambda: False,
    )
    assert status == 'failed'
    # I'd expect 2, but we shouldn't risk making this test platform-dependent
    assert rc > 0


def test_env_vars():
    stdout = cStringIO.StringIO()
    status, rc = run.run_pexpect(
        ['python', '-c', 'import os; print os.getenv("X_MY_ENV")'],
        HERE,
        {'X_MY_ENV': 'abc123'},
        stdout,
        cancelled_callback=lambda: False,
    )
    assert status == 'successful'
    assert rc == 0
    assert 'abc123' in stdout.getvalue()


def test_password_prompt():
    stdout = cStringIO.StringIO()
    expect_passwords = OrderedDict()
    expect_passwords[re.compile(r'Password:\s*?$', re.M)] = '<PASSWORD>'
    status, rc = run.run_pexpect(
        ['python', '-c', 'print raw_input("Password: ")'],
        HERE,
        {},
        stdout,
        cancelled_callback=lambda: False,
        expect_passwords=expect_passwords
    )
    assert status == 'successful'<commit_msg>fix a unit test that periodically fails<commit_after>import cStringIO
import mock
import os
import pytest
import re
import shutil
import stat
import tempfile
import time
from collections import OrderedDict

from Crypto.PublicKey import RSA
from Crypto import Random

from awx.main.isolated import run, isolated_manager

from django.conf import settings

HERE, FILENAME = os.path.split(__file__)


@pytest.fixture(scope='function')
def rsa_key(request):
    passphrase = '<PASSWORD>'
    key = RSA.generate(1024, Random.new().read)
    return (key.exportKey('PEM', passphrase, pkcs=1), passphrase)


@pytest.fixture(scope='function')
def private_data_dir(request):
    path = tempfile.mkdtemp(prefix='ansible_tower_unit_test')
    request.addfinalizer(lambda: shutil.rmtree(path))
    return path


@pytest.fixture(autouse=True)
def mock_sleep(request):
    # the process teardown mechanism uses `time.sleep` to wait on processes to
    # respond to SIGTERM; these are tests and don't care about being nice
    m = mock.patch('time.sleep')
    m.start()
    request.addfinalizer(m.stop)


def test_simple_spawn():
    stdout = cStringIO.StringIO()
    status, rc = run.run_pexpect(
        ['ls', '-la'],
        HERE,
        {},
        stdout,
        cancelled_callback=lambda: False,
    )
    assert status == 'successful'
    assert rc == 0
    assert FILENAME in stdout.getvalue()


def test_error_rc():
    stdout = cStringIO.StringIO()
    status, rc = run.run_pexpect(
        ['ls', '-nonsense'],
        HERE,
        {},
        stdout,
        cancelled_callback=lambda: False,
    )
    assert status == 'failed'
    # I'd expect 2, but we shouldn't risk making this test platform-dependent
    assert rc > 0


def test_env_vars():
    stdout = cStringIO.StringIO()
    status, rc = run.run_pexpect(
        ['python', '-c', 'import os; print os.getenv("X_MY_ENV")'],
        HERE,
        {'X_MY_ENV': 'abc123'},
        stdout,
        cancelled_callback=lambda: False,
    )
    assert status == 'successful'
    assert rc == 0
    assert 'abc123' in stdout.getvalue()


def test_password_prompt():
    stdout = cStringIO.StringIO()
    expect_passwords = OrderedDict()
    expect_passwords[re.compile(r'Password:\s*?$', re.M)] = '<PASSWORD>'
    status, rc = run.run_pexpect(
        ['python', '-c', 'import time; print raw_input("Password: "); time.sleep(.05)'],
        HERE,
        {},
        stdout,
        cancelled_callback=lambda: False,
        expect_passwords=expect_passwords
    )
    assert status == 'successful'
{'fraction_non_alphanumeric': 0.1190003966679889, 'fraction_numerical': 0.006346687822292741, 'mean_word_length': 3.167768595041322, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5990319', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1607, 'n_words': 408}