# Path: kubeface/bucket_storage.py
# RETRIES_BEFORE_FAILURE = 12
# FIRST_RETRY_SLEEP = 2.0
# _SERVICE = None
#         _SERVICE = create_service()
# def get_service():
# def create_service():
# def robustify(function):
#     def robust_function(*args, **kwargs):
# def split_bucket_and_name(url):
# def list_contents(prefix):
# def move(source, dest):
# def put(
#         name,
#         input_handle,
#         readers=[],
#         owners=[],
#         mime_type='application/octet-stream'):
# def get(name, output_handle=None):
# def delete(name):
# def access_info(name):
# 
# Path: kubeface/storage.py
# def is_google_storage_bucket(name):
# def list_contents(prefix):
# def put(name, input_handle, readers=[], owners=[], **kwargs):
# def get(name, output_handle=None):
# def delete(name):
# def move(source, dest):
# def access_info(name):
# 
# Path: tests/util.py
# def with_local_and_bucket_storage(function):
#     bucket = os.environ.get("KUBEFACE_STORAGE")
#     if not bucket:
#         logging.warning(
#             "Set KUBEFACE_STORAGE to run test: %s" % str(function))
#         return with_local_storage(function)
# 
#     def test_function():
#         with_local_storage(function)()
#         with_bucket_storage(function)()
#     return test_function


import time
from six import BytesIO
from numpy import testing
from kubeface import bucket_storage, storage
from .util import with_local_and_bucket_storage

def test_url_parse():
    testing.assert_equal(
        bucket_storage.split_bucket_and_name("gs://foo/bar"),
        ("foo", "bar"))

    testing.assert_equal(
        bucket_storage.split_bucket_and_name("gs://foo/bar/baz.txt"),
        ("foo", "bar/baz.txt"))


@with_local_and_bucket_storage
def test_put_and_get_to_bucket(bucket):
    data = "ABCDe" * 1000
    data_handle = BytesIO(data.encode("UTF-8"))
    file_name = "kubeface-test-%s.txt" % (
        str(time.time()).replace(".", ""))
    name = "%s/%s" % (bucket, file_name)
    storage.put(name, data_handle)
    testing.assert_equal(storage.list_contents(name), [file_name])
    testing.assert_(
        file_name in storage.list_contents("%s/kubeface-test-" % bucket))

    result_handle = storage.get(name)
    testing.assert_equal(result_handle.read().decode("UTF-8"), data)
    storage.delete(name)
    testing.assert_(
        file_name not in storage.list_contents("%s/" % bucket))



# Path: kubeface/bucket_storage.py
# RETRIES_BEFORE_FAILURE = 12
# FIRST_RETRY_SLEEP = 2.0
# _SERVICE = None
#         _SERVICE = create_service()
# def get_service():
# def create_service():
# def robustify(function):
#     def robust_function(*args, **kwargs):
# def split_bucket_and_name(url):
# def list_contents(prefix):
# def move(source, dest):
# def put(
#         name,
#         input_handle,
#         readers=[],
#         owners=[],
#         mime_type='application/octet-stream'):
# def get(name, output_handle=None):
# def delete(name):
# def access_info(name):
# 
# Path: kubeface/storage.py
# def is_google_storage_bucket(name):
# def list_contents(prefix):
# def put(name, input_handle, readers=[], owners=[], **kwargs):
# def get(name, output_handle=None):
# def delete(name):
# def move(source, dest):
# def access_info(name):
# 
# Path: tests/util.py
# def with_local_and_bucket_storage(function):
#     bucket = os.environ.get("KUBEFACE_STORAGE")
#     if not bucket:
#         logging.warning(
#             "Set KUBEFACE_STORAGE to run test: %s" % str(function))
#         return with_local_storage(function)
# 
#     def test_function():
#         with_local_storage(function)()
#         with_bucket_storage(function)()
#     return test_function


# Path: tests/test_storage.py
import time
from six import BytesIO
from numpy import testing
from kubeface import bucket_storage, storage
from .util import with_local_and_bucket_storage


def test_url_parse():
    testing.assert_equal(
        bucket_storage.split_bucket_and_name("gs://foo/bar"),
        ("foo", "bar"))

    testing.assert_equal(
        bucket_storage.split_bucket_and_name("gs://foo/bar/baz.txt"),
        ("foo", "bar/baz.txt"))


@with_local_and_bucket_storage
def test_put_and_get_to_bucket(bucket):
    data = "ABCDe" * 1000
    data_handle = BytesIO(data.encode("UTF-8"))
    file_name = "kubeface-test-%s.txt" % (
        str(time.time()).replace(".", ""))
    name = "%s/%s" % (bucket, file_name)
    storage.put(name, data_handle)
    testing.assert_equal(storage.list_contents(name), [file_name])
    testing.assert_(
        file_name in storage.list_contents("%s/kubeface-test-" % bucket))

    result_handle = storage.get(name)
    testing.assert_equal(result_handle.read().decode("UTF-8"), data)
    storage.delete(name)
    testing.assert_(
        file_name not in storage.list_contents("%s/" % bucket))



@with_local_and_bucket_storage