Skip to content

Commit 1c490eb

Browse files
committed
Tests: Split fixtures and mocks into conftest
1 parent a4190b9 commit 1c490eb

File tree

2 files changed

+46
-44
lines changed

2 files changed

+46
-44
lines changed

tests/s3/conftest.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import pytest
2+
from pathlib import Path
3+
from moto import mock_aws
4+
from hashlib import md5, sha256
5+
import boto3
6+
7+
8+
# Dummy CName replacement
9+
class DummyCName:
10+
def __init__(self, cname): # pylint: disable=unused-argument
11+
self.platform = "aws"
12+
self.arch = "amd64"
13+
self.version = "1234.1"
14+
self.commit_id = "abc123"
15+
16+
17+
# Helpers to compute digests for fake files
18+
def dummy_digest(data: bytes, algo: str) -> str:
19+
"""
20+
Dummy for file_digest() to compute hashes for in-memory byte streams
21+
"""
22+
content = data.read()
23+
data.seek(0) # Reset byte cursor to start for multiple uses
24+
25+
if algo == "md5":
26+
return md5(content) # nosec B324
27+
elif algo == "sha256":
28+
return sha256(content)
29+
else:
30+
raise ValueError(f"Unsupported algo: {algo}")
31+
32+
33+
@pytest.fixture(autouse=True)
34+
def s3_setup(tmp_path, monkeypatch):
35+
"""
36+
Provides a clean S3 setup for each test.
37+
"""
38+
with mock_aws():
39+
s3 = boto3.resource("s3", region_name="us-east-1")
40+
bucket_name = "test-bucket"
41+
s3.create_bucket(Bucket=bucket_name)
42+
43+
monkeypatch.setattr("gardenlinux.s3.s3_artifacts.CName", DummyCName)
44+
monkeypatch.setattr("gardenlinux.s3.s3_artifacts.file_digest", dummy_digest)
45+
46+
yield s3, bucket_name, tmp_path

tests/s3/test_s3_artifacts.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,12 @@
11
import pytest
2-
import boto3
32
from pathlib import Path
43
from tempfile import TemporaryDirectory
5-
from hashlib import md5, sha256
6-
from moto import mock_aws
74

85
from gardenlinux.s3.s3_artifacts import S3Artifacts
96

107
CNAME = "testcname"
118

129

13-
# Dummy CName replacement
14-
class DummyCName:
15-
def __init__(self, cname): # pylint: disable=unused-argument
16-
self.platform = "aws"
17-
self.arch = "amd64"
18-
self.version = "1234.1"
19-
self.commit_id = "abc123"
20-
21-
22-
# Helpers to compute digests for fake files
23-
def dummy_digest(data: bytes, algo: str) -> str:
24-
"""
25-
Dummy for file_digest() to compute hashes for in-memory byte streams
26-
"""
27-
content = data.read()
28-
data.seek(0) # Reset byte cursor to start for multiple uses
29-
30-
if algo == "md5":
31-
return md5(content) # nosec B324
32-
elif algo == "sha256":
33-
return sha256(content)
34-
else:
35-
raise ValueError(f"Unsupported algo: {algo}")
36-
37-
38-
@pytest.fixture(autouse=True)
39-
def s3_setup(tmp_path, monkeypatch):
40-
"""
41-
Provides a clean S3 setup for each test.
42-
"""
43-
with mock_aws():
44-
s3 = boto3.resource("s3", region_name="us-east-1")
45-
bucket_name = "test-bucket"
46-
s3.create_bucket(Bucket=bucket_name)
47-
48-
monkeypatch.setattr("gardenlinux.s3.s3_artifacts.CName", DummyCName)
49-
monkeypatch.setattr("gardenlinux.s3.s3_artifacts.file_digest", dummy_digest)
50-
51-
yield s3, bucket_name, tmp_path
52-
53-
5410
def test_s3artifacts_init_success(s3_setup):
5511
# Arrange
5612
_, bucket_name, _ = s3_setup

0 commit comments

Comments
 (0)