예제 #1
0
def easy_server_file(server_filename,
                     server_yaml,
                     vault_filename=None,
                     vault_yaml=None,
                     vault_password=None):
    """
    Context manager that creates an easy-server server file and optionally
    a corresponding vault file from their YAML content, which is optionally
    encrypted with a password.
    """
    with testfixtures.TempDirectory() as tmp_dir:

        # Create the server file
        server_filepath = os.path.join(tmp_dir.path, server_filename)
        if isinstance(server_yaml, six.text_type):
            server_yaml = server_yaml.encode('utf-8')
        tmp_dir.write(server_filename, server_yaml)

        # Create the vault file, if specified
        if vault_yaml:
            vault_filepath = os.path.join(tmp_dir.path, vault_filename)
            if isinstance(vault_yaml, six.text_type):
                vault_yaml = vault_yaml.encode('utf-8')
            tmp_dir.write(vault_filename, vault_yaml)

            # Encrypt the vault file, if specified
            if vault_password:
                vault = easy_vault.EasyVault(vault_filepath, vault_password)
                vault.encrypt()
        else:
            vault_filepath = None

        yield server_filepath
예제 #2
0
def tempdir():
    old_cwd = os.getcwd()
    with testfixtures.TempDirectory(create=True) as directory:
        os.chdir(directory.path)
        yield
        os.chdir(old_cwd)
        directory.cleanup()
예제 #3
0
def test_create_no_volume_root():
    with pytest.raises(SystemExit):
        with testfixtures.LogCapture() as log_capture:
            with testfixtures.TempDirectory() as d:
                d.write('config', 'volume_root:')
                config.load(path=os.path.join(d.path, 'config'))
                cli.main(args=['create'])
    log_capture.check(('gpucrate', 'ERROR', exception.NoVolumeRoot.message), )
예제 #4
0
def before_scenario(context, scenario):
    """Prepare a fresh environment for each scenario."""

    # Prepare a new temporary directory.
    context.directory = testfixtures.TempDirectory(create=True)
    context.old_cwd = os.getcwd()
    context.new_cwd = context.directory.path

    # Move into our new working directory.
    os.chdir(context.new_cwd)
예제 #5
0
    def _test_helper(self, *args, **kwargs):
        with testfixtures.TempDirectory() as d:
            for image, label in zip(self.test_images, self.test_labels):
                d.write(os.path.join(label, image), b'any')

            f = tempfile.NamedTemporaryFile(mode='r+t', suffix='.csv')
            dataset.gen_csv_from_images(d.path, f.name, *args, **kwargs)
            f.seek(0)
            reader = csv.reader(f)
            return reader
예제 #6
0
def before_scenario(context, scenario):
    """Ensure tests run with a clean environment."""

    # Make sure tests can resolve test data.
    context.test_root = os.path.dirname(os.path.abspath(__file__))

    # Move into a new working folder for each scenario.
    context.old_cwd = os.getcwd()
    context.tempdir = testfixtures.TempDirectory(create=True)
    os.chdir(context.tempdir.path)
예제 #7
0
def test_create_volume_root_dne():
    with testfixtures.LogCapture() as log_capture:
        with testfixtures.TempDirectory() as d:
            vroot = os.path.join(d.path, 'vroot')
            d.write('config', 'volume_root: {vroot}'.format(vroot=vroot))
            config.load(path=os.path.join(d.path, 'config'))
            cli.main(args=['create'])
            assert os.path.isdir(vroot)
    log_capture.check(
        ('gpucrate', 'INFO',
         'Volume root {vroot} does not exist - creating'.format(vroot=vroot)))
예제 #8
0
    def _test_helper(self, *args, **kwargs):
        with testfixtures.TempDirectory() as d:
            for f in self.files:
                d.write(f, b'any')

            f = tempfile.NamedTemporaryFile(mode='r+t', suffix='.csv')
            mock_ret_val = [self.bounding_box] * self.NUM_BOUNDING_BOXES
            with mock.patch.object(annotation,
                                   'read',
                                   return_value=mock_ret_val):
                dataset.gen_csv_from_annotations(d.path, f.name, *args,
                                                 **kwargs)
                f.seek(0)
                reader = csv.reader(f)
            return reader
예제 #9
0
def test_singularity_gpu_volume_dne():
    with pytest.raises(SystemExit):
        with testfixtures.LogCapture() as log_capture:
            with testfixtures.TempDirectory() as d:
                vroot = os.path.join(d.path, 'vroot')
                vol = os.path.join(vroot, TEST_DRIVER_VERSION)
                d.write('config', 'volume_root: {vroot}'.format(vroot=vroot))
                config.load(path=os.path.join(d.path, 'config'))
                SINGULARITY.reset_mock()
                cli.singularity_gpu(
                    args='exec -B /some/path:/some/path c.img'.split())

    log_capture.check(
        ('gpucrate', 'ERROR',
         'volume {vol} does not exist - run "gpucrate create"'.format(
             vol=vol)))
예제 #10
0
def repository():
    with testfixtures.TempDirectory(create=True) as directory:

        # Move into the directory.
        with cwd(directory.path):

            # Prepare a new git repository and add some contents.
            check_output(['git', 'init'])
            check_output(['git', 'config', 'user.name', '"py.test"'])
            check_output([
                'git', 'config', 'user.email', '"*****@*****.**"',
            ])
            with open('README.txt', 'w') as stream:
                stream.write('Hello, world!')
            check_output(['git', 'add', 'README.txt'])
            check_output(['git', 'commit', '-m', '"Starts project."'])

            # Let the test run.
            yield

        # Remove anything created in this folder.
        directory.cleanup()
예제 #11
0
def test_with_config_file():
    with testfixtures.TempDirectory() as d:
        vroot = '/path/to/some/gpucrate/root'
        d.write('config', 'volume_root: {vroot}'.format(vroot=vroot))
        config.load(path=os.path.join(d.path, 'config'))
        assert config.get('volume_root') == vroot
예제 #12
0
 def setUp(self):
     self.tmpdir = testfixtures.TempDirectory(path=unmerged_location)
예제 #13
0
def tempdir():
    """py.test fixture to create and cleanup a temporary directory."""
    directory = testfixtures.TempDirectory(create=True)
    yield directory
    directory.cleanup()
예제 #14
0
def storage():
    with testfixtures.TempDirectory(create=True) as directory:
        yield directory.path
        directory.cleanup()