Skip to content

Commit

Permalink
Merge pull request #13 from globocom/tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
ribeiro-rodrigo committed Jan 4, 2021
2 parents 20b0789 + 5b64b01 commit 2e27911
Show file tree
Hide file tree
Showing 49 changed files with 980 additions and 177 deletions.
35 changes: 19 additions & 16 deletions .github/workflows/continous-integration.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
name: CI
name: build

on: [push, pull_request]
on: [ push, pull_request ]

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-18.04
strategy:
matrix:
python-version: [3.8]

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Checkout
uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}

- name: Setup pipenv
uses: dschep/install-pipenv-action@v1
- name: Setup pipenv
uses: dschep/install-pipenv-action@v1

- name: Install dependencies
run: pipenv sync -d

- name: Install dependencies
run: pipenv sync -d

- name: Run static type tests
run: pipenv run mypy ./**/*.py
- name: Run static type tests
run: pipenv run mypy ./**/*.py

- name: Run tests
run:
pipenv run coverage run -m unittest discover -s test -v
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,7 @@ cython_debug/
config.ini

crd
.vscode/

.vscode/

Pipfile.lock
4 changes: 3 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ verify_ssl = true

[dev-packages]
mypy = "*"
pylint = "*"

[packages]
argocd-client = {editable = true,git = "https://github.com/globocom/argocd-client.git"}
argocd-client = { editable = true, git = "https://github.com/globocom/argocd-client.git" }
injector = "*"
requests = "*"
kopf = "==0.28.1"
pydantic = "*"
coverage = "*"

[requires]
python_version = "3.8"
217 changes: 176 additions & 41 deletions Pipfile.lock

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Actions Status](https://github.com/globocom/enforcement/workflows/build/badge.svg)](https://github.com/{owner}/{repo}/actions)

# Enforcement
## Introduction
Enforcement is an open source project focused on the management and simultaneous deployment of applications and policies across multiple clusters through GitOps.
Expand Down Expand Up @@ -99,3 +101,20 @@ spec:
- cluster3
```
The rancher.filters, rancher.labels and rancher.ignore fields are specific to Rancher. Other cluster sources may have other values. You can get all the examples of ClusterRules objects [here](https://github.com/globocom/enforcement-service/tree/master/examples).

## HOW TO TEST

To run the tests without coverage you may call:
```shell
make test
```

To run the tests with coverage you may call and before called make test:
```shell
make coverage
```

To generate the html you may call and called make test and make coverage before:
```shell
make generate
```
2 changes: 1 addition & 1 deletion app/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from app.config.data_module import DataModule
from app.config.use_case_module import UseCaseModule
from app.config.domain_module import DomainModule
from app.config.use_case_module import UseCaseModule

__all__ = ['DataModule', 'UseCaseModule', 'DomainModule']
19 changes: 7 additions & 12 deletions app/config/data_module.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
from injector import Module, provider, singleton
from argocd_client import (
SessionSessionCreateRequest,
SessionSessionResponse,
ApplicationServiceApi,
ProjectServiceApi
)
from argocd_client.api import SessionServiceApi
from argocd_client.api.cluster_service_api import ClusterServiceApi
from argocd_client.api_client import ApiClient
from argocd_client.configuration import Configuration
from argocd_client.api.cluster_service_api import ClusterServiceApi

from app.infra.config import Config

from app.domain.repositories import ClusterRepository, EnforcementRepository, ProjectRepository
from app.domain.source_locator import SourceLocator
from injector import Module, provider, singleton

from app.data.argo.cluster import ClusterService
from app.data.argo.application import ApplicationService
from app.data.source.definition.locator import SourceLocatorImpl

from app.data.argo.cluster import ClusterService
from app.data.argo.project import ProjectService
from app.data.source.definition.locator import SourceLocatorImpl
from app.domain.repositories import ClusterRepository, EnforcementRepository, ProjectRepository
from app.domain.source_locator import SourceLocator
from app.infra.config import Config


class DataModule(Module):
Expand Down Expand Up @@ -92,5 +89,3 @@ def provide_project_service(self, api_client: ApiClient) -> ProjectServiceApi:
@singleton
def provide_project_repository(self, project_service: ProjectServiceApi) -> ProjectRepository:
return ProjectService(project_service=project_service)


23 changes: 19 additions & 4 deletions app/config/domain_module.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
from injector import Module, provider, singleton

from app.domain.repositories import ClusterRepository, ProjectRepository
from app.domain.cluster_group_builder import ClusterGroupBuilder
from app.domain.enforcement_change_detector_builder import EnforcementChangeDetectorBuilder
from app.domain.enforcement_installer_builder import EnforcementInstallerBuilder
from app.domain.repositories import ClusterRepository, ProjectRepository, EnforcementRepository


class DomainModule(Module):


@provider
@singleton
def provide_cluster_group_builder(self, cluster_repository: ClusterRepository,
project_repository: ProjectRepository) -> ClusterGroupBuilder:
return ClusterGroupBuilder(cluster_repository=cluster_repository, project_repository=project_repository)

@provider
@singleton
def provide_enforcement_installer_builder(self,
enforcement_repository: EnforcementRepository) -> EnforcementInstallerBuilder:
return EnforcementInstallerBuilder(enforcement_repository=enforcement_repository)

@provider
@singleton
def provide_cluster_group_builder(self, cluster_repository: ClusterRepository, project_repository: ProjectRepository) -> ClusterGroupBuilder:
return ClusterGroupBuilder(cluster_repository=cluster_repository, project_repository=project_repository)
def provide_enforcement_change_detector_builder(self) -> EnforcementChangeDetectorBuilder:
return EnforcementChangeDetectorBuilder()
34 changes: 19 additions & 15 deletions app/config/use_case_module.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
from injector import Module, provider, singleton

from app.domain.source_locator import SourceLocator
from app.domain.cluster_group_builder import ClusterGroupBuilder
from app.domain.enforcement_change_detector_builder import EnforcementChangeDetectorBuilder
from app.domain.enforcement_installer_builder import EnforcementInstallerBuilder
from app.domain.repositories import EnforcementRepository
from app.domain.source_locator import SourceLocator
from app.domain.use_case import ApplyRulesUseCase, SyncRulesUseCase, UpdateRulesUseCase
from app.domain.cluster_group_builder import ClusterGroupBuilder


class UseCaseModule(Module):

@provider
@singleton
def provider_apply_rules(
self, locator: SourceLocator, enforcement_repo: EnforcementRepository,
cluster_group_builder: ClusterGroupBuilder
self, locator: SourceLocator,
cluster_group_builder: ClusterGroupBuilder, enforcement_installer_builder: EnforcementInstallerBuilder
) -> ApplyRulesUseCase:
return ApplyRulesUseCase(
enforcement_repository=enforcement_repo,
source_locator=locator,
cluster_group_builder=cluster_group_builder
cluster_group_builder=cluster_group_builder,
enforcement_installer_builder=enforcement_installer_builder
)

@provider
@singleton
def provider_sync_rules(
self, locator: SourceLocator, enforcement_repo: EnforcementRepository,
cluster_group_builder: ClusterGroupBuilder
self, locator: SourceLocator,
cluster_group_builder: ClusterGroupBuilder, enforcement_installer_builder: EnforcementInstallerBuilder
) -> SyncRulesUseCase:
return SyncRulesUseCase(
enforcement_repository=enforcement_repo,
cluster_group_builder=cluster_group_builder,
source_locator=locator
source_locator=locator,
enforcement_installer_builder=enforcement_installer_builder
)

@provider
@singleton
def provider_update_rules(self, enforcement_repo: EnforcementRepository,
cluster_group_builder: ClusterGroupBuilder) -> UpdateRulesUseCase:
def provider_update_rules(self,
cluster_group_builder: ClusterGroupBuilder,
enforcement_installer_builder: EnforcementInstallerBuilder,
enforcement_change_detector_builder: EnforcementChangeDetectorBuilder) -> UpdateRulesUseCase:
return UpdateRulesUseCase(
enforcement_repository=enforcement_repo,
cluster_group_builder=cluster_group_builder
cluster_group_builder=cluster_group_builder,
enforcement_installer_builder=enforcement_installer_builder,
enforcement_change_detector_builder=enforcement_change_detector_builder
)

22 changes: 11 additions & 11 deletions app/data/argo/application.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict, List, Any
import attr

import attr
from argocd_client import (
V1alpha1ApplicationDestination,
V1alpha1Application,
Expand Down Expand Up @@ -49,7 +49,7 @@ def update_enforcement(self, cluster_name: str, instance_name: str, enforcement:
def remove_enforcement(self, enforcement_name: str) -> None:
application = V1alpha1Application(
metadata=V1ObjectMeta(
name=enforcement_name
name=enforcement_name
)
)

Expand Down Expand Up @@ -82,19 +82,19 @@ def _make_application_by_enforcement(self, cluster_name: str, instance_name: str

if enforcement.helm:
source.helm = V1alpha1ApplicationSourceHelm(
parameters=[
V1alpha1HelmParameter(
name=key,
value=value
)
for key, value in enforcement.helm.parameters.items()
] if enforcement.helm.parameters else []
parameters=[
V1alpha1HelmParameter(
name=key,
value=value
)
for key, value in enforcement.helm.parameters.items()
] if enforcement.helm.parameters else []
)

return V1alpha1Application(
metadata=V1ObjectMeta(
name=f"{instance_name}",
labels={"cluster_name": cluster_name},
name=f"{instance_name}",
labels={"cluster_name": cluster_name},
),
spec=V1alpha1ApplicationSpec(
destination=V1alpha1ApplicationDestination(
Expand Down
2 changes: 1 addition & 1 deletion app/data/argo/cluster.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict, List
import attr

import attr
from argocd_client import (
ClusterServiceApi,
V1alpha1Cluster,
Expand Down
9 changes: 3 additions & 6 deletions app/data/argo/project.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import attr

from argocd_client import ProjectServiceApi

from app.domain.entities import Cluster
from app.domain.repositories import ProjectRepository


@attr.s(auto_attribs=True)
class ProjectService(ProjectRepository):
_project_service: ProjectServiceApi

def create_project(self, cluster: Cluster) -> None:

def create_project(self, cluster: Cluster) -> None:
body = {
'project': {
'metadata': {
Expand All @@ -34,7 +33,5 @@ def create_project(self, cluster: Cluster) -> None:

self._project_service.create_mixin6(body)

def remove_project(self, project_name: str) -> None:
def remove_project(self, project_name: str) -> None:
self._project_service.delete_mixin6(name=project_name)


1 change: 0 additions & 1 deletion app/data/source/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from app.data.source.definition.locator import SourceLocatorImpl

__all__ = ['ClusterDatasource', 'SourceLocatorImpl']

4 changes: 1 addition & 3 deletions app/data/source/definition/base.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import attr

from app.domain.repositories import SourceRepository
from app.domain.entities import EnforcementSource
from app.domain.repositories import SourceRepository
from app.infra.config import Config


@attr.s(auto_attribs=True)
class BaseSource(SourceRepository):
config: Config
source: EnforcementSource


7 changes: 3 additions & 4 deletions app/data/source/definition/locator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

from app.infra.config import Config
from app.domain.repositories import SourceRepository
from app.domain.source_locator import SourceLocator
from app.data.source.definition.register import SourceRegister
from app.domain.entities import EnforcementSource
from app.domain.repositories import SourceRepository
from app.domain.source_locator import SourceLocator
from app.infra.config import Config


class SourceLocatorImpl(SourceLocator):
Expand Down
3 changes: 1 addition & 2 deletions app/data/source/definition/register.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Dict, Callable

from app.data.source.rancher import RancherDatasource


Expand All @@ -10,5 +11,3 @@ class SourceRegister:
@classmethod
def find_source(cls, source_name: str) -> Callable:
return cls.sources.get(source_name)


1 change: 1 addition & 0 deletions app/data/source/rancher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List, Dict

import requests

from app.data.source.definition.base import BaseSource
Expand Down
Loading

0 comments on commit 2e27911

Please sign in to comment.