Beispiel #1
0
def test_build_status_ne_by_status(build_status_failed: BuildStatus) -> None:
    """
    must be not equal by status
    """
    other = BuildStatus.from_json(build_status_failed.view())
    other.status = BuildStatusEnum.Success
    assert build_status_failed != other
Beispiel #2
0
def test_build_status_ne_by_timestamp(
        build_status_failed: BuildStatus) -> None:
    """
    must be not equal by timestamp
    """
    other = BuildStatus.from_json(build_status_failed.view())
    other.timestamp = datetime.datetime.utcnow().timestamp()
    assert build_status_failed != other
Beispiel #3
0
def test_build_status_init_1() -> None:
    """
    must construct status object from None
    """
    status = BuildStatus()
    assert status.status == BuildStatusEnum.Unknown
    assert status.timestamp > 0
Beispiel #4
0
def test_get(watcher: Watcher, package_ahriman: Package) -> None:
    """
    must return package status
    """
    watcher.known = {package_ahriman.base: (package_ahriman, BuildStatus())}
    package, status = watcher.get(package_ahriman.base)
    assert package == package_ahriman
    assert status.status == BuildStatusEnum.Unknown
Beispiel #5
0
    def get_self(self) -> BuildStatus:
        """
        get ahriman status itself
        :return: current ahriman status
        """
        try:
            response = requests.get(self._ahriman_url())
            response.raise_for_status()

            status_json = response.json()
            return BuildStatus.from_json(status_json)
        except requests.exceptions.HTTPError as e:
            self.logger.exception(
                f"could not get service status: {exception_response_text(e)}")
        except Exception:
            self.logger.exception("could not get service status")
        return BuildStatus()
Beispiel #6
0
async def test_get(client: TestClient) -> None:
    """
    must return valid service status
    """
    response = await client.get("/api/v1/ahriman")
    status = BuildStatus.from_json(await response.json())

    assert response.status == 200
    assert status.status == BuildStatusEnum.Unknown
Beispiel #7
0
def test_counters_from_packages(package_ahriman: Package,
                                package_python_schedule: Package) -> None:
    """
    must construct object from list of packages with their statuses
    """
    payload = [
        (package_ahriman, BuildStatus(status=BuildStatusEnum.Success)),
        (package_python_schedule, BuildStatus(status=BuildStatusEnum.Failed)),
    ]

    counters = Counters.from_packages(payload)
    assert counters.total == 2
    assert counters.success == 1
    assert counters.failed == 1

    json = asdict(counters)
    total = json.pop("total")
    assert total == sum(i for i in json.values())
Beispiel #8
0
def test_cache_save(watcher: Watcher, package_ahriman: Package,
                    mocker: MockerFixture) -> None:
    """
    must save state to cache
    """
    mocker.patch("pathlib.Path.open")
    json_mock = mocker.patch("json.dump")

    watcher.known = {package_ahriman.base: (package_ahriman, BuildStatus())}
    watcher._cache_save()
    json_mock.assert_called_once()
Beispiel #9
0
def test_remove(watcher: Watcher, package_ahriman: Package,
                mocker: MockerFixture) -> None:
    """
    must remove package base
    """
    cache_mock = mocker.patch(
        "ahriman.core.status.watcher.Watcher._cache_save")
    watcher.known = {package_ahriman.base: (package_ahriman, BuildStatus())}

    watcher.remove(package_ahriman.base)
    assert not watcher.known
    cache_mock.assert_called_once()
Beispiel #10
0
 def load(self) -> None:
     """
     load packages from local repository. In case if last status is known, it will use it
     """
     for package in self.repository.packages():
         # get status of build or assign unknown
         current = self.known.get(package.base)
         if current is None:
             status = BuildStatus()
         else:
             _, status = current
         self.known[package.base] = (package, status)
     self._cache_load()
Beispiel #11
0
def test_run_with_package_filter(args: argparse.Namespace, configuration: Configuration, package_ahriman: Package,
                                 mocker: MockerFixture) -> None:
    """
    must run command
    """
    args = _default_args(args)
    args.package = [package_ahriman.base]
    mocker.patch("pathlib.Path.mkdir")
    packages_mock = mocker.patch("ahriman.core.status.client.Client.get",
                                 return_value=[(package_ahriman, BuildStatus())])

    Status.run(args, "x86_64", configuration)
    packages_mock.assert_called_once()
Beispiel #12
0
def test_get_self(web_client: WebClient, mocker: MockerFixture) -> None:
    """
    must return service status
    """
    response_obj = Response()
    response_obj._content = json.dumps(BuildStatus().view()).encode("utf8")
    response_obj.status_code = 200

    requests_mock = mocker.patch("requests.get", return_value=response_obj)

    result = web_client.get_self()
    requests_mock.assert_called_once()
    assert result.status == BuildStatusEnum.Unknown
Beispiel #13
0
async def test_post(client: TestClient) -> None:
    """
    must update service status correctly
    """
    payload = {"status": BuildStatusEnum.Success.value}
    post_response = await client.post("/api/v1/ahriman", json=payload)
    assert post_response.status == 204

    response = await client.get("/api/v1/ahriman")
    status = BuildStatus.from_json(await response.json())

    assert response.status == 200
    assert status.status == BuildStatusEnum.Success
Beispiel #14
0
    def __init__(self, architecture: str,
                 configuration: Configuration) -> None:
        """
        default constructor
        :param architecture: repository architecture
        :param configuration: configuration instance
        """
        self.logger = logging.getLogger("http")

        self.architecture = architecture
        self.repository = Repository(architecture, configuration)

        self.known: Dict[str, Tuple[Package, BuildStatus]] = {}
        self.status = BuildStatus()
Beispiel #15
0
def test_update_ping(watcher: Watcher, package_ahriman: Package,
                     mocker: MockerFixture) -> None:
    """
    must update package status only for known package
    """
    cache_mock = mocker.patch(
        "ahriman.core.status.watcher.Watcher._cache_save")
    watcher.known = {package_ahriman.base: (package_ahriman, BuildStatus())}

    watcher.update(package_ahriman.base, BuildStatusEnum.Success, None)
    cache_mock.assert_called_once()
    package, status = watcher.known[package_ahriman.base]
    assert package == package_ahriman
    assert status.status == BuildStatusEnum.Success
Beispiel #16
0
 def update(self, base: str, status: BuildStatusEnum,
            package: Optional[Package]) -> None:
     """
     update package status and description
     :param base: package base to update
     :param status: new build status
     :param package: optional new package description. In case if not set current properties will be used
     """
     if package is None:
         try:
             package, _ = self.known[base]
         except KeyError:
             raise UnknownPackage(base)
     full_status = BuildStatus(status)
     self.known[base] = (package, full_status)
     self._cache_save()
Beispiel #17
0
def test_load_known(watcher: Watcher, package_ahriman: Package,
                    mocker: MockerFixture) -> None:
    """
    must correctly load packages with known statuses
    """
    mocker.patch("ahriman.core.repository.repository.Repository.packages",
                 return_value=[package_ahriman])
    mocker.patch("ahriman.core.status.watcher.Watcher._cache_load")
    watcher.known = {
        package_ahriman.base:
        (package_ahriman, BuildStatus(BuildStatusEnum.Success))
    }

    watcher.load()
    _, status = watcher.known[package_ahriman.base]
    assert status.status == BuildStatusEnum.Success
Beispiel #18
0
def test_cache_save_load(watcher: Watcher, package_ahriman: Package,
                         mocker: MockerFixture) -> None:
    """
    must save state to cache which can be loaded later
    """
    dump_file = Path(tempfile.mktemp())
    mocker.patch("ahriman.core.status.watcher.Watcher.cache_path",
                 new_callable=PropertyMock,
                 return_value=dump_file)
    known_current = {package_ahriman.base: (package_ahriman, BuildStatus())}

    watcher.known = known_current
    watcher._cache_save()

    watcher.known = {package_ahriman.base: (None, None)}
    watcher._cache_load()
    assert watcher.known == known_current

    dump_file.unlink()
Beispiel #19
0
    def get(self, base: Optional[str]) -> List[Tuple[Package, BuildStatus]]:
        """
        get package status
        :param base: package base to get
        :return: list of current package description and status if it has been found
        """
        try:
            response = requests.get(self._package_url(base or ""))
            response.raise_for_status()

            status_json = response.json()
            return [(Package.from_json(package["package"]),
                     BuildStatus.from_json(package["status"]))
                    for package in status_json]
        except requests.exceptions.HTTPError as e:
            self.logger.exception(
                f"could not get {base}: {exception_response_text(e)}")
        except Exception:
            self.logger.exception(f"could not get {base}")
        return []
Beispiel #20
0
def test_build_status_repr(build_status_failed: BuildStatus) -> None:
    """
    must return string in __repr__ function
    """
    assert build_status_failed.__repr__()
    assert isinstance(build_status_failed.__repr__(), str)
Beispiel #21
0
 def parse_single(properties: Dict[str, Any]) -> None:
     package = Package.from_json(properties["package"])
     status = BuildStatus.from_json(properties["status"])
     if package.base in self.known:
         self.known[package.base] = (package, status)
Beispiel #22
0
def test_build_status_eq(build_status_failed: BuildStatus) -> None:
    """
    must be equal
    """
    other = BuildStatus.from_json(build_status_failed.view())
    assert other == build_status_failed
Beispiel #23
0
def build_status_failed() -> BuildStatus:
    return BuildStatus(BuildStatusEnum.Failed, 42)
Beispiel #24
0
def test_build_status_init_2(build_status_failed: BuildStatus) -> None:
    """
    must construct status object from objects
    """
    status = BuildStatus(BuildStatusEnum.Failed, 42)
    assert status == build_status_failed
Beispiel #25
0
 def update_self(self, status: BuildStatusEnum) -> None:
     """
     update service status
     :param status: new service status
     """
     self.status = BuildStatus(status)
Beispiel #26
0
def get_package_status_extended(package: Package) -> Dict[str, Any]:
    return {"status": BuildStatus().view(), "package": package.view()}
Beispiel #27
0
def test_build_status_from_json_view(build_status_failed: BuildStatus) -> None:
    """
    must construct same object from json
    """
    assert BuildStatus.from_json(
        build_status_failed.view()) == build_status_failed
Beispiel #28
0
 def get_self(self) -> BuildStatus:
     """
     get ahriman status itself
     :return: current ahriman status
     """
     return BuildStatus()
Beispiel #29
0
def test_build_status_pretty_print(build_status_failed: BuildStatus) -> None:
    """
    must return string in pretty print function
    """
    assert build_status_failed.pretty_print()
    assert isinstance(build_status_failed.pretty_print(), str)