コード例 #1
0
 def test_bad_base_path_setting(self):
     """Raise exception with bogus base path settings. """
     for setting in ["path/does/not/exist", 12345, True]:
         in_toto.settings.ARTIFACT_BASE_PATH = setting
         with self.assertRaises(in_toto.exceptions.SettingsError):
             print setting
             record_artifacts_as_dict(["."])
コード例 #2
0
 def test_bad_artifact_exclude_patterns_setting(self):
     """Raise exception with bogus artifact exclude patterns settings. """
     for setting in ["not a list of settings", 12345, True]:
         in_toto.settings.ARTIFACT_EXCLUDE_PATTERNS = setting
         with self.assertRaises(in_toto.exceptions.SettingsError):
             print setting
             record_artifacts_as_dict(["."])
コード例 #3
0
    def test_record_follow_symlinked_directories(self):
        """Record files in symlinked dirs if follow_symlink_dirs is True. """

        # Link to subdir
        os.symlink("subdir", "subdir_link")

        link_pairs = [
            ("subdir/foosub1", "subdir_link/foosub1"),
            ("subdir/foosub2", "subdir_link/foosub2"),
            ("subdir/subsubdir/foosubsub", "subdir_link/subsubdir/foosubsub"),
        ]

        # Record with follow_symlink_dirs TRUE
        artifacts_dict = record_artifacts_as_dict(["."],
                                                  follow_symlink_dirs=True)
        # Test that all files were recorded including files in linked subdir ...
        self.assertListEqual(
            sorted(list(artifacts_dict.keys())),
            sorted(self.full_file_path_list + [pair[1]
                                               for pair in link_pairs]))

        # ... and the hashes of each link/file pair match
        for pair in link_pairs:
            self.assertDictEqual(artifacts_dict[pair[0]],
                                 artifacts_dict[pair[1]])

        # Record with follow_symlink_dirs FALSE (default)
        artifacts_dict = record_artifacts_as_dict(["."])
        self.assertListEqual(sorted(list(artifacts_dict.keys())),
                             sorted(self.full_file_path_list))

        os.unlink("subdir_link")
コード例 #4
0
    def test_exclude_patterns(self):
        """Test excluding artifacts using passed pattern or setting. """
        excludes_and_results = [
            # Exclude files containing 'foo' everywhere
            (["*foo*"], ["bar"]),
            # Exclude subdirectory and all its contents
            (["subdir"], ["bar", "foo"]),
            # Exclude files 'subdir/foosub1' and 'subdir/foosub2'
            (["*foosub?"], ["bar", "foo", "subdir/subsubdir/foosubsub"]),
            # Exclude subsubdirectory and its contents
            (["*subsubdir"],
             ["foo", "bar", "subdir/foosub1", "subdir/foosub2"])
        ]

        for exclude_patterns, expected_results in excludes_and_results:
            # Exclude via setting
            in_toto.settings.ARTIFACT_EXCLUDE_PATTERNS = exclude_patterns
            artifacts1 = record_artifacts_as_dict(["."])

            # Exclude via argument
            in_toto.settings.ARTIFACT_EXCLUDE_PATTERNS = None
            artifacts2 = record_artifacts_as_dict(
                ["."], exclude_patterns=exclude_patterns)

            self.assertTrue(
                sorted(list(artifacts1)) == sorted(list(artifacts2)) == sorted(
                    expected_results))
コード例 #5
0
 def test_lstrip_paths_non_unique_key(self):
     os.mkdir("subdir_new")
     path = "subdir_new/foosub1"
     shutil.copy("subdir/foosub1", path)
     lstrip_paths = ["subdir/", "subdir_new/"]
     with self.assertRaises(in_toto.exceptions.PrefixError):
         record_artifacts_as_dict(["."], lstrip_paths=lstrip_paths)
     os.remove(path)
     os.rmdir("subdir_new")
コード例 #6
0
ファイル: test_runlib.py プロジェクト: yashsriv/in-toto
    def test_bad_base_path_setting(self):
        """Raise exception with bogus base path settings. """
        for base_path in ["path/does/not/exist", 12345, True]:
            in_toto.settings.ARTIFACT_BASE_PATH = base_path
            with self.assertRaises(ValueError):
                record_artifacts_as_dict(["."])
            in_toto.settings.ARTIFACT_BASE_PATH = None

            with self.assertRaises(ValueError):
                record_artifacts_as_dict(["."], base_path=base_path)
コード例 #7
0
    def test_bad_base_path_setting(self):
        """Raise exception with bogus base path settings. """
        for base_path in ["path/does/not/exist", 12345, True]:
            in_toto.settings.ARTIFACT_BASE_PATH = base_path
            with self.assertRaises(ValueError):
                record_artifacts_as_dict(["."])
            in_toto.settings.ARTIFACT_BASE_PATH = None

            with self.assertRaises(ValueError):
                record_artifacts_as_dict(["."], base_path=base_path)

        def tearDown(self):
            """ change back to the test dir, in case any tests fail """
            os.chdir(self.test_dir)
コード例 #8
0
ファイル: test_runlib.py プロジェクト: pianomanx/in-toto
  def test_base_path_is_child_dir(self):
    """Test path of recorded artifacts and cd back with child as base."""
    base_path = "subdir"
    expected_artifacts = sorted(["foosub1", "foosub2", "subsubdir/foosubsub"])

    in_toto.settings.ARTIFACT_BASE_PATH = base_path
    artifacts_dict = record_artifacts_as_dict(["."])
    self.assertListEqual(sorted(list(artifacts_dict.keys())),
        expected_artifacts)
    in_toto.settings.ARTIFACT_BASE_PATH = None

    artifacts_dict = record_artifacts_as_dict(["."], base_path=base_path)
    self.assertListEqual(sorted(list(artifacts_dict.keys())),
        expected_artifacts)
コード例 #9
0
 def test_exclude_files_in_subdir(self):
     """Traverse dot. Exclude files in subdir but not subsubdir. """
     in_toto.settings.ARTIFACT_EXCLUDE_PATTERNS = ["*foosub?"]
     artifacts_dict = record_artifacts_as_dict(["."])
     self.assertListEqual(
         sorted(artifacts_dict.keys()),
         sorted(["bar", "foo", "subdir/subsubdir/foosubsub"]))
コード例 #10
0
 def test_lstrip_paths_valid_prefix_file(self):
     lstrip_paths = ["subdir/subsubdir/"]
     expected_artifacts = sorted(["foosubsub"])
     artifacts_dict = record_artifacts_as_dict(
         ["./subdir/subsubdir/foosubsub"], lstrip_paths=lstrip_paths)
     self.assertListEqual(sorted(list(artifacts_dict.keys())),
                          expected_artifacts)
コード例 #11
0
    def test_record_symlinked_files(self):
        """Symlinked files are always recorded. """
        # Symlinked **files** are always recorded ...
        link_pairs = [("foo", "foo_link"),
                      ("subdir/foosub1", "subdir/foosub2_link"),
                      ("subdir/subsubdir/foosubsub",
                       "subdir/subsubdir/foosubsub_link")]

        # Create links
        for pair in link_pairs:
            # We only use the basename of the file (source) as it is on the same
            # level as the link (target)
            os.symlink(os.path.basename(pair[0]), pair[1])

        # Record files and linked files
        # follow_symlink_dirs does not make a difference as it only concerns linked dirs
        for follow_symlink_dirs in [True, False]:
            artifacts_dict = record_artifacts_as_dict(
                ["."], follow_symlink_dirs=follow_symlink_dirs)

            # Test that everything was recorded ...
            self.assertListEqual(
                sorted(list(artifacts_dict.keys())),
                sorted(self.full_file_path_list +
                       [pair[1] for pair in link_pairs]))

            # ... and the hashes of each link/file pair match
            for pair in link_pairs:
                self.assertDictEqual(artifacts_dict[pair[0]],
                                     artifacts_dict[pair[1]])

        for pair in link_pairs:
            os.unlink(pair[1])
コード例 #12
0
    def test_record_without_dead_symlinks(self):
        """Dead symlinks are never recorded. """

        # Dead symlinks are never recorded ...
        links = [
            "foo_link", "subdir/foosub2_link",
            "subdir/subsubdir/foosubsub_link"
        ]

        # Create dead links
        for link in links:
            os.symlink("does/not/exist", link)

        # Record files without dead links
        # follow_symlink_dirs does not make a difference as it only concerns linked dirs
        for follow_symlink_dirs in [True, False]:
            artifacts_dict = record_artifacts_as_dict(
                ["."], follow_symlink_dirs=follow_symlink_dirs)

            # Test only the files were recorded ...
            self.assertListEqual(sorted(list(artifacts_dict.keys())),
                                 sorted(self.full_file_path_list))

        for link in links:
            os.unlink(link)
コード例 #13
0
    def test_record_dot_exclude_star_foo_star_from_recording(self):
        """Traverse dot. Exclude pattern. Record one file. """
        in_toto.settings.ARTIFACT_EXCLUDE_PATTERNS = ["*foo*"]
        artifacts_dict = record_artifacts_as_dict(["."])

        securesystemslib.formats.HASHDICT_SCHEMA.check_match(
            artifacts_dict["bar"])
        self.assertListEqual(artifacts_dict.keys(), ["bar"])
コード例 #14
0
 def test_base_path_is_child_dir(self):
     """Test path of recorded artifacts and cd back with child as base."""
     in_toto.settings.ARTIFACT_BASE_PATH = "subdir"
     artifacts_dict = record_artifacts_as_dict(["."])
     self.assertListEqual(
         sorted(artifacts_dict.keys()),
         sorted(["foosub1", "foosub2", "subsubdir/foosubsub"]))
     self.assertEquals(os.getcwd(), self.test_dir)
コード例 #15
0
ファイル: test_runlib.py プロジェクト: pianomanx/in-toto
 def test_lstrip_paths_invalid_prefix_directory(self):
   lstrip_paths = ["not/a/directory/"]
   expected_artifacts = sorted(["bar", "foo", "subdir/foosub1",
                                "subdir/foosub2", "subdir/subsubdir/foosubsub"])
   artifacts_dict = record_artifacts_as_dict(["."],
       lstrip_paths=lstrip_paths)
   self.assertListEqual(sorted(list(artifacts_dict.keys())),
                        expected_artifacts)
コード例 #16
0
    def test_record_dot_check_files_hash_dict_schema(self):
        """Traverse dir and subdirs. Record three files. """
        artifacts_dict = record_artifacts_as_dict(["."])

        for key, val in six.iteritems(artifacts_dict):
            securesystemslib.formats.HASHDICT_SCHEMA.check_match(val)

        self.assertListEqual(sorted(list(artifacts_dict.keys())),
                             sorted(self.full_file_path_list))
コード例 #17
0
ファイル: test_runlib.py プロジェクト: pianomanx/in-toto
  def test_record_files_and_subdirs(self):
    """Explicitly record files and subdirs. """
    artifacts_dict = record_artifacts_as_dict(["foo", "subdir"])

    for val in list(artifacts_dict.values()):
      securesystemslib.formats.HASHDICT_SCHEMA.check_match(val)

    self.assertListEqual(sorted(list(artifacts_dict.keys())),
      sorted(["foo", "subdir/foosub1", "subdir/foosub2",
          "subdir/subsubdir/foosubsub"]))
コード例 #18
0
 def test_base_path_is_parent_dir(self):
     """Test path of recorded artifacts and cd back with parent as base. """
     in_toto.settings.ARTIFACT_BASE_PATH = ".."
     os.chdir("subdir/subsubdir")
     artifacts_dict = record_artifacts_as_dict(["."])
     self.assertListEqual(
         sorted(artifacts_dict.keys()),
         sorted(["foosub1", "foosub2", "subsubdir/foosubsub"]))
     self.assertEquals(os.getcwd(),
                       os.path.join(self.test_dir, "subdir/subsubdir"))
     os.chdir(self.test_dir)
コード例 #19
0
    def test_exclude_subsubdir(self):
        """Traverse dot. Exclude subsubdir. """
        in_toto.settings.ARTIFACT_EXCLUDE_PATTERNS = ["*subsubdir"]
        artifacts_dict = record_artifacts_as_dict(["."])

        for key, val in artifacts_dict.iteritems():
            securesystemslib.formats.HASHDICT_SCHEMA.check_match(val)

        self.assertListEqual(
            sorted(artifacts_dict.keys()),
            sorted(["foo", "bar", "subdir/foosub1", "subdir/foosub2"]))
コード例 #20
0
    def test_record_dot_check_files_hash_dict_schema(self):
        """Traverse dir and subdirs. Record three files. """
        artifacts_dict = record_artifacts_as_dict(["."])

        for key, val in artifacts_dict.iteritems():
            securesystemslib.formats.HASHDICT_SCHEMA.check_match(val)

        self.assertListEqual(
            sorted(artifacts_dict.keys()),
            sorted([
                "foo", "bar", "subdir/foosub1", "subdir/foosub2",
                "subdir/subsubdir/foosubsub"
            ]))
コード例 #21
0
    def test_lstrip_paths_valid_unicode_prefix_file(self):
        # Try to create a file with unicode character
        try:
            os.mkdir("ಠ")
            path = "ಠ/foo"
            shutil.copy("foo", path)

            # Attempt to left strip the path now that the file has been created
            lstrip_paths = ["ಠ/"]
            expected_artifacts = sorted(["foo"])
            artifacts_dict = record_artifacts_as_dict(
                ["./ಠ/"], lstrip_paths=lstrip_paths)
            self.assertListEqual(sorted(list(artifacts_dict.keys())),
                                 expected_artifacts)
            os.remove(path)
            os.rmdir("ಠ")
        except OSError:
            # OS doesn't support unicode explicit files
            pass
コード例 #22
0
 def test_not_existing_artifacts_in_list_record_nothing(self):
     """List with not existing artifact passed. Return empty dict. """
     self.assertDictEqual(record_artifacts_as_dict(["baz"]), {})
コード例 #23
0
 def test_empty_artifacts_list_record_nothing(self):
     """Empty list passed. Return empty dict. """
     self.assertDictEqual(record_artifacts_as_dict([]), {})
コード例 #24
0
 def test_exclude_subdir(self):
     """Traverse dot. Exclude subdir (and subsubdir). """
     in_toto.settings.ARTIFACT_EXCLUDE_PATTERNS = ["*subdir"]
     artifacts_dict = record_artifacts_as_dict(["."])
     self.assertListEqual(sorted(artifacts_dict.keys()),
                          sorted(["bar", "foo"]))
コード例 #25
0
 def test_lstrip_paths_substring_prefix_directory(self):
     lstrip_paths = ["subdir/subsubdir/", "subdir/"]
     with self.assertRaises(in_toto.exceptions.PrefixError):
         record_artifacts_as_dict(["."], lstrip_paths=lstrip_paths)