Skip to content

Commit

Permalink
Remove obsolete prompt test patching
Browse files Browse the repository at this point in the history
Remove 'with' statements that patch the password prompt return
value in test cases that would not prompt for a password anyways.

These were added in in-toto#209, but neither commit message nor the PR
page indicate why.

Signed-off-by: Lukas Puehringer <[email protected]>
  • Loading branch information
lukpueh committed Oct 20, 2020
1 parent 4e227e2 commit 28a3ed5
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 140 deletions.
173 changes: 83 additions & 90 deletions tests/test_in_toto_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,90 +69,87 @@ def tearDownClass(self):
def test_start_stop(self):
"""Test CLI command record start/stop with various arguments. """

# Give wrong password whenever prompted.
with mock.patch('in_toto.util.prompt_password', return_value='x'):

# Start/stop recording using rsa key
args = ["--step-name", "test1", "--key", self.rsa_key_path]
self.assert_cli_sys_exit(["start"] + args, 0)
self.assert_cli_sys_exit(["stop"] + args, 0)

# Start/stop with recording one artifact using rsa key
args = ["--step-name", "test2", "--key", self.rsa_key_path]
self.assert_cli_sys_exit(["start"] + args + ["--materials",
self.test_artifact1], 0)
self.assert_cli_sys_exit(["stop"] + args + ["--products",
self.test_artifact1], 0)

# Start/stop with excluding one artifact using rsa key
args = ["--step-name", "test2.5", "--key", self.rsa_key_path]
self.assert_cli_sys_exit(["start"] + args + ["--materials",
self.test_artifact1, "--exclude", "test*"], 0)
self.assert_cli_sys_exit(["stop"] + args + ["--products",
self.test_artifact1, "--exclude", "test*"], 0)

# Start/stop with base-path using rsa key
args = ["--step-name", "test2.6", "--key", self.rsa_key_path, "--base-path",
self.test_dir]
self.assert_cli_sys_exit(["start"] + args, 0)
self.assert_cli_sys_exit(["stop"] + args, 0)

# Start/stop with recording multiple artifacts using rsa key
args = ["--step-name", "test3", "--key", self.rsa_key_path]
self.assert_cli_sys_exit(["start"] + args + ["--materials",
self.test_artifact1, self.test_artifact2], 0)
self.assert_cli_sys_exit(["stop"] + args + ["--products",
self.test_artifact2, self.test_artifact2], 0)

# Start/stop recording using ed25519 key
args = ["--step-name", "test4", "--key", self.ed25519_key_path, "--key-type", "ed25519"]
self.assert_cli_sys_exit(["start"] + args, 0)
self.assert_cli_sys_exit(["stop"] + args, 0)

# Start/stop with recording one artifact using ed25519 key
args = ["--step-name", "test5", "--key", self.ed25519_key_path, "--key-type", "ed25519"]
self.assert_cli_sys_exit(["start"] + args + ["--materials",
self.test_artifact1], 0)
self.assert_cli_sys_exit(["stop"] + args + ["--products",
self.test_artifact1], 0)

# Start/stop with excluding one artifact using ed25519 key
args = ["--step-name", "test5.5", "--key", self.ed25519_key_path, "--key-type", "ed25519"]
self.assert_cli_sys_exit(["start"] + args + ["--materials",
self.test_artifact1, "--exclude", "test*"], 0)
self.assert_cli_sys_exit(["stop"] + args + ["--products",
self.test_artifact1, "--exclude", "test*"], 0)

# Start/stop with base-path using ed25519 key
args = ["--step-name", "test5.6", "--key", self.ed25519_key_path,
"--key-type", "ed25519", "--base-path", self.test_dir]
self.assert_cli_sys_exit(["start"] + args, 0)
self.assert_cli_sys_exit(["stop"] + args, 0)

# Start/stop with recording multiple artifacts using ed25519 key
args = ["--step-name", "test6", "--key", self.ed25519_key_path, "--key-type", "ed25519"]
self.assert_cli_sys_exit(["start"] + args + ["--materials",
self.test_artifact1, self.test_artifact2], 0)
self.assert_cli_sys_exit(["stop"] + args + ["--products",
self.test_artifact2, self.test_artifact2], 0)

# Start/stop sign with specified gpg keyid
args = ["--step-name", "test7", "--gpg", self.gpg_key_768C43, "--gpg-home",
self.gnupg_home]
self.assert_cli_sys_exit(["start"] + args, 0)
self.assert_cli_sys_exit(["stop"] + args, 0)

# Start/stop sign with default gpg keyid
args = ["--step-name", "test8", "--gpg", "--gpg-home", self.gnupg_home]
self.assert_cli_sys_exit(["start"] + args, 0)
self.assert_cli_sys_exit(["stop"] + args, 0)

# Start/stop sign with metadata directory
args = ["--step-name", "test9", "--key", self.rsa_key_path]
tmp_dir = os.path.realpath(tempfile.mkdtemp(dir=os.getcwd()))
metadata_directory_arg = ["--metadata-directory", tmp_dir]
self.assert_cli_sys_exit(["start"] + args, 0)
self.assert_cli_sys_exit(["stop"] + metadata_directory_arg + args, 0)
# Start/stop recording using rsa key
args = ["--step-name", "test1", "--key", self.rsa_key_path]
self.assert_cli_sys_exit(["start"] + args, 0)
self.assert_cli_sys_exit(["stop"] + args, 0)

# Start/stop with recording one artifact using rsa key
args = ["--step-name", "test2", "--key", self.rsa_key_path]
self.assert_cli_sys_exit(["start"] + args + ["--materials",
self.test_artifact1], 0)
self.assert_cli_sys_exit(["stop"] + args + ["--products",
self.test_artifact1], 0)

# Start/stop with excluding one artifact using rsa key
args = ["--step-name", "test2.5", "--key", self.rsa_key_path]
self.assert_cli_sys_exit(["start"] + args + ["--materials",
self.test_artifact1, "--exclude", "test*"], 0)
self.assert_cli_sys_exit(["stop"] + args + ["--products",
self.test_artifact1, "--exclude", "test*"], 0)

# Start/stop with base-path using rsa key
args = ["--step-name", "test2.6", "--key", self.rsa_key_path, "--base-path",
self.test_dir]
self.assert_cli_sys_exit(["start"] + args, 0)
self.assert_cli_sys_exit(["stop"] + args, 0)

# Start/stop with recording multiple artifacts using rsa key
args = ["--step-name", "test3", "--key", self.rsa_key_path]
self.assert_cli_sys_exit(["start"] + args + ["--materials",
self.test_artifact1, self.test_artifact2], 0)
self.assert_cli_sys_exit(["stop"] + args + ["--products",
self.test_artifact2, self.test_artifact2], 0)

# Start/stop recording using ed25519 key
args = ["--step-name", "test4", "--key", self.ed25519_key_path, "--key-type", "ed25519"]
self.assert_cli_sys_exit(["start"] + args, 0)
self.assert_cli_sys_exit(["stop"] + args, 0)

# Start/stop with recording one artifact using ed25519 key
args = ["--step-name", "test5", "--key", self.ed25519_key_path, "--key-type", "ed25519"]
self.assert_cli_sys_exit(["start"] + args + ["--materials",
self.test_artifact1], 0)
self.assert_cli_sys_exit(["stop"] + args + ["--products",
self.test_artifact1], 0)

# Start/stop with excluding one artifact using ed25519 key
args = ["--step-name", "test5.5", "--key", self.ed25519_key_path, "--key-type", "ed25519"]
self.assert_cli_sys_exit(["start"] + args + ["--materials",
self.test_artifact1, "--exclude", "test*"], 0)
self.assert_cli_sys_exit(["stop"] + args + ["--products",
self.test_artifact1, "--exclude", "test*"], 0)

# Start/stop with base-path using ed25519 key
args = ["--step-name", "test5.6", "--key", self.ed25519_key_path,
"--key-type", "ed25519", "--base-path", self.test_dir]
self.assert_cli_sys_exit(["start"] + args, 0)
self.assert_cli_sys_exit(["stop"] + args, 0)

# Start/stop with recording multiple artifacts using ed25519 key
args = ["--step-name", "test6", "--key", self.ed25519_key_path, "--key-type", "ed25519"]
self.assert_cli_sys_exit(["start"] + args + ["--materials",
self.test_artifact1, self.test_artifact2], 0)
self.assert_cli_sys_exit(["stop"] + args + ["--products",
self.test_artifact2, self.test_artifact2], 0)

# Start/stop sign with specified gpg keyid
args = ["--step-name", "test7", "--gpg", self.gpg_key_768C43, "--gpg-home",
self.gnupg_home]
self.assert_cli_sys_exit(["start"] + args, 0)
self.assert_cli_sys_exit(["stop"] + args, 0)

# Start/stop sign with default gpg keyid
args = ["--step-name", "test8", "--gpg", "--gpg-home", self.gnupg_home]
self.assert_cli_sys_exit(["start"] + args, 0)
self.assert_cli_sys_exit(["stop"] + args, 0)

# Start/stop sign with metadata directory
args = ["--step-name", "test9", "--key", self.rsa_key_path]
tmp_dir = os.path.realpath(tempfile.mkdtemp(dir=os.getcwd()))
metadata_directory_arg = ["--metadata-directory", tmp_dir]
self.assert_cli_sys_exit(["start"] + args, 0)
self.assert_cli_sys_exit(["stop"] + metadata_directory_arg + args, 0)


def test_glob_no_unfinished_files(self):
Expand Down Expand Up @@ -187,14 +184,10 @@ def test_no_key(self):
def test_missing_unfinished_link(self):
"""Error exit with missing unfinished link file. """
args = ["--step-name", "no-link", "--key", self.rsa_key_path]
# Give wrong password whenever prompted.
with mock.patch('in_toto.util.prompt_password', return_value='x'):
self.assert_cli_sys_exit(["stop"] + args, 1)
self.assert_cli_sys_exit(["stop"] + args, 1)

args = ["--step-name", "no-link", "--key", self.ed25519_key_path, "--key-type", "ed25519"]
# Give wrong password whenever prompted.
with mock.patch('in_toto.util.prompt_password', return_value='x'):
self.assert_cli_sys_exit(["stop"] + args, 1)
self.assert_cli_sys_exit(["stop"] + args, 1)


if __name__ == '__main__':
Expand Down
91 changes: 41 additions & 50 deletions tests/test_in_toto_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ def test_main_required_args(self):
args = ["--step-name", self.test_step, "--key", self.rsa_key_path, "--",
"python", "--version"]

# Give wrong password whenever prompted.
with mock.patch('in_toto.util.prompt_password', return_value='x'):
self.assert_cli_sys_exit(args, 0)
self.assert_cli_sys_exit(args, 0)

self.assertTrue(os.path.exists(self.test_link_rsa))

Expand All @@ -98,47 +96,44 @@ def test_main_optional_args(self):
self.test_artifact, "--record-streams"]
positional_args = ["--", "python", "--version"]

# Give wrong password whenever prompted.
with mock.patch('in_toto.util.prompt_password', return_value='x'):

# Test and assert recorded artifacts
args1 = named_args + positional_args
self.assert_cli_sys_exit(args1, 0)
link_metadata = Metablock.load(self.test_link_rsa)
self.assertTrue(self.test_artifact in
list(link_metadata.signed.materials.keys()))
self.assertTrue(self.test_artifact in
list(link_metadata.signed.products.keys()))

# Test and assert exlcuded artifacts
args2 = named_args + ["--exclude", "*test*"] + positional_args
self.assert_cli_sys_exit(args2, 0)
link_metadata = Metablock.load(self.test_link_rsa)
self.assertFalse(link_metadata.signed.materials)
self.assertFalse(link_metadata.signed.products)

# Test with base path
args3 = named_args + ["--base-path", self.test_dir] + positional_args
self.assert_cli_sys_exit(args3, 0)
link_metadata = Metablock.load(self.test_link_rsa)
self.assertListEqual(list(link_metadata.signed.materials.keys()),
[self.test_artifact])
self.assertListEqual(list(link_metadata.signed.products.keys()),
[self.test_artifact])

# Test with bogus base path
args4 = named_args + ["--base-path", "bogus/path"] + positional_args
self.assert_cli_sys_exit(args4, 1)

# Test with lstrip path
strip_prefix = self.test_artifact[:-1]
args5 = named_args + ["--lstrip-paths", strip_prefix] + positional_args
self.assert_cli_sys_exit(args5, 0)
link_metadata = Metablock.load(self.test_link_rsa)
self.assertListEqual(list(link_metadata.signed.materials.keys()),
[self.test_artifact[len(strip_prefix):]])
self.assertListEqual(list(link_metadata.signed.products.keys()),
[self.test_artifact[len(strip_prefix):]])
# Test and assert recorded artifacts
args1 = named_args + positional_args
self.assert_cli_sys_exit(args1, 0)
link_metadata = Metablock.load(self.test_link_rsa)
self.assertTrue(self.test_artifact in
list(link_metadata.signed.materials.keys()))
self.assertTrue(self.test_artifact in
list(link_metadata.signed.products.keys()))

# Test and assert exlcuded artifacts
args2 = named_args + ["--exclude", "*test*"] + positional_args
self.assert_cli_sys_exit(args2, 0)
link_metadata = Metablock.load(self.test_link_rsa)
self.assertFalse(link_metadata.signed.materials)
self.assertFalse(link_metadata.signed.products)

# Test with base path
args3 = named_args + ["--base-path", self.test_dir] + positional_args
self.assert_cli_sys_exit(args3, 0)
link_metadata = Metablock.load(self.test_link_rsa)
self.assertListEqual(list(link_metadata.signed.materials.keys()),
[self.test_artifact])
self.assertListEqual(list(link_metadata.signed.products.keys()),
[self.test_artifact])

# Test with bogus base path
args4 = named_args + ["--base-path", "bogus/path"] + positional_args
self.assert_cli_sys_exit(args4, 1)

# Test with lstrip path
strip_prefix = self.test_artifact[:-1]
args5 = named_args + ["--lstrip-paths", strip_prefix] + positional_args
self.assert_cli_sys_exit(args5, 0)
link_metadata = Metablock.load(self.test_link_rsa)
self.assertListEqual(list(link_metadata.signed.materials.keys()),
[self.test_artifact[len(strip_prefix):]])
self.assertListEqual(list(link_metadata.signed.products.keys()),
[self.test_artifact[len(strip_prefix):]])


def test_main_with_metadata_directory(self):
Expand All @@ -147,9 +142,7 @@ def test_main_with_metadata_directory(self):
args = ["--step-name", self.test_step, "--key", self.rsa_key_path,
"--metadata-directory", tmp_dir, "--", "python", "--version"]

# Give wrong password whenever prompted.
with mock.patch('in_toto.util.prompt_password', return_value='x'):
self.assert_cli_sys_exit(args, 0)
self.assert_cli_sys_exit(args, 0)

linkpath = os.path.join(tmp_dir, self.test_link_rsa)

Expand Down Expand Up @@ -215,9 +208,7 @@ def test_main_no_command_arg(self):
args = ["in_toto_run.py", "--step-name", self.test_step, "--key",
self.rsa_key_path, "--no-command"]

# Give wrong password whenever prompted.
with mock.patch('in_toto.util.prompt_password', return_value='x'):
self.assert_cli_sys_exit(args, 0)
self.assert_cli_sys_exit(args, 0)

self.assertTrue(os.path.exists(self.test_link_rsa))

Expand Down

0 comments on commit 28a3ed5

Please sign in to comment.