Skip to content

Commit

Permalink
Merge branch 'devopshobbies:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
abolfazl8131 authored Dec 10, 2024
2 parents 818d508 + 020aef5 commit 5aa2a2a
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
submodules: true

- name: install requirements
run: pip install -r requirements.txt
Expand Down
61 changes: 59 additions & 2 deletions app/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from app.models import (
IaCTemplateGenerationDocker, IaCTemplateGenerationEC2, IaCTemplateGenerationS3, IaCTemplateGenerationIAM,
IaCTemplateGenerationArgoCD, IaCTemplateGenerationELB, IaCTemplateGenerationEFS, SyncPolicy, ArgoApplication,
HelmTemplateGeneration, Pod, Persistance, Ingress, Environment, IaCBasicInput, IaCBugfixInput, IaCInstallationInput
HelmTemplateGeneration, Pod, Persistance, Ingress, Environment, IaCBasicInput, IaCBugfixInput, IaCInstallationInput,
AnsibleInstallNginx, AnsibleInstallDocker, AnsibleInstallKuber, Build, Service, Network, PreCreatedNetwork,
DockerCompose
)


Expand Down Expand Up @@ -154,4 +156,59 @@ def iac_install_invalid_sample_input():
return {
'os': 'Kali', # Unsupported OS
'service': 'terraform',
}
}


@pytest.fixture
def ansible_nginx_sample_input():
return AnsibleInstallNginx().model_dump()


@pytest.fixture
def ansible_nginx_invalid_sample_input():
sample_input = AnsibleInstallNginx().model_dump()
sample_input['os'] = 'Kali'
return sample_input


@pytest.fixture
def ansible_docker_sample_input():
return AnsibleInstallDocker().model_dump()


@pytest.fixture
def ansible_docker_invalid_sample_input():
sample_input = AnsibleInstallDocker().model_dump()
sample_input['os'] = 'Kali'
return sample_input


@pytest.fixture
def ansible_kuber_sample_input():
return AnsibleInstallKuber(
k8s_worker_nodes=['node-1', 'node-2'],
k8s_master_nodes=['node-1', 'node-2']
).model_dump()


@pytest.fixture
def ansible_kuber_invalid_sample_input():
sample_input = AnsibleInstallKuber(
k8s_worker_nodes=['node-1', 'node-2'],
k8s_master_nodes=['node-1', 'node-2']
).model_dump()
sample_input['os'] = 'Kali'
return sample_input


@pytest.fixture
def docker_compose_sample_input():
return DockerCompose().model_dump()


@pytest.fixture
def docker_compose_invalid_sample_input():
sample_input = DockerCompose().model_dump()
sample_input['services']['web']['build'] = None
sample_input['services']['web']['image'] = None
return sample_input
46 changes: 46 additions & 0 deletions app/tests/test_ansible.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from unittest.mock import MagicMock, patch, mock_open

class TestAnsibleInstall:
def setup_method(self):
mock_client_instance = MagicMock()
mock_client_instance.chat.completions.create.return_value = MagicMock(
choices=[MagicMock(message=MagicMock(content='Mocked OpenAI Response'))]
)

self.mock_execute_python_file = patch('app.main.execute_pythonfile').start()
self.mock_edit_directory_generator = patch('app.main.edit_directory_generator').start()
self.mock_gpt_service = patch('app.main.gpt_service', return_value='Mocked GPT Response').start()
self.mock_openai = patch('app.gpt_services.OpenAI', return_value=mock_client_instance).start()
self.mock_builtin_open = patch('builtins.open', mock_open()).start()
self.mock_os_makedirs = patch('os.makedirs').start()
self.mock_os_path_join = patch('os.path.join', side_effect=lambda *args: '/'.join(args)).start()
self.mock_shutil_copy = patch('shutil.copy').start()
self.mock_shutil_rmtree = patch('shutil.rmtree').start()

self.ansible_nginx_url = '/api/ansible-install/nginx/'
self.ansible_docker_url = '/api/ansible-install/docker/'
self.ansible_kuber_url = '/api/ansible-install/kuber/'

def test_ansible_nginx_input(self, client, ansible_nginx_sample_input):
response = client.post(self.ansible_nginx_url, json=ansible_nginx_sample_input)
assert response.status_code == 200

def test_ansible_nginx_invalid_input(self, client, ansible_nginx_invalid_sample_input):
response = client.post(self.ansible_nginx_url, json=ansible_nginx_invalid_sample_input)
assert response.status_code == 422

def test_ansible_docker_input(self, client, ansible_docker_sample_input):
response = client.post(self.ansible_docker_url, json=ansible_docker_sample_input)
assert response.status_code == 200

def test_ansible_docker_invalid_input(self, client, ansible_docker_invalid_sample_input):
response = client.post(self.ansible_docker_url, json=ansible_docker_invalid_sample_input)
assert response.status_code == 422

def test_ansible_kuber_input(self, client, ansible_kuber_sample_input):
response = client.post(self.ansible_kuber_url, json=ansible_kuber_sample_input)
assert response.status_code == 200

def test_ansible_kuber_invalid_input(self, client, ansible_kuber_invalid_sample_input):
response = client.post(self.ansible_kuber_url, json=ansible_kuber_invalid_sample_input)
assert response.status_code == 422
13 changes: 13 additions & 0 deletions app/tests/test_docker_compose.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@


class TestDockerCompose:
def setup_method(self):
self.url = '/api/docker-compose/'

def test_docker_compose_input(self, client, docker_compose_sample_input):
response = client.post(self.url, json=docker_compose_sample_input)
assert response.status_code == 200

def test_docker_compose_invalid_input(self, client, docker_compose_invalid_sample_input):
response = client.post(self.url, json=docker_compose_invalid_sample_input)
assert response.status_code == 422
2 changes: 1 addition & 1 deletion app/tests/test_helm_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def setup_method(self):
self.mock_builtin_open = patch('builtins.open', mock_open()).start()
self.mock_shutil_rm = patch('shutil.rmtree').start()

self.url = '/Helm-template/'
self.url = '/api/Helm-template/'

def teardown_method(self):
patch.stopall()
Expand Down
2 changes: 1 addition & 1 deletion app/tests/test_iac_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def setup_method(self):
self.mock_gpt_service = patch('app.main.gpt_service', return_value='Mocked GPT Response').start()
self.mock_openai = patch('app.gpt_services.OpenAI', return_value=mock_client_instance).start()

self.url = '/IaC-basic/'
self.url = '/api/IaC-basic/'

def teardown_method(self):
patch.stopall()
Expand Down
2 changes: 1 addition & 1 deletion app/tests/test_iac_bugfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def setup_method(self):
self.mock_gpt_service = patch('app.main.gpt_service', return_value='Mocked GPT Response').start()
self.mock_openai = patch('app.gpt_services.OpenAI', return_value=mock_client_instance).start()

self.url = '/IaC-bugfix/'
self.url = '/api/IaC-bugfix/'

def teardown_method(self):
patch.stopall()
Expand Down
2 changes: 1 addition & 1 deletion app/tests/test_iac_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def setup_method(self):
self.mock_gpt_service = patch('app.main.gpt_service', return_value='Mocked GPT Response').start()
self.mock_openai = patch('app.gpt_services.OpenAI', return_value=mock_client_instance).start()

self.url = '/IaC-install/'
self.url = '/api/IaC-install/'

def teardown_method(self):
patch.stopall()
Expand Down
14 changes: 7 additions & 7 deletions app/tests/test_iac_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ def setup_method(self):
self.mock_openai = patch('app.gpt_services.OpenAI', return_value=mock_client_instance).start()
self.mock_builtin_open = patch('builtins.open', mock_open()).start()

self.iac_template_docker_url = '/IaC-template/docker'
self.iac_template_ec2_url = '/IaC-template/aws/ec2'
self.iac_template_s3_url = '/IaC-template/aws/s3'
self.iac_template_iam_url = '/IaC-template/aws/iam'
self.iac_template_argocd_url = '/IaC-template/argocd'
self.iac_template_elb_url = '/IaC-template/aws/elb'
self.iac_template_efs_url = '/IaC-template/aws/efs'
self.iac_template_docker_url = '/api/IaC-template/docker'
self.iac_template_ec2_url = '/api/IaC-template/aws/ec2'
self.iac_template_s3_url = '/api/IaC-template/aws/s3'
self.iac_template_iam_url = '/api/IaC-template/aws/iam'
self.iac_template_argocd_url = '/api/IaC-template/argocd'
self.iac_template_elb_url = '/api/IaC-template/aws/elb'
self.iac_template_efs_url = '/api/IaC-template/aws/efs'

def teardown_method(self):
patch.stopall()
Expand Down

0 comments on commit 5aa2a2a

Please sign in to comment.