class TestClient(unittest.TestCase):
    def setUp(self):
        self.pwd = get_installdir()
        self.cli = Singularity()
        self.tmpdir = tempfile.mkdtemp()

    def tearDown(self):
        shutil.rmtree(self.tmpdir)

    def test_commands(self):

        print('Testing client.create command')
        container = "%s/container.img" % (self.tmpdir)
        created_container = self.cli.create(container)
        self.assertEqual(created_container, container)
        self.assertTrue(os.path.exists(created_container))
        os.remove(container)

        print("Testing client.pull command")
        print("...Case 1: Testing naming pull by image name")
        image = self.cli.pull("shub://vsoch/singularity-images",
                              pull_folder=self.tmpdir)
        self.assertTrue(os.path.exists(image))
        self.assertTrue('vsoch-singularity-images-master' in image)
        print(image)
        os.remove(image)

        print("...Case 3: Testing docker pull")
        container = self.cli.pull("docker://ubuntu:14.04",
                                  pull_folder=self.tmpdir)
        self.assertTrue("ubuntu:14.04" in container)
        print(container)
        self.assertTrue(os.path.exists(container))

        print('Testing client.execute command')
        result = self.cli.execute(container, 'ls /')
        print(result)
        self.assertTrue('bin\nboot\ndev' in result)

        print("Testing client.inspect command")
        result = self.cli.inspect(container, quiet=True)
        labels = json.loads(result)
        self.assertTrue('data' in labels)
        os.remove(container)
def extract_content(image_path, member_name, cli=None, return_hash=False):
    '''extract_content will extract content from an image using cat.
    If hash=True, a hash sum is returned instead
    '''
    if member_name.startswith('./'):
        member_name = member_name.replace('.', '', 1)
    if return_hash:
        hashy = hashlib.md5()
    if cli == None:
        cli = Singularity()
    content = cli.execute(image_path, 'cat %s' % (member_name))
    if not isinstance(content, bytes):
        content = bytes(content)
    # If permissions don't allow read, return None
    if len(content) == 0:
        return None
    if return_hash:
        hashy.update(content)
        return hashy.hexdigest()
    return content
class TestClient(unittest.TestCase):
    def setUp(self):
        self.pwd = get_installdir()
        self.cli = Singularity()
        self.tmpdir = tempfile.mkdtemp()
        self.image1 = "%s/tests/data/busybox-2016-02-16.img" % (self.pwd)
        self.image2 = "%s/tests/data/cirros-2016-01-04.img" % (self.pwd)

    def tearDown(self):
        shutil.rmtree(self.tmpdir)

    def test_create(self):
        print('Testing client.create command')
        container = "%s/container.img" % (self.tmpdir)
        created_container = create_container(container)
        self.assertEqual(created_container, container)
        self.assertTrue(os.path.exists(container))

    def test_import(self):
        from singularity.build.utils import test_container
        print("Testing client.import command")
        container = create_container()

        # Container should not be valid
        print("Case 1: Before import, container is not valid")
        result = test_container(container)
        self.assertEqual(result['return_code'], 255)

        print("Case 2: After import, container is valid")
        self.cli.importcmd(container, 'docker://ubuntu')
        result = test_container(container)
        self.assertEqual(result['return_code'], 0)

    def test_run(self):
        print("Testing client.run command")
        container = create_container(do_import=True)
        result = self.cli.run(container)
        self.assertEqual(result, '')

    def test_exec(self):
        print('Testing client.execute command')
        container = create_container(do_import=True)
        result = self.cli.execute(container, 'ls /')
        print(result)
        #if isinstance(result,bytes):
        #    result = result.decode('utf-8')
        #self.assertTrue(len(result)>0)

    def test_pull(self):
        print("Testing client.pull command")

        print("Case 1: Testing naming pull by image name")
        image = self.cli.pull("shub://vsoch/singularity-images")
        print(image)

        print("Case 2: Testing naming pull by image commit")
        image = self.cli.pull("shub://vsoch/singularity-images",
                              name_by="commit")
        print(image)

        print("Case 3: Testing naming pull by image hash")
        image = self.cli.pull("shub://vsoch/singularity-images",
                              name_by="hash")
        print(image)

    def test_get_image(self):
        print("Testing singularity.cli.get_image")
        from singularity.cli import get_image
        from singularity.build.utils import test_container
        tmpimg = get_image('docker://ubuntu')
        self.assertTrue(os.path.exists(tmpimg))
        result = test_container(tmpimg)
        self.assertEqual(result['return_code'], 0)
# run commands. It is not stored anywhere, however you should not save / pickle
# the object as it will expose your password. 
S = Singularity()

# Get general help:
S.help()

# These are the defaults, which can be specified
S = Singularity(sudo=True,verbose=False)

# Let's define a path to an image
# wget http://www.vbmis.com/bmi/project/singularity/package_image/ubuntu:latest-2016-04-06.img
image_path = 'ubuntu:latest-2016-04-06.img'

# Run singularity --exec
S.execute(image_path=image_path,command='ls')
# $'docker2singularity.sh\nget_docker_container_id.sh\nget_docker_meta.py\nmakeBases.py\nsingularity\nubuntu:latest-2016-04-06.img\n'
# These are the defaults, which can be specified

# For any function you can get the docs:
S.help(command="exec")

# or return as string
help = S.help(command="exec",stdout=False)

# export an image, default export_type="tar" , pipe=False , output_file = None will produce file in tmp
tmptar = S.export(image_path=image_path)

# create an empty image
S.create(image_path='test.img')
S.help()

# These are the defaults, which can be specified
S = Singularity(sudo=False, debug=False)

# Note that the "create" and "import" workflow is deprecated in favor of build
# https://singularityware.github.io/docs-build

image = S.build('myimage.simg', 'docker://ubuntu:latest')  # requires sudo

# (Deprecated) create an image and import into it
image = S.create('myimage.simg')
S.importcmd(image, 'docker://ubuntu:latest')

# Execute command to container
result = S.execute(image, command='cat /singularity')
print(result)
'''
'#!/bin/sh\n\nexec "/bin/bash"\n'
'''

# For any function you can get the docs:
S.help(command="exec")

# export an image to tar
tar = S.export(image)

# Show apps and inspect
S.apps(image)
S.inspect(image)
'''