Skip to content

Commit

Permalink
Adds preliminary role gen linting tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mabashian committed Feb 3, 2025
1 parent 463cff1 commit 0251a4c
Showing 1 changed file with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
ContentMatchParameters,
PlaybookExplanationParameters,
PlaybookGenerationParameters,
RoleGenerationParameters,
)
from ansible_ai_connect.ai.api.model_pipelines.tests import mock_pipeline_config
from ansible_ai_connect.ai.api.model_pipelines.wca.pipelines_base import (
Expand All @@ -77,6 +78,7 @@
WCASaaSContentMatchPipeline,
WCASaaSPlaybookExplanationPipeline,
WCASaaSPlaybookGenerationPipeline,
WCASaaSRoleGenerationPipeline,
)
from ansible_ai_connect.test_utils import (
WisdomAppsBackendMocking,
Expand Down Expand Up @@ -335,7 +337,7 @@ def test_get_api_key_with_wca_configured(self):

@override_settings(WCA_SECRET_BACKEND_TYPE="dummy")
@override_settings(ENABLE_ANSIBLE_LINT_POSTPROCESS=False)
class TestWCAClientGeneration(WisdomAppsBackendMocking, WisdomServiceLogAwareTestCase):
class TestWCAClientPlaybookGeneration(WisdomAppsBackendMocking, WisdomServiceLogAwareTestCase):
def setUp(self):
super().setUp()
wca_client = WCASaaSPlaybookGenerationPipeline(
Expand Down Expand Up @@ -510,6 +512,64 @@ def test_playbook_gen_request_id_correlation_failure(self):
)


@override_settings(WCA_SECRET_BACKEND_TYPE="dummy")
@override_settings(ENABLE_ANSIBLE_LINT_POSTPROCESS=False)
class TestWCAClientRoleGeneration(WisdomAppsBackendMocking, WisdomServiceLogAwareTestCase):
def setUp(self):
super().setUp()
wca_client = WCASaaSRoleGenerationPipeline(
mock_pipeline_config("wca", api_key=None, model_id=None)
)
wca_client.get_api_key = Mock(return_value="some-key")
wca_client.get_token = Mock(return_value={"access_token": "a-token"})
wca_client.get_model_id = Mock(return_value="a-random-model")
wca_client.session = Mock()
response = Mock
response.text = (
'{"name": "foo_bar", "outline": "Ahh!", "files": [{"path": '
'"roles/foo_bar/tasks/main.yml", "content": "some content", '
'"file_type": "task"}, {"path": "roles/foo_bar/defaults/main.yml", '
'"content": "some content", "file_type": "default"}], "warnings": []}'
)
response.status_code = 200
response.headers = {WCA_REQUEST_ID_HEADER: WCA_REQUEST_ID_HEADER}
response.raise_for_status = Mock()
wca_client.session.post.return_value = response
self.wca_client = wca_client

@assert_call_count_metrics(metric=wca_codegen_playbook_hist)
@override_settings(ENABLE_ANSIBLE_LINT_POSTPROCESS=True)
def test_role_gen_with_lint(self):
fake_linter = Mock()
fake_linter.run_linter.return_value = "I'm super fake!"
self.mock_ansible_lint_caller_with(fake_linter)
name, files, outline, warnings = self.wca_client.invoke(
RoleGenerationParameters.init(
request=Mock(), text="Install Wordpress", create_outline=True
)
)
self.assertEqual(name, "foo_bar")
self.assertEqual(outline, "Ahh!")
self.assertEqual(warnings, [])
for file in files:
self.assertEqual(file["content"], "I'm super fake!")

@assert_call_count_metrics(metric=wca_codegen_playbook_hist)
@override_settings(ENABLE_ANSIBLE_LINT_POSTPROCESS=True)
def test_tole_gen_when_is_not_initialized(self):
self.mock_ansible_lint_caller_with(None)
name, files, outline, warnings = self.wca_client.invoke(
RoleGenerationParameters.init(
request=Mock(), text="Install Wordpress", create_outline=True
)
)
self.assertEqual(name, "foo_bar")
self.assertEqual(outline, "Ahh!")
self.assertEqual(warnings, [])
for file in files:
self.assertEqual(file["content"], "some content")


@override_settings(WCA_SECRET_BACKEND_TYPE="dummy")
@override_settings(ENABLE_ANSIBLE_LINT_POSTPROCESS=False)
class TestWCAClientExplanation(WisdomAppsBackendMocking, WisdomServiceLogAwareTestCase):
Expand Down

0 comments on commit 0251a4c

Please sign in to comment.