<commit_before>#!/usr/bin/env python
#
# test_iso.py - Unit test cases for ISO disk representation.
#
# October 2016, <NAME>
# Copyright (c) 2014-2016 the COT project developers.
# See the COPYRIGHT.txt file at the top-level directory of this distribution
# and at https://github.com/glennmatthews/cot/blob/master/COPYRIGHT.txt.
#
# This file is part of the Common OVF Tool (COT) project.
# It is subject to the license terms in the LICENSE.txt file found in the
# top-level directory of this distribution and at
# https://github.com/glennmatthews/cot/blob/master/LICENSE.txt. No part
# of COT, including this file, may be copied, modified, propagated, or
# distributed except according to the terms contained in the LICENSE.txt file.

"""Unit test cases for ISO subclass of DiskRepresentation."""

import logging
import os
import mock

from COT.tests.ut import COT_UT
from COT.disks import ISO
from COT.helpers import (
    helpers, HelperError, HelperNotFoundError,
)

logger = logging.getLogger(__name__)

# pylint: disable=protected-access,missing-type-doc,missing-param-doc


class TestISO(COT_UT):
    """Test cases for ISO class."""

    def tearDown(self):
        """Test case cleanup function called automatically after each test."""
        for name in ['mkisofs', 'genisoimage', 'xorriso', 'isoinfo']:
            helper = helpers[name]
            helper._installed = None
            helper._path = None
            helper._version = None
        super(TestISO, self).tearDown()

    def test_representation(self):
        """Representing an existing ISO."""
        iso = ISO(self.input_iso)
        self.assertEqual(iso.path, self.input_iso)
        self.assertEqual(iso.disk_format, 'iso')
        self.assertEqual(iso.capacity, str(self.FILE_SIZE['input.iso']))
        if helpers['isoinfo']:
            self.assertEqual(iso.disk_subformat, "")
            self.assertEqual(iso.files,
                             ['iosxr_config.txt', 'iosxr_config_admin.txt'])

    def test_create_with_files(self):
        """Creation of a ISO with specific file contents."""
        iso = ISO(path=os.path.join(self.temp_dir, "out.iso"),
                  files=[self.input_ovf])
        if helpers['isoinfo']:
            # Our default create format is rockridge
            self.assertEqual(iso.disk_subformat, "rockridge")
            self.assertEqual(iso.files,
                             [os.path.basename(self.input_ovf)])
        else:
            helpers['isoinfo']._installed = True

            with mock.patch.object(helpers['isoinfo'], "call",
                                   return_value="Rock Ridge extensions found"):
                self.assertEqual(iso.disk_subformat, "rockridge")

            with mock.patch.object(helpers['isoinfo'], "call",
                                   return_value="""
Setting input-charset to 'UTF-8' from locale.
/{0}
""".format(os.path.basename(self.input_ovf))):
                self.assertEqual(iso.files,
                                 [os.path.basename(self.input_ovf)])

    def test_create_with_files_non_rockridge(self):
        """Creation of a non-rock-ridge ISO with specific file contents."""
        iso = ISO(path=os.path.join(self.temp_dir, "out.iso"),
                  files=[self.input_ovf],
                  disk_subformat="")
        if helpers['isoinfo']:
            self.assertEqual(iso.disk_subformat, "")
            self.assertEqual(iso.files,
                             [os.path.basename(self.input_ovf)])
        else:
            helpers['isoinfo']._installed = True
            with mock.patch.object(helpers['isoinfo'], "call",
                                   return_value="No SUSP/Rock Ridge present"):
                self.assertEqual(iso.disk_subformat, "")

            with mock.patch.object(helpers['isoinfo'], "call",
                                   return_value="""
Setting input-charset to 'UTF-8' from locale.
/{0};1
""".format(os.path.basename(self.input_ovf).upper())):
                self.assertEqual(iso.files,
                                 [os.path.basename(self.input_ovf)])

    def test_create_without_files(self):
        """Can't create an empty ISO."""
        self.assertRaises(RuntimeError,
                          ISO,
                          path=os.path.join(self.temp_dir, "out.iso"),
                          capacity="100")

    @mock.patch("COT.helpers.mkisofs.MkISOFS.call")
    def test_create_with_mkisofs(self, mock_call):
        """Creation of an ISO with mkisofs (default)."""
        helpers['mkisofs']._installed = True
        ISO(path='foo.iso', files=[self.input_ovf])
        mock_call.assert_called_with(
            ['-output', 'foo.iso', '-full-iso9660-filenames',
             '-iso-level', '2', '-allow-lowercase', '-r', self.input_ovf])

    @mock.patch("COT.helpers.mkisofs.GenISOImage.call")
    def test_create_with_genisoimage(self, mock_call):
        """Creation of an ISO with genisoimage if mkisofs is unavailable."""
        helpers['mkisofs']._installed = False
        helpers['genisoimage']._installed = True
        ISO(path='foo.iso', files=[self.input_ovf])
        mock_call.assert_called_with(
            ['-output', 'foo.iso', '-full-iso9660-filenames',
             '-iso-level', '2', '-allow-lowercase', '-r', self.input_ovf])

    @mock.patch("COT.helpers.mkisofs.XorrISO.call")
    def test_create_with_xorriso(self, mock_call):
        """Creation of an ISO with xorriso as last resort."""
        helpers['mkisofs']._installed = False
        helpers['genisoimage']._installed = False
        helpers['xorriso']._installed = True
        ISO(path='foo.iso', files=[self.input_ovf])
        mock_call.assert_called_with(
            ['-as', 'mkisofs', '-output', 'foo.iso', '-full-iso9660-filenames',
             '-iso-level', '2', '-allow-lowercase', '-r', self.input_ovf])

    def test_create_no_helpers_available(self):
        """Creation of ISO should fail if no helpers are install[ed|able]."""
        helpers['mkisofs']._installed = False
        helpers['genisoimage']._installed = False
        helpers['xorriso']._installed = False
        helpers['apt-get']._installed = False
        helpers['port']._installed = False
        helpers['yum']._installed = False
        self.assertRaises(HelperNotFoundError,
                          ISO,
                          path='foo.iso',
                          files=[self.input_ovf])

    @mock.patch("COT.helpers.mkisofs.MkISOFS.call")
    def test_create_with_mkisofs_non_rockridge(self, mock_call):
        """Creation of a non-Rock-Ridge ISO with mkisofs (default)."""
        helpers['mkisofs']._installed = True
        ISO(path='foo.iso', files=[self.input_ovf], disk_subformat="")
        mock_call.assert_called_with(
            ['-output', 'foo.iso', '-full-iso9660-filenames',
             '-iso-level', '2', '-allow-lowercase', self.input_ovf])

    def test_file_is_this_type_nonexistent(self):
        """Call file_is_this_type should fail if file doesn't exist."""
        self.assertRaises(HelperError,
                          ISO.file_is_this_type, "/foo/bar")

    def test_file_is_this_type_isoinfo(self):
        """The file_is_this_type API should use isoinfo if available."""
        if helpers['isoinfo']:
            self.assertTrue(ISO.file_is_this_type(self.input_iso))
            self.assertFalse(ISO.file_is_this_type(self.blank_vmdk))
        else:
            # Fake it til you make it<commit_msg>Fix local file path potential bug<commit_after>#!/usr/bin/env python
#
# test_iso.py - Unit test cases for ISO disk representation.
#
# October 2016, <NAME>
# Copyright (c) 2014-2017 the COT project developers.
# See the COPYRIGHT.txt file at the top-level directory of this distribution
# and at https://github.com/glennmatthews/cot/blob/master/COPYRIGHT.txt.
#
# This file is part of the Common OVF Tool (COT) project.
# It is subject to the license terms in the LICENSE.txt file found in the
# top-level directory of this distribution and at
# https://github.com/glennmatthews/cot/blob/master/LICENSE.txt. No part
# of COT, including this file, may be copied, modified, propagated, or
# distributed except according to the terms contained in the LICENSE.txt file.

"""Unit test cases for ISO subclass of DiskRepresentation."""

import logging
import os
import mock

from COT.tests.ut import COT_UT
from COT.disks import ISO
from COT.helpers import (
    helpers, HelperError, HelperNotFoundError,
)

logger = logging.getLogger(__name__)

# pylint: disable=protected-access,missing-type-doc,missing-param-doc


class TestISO(COT_UT):
    """Test cases for ISO class."""

    def setUp(self):
        """Test case setup function called automatically before each test."""
        super(TestISO, self).setUp()
        self.foo_iso = os.path.join(self.temp_dir, "foo.iso")

    def tearDown(self):
        """Test case cleanup function called automatically after each test."""
        for name in ['mkisofs', 'genisoimage', 'xorriso', 'isoinfo']:
            helper = helpers[name]
            helper._installed = None
            helper._path = None
            helper._version = None
        super(TestISO, self).tearDown()

    def test_representation(self):
        """Representing an existing ISO."""
        iso = ISO(self.input_iso)
        self.assertEqual(iso.path, self.input_iso)
        self.assertEqual(iso.disk_format, 'iso')
        self.assertEqual(iso.capacity, str(self.FILE_SIZE['input.iso']))
        if helpers['isoinfo']:
            self.assertEqual(iso.disk_subformat, "")
            self.assertEqual(iso.files,
                             ['iosxr_config.txt', 'iosxr_config_admin.txt'])

    def test_create_with_files(self):
        """Creation of a ISO with specific file contents."""
        iso = ISO(path=os.path.join(self.temp_dir, "out.iso"),
                  files=[self.input_ovf])
        if helpers['isoinfo']:
            # Our default create format is rockridge
            self.assertEqual(iso.disk_subformat, "rockridge")
            self.assertEqual(iso.files,
                             [os.path.basename(self.input_ovf)])
        else:
            helpers['isoinfo']._installed = True

            with mock.patch.object(helpers['isoinfo'], "call",
                                   return_value="Rock Ridge extensions found"):
                self.assertEqual(iso.disk_subformat, "rockridge")

            with mock.patch.object(helpers['isoinfo'], "call",
                                   return_value="""
Setting input-charset to 'UTF-8' from locale.
/{0}
""".format(os.path.basename(self.input_ovf))):
                self.assertEqual(iso.files,
                                 [os.path.basename(self.input_ovf)])

    def test_create_with_files_non_rockridge(self):
        """Creation of a non-rock-ridge ISO with specific file contents."""
        iso = ISO(path=os.path.join(self.temp_dir, "out.iso"),
                  files=[self.input_ovf],
                  disk_subformat="")
        if helpers['isoinfo']:
            self.assertEqual(iso.disk_subformat, "")
            self.assertEqual(iso.files,
                             [os.path.basename(self.input_ovf)])
        else:
            helpers['isoinfo']._installed = True
            with mock.patch.object(helpers['isoinfo'], "call",
                                   return_value="No SUSP/Rock Ridge present"):
                self.assertEqual(iso.disk_subformat, "")

            with mock.patch.object(helpers['isoinfo'], "call",
                                   return_value="""
Setting input-charset to 'UTF-8' from locale.
/{0};1
""".format(os.path.basename(self.input_ovf).upper())):
                self.assertEqual(iso.files,
                                 [os.path.basename(self.input_ovf)])

    def test_create_without_files(self):
        """Can't create an empty ISO."""
        self.assertRaises(RuntimeError,
                          ISO,
                          path=os.path.join(self.temp_dir, "out.iso"),
                          capacity="100")

    @mock.patch("COT.helpers.mkisofs.MkISOFS.call")
    def test_create_with_mkisofs(self, mock_call):
        """Creation of an ISO with mkisofs (default)."""
        helpers['mkisofs']._installed = True
        ISO(path=self.foo_iso, files=[self.input_ovf])
        mock_call.assert_called_with(
            ['-output', self.foo_iso, '-full-iso9660-filenames',
             '-iso-level', '2', '-allow-lowercase', '-r', self.input_ovf])

    @mock.patch("COT.helpers.mkisofs.GenISOImage.call")
    def test_create_with_genisoimage(self, mock_call):
        """Creation of an ISO with genisoimage if mkisofs is unavailable."""
        helpers['mkisofs']._installed = False
        helpers['genisoimage']._installed = True
        ISO(path=self.foo_iso, files=[self.input_ovf])
        mock_call.assert_called_with(
            ['-output', self.foo_iso, '-full-iso9660-filenames',
             '-iso-level', '2', '-allow-lowercase', '-r', self.input_ovf])

    @mock.patch("COT.helpers.mkisofs.XorrISO.call")
    def test_create_with_xorriso(self, mock_call):
        """Creation of an ISO with xorriso as last resort."""
        helpers['mkisofs']._installed = False
        helpers['genisoimage']._installed = False
        helpers['xorriso']._installed = True
        ISO(path=self.foo_iso, files=[self.input_ovf])
        mock_call.assert_called_with(
            ['-as', 'mkisofs', '-output', self.foo_iso,
             '-full-iso9660-filenames', '-iso-level', '2', '-allow-lowercase',
             '-r', self.input_ovf])

    def test_create_no_helpers_available(self):
        """Creation of ISO should fail if no helpers are install[ed|able]."""
        helpers['mkisofs']._installed = False
        helpers['genisoimage']._installed = False
        helpers['xorriso']._installed = False
        helpers['apt-get']._installed = False
        helpers['port']._installed = False
        helpers['yum']._installed = False
        self.assertRaises(HelperNotFoundError,
                          ISO,
                          path=self.foo_iso,
                          files=[self.input_ovf])

    @mock.patch("COT.helpers.mkisofs.MkISOFS.call")
    def test_create_with_mkisofs_non_rockridge(self, mock_call):
        """Creation of a non-Rock-Ridge ISO with mkisofs (default)."""
        helpers['mkisofs']._installed = True
        ISO(path=self.foo_iso, files=[self.input_ovf], disk_subformat="")
        mock_call.assert_called_with(
            ['-output', self.foo_iso, '-full-iso9660-filenames',
             '-iso-level', '2', '-allow-lowercase', self.input_ovf])

    def test_file_is_this_type_nonexistent(self):
        """Call file_is_this_type should fail if file doesn't exist."""
        self.assertRaises(HelperError,
                          ISO.file_is_this_type, "/foo/bar")

    def test_file_is_this_type_isoinfo(self):
        """The file_is_this_type API should use isoinfo if available."""
        if helpers['isoinfo']:
            self.assertTrue(ISO.file_is_this_type(self.input_iso))
            self.assertFalse(ISO.file_is_this_type(self.blank_vmdk))
        else:
            # Fake it til you make it
{'fraction_non_alphanumeric': 0.12707908750246533, 'fraction_numerical': 0.005259351784892512, 'mean_word_length': 2.406180026869682, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7290342', 'n_tokens_mistral': 4641, 'n_tokens_neox': 4479, 'n_words': 940}