| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Test how secrets in files are passed to podman.""" |
| 5 |
|
| 6 |
import os |
| 7 |
import subprocess |
| 8 |
import unittest |
| 9 |
|
| 10 |
from tests.integration.test_utils import podman_compose_path |
| 11 |
from tests.integration.test_utils import test_path |
| 12 |
|
| 13 |
|
| 14 |
def compose_yaml_path(): |
| 15 |
""" "Returns the path to the compose file used for this test module""" |
| 16 |
return os.path.join(test_path(), "build_secrets") |
| 17 |
|
| 18 |
|
| 19 |
class TestComposeBuildSecrets(unittest.TestCase): |
| 20 |
def test_run_secret(self): |
| 21 |
"""podman run should receive file secrets as --volume |
| 22 |
|
| 23 |
See build_secrets/docker-compose.yaml for secret names and mount points (aka targets) |
| 24 |
|
| 25 |
""" |
| 26 |
cmd = ( |
| 27 |
"coverage", |
| 28 |
"run", |
| 29 |
podman_compose_path(), |
| 30 |
"--dry-run", |
| 31 |
"--verbose", |
| 32 |
"-f", |
| 33 |
os.path.join(compose_yaml_path(), "docker-compose.yaml"), |
| 34 |
"run", |
| 35 |
"test", |
| 36 |
) |
| 37 |
p = subprocess.run( |
| 38 |
cmd, stdout=subprocess.PIPE, check=False, stderr=subprocess.STDOUT, text=True |
| 39 |
) |
| 40 |
self.assertEqual(p.returncode, 0) |
| 41 |
secret_path = os.path.join(compose_yaml_path(), "my_secret") |
| 42 |
self.assertIn(f"--volume {secret_path}:/run/secrets/run_secret:ro,rprivate,rbind", p.stdout) |
| 43 |
self.assertIn(f"--volume {secret_path}:/tmp/run_secret2:ro,rprivate,rbind", p.stdout) |
| 44 |
|
| 45 |
def test_build_secret(self): |
| 46 |
"""podman build should receive secrets as --secret, so that they can be used inside the |
| 47 |
Dockerfile in "RUN --mount=type=secret ..." commands. |
| 48 |
|
| 49 |
""" |
| 50 |
cmd = ( |
| 51 |
"coverage", |
| 52 |
"run", |
| 53 |
podman_compose_path(), |
| 54 |
"--dry-run", |
| 55 |
"--verbose", |
| 56 |
"-f", |
| 57 |
os.path.join(compose_yaml_path(), "docker-compose.yaml"), |
| 58 |
"build", |
| 59 |
) |
| 60 |
p = subprocess.run( |
| 61 |
cmd, stdout=subprocess.PIPE, check=False, stderr=subprocess.STDOUT, text=True |
| 62 |
) |
| 63 |
self.assertEqual(p.returncode, 0) |
| 64 |
secret_path = os.path.join(compose_yaml_path(), "my_secret") |
| 65 |
|
| 66 |
self.assertIn(f"--secret id=build_secret_file,src={secret_path}", p.stdout) |
| 67 |
self.assertIn(f"--secret id=build_secret_file2,src={secret_path}", p.stdout) |
| 68 |
|
| 69 |
self.assertIn("--secret id=build_secret_env,env=MY_SECRET_VAR", p.stdout) |
| 70 |
self.assertIn("--secret id=build_secret_env2,env=MY_SECRET_VAR", p.stdout) |
| 71 |
|
| 72 |
def test_invalid_build_secret(self): |
| 73 |
"""build secrets in docker-compose file can only have a target argument without directory |
| 74 |
component |
| 75 |
|
| 76 |
""" |
| 77 |
cmd = ( |
| 78 |
"coverage", |
| 79 |
"run", |
| 80 |
podman_compose_path(), |
| 81 |
"--dry-run", |
| 82 |
"--verbose", |
| 83 |
"-f", |
| 84 |
os.path.join(compose_yaml_path(), "docker-compose.yaml.invalid"), |
| 85 |
"build", |
| 86 |
) |
| 87 |
p = subprocess.run( |
| 88 |
cmd, stdout=subprocess.PIPE, check=False, stderr=subprocess.STDOUT, text=True |
| 89 |
) |
| 90 |
self.assertNotEqual(p.returncode, 0) |
| 91 |
self.assertIn( |
| 92 |
'ValueError: ERROR: Build secret "build_secret" has invalid target "/build_secret"', |
| 93 |
p.stdout, |
| 94 |
) |