Skip to content

Commit

Permalink
Starting of config validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane Brown committed Aug 18, 2023
1 parent 6487b9f commit 3bb425d
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
56 changes: 56 additions & 0 deletions buildrunner/config_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from re import L, T
from typing import Optional
from pydantic import BaseModel


class StepBuild(BaseModel):
path: Optional[str]
dockerfile: Optional[str]
pull: Optional[bool]
platform: Optional[str]
platforms: Optional[list[str]]

class StepPush(BaseModel):
repository: str
tags: list[str]

class Step(BaseModel):
build: Optional[StepBuild]
push: Optional[StepPush | list[str | StepPush]]

class Config(BaseModel):
version: Optional[float]
steps: dict[str, Step]

def __init__(self, **data) -> None:
super().__init__(**data)
self.validate()

def has_multi_platform_build(self):
for step in self.steps.values():
if step.build is not None and \
step.build.platforms is not None and \
isinstance(step.build.platforms, list):
return True
return False

def validate_multi_platform_build(self):
push_tags = {}
print(self.steps)
for step_name, step in self.steps.items():
print(step)
if step.build is not None and \
step.build.platforms is not None:
if step.build.platform is not None:
raise ValueError(f'Cannot specify both platform ({step.build.platform}) and platforms ({step.build.platforms}) in build step {step_name}')
# check for push tags
# step.push
return True
return True

def validate(self):
is_valid = True
if self.has_multi_platform_build():
is_valid = self.validate_multi_platform_build() and is_valid

return is_valid
121 changes: 121 additions & 0 deletions tests/test_config_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@

from calendar import c
from math import e
import buildrunner.config_model as config_model
from pydantic import ValidationError
import pytest


def test_valid_version_config():
# Invalid version
config = {
'version': 'string'
}
with pytest.raises(ValidationError):
config_model.Config(**config)

# Valid version
config = {
'version': 2.0,
'steps': {
}
}
try:
config_model.Config(**config)
except ValidationError as err:
pytest.fail(f'Config should be valid {err}')

# Optional version
config = {
'steps': {
}
}
try:
config_model.Config(**config)
except ValidationError as err:
pytest.fail(f'Config should be valid {err}')

def test_platform_and_platforms():
config = {
'steps': {
'build-container-multi-platform': {
'build': {
'path': '.',
'dockerfile': 'Dockerfile',
'pull': False,
'platform': 'linux/amd64',
'platforms': [
'linux/amd64',
'linux/arm64',
],
},
'push': {
'repository': 'shanejbrown/buildrunner-test-multi-platform',
'tags': [ 'latest' ],
},
},
}
}
try:
config_model.Config(**config)
# except ValidationError as err:
except ValueError as err:
print(err)


def test_valid_config():
config = {
'version': 2.0,
'steps': {
'build-container-single-platform': {
'build': {
'path': '.',
'dockerfile': 'Dockerfile',
'pull': False,
'platform': 'linux/amd64',
},
'push': {
'repository': 'shanejbrown/buildrunner-test',
'tags': [ 'latest' ],
},
},
'build-container-multi-platform': {
'build': {
'path': '.',
'dockerfile': 'Dockerfile',
'pull': False,
'platforms': [
'linux/amd64',
'linux/arm64',
],
},
'push': {
'repository': 'shanejbrown/buildrunner-test-multi-platform',
'tags': [ 'latest' ],
},
},
'build-container-multi-platform-push': {
'build': {
'path': '.',
'dockerfile': 'Dockerfile',
'pull': False,
'platforms': [
'linux/amd64',
'linux/arm64',
],
},
'push': [
'myimages/image1',
{
'repository': 'myimages/image2',
'tags': [ 'latest' ],
}
],
},
}
}

try:
config_model.Config(**config)
except ValidationError as err:
pytest.fail(f'Config should be valid {err}')

0 comments on commit 3bb425d

Please sign in to comment.