<commit_before>import multiprocessing
import os
import subprocess
import time

import pytest
import requests

from clair_singularity.image import image_to_tgz, check_image, http_server
from clair_singularity.util import sha256, err_and_exit, wait_net_service


@pytest.fixture
def testimage(tmpdir):
    """Fetch a test singularity image"""
    cwd = os.getcwd()
    os.chdir(tmpdir.strpath)
    # This pulls a singularity hello world image
    subprocess.check_output(['singularity', 'pull', 'shub://396'])
    os.chdir(cwd)
    return os.path.join(tmpdir.strpath, 'vsoch-singularity-hello-world-master.img')


def test_check_image(testimage):
    # Valid image return True
    assert check_image(testimage)
    # Sys exit for invalid image
    with pytest.raises(SystemExit) as pytest_wrapped_e:
        check_image('i_do_not_exist.img')
    assert pytest_wrapped_e.type == SystemExit
    assert pytest_wrapped_e.value.code == 66


def test_image_to_tgz(testimage):
    (temp_dir, tar_file) = image_to_tgz(testimage, False)
    # Should have created a temporary dir
    assert os.path.isdir(temp_dir)
    # The tar.gz should exist
    assert os.path.isfile(tar_file)
    # With the correct sha256
    # NO - the tar create in not reproducible (dir/file order?)
    # assert sha256(tar_file) == '337436d1b561fd4d174a43474baf742c9d436d4a58a343275322517bad044d75'


def test_http_server(testimage, tmpdir):
    """Test we can retrieve the test image from in-built http server"""
    httpd = multiprocessing.Process(target=http_server,
                                    args=(os.path.dirname(testimage), '127.0.0.1', 8088, False))<commit_msg>Fix ImageException test<commit_after>import multiprocessing
import os
import subprocess
import time

import pytest
import requests

from clair_singularity.image import image_to_tgz, check_image, http_server, ImageException
from clair_singularity.util import sha256, err_and_exit, wait_net_service


@pytest.fixture
def testimage(tmpdir):
    """Fetch a test singularity image"""
    cwd = os.getcwd()
    os.chdir(tmpdir.strpath)
    # This pulls a singularity hello world image
    subprocess.check_output(['singularity', 'pull', 'shub://396'])
    os.chdir(cwd)
    return os.path.join(tmpdir.strpath, 'vsoch-singularity-hello-world-master.img')


def test_check_image(testimage):
    # Valid image return True
    assert check_image(testimage)
    # Sys exit for invalid image
    with pytest.raises(ImageException) as pytest_wrapped_e:
        check_image('i_do_not_exist.img')
    assert pytest_wrapped_e.type == ImageException


def test_image_to_tgz(testimage):
    (temp_dir, tar_file) = image_to_tgz(testimage, False)
    # Should have created a temporary dir
    assert os.path.isdir(temp_dir)
    # The tar.gz should exist
    assert os.path.isfile(tar_file)
    # With the correct sha256
    # NO - the tar create in not reproducible (dir/file order?)
    # assert sha256(tar_file) == '337436d1b561fd4d174a43474baf742c9d436d4a58a343275322517bad044d75'


def test_http_server(testimage, tmpdir):
    """Test we can retrieve the test image from in-built http server"""
    httpd = multiprocessing.Process(target=http_server,
                                    args=(os.path.dirname(testimage), '127.0.0.1', 8088, False))
{'fraction_non_alphanumeric': 0.09135349831958448, 'fraction_numerical': 0.04216315307057745, 'mean_word_length': 4.068111455108359, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1415626', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1085, 'n_words': 257}