Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AnyHttpUrl validator for jenkins's server_url config field #35

Merged
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .dockerignore

This file was deleted.

2 changes: 1 addition & 1 deletion jenkins_agent_k8s_rock/rockcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ parts:
- default-jre-headless
- git
override-prime: |
craftctl default
craftctl default
/bin/bash -c "mkdir -p --mode=775 var/{lib/jenkins,lib/jenkins/agents,log/jenkins}"
/bin/bash -c "chown -R 584792 var/{lib/jenkins,lib/jenkins/agents,log/jenkins}"
entrypoint:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ copyright-regexp = "Copyright\\s\\d{4}([-,]\\d{4})*\\s+%(author)s"
ignore_missing_imports = true
check_untyped_defs = true
disallow_untyped_defs = true
plugins = ["pydantic.mypy"]

[[tool.mypy.overrides]]
module = "tests.*"
Expand Down
18 changes: 14 additions & 4 deletions src/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from dataclasses import dataclass

import ops
from pydantic import BaseModel, Field, ValidationError
from pydantic import AnyHttpUrl, BaseModel, Field, ValidationError, tools

import metadata
import server
Expand Down Expand Up @@ -42,13 +42,20 @@ class JenkinsConfig(BaseModel):
"""The Jenkins config from juju config values.

Attrs:
server_url: The Jenkins server url.
server_url_not_validated: The Jenkins server url, to be validated with pydantic.
server_url: The Jenkins server url, to be used by the charm.
agent_name_token_pairs: Jenkins agent names paired with corresponding token value.
"""

server_url: str = Field(..., min_length=1)
server_url_not_validated: AnyHttpUrl

agent_name_token_pairs: typing.List[typing.Tuple[str, str]] = Field(..., min_items=1)

@property
def server_url(self) -> str:
"""Convert validated server_url to string."""
return str(self.server_url_not_validated)

@classmethod
def from_charm_config(cls, config: ops.ConfigData) -> typing.Optional["JenkinsConfig"]:
"""Instantiate JenkinsConfig from charm config.
Expand All @@ -69,7 +76,10 @@ def from_charm_config(cls, config: ops.ConfigData) -> typing.Optional["JenkinsCo
agent_names = agent_name_config.split(":") if agent_name_config else []
agent_tokens = agent_token_config.split(":") if agent_token_config else []
agent_name_token_pairs = list(zip(agent_names, agent_tokens))
return cls(server_url=server_url or "", agent_name_token_pairs=agent_name_token_pairs)
return cls(
server_url_not_validated=tools.parse_obj_as(AnyHttpUrl, server_url) or "",
agent_name_token_pairs=agent_name_token_pairs,
)


def _get_jenkins_unit(
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def harness_fixture():
def config_fixture():
"""The Jenkins testing configuration values."""
return {
"jenkins_url": "http://testingurl",
"jenkins_url": "http://testingurl.com",
"jenkins_agent_name": "testing_agent_name",
"jenkins_agent_token": secrets.token_hex(16),
}
Expand Down
20 changes: 19 additions & 1 deletion tests/unit/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,25 @@ def test_from_charm_invalid_charm_config(harness: ops.testing.Harness):
state.State.from_charm(charm=harness.charm)


def test_from_charm_vailid_config(harness: ops.testing.Harness, config: typing.Dict[str, str]):
def test_from_charm_invalid_server_url(
harness: ops.testing.Harness, config: typing.Dict[str, str]
):
"""
arrange: given charm configuration data with invalid server_url attribute
act: when the state is initialized from_charm.
assert: InvalidStateError is raised. (due to pydantic's AnyHttpUrl validator)
"""
invalid_config = config
# This configuration is invalid because schema (http or https) must be specified
invalid_config["jenkins_url"] = "example.com"
harness.update_config(invalid_config)
arturo-seijas marked this conversation as resolved.
Show resolved Hide resolved
harness.begin()

with pytest.raises(state.InvalidStateError):
state.State.from_charm(charm=harness.charm)


def test_from_charm_valid_config(harness: ops.testing.Harness, config: typing.Dict[str, str]):
"""
arrange: given valid charm configuration data.
act: when the state is initialized from_charm.
Expand Down
Loading