|
1 | 1 | import pytest |
2 | | -import boto3 |
3 | 2 | from pathlib import Path |
4 | 3 | from tempfile import TemporaryDirectory |
5 | | -from hashlib import md5, sha256 |
6 | | -from moto import mock_aws |
7 | 4 |
|
8 | 5 | from gardenlinux.s3.s3_artifacts import S3Artifacts |
9 | 6 |
|
10 | 7 | CNAME = "testcname" |
11 | 8 |
|
12 | 9 |
|
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 | | - |
54 | 10 | def test_s3artifacts_init_success(s3_setup): |
55 | 11 | # Arrange |
56 | 12 | _, bucket_name, _ = s3_setup |
|
0 commit comments