Exemplo n.º 1
0
def create_container(container=None, do_import=False):
    '''supporting function to create empty container
    '''
    cli = Singularity()
    tmpdir = tempfile.mkdtemp()
    if container is None:
        container = "%s/container.img" % (tmpdir)
    if do_import is True:
        cli.importcmd(container, 'docker://ubuntu')
    return cli.create(container)
Exemplo n.º 2
0
    def test_test_container(self):
        '''the retry function should return None if result is None
        '''
        from singularity.build.utils import test_container
        from singularity.cli import Singularity
        cli = Singularity()

        unfinished_container = cli.create("%s/container.img" % self.tmpdir)
        print("Case 1: Testing that errored container does not run")
        result = test_container(unfinished_container)
        self.assertEqual(result["return_code"], 255)

        print("Case 2: Testing that finished container does run")
        finished_container = cli.importcmd(unfinished_container,
                                           'docker://ubuntu')
        result = test_container(finished_container)
        self.assertEqual(result["return_code"], 0)
Exemplo n.º 3
0
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)
S = Singularity()

# Get general help:
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
class TestPackage(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)
        # We can't test creating the packages, because requires sudo :/
        self.pkg1 = "%s/tests/data/busybox-2016-02-16.img.zip" % (self.pwd)
        self.pkg2 = "%s/tests/data/cirros-2016-01-04.img.zip" % (self.pwd)

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

    def test_packaging(self):

        # Check content of packages
        print("TESTING content extraction of packages")
        self.pkg1_includes = list_package(self.pkg1)
        self.pkg2_includes = list_package(self.pkg2)

        includes = ['files.txt', 'VERSION', 'NAME', 'folders.txt']
        for pkg_includes in [self.pkg1_includes, self.pkg2_includes]:
            [self.assertTrue(x in pkg_includes) for x in includes]
        self.assertTrue(os.path.basename(self.image1) in self.pkg1_includes)
        self.assertTrue(os.path.basename(self.image2) in self.pkg2_includes)

        print("TESTING loading packages...")
        pkg1_loaded = load_package(self.pkg1)
        self.assertTrue(len(pkg1_loaded["files.txt"]) == 12)
        self.assertTrue(len(pkg1_loaded["folders.txt"]) == 18)

        # Did it extract successfully?
        image1_extraction = pkg1_loaded[os.path.basename(self.image1)]
        self.assertTrue(os.path.exists(image1_extraction))
        shutil.rmtree(os.path.dirname(image1_extraction))

    '''
    def test_estimate_from_size(self):
        """test estimate from size will ensure that we correctly estimate the size
        of a container build plus some optional padding
        Note: we currently can't test this in CI due to needing 
        sudo password for bootstrap
        """
        from singularity.package import estimate_image_size
        spec = "From: ubuntu:16.04\nBootstrap: docker"        
        spec_file = "%s/Singularity" %(self.tmpdir)
        spec_file = write_file(spec_file,spec)     
        print("Case 1: Testing that no specification of padding uses default 200")   
        image_size = estimate_image_size(spec_file)        

    '''

    def test_package(self):
        '''test package will ensure that we can generate an image package'''
        from singularity.package import package
        container = self.cli.create("%s/container.img" % self.tmpdir)
        container = self.cli.importcmd(container, "docker://ubuntu")
        image_package = package(image_path=container,
                                output_folder=self.tmpdir,
                                S=self.cli)
        self.assertTrue(os.path.exists(image_package))