class TestAnalysisCompare(unittest.TestCase):
    def setUp(self):
        self.tmpdir = tempfile.mkdtemp()
        self.cli = Singularity()
        self.container = self.cli.pull('docker://ubuntu:16.04',
                                       pull_folder=self.tmpdir)

        self.comparator = self.cli.pull('docker://ubuntu:12.04',
                                        pull_folder=self.tmpdir)

    def tearDown(self):
        os.remove(self.container)
        os.remove(self.comparator)
        shutil.rmtree(self.tmpdir)

    def test_container_similarity(self):
        print(
            "Testing singularity.analysis.compare.container_similarity_vector")
        import pandas
        from singularity.analysis.compare import container_similarity_vector
        from singularity.package import get_packages
        packages_set = get_packages('docker-os')[0:2]
        vector = container_similarity_vector(container1=self.container,
                                             custom_set=packages_set)
        self.assertTrue('files.txt' in vector)
        self.assertTrue(isinstance(vector['files.txt'], pandas.DataFrame))

        print(
            "Testing singularity.analysis.compare.compare_singularity_images")
        from singularity.analysis.compare import compare_singularity_images
        sim = compare_singularity_images(self.container, self.comparator)
        self.assertTrue(isinstance(sim, pandas.DataFrame))
        self.assertTrue(sim.loc[self.container, self.comparator] -
                        0.4803262269280298 < 0.01)

        print("Testitng singularity.analysis.compare.compare_containers")
        from singularity.analysis.compare import compare_containers
        comparison = compare_containers(self.container, self.comparator)
        self.assertTrue('files.txt' in comparison)
        for key in ['total1', 'total2', 'intersect', 'unique2', 'unique1']:
            self.assertTrue(key in comparison['files.txt'])

        print("Testing singularity.analysis.compare.calculate_similarity")
        from singularity.analysis.compare import calculate_similarity
        sim = calculate_similarity(self.container, self.comparator)
        self.assertTrue(sim['files.txt'] - 0.4921837537163134 < 0.01)

    def test_information_coefficient(self):
        print("Testing singularity.analysis.metrics.information_coefficient")
        from singularity.analysis.metrics import information_coefficient
        self.assertEqual(information_coefficient(100, 100, range(0, 50)), 0.5)
        self.assertEqual(information_coefficient(100, 100, range(0, 100)), 1.0)
def main(args, parser, subparser):

    # We can only continue if singularity is installed
    if check_install() is not True:
        bot.error("Cannot find Singularity! Is it installed?")
        sys.exit(1)

    # Output folder will be pwd if not specified
    output_folder = os.getcwd()
    if args.outfolder is not None:
        output_folder = os.getcwd()

    # If we are given an image, ensure full path
    if args.image is not None:

        image = args.image
        if not os.path.exists(image):
            cli = Singularity(debug=args.debug)
            image = cli.pull(image)

        if image is None:
            bot.error("Cannot find image. Exiting.")
            sys.exit(1)

        remove_image = not args.include_image
        package(image_path=image,
                output_folder=output_folder,
                remove_image=remove_image)

    else:
        print("Please specify an image to package with --image")
        subparser.print_help()
Exemple #3
0
def main(args, parser, subparser):

    # We can only continue if singularity is installed
    if check_install() is not True:
        bot.error("Cannot find Singularity! Is it installed?")
        sys.exit(1)

    # Output folder will be pwd if not specified
    output_folder = os.getcwd()
    if args.outfolder is not None:
        output_folder = os.getcwd()

    # If we are given an image, ensure full path
    if args.image is not None:

        if not os.path.exists(args.image):
            cli = Singularity(debug=args.debug)
            image = cli.pull(args.image)

        if image is None:
            bot.error("Cannot find image. Exiting.")
            sys.exit(1)

        # the user wants to make a tree
        if args.tree is True:
            from singularity.cli.app import make_tree
            make_tree(image)
            clean_up(image, existed)

    else:
        print("Please specify an image to inspect with --image")
        subparser.print_help()
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)
Exemple #5
0
def main(args, parser, subparser):

    # We can only continue if singularity is installed
    if check_install() is not True:
        bot.error("Cannot find Singularity! Is it installed?")
        sys.exit(1)

    # Output folder will be pwd if not specified
    output_folder = os.getcwd()
    if args.outfolder is not None:
        output_folder = os.getcwd()

    # If we are given an image, ensure full path
    image = args.image
    if image is not None:

        existed = True
        if not os.path.exists(image):
            cli = Singularity(debug=args.debug)
            image = cli.pull(image)
            existed = False

        if image is None:
            bot.error("Cannot find image. Exiting.")
            sys.exit(1)

        # The user wants to estimate the os
        if args.os is True:
            from singularity.analysis.classify import estimate_os
            estimated_os = estimate_os(container=image)
            print(estimated_os)

        # The user wants to get a list of all os
        elif args.oscalc is True:
            from singularity.analysis.classify import estimate_os
            estimated_os = estimate_os(container=image, return_top=False)
            print(estimated_os["SCORE"].to_dict())

        # The user wants to get a list of tags
        elif args.tags is True:
            from singularity.analysis.classify import get_tags
            tags = get_tags(container=image)
            print(tags)

        # The user wants to plot image vs. the docker os
        elif args.osplot is True:
            from singularity.app import plot_os_sims
            plot_os_sims(image)

        clean_up(image, existed)

    else:
        print("Please specify an image to analyze with --image")
        subparser.print_help()
def main(args, parser, subparser):

    # We can only continue if singularity is installed
    if check_install() is not True:
        bot.error("Cannot find Singularity! Is it installed?")
        sys.exit(1)

    # Output folder will be pwd if not specified
    output_folder = os.getcwd()
    if args.outfolder is not None:
        output_folder = os.getcwd()

    if args.images is not None:

        image1, image2 = args.images.split(',')
        bot.debug("Image1: %s" % image1)
        bot.debug("Image2: %s" % image2)

        images = dict()
        cli = Singularity(debug=args.debug)
        for image in [image1, image2]:
            existed = True
            if not os.path.exists(image):
                image = cli.pull(image)
                existed = False
            images[image] = existed

        # Just for clarity
        image1, image2 = list(images.keys())

        # the user wants to make a similarity tree
        if args.simtree is True:
            from singularity.cli.app import make_sim_tree
            make_sim_tree(image1, image2)

        # the user wants to make a difference tree
        elif args.subtract is True:
            from singularity.cli.app import make_diff_tree
            make_diff_tree(image1, image2)

        else:  # If none specified, just print score
            from singularity.analysis.compare import calculate_similarity
            score = calculate_similarity(image1, image2, by="files.txt")
            print(score["files.txt"])

        for image, existed in images.items():
            clean_up(image, existed)

    else:
        print("Please specify images to compare with --images")
        subparser.print_help()
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)
 def pull_image(self,image_url):
     """Pull the singularity image to specified image path."""
     image_path = self.get_image_path()
     s = Singularity()
     container_path = s.pull(image_url[0],image_name=image_path[0])
     return Unicode(container_path)