diff --git a/build/test/test_rest.py b/build/test/test_rest.py index 65ecb7d..df3b5cd 100644 --- a/build/test/test_rest.py +++ b/build/test/test_rest.py @@ -15,6 +15,7 @@ from django.test import override_settings from django.urls import reverse from rest_framework import status +from rest_framework.test import APISimpleTestCase from build import models from repoapi.test.base import APIAuthenticatedTestCase @@ -216,3 +217,27 @@ class TestBuildPatchRest(APIAuthenticatedTestCase): self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) br = models.BuildRelease.objects.get(uuid=self.release_uuid) self.assertEqual(br.projects, "kamailio,lua-ngcp-kamailio,ngcp-panel") + + +class TestCheckConfig(APISimpleTestCase): + url = reverse("check-config") + + def setUp(self): + self.data = { + "jenkins-jobs": {}, + "distris": ["release-trunk-buster"], + "release-trunk-buster": [], + } + + def test_empty(self): + response = self.client.post(self.url, {}, format="json") + self.assertEqual(response.status_code, status.HTTP_406_NOT_ACCEPTABLE) + + def test_clean(self): + response = self.client.post(self.url, self.data, format="json") + self.assertEqual(response.status_code, status.HTTP_200_OK) + + def test_circular_deps(self): + self.data["jenkins-jobs"]["build_deps"] = {"A": ["A1", "A"]} + response = self.client.post(self.url, self.data, format="json") + self.assertEqual(response.status_code, status.HTTP_406_NOT_ACCEPTABLE) diff --git a/build/test/test_utils.py b/build/test/test_utils.py index d7b4157..99e9c2e 100644 --- a/build/test/test_utils.py +++ b/build/test/test_utils.py @@ -589,3 +589,35 @@ class WannaBuildTestCase(SimpleTestCase): } config = self.FakeConfig(build_deps) config.check_circular_dependencies() + + +class CheckConfig(SimpleTestCase): + def setUp(self): + self.data = { + "jenkins-jobs": {}, + "distris": ["release-trunk-buster"], + "release-trunk-buster": [], + } + + def test_empty(self): + data = {} + with self.assertRaises(err.NoJenkinsJobsInfo): + ReleaseConfig("fake", config=data) + + def test_no_distris(self): + data = {"jenkins-jobs": []} + with self.assertRaises(err.NoDistrisInfo): + ReleaseConfig("fake", config=data) + + def test_no_release(self): + data = {"jenkins-jobs": [], "distris": []} + with self.assertRaises(err.NoReleaseInfo): + ReleaseConfig("fake", config=data) + + def test_simple(self): + ReleaseConfig("fake", config=self.data) + + def test_circular_dep(self): + self.data["jenkins-jobs"]["build_deps"] = {"A": ["A1", "A"]} + with self.assertRaises(err.CircularBuildDependencies): + ReleaseConfig("fake", config=self.data) diff --git a/build/utils.py b/build/utils.py index 2005f10..e07ece8 100644 --- a/build/utils.py +++ b/build/utils.py @@ -226,7 +226,7 @@ class ReleaseConfig(object): def check_circular_dependencies(self): levels = self.levels_build_deps builds = list(self.build_deps.keys()) - print(f"{levels}") + print(f"builds:{builds} levels:{levels}") for vals in levels: for prj in vals: builds.remove(prj) @@ -276,7 +276,7 @@ class ReleaseConfig(object): ] return humansorted(res, lambda x: x["release"], reverse=True) - def __init__(self, name, distribution=None): + def _get_config(self, name, distribution=None): ok, self.distribution = is_release_trunk(name) if not ok and name == "trunk": self.distribution = distribution @@ -288,6 +288,14 @@ class ReleaseConfig(object): settings.BUILD_REPOS_SCRIPTS_CONFIG_DIR / self.config_file ) self.config = self.load_config(self.config_path) + + def __init__(self, name, distribution=None, config=None): + if config is None: + self._get_config(name, distribution) + else: + self.config_file = "fake.yml" + self.config_path = "/dev/null" + self.config = config try: self.jenkins_jobs = self.config["jenkins-jobs"] except KeyError: diff --git a/build/views.py b/build/views.py index 3353fab..f0dbd06 100644 --- a/build/views.py +++ b/build/views.py @@ -16,14 +16,17 @@ import django_filters from django.http import JsonResponse from django.shortcuts import get_object_or_404 from rest_framework import generics +from rest_framework.parsers import JSONParser from rest_framework.permissions import DjangoModelPermissions from rest_framework.response import Response from rest_framework.views import APIView from rest_framework_api_key.permissions import HasAPIKey +from rest_framework_yaml.parsers import YAMLParser from . import models from . import serializers from . import tasks +from . import utils from repoapi.serializers import JenkinsBuildInfoSerializer as JBISerializer @@ -107,3 +110,14 @@ class ReleaseJobsUUID(APIView): serializer = JBISerializer(jbi, context={"request": request}) res.append(serializer.data) return Response(res) + + +class CheckConfig(APIView): + parser_classes = [YAMLParser, JSONParser] + + def post(self, request, format=None): + try: + utils.ReleaseConfig("fake", config=request.data) + return JsonResponse({"result": "All ok"}, status=200) + except Exception as e: + return JsonResponse({"error": f"{e}"}, status=406) diff --git a/repoapi/urls.py b/repoapi/urls.py index 916d138..4468ec4 100644 --- a/repoapi/urls.py +++ b/repoapi/urls.py @@ -94,6 +94,11 @@ api_patterns = [ docker.DockerTagDetail.as_view(), name="dockertag-detail", ), + re_path( + r"^config/check/$", + build_views.CheckConfig.as_view(), + name="check-config", + ), re_path(r"^build/", include("build.urls")), re_path(r"^release_changed/", include("release_changed.urls")), ] diff --git a/requirements/common.txt b/requirements/common.txt index f04ad85..1bcbe65 100644 --- a/requirements/common.txt +++ b/requirements/common.txt @@ -13,6 +13,7 @@ django-structlog django-timezone-field djangorestframework>=3.6 djangorestframework-api-key==2.* +djangorestframework-yaml drf-spectacular flower>=0.9.5 markdown diff --git a/t/Dockerfile b/t/Dockerfile index 6a582a3..5315082 100644 --- a/t/Dockerfile +++ b/t/Dockerfile @@ -5,7 +5,7 @@ FROM docker.mgm.sipwise.com/sipwise-bullseye:latest # is updated with the current date. It will force refresh of all # of the base images and things like `apt-get update` won't be using # old cached versions when the Dockerfile is built. -ENV REFRESHED_AT 2022-08-12 +ENV REFRESHED_AT 2022-09-22 RUN apt-get update && apt-get install --assume-yes python3 python3-dev \ python3-pytest python3-pytest-pep8 \