Skip to content

Commit 869a174

Browse files
committed
generate test data in a pythonic way
add create_test_data to conftest remove generate_test_certificates from helper as it is on conftest already
1 parent 7201f7f commit 869a174

File tree

4 files changed

+69
-121
lines changed

4 files changed

+69
-121
lines changed

test-data/build-test-data.sh

Lines changed: 0 additions & 86 deletions
This file was deleted.

tests/conftest.py

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,19 @@
1313
from cryptography.hazmat.primitives.asymmetric import rsa
1414
from dotenv import load_dotenv
1515

16-
from .constants import TEST_DATA_DIR, GL_ROOT_DIR, CERT_DIR
17-
from .helper import call_command, spawn_background_process, generate_test_certificates
16+
from .constants import (
17+
TEST_DATA_DIR,
18+
GL_ROOT_DIR,
19+
CERT_DIR,
20+
GARDENLINUX_ROOT_DIR_EXAMPLE,
21+
TEST_COMMIT,
22+
TEST_VERSION,
23+
TEST_PLATFORMS,
24+
TEST_FEATURE_SET,
25+
TEST_FEATURE_STRINGS_SHORT,
26+
TEST_ARCHITECTURES,
27+
)
28+
from .helper import call_command, spawn_background_process
1829

1930

2031
def generate_test_certificates():
@@ -73,6 +84,48 @@ def write_zot_config(config_dict, file_path):
7384
json.dump(config_dict, config_file, indent=4)
7485

7586

87+
def create_test_data():
88+
"""Generate test data for OCI registry tests (replaces build-test-data.sh)"""
89+
print("Creating fake artifacts...")
90+
91+
# Ensure the build directory exists
92+
os.makedirs(GARDENLINUX_ROOT_DIR_EXAMPLE, exist_ok=True)
93+
94+
# Generate test artifacts for each combination
95+
for platform in TEST_PLATFORMS:
96+
for feature_string in TEST_FEATURE_STRINGS_SHORT:
97+
for arch in TEST_ARCHITECTURES:
98+
# Base name for the artifact
99+
cname = (
100+
f"{platform}-{feature_string}-{arch}-{TEST_VERSION}-{TEST_COMMIT}"
101+
)
102+
103+
print("Building mocked test data...")
104+
# Create release file with metadata
105+
release_file = f"{GARDENLINUX_ROOT_DIR_EXAMPLE}/{cname}.release"
106+
with open(release_file, "w") as f:
107+
f.write(f"GARDENLINUX_FEATURES={TEST_FEATURE_SET}\n")
108+
f.write(f"GARDENLINUX_COMMIT_ID={TEST_COMMIT}\n")
109+
110+
# Create various file formats
111+
for ext in ["raw", "tar", "qcow2", "vmdk"]:
112+
file_path = f"{GARDENLINUX_ROOT_DIR_EXAMPLE}/{cname}.{ext}"
113+
with open(file_path, "w") as f:
114+
f.write(f"dummy content for {file_path}")
115+
116+
# Create platform-specific files
117+
if platform == "gcp":
118+
for ext in ["tar.gz", "gcpimage.tar.gz"]:
119+
file_path = f"{GARDENLINUX_ROOT_DIR_EXAMPLE}/{cname}.{ext}"
120+
with open(file_path, "w") as f:
121+
f.write(f"dummy content for {file_path}")
122+
123+
if platform == "azure":
124+
file_path = f"{GARDENLINUX_ROOT_DIR_EXAMPLE}/{cname}.vhd"
125+
with open(file_path, "w") as f:
126+
f.write(f"dummy content for {file_path}")
127+
128+
76129
@pytest.fixture(autouse=False, scope="function")
77130
def zot_session():
78131
load_dotenv()
@@ -108,8 +161,9 @@ def zot_session():
108161

109162
def pytest_sessionstart(session):
110163
generate_test_certificates()
111-
call_command("./test-data/build-test-data.sh --dummy")
112-
call_command("mkdir -p manifests")
164+
# Replace the bash script call with our Python function
165+
create_test_data()
166+
os.makedirs("./manifests", exist_ok=True)
113167

114168

115169
def pytest_sessionfinish(session):
@@ -119,3 +173,5 @@ def pytest_sessionfinish(session):
119173
os.remove(CERT_DIR + "/oci-sign.key")
120174
if os.path.isdir("./manifests"):
121175
shutil.rmtree("./manifests")
176+
if os.path.isdir(GARDENLINUX_ROOT_DIR_EXAMPLE):
177+
shutil.rmtree(GARDENLINUX_ROOT_DIR_EXAMPLE)

tests/constants.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3+
from python_gardenlinux_lib.features.parse_features import get_gardenlinux_commit
4+
35
TEST_DATA_DIR = "test-data"
46
GL_ROOT_DIR = f"{TEST_DATA_DIR}/gardenlinux"
57
CERT_DIR = f"{TEST_DATA_DIR}/cert"
@@ -10,3 +12,10 @@
1012
REPO_NAME = "gardenlinux-example"
1113
CONTAINER_NAME_ZOT_EXAMPLE = f"{REGISTRY}/{REPO_NAME}"
1214
GARDENLINUX_ROOT_DIR_EXAMPLE = f"{TEST_DATA_DIR}/gardenlinux/.build"
15+
16+
TEST_PLATFORMS = ["aws", "azure", "gcp", "openstack", "openstackbaremetal", "metal"]
17+
TEST_ARCHITECTURES = ["arm64", "amd64"]
18+
TEST_FEATURE_STRINGS_SHORT = ["gardener_prod"]
19+
TEST_FEATURE_SET = "_slim,base,container"
20+
TEST_COMMIT = get_gardenlinux_commit(GL_ROOT_DIR, 8)
21+
TEST_VERSION = "1000.0"

tests/helper.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,3 @@ def call_command(cmd):
2424
except subprocess.CalledProcessError as e:
2525
error_message = e.stderr.decode("utf-8")
2626
return f"An error occurred: {error_message}"
27-
28-
29-
def generate_test_certificates():
30-
"""Generate self-signed certificates for testing"""
31-
os.makedirs(CERT_DIR, exist_ok=True)
32-
key_path = os.path.join(CERT_DIR, "oci-sign.key")
33-
cert_path = os.path.join(CERT_DIR, "oci-sign.crt")
34-
cmd = [
35-
"openssl",
36-
"req",
37-
"-x509",
38-
"-newkey",
39-
"rsa:4096",
40-
"-keyout",
41-
key_path,
42-
"-out",
43-
cert_path,
44-
"-days",
45-
"365",
46-
"-nodes",
47-
"-subj",
48-
"/CN=Garden Linux test signing key for oci",
49-
]
50-
try:
51-
subprocess.run(cmd, check=True)
52-
# Set proper permissions
53-
os.chmod(key_path, 0o600)
54-
print(f"Generated test certificates in {CERT_DIR}")
55-
except subprocess.CalledProcessError as e:
56-
print(f"Error generating certificates: {e}")
57-
raise

0 commit comments

Comments
 (0)