Example #1
0
def test_target_parse_custom_target_null_target_value():
    # target value must be given
    target = Target('some_bucket')
    with pytest.raises(Exception) as ex:
        target.parse_custom_target(target_key='file:///data1/out/somefile',
                                   target_value=None)
    assert 'target' in str(ex.value)
Example #2
0
def test_target_parse_target_value_unzip_wo_prefix():
    target = Target('some_bucket')
    with pytest.raises(Exception) as ex:
        target.parse_target_value({
            'unzip': True,
            'object_key': 'some_object_key'
        })
    assert 'prefix' in str(ex.value)
Example #3
0
def test_target_parse_custom_target_dict_object_key():
    target = Target('some_bucket')
    target.parse_custom_target(target_key='file:///data1/out/somefile',
                               target_value={'object_key': 'some_object_key'})
    assert target.dest == 'some_object_key'
    assert target.bucket == 'some_bucket'
    assert target.unzip is False
    assert target.source == '/data1/out/somefile'
Example #4
0
def test_target_parse_target_value_object_key_prefix_conflict():
    target = Target('some_bucket')
    with pytest.raises(Exception) as ex:
        target.parse_target_value({
            'object_prefix': 'some_dir/',
            'object_key': 'some_object_key'
        })
    assert 'not both' in str(ex.value)
Example #5
0
def test_target_parse_custom_target_str_url():
    target = Target('some_bucket')
    target.parse_custom_target(
        target_key='file:///data1/out/somefile',
        target_value='s3://another_bucket/some_object_key')
    assert target.dest == 'some_object_key'
    assert target.bucket == 'another_bucket'
    assert target.unzip is False
    assert target.source == '/data1/out/somefile'
Example #6
0
def test_target_parse_target_value_dict_object_key_bucket():
    target = Target('some_bucket')
    target.parse_target_value({
        'object_key': 'some_object_key',
        'bucket_name': 'another_bucket'
    })
    assert target.dest == 'some_object_key'
    assert target.bucket == 'another_bucket'
    assert target.unzip is False
Example #7
0
def test_target_parse_cwl_target_dict_object_key():
    target = Target('some_bucket')
    outfile = AwsemPostRunJsonOutputFile(**{'path': '/data1/out/1/somefile'})
    target.parse_cwl_target(target_key='some_argname',
                            target_value={'object_key': 'some_object_key'},
                            prj_output_files={'some_argname': outfile})
    assert target.dest == 'some_object_key'
    assert target.bucket == 'some_bucket'
    assert target.unzip is False
    assert target.source == '/data1/out/1/somefile'
Example #8
0
def test_target_as_dict():
    target = Target('some_bucket')
    target.dest = 'some_dest'
    target.source = 'some_source'
    assert target.as_dict() == {
        'source': 'some_source',
        'dest': 'some_dest',
        'bucket': 'some_bucket',
        'unzip': False,
        'tag': None
    }
Example #9
0
def test_target_parse_cwl_target_null_target_value():
    target = Target('some_bucket')
    outfile = AwsemPostRunJsonOutputFile(**{'path': '/data1/out/1/somefile'})
    target.parse_cwl_target(target_key='some_argname',
                            target_value=None,
                            prj_output_files={'some_argname': outfile})
    # use sourcename as dest if target_value is not given
    assert target.dest == '1/somefile'
    assert target.bucket == 'some_bucket'
    assert target.unzip is False
    assert target.source == '/data1/out/1/somefile'
Example #10
0
def test_upload_zip_directory_conflict(capsys):
    target = Target(upload_test_bucket)
    target.source = 'tests/awsf3/test_files/some_test_dir_to_upload'  # has two files and one subdir
    target.dest = 'some_test_object_prefix/'
    target.unzip = True  # conflict, since the source is a directory

    # test stdout
    cap = CaptureOut()
    with cap:
        target.upload_to_s3()
    assert 'Warning' in cap.get_captured_out()

    # still the directory should be uploaded despite the unzip conflict
    s3 = boto3.client('s3')

    def test_and_delete_key(key):
        res = s3.get_object(Bucket=upload_test_bucket, Key=key)
        assert res['Body'].read()
        s3.delete_object(Bucket=upload_test_bucket, Key=key)
        with pytest.raises(Exception) as ex:
            res = s3.get_object(Bucket=upload_test_bucket, Key=key)
        assert 'NoSuchKey' in str(ex.value)

    test_and_delete_key('some_test_object_prefix/file1')
    test_and_delete_key('some_test_object_prefix/file2')
    test_and_delete_key('some_test_object_prefix/dir1/file1')
Example #11
0
def test_upload_zip_not_a_zip_file_err():
    target = Target(upload_test_bucket)
    target.source = 'tests/awsf3/test_files/some_test_file_to_upload'  # not a zip file
    target.dest = 'some_test_object_prefix/'
    target.unzip = True
    with pytest.raises(Exception) as ex:
        target.upload_to_s3()
    assert 'not a zip file' in str(ex.value)
Example #12
0
def test_upload_zip_not_a_zip_file_err2():
    target = Target(upload_test_bucket)
    target.source = 'some_test_file_that_does_not_exist'
    target.dest = 'some_test_object_prefix/'
    target.unzip = True
    with pytest.raises(FileNotFoundError) as ex:
        target.upload_to_s3()
    assert 'No such file' in str(ex.value)
Example #13
0
def test_upload_file_prefix_encrypt_s3_upload():
    target = Target(upload_test_bucket)
    target.source = 'tests/awsf3/test_files/some_test_file_to_upload'
    target.dest = 'some_test_object_prefix/'
    target.upload_to_s3(encrypt_s3_upload=True)
    s3 = boto3.client('s3')
    res = s3.get_object(
        Bucket=upload_test_bucket,
        Key=
        'some_test_object_prefix/tests/awsf3/test_files/some_test_file_to_upload'
    )
    assert res['Body'].read().decode('utf-8') == 'abcd\n'
    res = s3.head_object(
        Bucket=upload_test_bucket,
        Key=
        'some_test_object_prefix/tests/awsf3/test_files/some_test_file_to_upload'
    )
    assert res['ServerSideEncryption'] == 'aws:kms'
    s3.delete_object(
        Bucket=upload_test_bucket,
        Key=
        'some_test_object_prefix/tests/awsf3/test_files/some_test_file_to_upload'
    )
    with pytest.raises(Exception) as ex:
        res = s3.get_object(
            Bucket=upload_test_bucket,
            Key=
            'some_test_object_prefix/tests/awsf3/test_files/some_test_file_to_upload'
        )
    assert 'NoSuchKey' in str(ex.value)
Example #14
0
def test_upload_file_err():
    target = Target(upload_test_bucket)
    target.source = 'some_test_file_that_does_not_exist'
    target.dest = 'whatever'
    with pytest.raises(Exception) as ex:
        target.upload_to_s3()
    assert 'failed to upload output file some_test_file_that_does_not_exist' + \
           ' to %s/whatever' % upload_test_bucket in str(ex.value)
Example #15
0
def test_upload_file():
    target = Target(upload_test_bucket)
    target.source = 'tests/awsf3/test_files/some_test_file_to_upload'
    target.dest = 'some_test_object_key'
    target.upload_to_s3()
    s3 = boto3.client('s3')
    res = s3.get_object(Bucket=upload_test_bucket, Key='some_test_object_key')
    assert res['Body'].read().decode('utf-8') == 'abcd\n'
    s3.delete_object(Bucket=upload_test_bucket, Key='some_test_object_key')
    with pytest.raises(Exception) as ex:
        res = s3.get_object(Bucket=upload_test_bucket,
                            Key='some_test_object_key')
    assert 'NoSuchKey' in str(ex.value)
Example #16
0
def test_upload_zip():
    target = Target(upload_test_bucket)
    target.source = 'tests/awsf3/test_files/some_zip_file_to_upload.zip'  # has two files and one subdir
    target.dest = 'some_test_object_prefix/'
    target.unzip = True
    target.upload_to_s3()
    s3 = boto3.client('s3')

    def test_and_delete_key(key):
        res = s3.get_object(Bucket=upload_test_bucket, Key=key)
        assert res['Body'].read()
        s3.delete_object(Bucket=upload_test_bucket, Key=key)
        with pytest.raises(Exception) as ex:
            res = s3.get_object(Bucket=upload_test_bucket, Key=key)
        assert 'NoSuchKey' in str(ex.value)

    test_and_delete_key('some_test_object_prefix/file1')
    test_and_delete_key('some_test_object_prefix/file2')
    test_and_delete_key('some_test_object_prefix/dir1/file1')
Example #17
0
def test_upload_dir_encrypt_s3_upload():
    target = Target(upload_test_bucket)
    target.source = 'tests/awsf3/test_files/some_test_dir_to_upload'  # has two files and one subdir
    target.dest = 'some_test_object_prefix/'
    target.upload_to_s3(encrypt_s3_upload=True)
    s3 = boto3.client('s3')

    def test_and_delete_key(key):
        res = s3.get_object(Bucket=upload_test_bucket, Key=key)
        assert res['Body'].read()
        res = s3.head_object(Bucket=upload_test_bucket, Key=key)
        assert res['ServerSideEncryption'] == 'aws:kms'
        s3.delete_object(Bucket=upload_test_bucket, Key=key)
        with pytest.raises(Exception) as ex:
            res = s3.get_object(Bucket=upload_test_bucket, Key=key)
        assert 'NoSuchKey' in str(ex.value)

    test_and_delete_key('some_test_object_prefix/file1')
    test_and_delete_key('some_test_object_prefix/file2')
    test_and_delete_key('some_test_object_prefix/dir1/file1')
Example #18
0
def test_target_parse_target_value_dict_object_key_err():
    target = Target('some_bucket')
    with pytest.raises(Exception) as ex:
        target.parse_target_value({'object_key': 'some_object_key/'})
    assert 'object_prefix' in str(ex.value)
Example #19
0
def test_target_parse_target_value_str_object_key():
    target = Target('some_bucket')
    target.parse_target_value('some_object_key')
    assert target.dest == 'some_object_key'
    assert target.bucket == 'some_bucket'
    assert target.unzip is False
Example #20
0
def test_target_parse_target_value_tag():
    target = Target('some_bucket')
    target.parse_target_value({'tag': 'Key1=Name1'})
    assert target.tag == 'Key1=Name1'
Example #21
0
def test_target_init():
    target = Target('some_bucket')
    assert target.bucket == 'some_bucket'
    assert target.unzip is False
    assert target.is_valid is False  # source/dest not set yet
Example #22
0
def test_target_parse_target_value_unzip():
    target = Target('some_bucket')
    target.parse_target_value({'unzip': True, 'object_prefix': 'some_dir/'})
    assert target.dest == 'some_dir/'
    assert target.bucket == 'some_bucket'
    assert target.unzip is True
Example #23
0
def test_target_parse_target_value_dict_object_prefix_wo_slash():
    target = Target('some_bucket')
    target.parse_target_value({'object_prefix': 'some_dir'})
    assert target.dest == 'some_dir/'
    assert target.bucket == 'some_bucket'
    assert target.unzip is False
Example #24
0
def test_target_sourcename3():
    target = Target('some_bucket')
    target.source = '/data1/shell/1/somefile'
    assert target.source_name == '1/somefile'
Example #25
0
def test_target_sourcename4():
    target = Target('some_bucket')
    target.source = '/data1/whatever/1/somefile'
    assert target.source_name == 'whatever/1/somefile'
Example #26
0
def test_target_is_valid():
    target = Target('some_bucket')
    target.source = '/data1/whatever/1/somefile'
    target.dest = 'some_dest'
    assert target.is_valid is True
Example #27
0
def test_target_is_valid():
    target = Target('some_bucket')
    target.source = '/data1/whatever/1/somefile'
    assert target.is_valid is False  # no destination set
Example #28
0
def test_target_is_valid():
    target = Target('some_bucket')
    target.dest = 'some_dest'
    assert target.is_valid is False  # no source set
Example #29
0
def test_target_sourcename():
    target = Target('some_bucket')
    target.source = '/data1/out/somefile'
    assert target.source_name == 'somefile'
Example #30
0
def test_target_parse_target_value_str_url():
    target = Target('some_bucket')
    target.parse_target_value('s3://another_bucket/some_object_key')
    assert target.dest == 'some_object_key'
    assert target.bucket == 'another_bucket'
    assert target.unzip is False