Skip to content

Commit

Permalink
Fix bug and add tests for it in validate region or endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
javierdelapuente committed Jan 19, 2024
1 parent 8d11c61 commit 789988d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src-docs/backup.py.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,25 @@ Configuration for accessing S3 bucket.

<a href="../src/backup.py#L38"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>classmethod</kbd> `check_region_or_endpoint_set`
### <kbd>classmethod</kbd> `check_endpoint_or_region_set`

```python
check_region_or_endpoint_set(region: str, values: dict[str, Any]) → str
check_endpoint_or_region_set(endpoint: str, values: dict[str, Any]) → str
```

Validate that either that region or endpoint is set.
Validate that either region or endpoint is set.



**Args:**

- <b>`region`</b>: region attribute
- <b>`endpoint`</b>: endpoint attribute
- <b>`values`</b>: all attributes in S3 configuration



**Returns:**
value of the region attribute
value of the endpoint attribute



Expand Down
16 changes: 8 additions & 8 deletions src/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,31 @@ class S3Parameters(BaseModel):

access_key: str = Field(alias="access-key")
secret_key: str = Field(alias="secret-key")
region: Optional[str] = Field(default=None)
region: Optional[str]
bucket: str
endpoint: Optional[str]
path: str = Field(default="")
s3_uri_style: str = Field(alias="s3-uri-style", default="host")

@validator("region", always=True)
@validator("endpoint", always=True)
@classmethod
def check_region_or_endpoint_set(cls, region: str, values: dict[str, Any]) -> str:
"""Validate that either that region or endpoint is set.
def check_endpoint_or_region_set(cls, endpoint: str, values: dict[str, Any]) -> str:
"""Validate that either region or endpoint is set.
Args:
region: region attribute
endpoint: endpoint attribute
values: all attributes in S3 configuration
Returns:
value of the region attribute
value of the endpoint attribute
Raises:
ValueError: if the configuration is invalid.
"""
endpoint = values.get("endpoint")
region = values.get("region")
if not region and not endpoint:
raise ValueError('one of "region" or "endpoint" needs to be set')
return region
return endpoint


def can_use_bucket(s3_parameters: S3Parameters) -> bool:
Expand Down
45 changes: 45 additions & 0 deletions tests/unit/test_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,51 @@ def test_s3_relation_validation_fails_when_region_and_endpoint_not_set():
backup.S3Parameters(**s3_relation_data)


@pytest.mark.parametrize(
"s3_relation_data",
[
pytest.param(
{
"access-key": token_hex(16),
"secret-key": token_hex(16),
"bucket": "synapse-backup-bucket",
"region": "us-west-2",
},
id="region defined but not endpoint",
),
pytest.param(
{
"access-key": token_hex(16),
"secret-key": token_hex(16),
"bucket": "synapse-backup-bucket",
"endpoint": "https://example.com",
},
id="endpoint defined but not region",
),
pytest.param(
{
"access-key": token_hex(16),
"secret-key": token_hex(16),
"bucket": "synapse-backup-bucket",
"region": "us-west-2",
"endpoint": "https://example.com",
},
id="both region and endpoint defined",
),
],
)
def test_s3_relation_validation_correct(s3_relation_data):
"""
arrange: Create s3 relation data with correct data.
act: Create S3Parameters pydantic BaseModel from relation data.
assert: Relation data does not raise and region and endpoint equal
to the original values.
"""
s3_parameters = backup.S3Parameters(**s3_relation_data)
assert s3_parameters.endpoint == s3_relation_data.get("endpoint")
assert s3_parameters.region == s3_relation_data.get("region")


def test_can_use_bucket_wrong_boto3_resource(monkeypatch: pytest.MonkeyPatch):
"""
arrange: Create S3Parameters and mock boto3 library so it raises on accessing S3 resource.
Expand Down

0 comments on commit 789988d

Please sign in to comment.