Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gilesknap committed Feb 9, 2024
1 parent d59cf36 commit c614f17
Show file tree
Hide file tree
Showing 19 changed files with 61 additions and 189 deletions.
10 changes: 5 additions & 5 deletions src/ec_cli/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def read_cached_dict(cache_folder: str, cached_file: str) -> dict:
return read_dict


def fetch_ioc_graph(beamline_repo: str) -> dict:
def fetch_service_graph(beamline_repo: str) -> dict:
ioc_graph = read_cached_dict(url_encode(beamline_repo), globals.IOC_CACHE)
if not ioc_graph:
tmp_dir = Path(tempfile.mkdtemp())
Expand All @@ -59,12 +59,12 @@ def fetch_ioc_graph(beamline_repo: str) -> dict:

def avail_IOCs(ctx: typer.Context) -> List[str]:
params = ctx.parent.parent.params # type: ignore
beamline_repo = params["repo"] or globals.EC_SERVICES_REPO
services_repo = params["repo"] or globals.EC_SERVICES_REPO

# This block prevents getting a stack trace during autocompletion
try:
ioc_graph = fetch_ioc_graph(beamline_repo)
return list(ioc_graph.keys())
services_graph = fetch_service_graph(services_repo)
return list(services_graph.keys())
except typer.Exit:
return [" "]
except CalledProcessError:
Expand All @@ -78,7 +78,7 @@ def avail_versions(ctx: typer.Context) -> List[str]:

# This block prevents getting a stack trace during autocompletion
try:
ioc_graph = fetch_ioc_graph(beamline_repo)
ioc_graph = fetch_service_graph(beamline_repo)
ioc_versions = ioc_graph[ioc_name]
return ioc_versions
except KeyError:
Expand Down
9 changes: 2 additions & 7 deletions src/ec_cli/cmds/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ def deploy_local(self, service_path: Path, yes: bool = False):
if not typer.confirm("Are you sure ?"):
raise typer.Abort()

# to update the local helm charts we may need to remove the lock files
lock_files = service_path.glob("../../**/Chart.lock")
for lock_file in lock_files:
lock_file.unlink()

self._do_deploy(service_path)

def deploy(self):
Expand Down Expand Up @@ -92,11 +87,11 @@ def _do_deploy(self, service_folder: Path):

# package up the charts to get the appVersion set
for chart in chart_paths:
shell.run_command(f"helm dependency build {chart}", interactive=False)
shell.run_command(f"helm dependency update {chart}", interactive=False)

with chdir(service_folder):
shell.run_command(
f"helm dependency build {service_folder}; "
f"helm dependency update {service_folder}; "
f"helm package {service_folder} --app-version {self.version}",
interactive=False,
)
Expand Down
4 changes: 2 additions & 2 deletions src/ec_cli/cmds/ioc_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from ec_cli.cmds.local_commands import IocLocalCommands
from ec_cli.git import create_ioc_graph
from ec_cli.logging import log
from ec_cli.utils import cleanup_temp, drop_ioc_path
from ec_cli.utils import cleanup_temp, drop_path

cli = typer.Typer(pretty_exceptions_show_locals=False)

Expand Down Expand Up @@ -143,7 +143,7 @@ def deploy(
"""
Pull an IOC helm chart version from the domain repo and deploy it to the cluster
"""
service_name = drop_ioc_path(service_name)
service_name = drop_path(service_name)
if ctx.obj.namespace == globals.LOCAL_NAMESPACE:
IocLocalCommands(ctx.obj, service_name).deploy(service_name, version, args)
else:
Expand Down
4 changes: 3 additions & 1 deletion src/ec_cli/cmds/kubectl.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@
r'{"\n"}{end}'
"'"
)
json_service_types = {"name": str, "ready": bool, "restarts": int, "started": str}

# force all the values to be strings so there are never parsing errors
json_service_types = {"name": str, "ready": str, "restarts": str, "started": str}
2 changes: 1 addition & 1 deletion src/ec_cli/cmds/local_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(
self.ioc_name: str = ioc_name

self.tmp = Path(tempfile.mkdtemp())
self.ioc_folder = self.tmp / "iocs" / ioc_name
self.ioc_folder = self.tmp / "services" / ioc_name
self.docker = Docker(check=with_docker)

def __del__(self):
Expand Down
10 changes: 4 additions & 6 deletions src/ec_cli/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,18 @@ def repo2registry(repo_name: str) -> str:
return registry


def create_ioc_graph(beamline_repo: str, folder: Path, branch="main") -> Dict:
def create_ioc_graph(repo: str, folder: Path, branch="main") -> Dict:
"""
return a dictionary of the available IOCs (by discovering the children
to the iocs/ folder in the beamline repo) as well as a list of the corresponding
to the services/ folder in the beamline repo) as well as a list of the corresponding
available versions for each IOC (by discovering the tags in the beamline repo at
which changes to the instance were made since the last tag) and the respective
list of available versions
"""
ioc_graph = {}

check_services_repo(beamline_repo)
shell.run_command(
f"git clone {beamline_repo} {folder} -b {branch}", interactive=False
)
check_services_repo(repo)
shell.run_command(f"git clone {repo} {folder} -b {branch}", interactive=False)
path_list = os.listdir(os.path.join(folder, "services"))
service_list = [
path
Expand Down
22 changes: 11 additions & 11 deletions src/ec_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ def get_instance_image_name(ioc_instance: Path, tag: Optional[str] = None) -> st
return image


def check_instance_path(ioc_path: Path):
def check_instance_path(service_path: Path):
"""
verify that the ioc instance path is valid
verify that the service instance path is valid
"""
ioc_path = ioc_path.absolute()
ioc_name = ioc_path.name.lower()
service_path = service_path.absolute()
service_name = service_path.name.lower()

log.info(f"checking IOC instance {ioc_name} at {ioc_path}")
if ioc_path.is_dir():
if not (ioc_path / "values.yaml").exists():
log.info(f"checking instance {service_name} at {service_path}")
if service_path.is_dir():
if not (service_path / "values.yaml").exists():
log.error("IOC instance requires values.yaml")
raise typer.Exit(1)
else:
log.error(f"IOC instance path {ioc_path} does not exist")
log.error(f"instance path {service_path} does not exist")
raise typer.Exit(1)

return ioc_name, ioc_path
return service_name, service_path


def generic_ioc_from_image(image_name: str) -> str:
Expand All @@ -65,12 +65,12 @@ def generic_ioc_from_image(image_name: str) -> str:
return match[0]


def drop_ioc_path(raw_input: str):
def drop_path(raw_input: str):
"""
Extracts the IOC name if is a path through ioc
"""
match = re.findall(
r"iocs\/(.*?)(?:/|\s|$)", raw_input
r"services\/(.*?)(?:/|\s|$)", raw_input
) # https://regex101.com/r/L3GUvk/1
if not match:
return raw_input
Expand Down
28 changes: 0 additions & 28 deletions tests/data/beamline-chart/.helmignore

This file was deleted.

25 changes: 0 additions & 25 deletions tests/data/beamline-chart/Chart.yaml.jinja

This file was deleted.

65 changes: 0 additions & 65 deletions tests/data/beamline-chart/values.yaml

This file was deleted.

25 changes: 11 additions & 14 deletions tests/data/ioc.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
checks:
- cmd: kubectl get namespace bl45p -o name
rsp: namespace/bl45p
- cmd: kubectl get -n bl45p statefulset/bl45p-ea-ioc-01
rsp: bl45p-ea-ioc-01 1/1 1 1 5d5h
- cmd: helm list -n bl45p -qf bl45p-ea-ioc-01
rsp: bl45p-ea-ioc-01

attach:
- cmd: kubectl -it -n bl45p attach statefulset/bl45p-ea-ioc-01
Expand All @@ -13,31 +13,28 @@ delete:
rsp: True

template:
- cmd: helm dependency update /tmp/ec_tests/beamline-chart
- cmd: 'helm dependency update .*\/tests\/data\/services\/bl45p-ea-ioc-01\; helm package .*\/tests\/data\/services\/bl45p-ea-ioc-01 --app-version .*'
rsp: ""
- cmd:
'bash -c "helm template bl45p-ea-ioc-01 /tmp/ec_tests/beamline-chart --version
20.*--namespace bl45p -f \/.*\/data\/iocs\/bl45p-ea-ioc-01\/values.yaml.*'
- cmd: 'bash -c "helm template bl45p-ea-ioc-01 .*pretend_helm.tgz --namespace bl45p --debug'
rsp: |
# Source: bl45p-ea-ioc-01/templates/configmap.yaml
apiVersion: v1
...
# TODO blank response is that OK?
deploy_local:
- cmd: helm dependency update /tmp/ec_tests/beamline-chart
- cmd: 'helm dependency update .*\/tests\/data\/services\/bl45p-ea-ioc-01\; helm package .*\/tests\/data\/services\/bl45p-ea-ioc-01 --app-version .*'
rsp: ""
- cmd:
'bash -c "helm upgrade --install bl45p-ea-ioc-01 /tmp/ec_tests/beamline-chart
--version 20.*--namespace bl45p -f \/.*\/data\/iocs\/bl45p-ea-ioc-01\/values.yaml.*'
- cmd: 'bash -c "helm upgrade --install bl45p-ea-ioc-01 .*pretend_helm.tgz --namespace bl45p'
rsp: ""

deploy:
- cmd: git clone https://github.com/epics-containers/bl45p /tmp/ec_tests --depth=1 --single-branch --branch=2.0
- cmd: kubectl get namespace bl45p -o name
rsp: namespace/bl45p
- cmd: git clone https://github.com/epics-containers/bl45p /tmp/ec_tests --depth=1 --single-branch --branch=main
rsp: ""
- cmd: helm dependency update /tmp/ec_tests/beamline-chart
- cmd: helm dependency update /tmp/ec_tests/services/bl45p-ea-ioc-01; helm package /tmp/ec_tests/services/bl45p-ea-ioc-01 --app-version 2.0
rsp: ""
- cmd: 'bash -c "helm upgrade --install bl45p-ea-ioc-01 /tmp/ec_tests/beamline-chart --version 2.0 --namespace bl45p -f /tmp/ec_tests/iocs/bl45p-ea-ioc-01/values.yaml.*'
- cmd: 'bash -c "helm upgrade --install bl45p-ea-ioc-01 .*pretend_helm.tgz --namespace bl45p.*'
rsp: ""

instances:
Expand Down
7 changes: 2 additions & 5 deletions tests/data/local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ deploy_local:
rsp: ""
- cmd: docker container create --name busybox -v bl45p-ea-ioc-01_config:/copyto busybox
rsp: ""
- cmd: docker cp {data}/iocs/bl45p-ea-ioc-01/config/ioc.yaml busybox:copyto
- cmd: docker cp {data}/services/bl45p-ea-ioc-01/config/ioc.yaml busybox:copyto
rsp: ""
- cmd: docker rm -f busybox
rsp: ""
Expand All @@ -54,7 +54,7 @@ deploy:
rsp: ""
- cmd: docker container create --name busybox -v bl45p-ea-ioc-01_config:/copyto busybox
rsp: ""
- cmd: docker cp /tmp/ec_tests/iocs/bl45p-ea-ioc-01/config/ioc.yaml busybox:copyto
- cmd: docker cp /tmp/ec_tests/services/bl45p-ea-ioc-01/config/ioc.yaml busybox:copyto
rsp: ""
- cmd: docker rm -f busybox
rsp: ""
Expand All @@ -72,9 +72,6 @@ instances:
- cmd: git diff --name-only 2.0 2.0\^
rsp: |
2.0
- cmd: git diff --name-only 2.0 $(git hash-object -t tree /dev/null)
rsp: |
""
exec:
- cmd: docker ps -f name=bl45p-ea-ioc-01 --format .*
Expand Down
Empty file.
File renamed without changes.
4 changes: 2 additions & 2 deletions tests/test_autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_all_iocs_local(mock_run, mocker, autocomplete, ctx):

def test_avail_IOCs(mock_run, data, autocomplete, ctx):
mock_run.set_seq(autocomplete.avail_IOCs)
shutil.copytree(data / "iocs", TMPDIR / "iocs")
shutil.copytree(data / "services", TMPDIR / "services")

ctx.parent.parent.params["repo"] = "" # use env variable
result = mock_run.call(avail_IOCs, ctx)
Expand All @@ -50,7 +50,7 @@ def test_avail_IOCs(mock_run, data, autocomplete, ctx):

def test_avail_versions(mock_run, data, autocomplete, ctx):
mock_run.set_seq(autocomplete.avail_versions)
# shutil.copytree(data / "iocs", TMPDIR / "iocs") already exists
# shutil.copytree(data / "services", TMPDIR / "services") already exists

ctx.parent.parent.params["repo"] = "" # use env variable
ctx.parent.parent.params["ioc_name"] = "bl45p-ea-ioc-01"
Expand Down
Loading

0 comments on commit c614f17

Please sign in to comment.