Ejemplo n.º 1
0
 def test_exists_relative(self):
     """Test the exists property when initialized with relative path."""
     os.chdir(os.path.dirname(self.tarfile))
     relative_ref = FileInTAR(os.path.basename(self.tarfile),
                              "sample_cfg.txt")
     self.assertTrue(relative_ref.exists)
     self.assertLogged(**self.FILE_REF_RELATIVE)
Ejemplo n.º 2
0
 def setUp(self):
     """Test case setup function called automatically prior to each test."""
     super(TestFileInTAR, self).setUp()
     self.tarfile = resource_filename(__name__, "test.tar")
     self.valid_ref = FileInTAR(self.tarfile, "sample_cfg.txt")
Ejemplo n.º 3
0
class TestFileInTAR(COTTestCase):
    """Test cases for FileInTAR class."""

    def setUp(self):
        """Test case setup function called automatically prior to each test."""
        super(TestFileInTAR, self).setUp()
        self.tarfile = resource_filename(__name__, "test.tar")
        self.valid_ref = FileInTAR(self.tarfile, "sample_cfg.txt")

    def test_nonexistent_tarfile(self):
        """Test error handling when TAR file doesn't exist."""
        self.assertRaises(IOError, FileInTAR, "/foo/bar", "filename")

    def test_nonexistent_entry(self):
        """Test error handling when filename isn't in the TAR."""
        self.assertRaises(IOError, FileInTAR, self.tarfile, "foo.bar")

    def test_not_tarfile(self):
        """Test error handling when file is not a TAR file."""
        self.assertRaises(IOError, FileInTAR, self.input_ovf, self.input_ovf)

    def test_exists(self):
        """Test the exists property."""
        self.assertTrue(self.valid_ref.exists)
        # false case is covered in test_nonexistent_entry

    def test_exists_relative(self):
        """Test the exists property when initialized with relative path."""
        os.chdir(os.path.dirname(self.tarfile))
        relative_ref = FileInTAR(os.path.basename(self.tarfile),
                                 "sample_cfg.txt")
        self.assertTrue(relative_ref.exists)
        self.assertLogged(**self.FILE_REF_RELATIVE)

    def test_size(self):
        """Test the size property."""
        self.assertEqual(self.valid_ref.size,
                         os.path.getsize(resource_filename(__name__,
                                                           'sample_cfg.txt')))

    def test_open(self):
        """Test the open() API."""
        # open() only supports r/rb mode
        with self.assertRaises(ValueError):
            with self.valid_ref.open('w') as obj:
                assert "Should never get here"
        with self.assertRaises(ValueError):
            with self.valid_ref.open('a') as obj:
                assert "Should never get here"

        with self.valid_ref.open('r') as obj:
            # Check that file contents are as expected
            self.assertEqual(obj.readline(), b'!\n')
            self.assertEqual(obj.readline(),
                             b'interface GigabitEthernet0/0/0/0\n')
        # obj should be closed now
        self.assertRaises(ValueError, obj.read)

    def test_copy_to(self):
        """Test the copy_to() API."""
        self.valid_ref.copy_to(self.temp_dir)
        self.check_diff("",
                        file1=resource_filename(__name__, 'sample_cfg.txt'),
                        file2=os.path.join(self.temp_dir, 'sample_cfg.txt'))

    def test_add_to_archive(self):
        """Test the add_to_archive() API."""
        output_tarfile = os.path.join(self.temp_dir, 'test_output.tar')
        with tarfile.open(output_tarfile, 'w') as tarf:
            self.valid_ref.add_to_archive(tarf)
        with tarfile.open(output_tarfile, 'r') as tarf:
            tarf.extract('sample_cfg.txt', self.temp_dir)
        self.check_diff("",
                        file1=resource_filename(__name__, 'sample_cfg.txt'),
                        file2=os.path.join(self.temp_dir, 'sample_cfg.txt'))
Ejemplo n.º 4
0
class TestFileInTAR(COT_UT):

    """Test cases for FileInTAR class."""

    def setUp(self):
        """Test case setup function called automatically prior to each test."""
        super(TestFileInTAR, self).setUp()
        self.tarfile = resource_filename(__name__, "test.tar")
        self.valid_ref = FileInTAR(self.tarfile, "sample_cfg.txt")

    def test_nonexistent_tarfile(self):
        """Test error handling when TAR file doesn't exist."""
        self.assertRaises(IOError, FileInTAR, "/foo/bar", "filename")

    def test_nonexistent_entry(self):
        """Test error handling when filename isn't in the TAR."""
        self.assertRaises(IOError, FileInTAR, self.tarfile, "foo.bar")

    def test_not_tarfile(self):
        """Test error handling when file is not a TAR file."""
        self.assertRaises(IOError, FileInTAR, self.input_ovf, self.input_ovf)

    def test_exists(self):
        """Test the exists() API."""
        self.assertTrue(self.valid_ref.exists())
        # false case is covered in test_nonexistent_entry

    def test_size(self):
        """Test the size() API."""
        self.assertEqual(self.valid_ref.size(),
                         os.path.getsize(resource_filename(__name__,
                                                           'sample_cfg.txt')))

    def test_open_close(self):
        """Test the open() and close() APIs."""
        # open() only supports r/rb mode
        self.assertRaises(ValueError, self.valid_ref.open, 'w')
        self.assertRaises(ValueError, self.valid_ref.open, 'a')

        # No-op:
        self.valid_ref.close()

        obj = self.valid_ref.open('r')
        # Check that file contents are as expected
        self.assertEqual(obj.readline(), b'!\n')
        self.assertEqual(obj.readline(), b'interface GigabitEthernet0/0/0/0\n')
        # TODO: this should clean up nicely
        obj.close()
        self.valid_ref.close()
        self.assertRaises(ValueError, obj.read)
        # No-op:
        self.valid_ref.close()

    def test_copy_to(self):
        """Test the copy_to() API."""
        self.valid_ref.copy_to(self.temp_dir)
        self.check_diff("",
                        file1=resource_filename(__name__, 'sample_cfg.txt'),
                        file2=os.path.join(self.temp_dir, 'sample_cfg.txt'))

    def test_add_to_archive(self):
        """Test the add_to_archive() API."""
        output_tarfile = os.path.join(self.temp_dir, 'test_output.tar')
        with closing(tarfile.open(output_tarfile, 'w')) as tarf:
            self.valid_ref.add_to_archive(tarf)
        with closing(tarfile.open(output_tarfile, 'r')) as tarf:
            tarf.extract('sample_cfg.txt', self.temp_dir)
        self.check_diff("",
                        file1=resource_filename(__name__, 'sample_cfg.txt'),
                        file2=os.path.join(self.temp_dir, 'sample_cfg.txt'))
Ejemplo n.º 5
0
class TestFileInTAR(COTTestCase):
    """Test cases for FileInTAR class."""

    def setUp(self):
        """Test case setup function called automatically prior to each test."""
        super(TestFileInTAR, self).setUp()
        self.tarfile = resource_filename(__name__, "test.tar")
        self.valid_ref = FileInTAR(self.tarfile, "sample_cfg.txt")

    def test_nonexistent_tarfile(self):
        """Test error handling when TAR file doesn't exist."""
        self.assertRaises(IOError, FileInTAR, "/foo/bar", "filename")

    def test_nonexistent_entry(self):
        """Test error handling when filename isn't in the TAR."""
        self.assertRaises(IOError, FileInTAR, self.tarfile, "foo.bar")

    def test_not_tarfile(self):
        """Test error handling when file is not a TAR file."""
        self.assertRaises(IOError, FileInTAR, self.input_ovf, self.input_ovf)

    def test_exists(self):
        """Test the exists property."""
        self.assertTrue(self.valid_ref.exists)
        # false case is covered in test_nonexistent_entry

    def test_size(self):
        """Test the size property."""
        self.assertEqual(self.valid_ref.size,
                         os.path.getsize(resource_filename(__name__,
                                                           'sample_cfg.txt')))

    def test_open_close(self):
        """Test the open() and close() APIs."""
        # open() only supports r/rb mode
        self.assertRaises(ValueError, self.valid_ref.open, 'w')
        self.assertRaises(ValueError, self.valid_ref.open, 'a')

        # No-op:
        self.valid_ref.close()

        obj = self.valid_ref.open('r')
        # Check that file contents are as expected
        self.assertEqual(obj.readline(), b'!\n')
        self.assertEqual(obj.readline(), b'interface GigabitEthernet0/0/0/0\n')
        # TODO: this should clean up nicely
        obj.close()
        self.valid_ref.close()
        self.assertRaises(ValueError, obj.read)
        # No-op:
        self.valid_ref.close()

    def test_copy_to(self):
        """Test the copy_to() API."""
        self.valid_ref.copy_to(self.temp_dir)
        self.check_diff("",
                        file1=resource_filename(__name__, 'sample_cfg.txt'),
                        file2=os.path.join(self.temp_dir, 'sample_cfg.txt'))

    def test_add_to_archive(self):
        """Test the add_to_archive() API."""
        output_tarfile = os.path.join(self.temp_dir, 'test_output.tar')
        with closing(tarfile.open(output_tarfile, 'w')) as tarf:
            self.valid_ref.add_to_archive(tarf)
        with closing(tarfile.open(output_tarfile, 'r')) as tarf:
            tarf.extract('sample_cfg.txt', self.temp_dir)
        self.check_diff("",
                        file1=resource_filename(__name__, 'sample_cfg.txt'),
                        file2=os.path.join(self.temp_dir, 'sample_cfg.txt'))

    def test_equality(self):
        """Test the __eq__ and __ne__ operators."""
        same_ref = FileInTAR(self.tarfile, "sample_cfg.txt")
        self.assertEqual(self.valid_ref, same_ref)
        another_ref = FileInTAR(self.tarfile, "input.mf")
        self.assertNotEqual(self.valid_ref, another_ref)
Ejemplo n.º 6
0
 def test_equality(self):
     """Test the __eq__ and __ne__ operators."""
     same_ref = FileInTAR(self.tarfile, "sample_cfg.txt")
     self.assertEqual(self.valid_ref, same_ref)
     another_ref = FileInTAR(self.tarfile, "input.mf")
     self.assertNotEqual(self.valid_ref, another_ref)