diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 607695b..0276807 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,6 +16,37 @@ on: schedule: - cron: "0 6 * * *" jobs: + check-tree-clean: + runs-on: ubuntu-latest + steps: + + - name: Check out code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: 3.x + cache: pip + + - name: Install tox + run: | + python -m pip install --upgrade pip + pip install tox + + - name: Run make format + run: | + make format + + - name: Check if tree is clean + run: | + if [ -n "$(git status --porcelain)" ]; then + echo "There are uncommitted changes!" + exit 1 + fi + linter: runs-on: ubuntu-latest env: @@ -55,6 +86,8 @@ jobs: run: | find . -type f -iname '*.sh' -exec shellcheck {} \; working-directory: ${{ env.collection_dir }} + needs: + - check-tree-clean sanity: uses: ansible-network/github_actions/.github/workflows/sanity.yml@main diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 18f7fc6..aba6920 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -112,7 +112,7 @@ jobs: if: inputs.ansible_test_targets != '' uses: helm/kind-action@v1.9.0 with: - version: v0.22.0 + version: v0.23.0 install_only: true - name: Deploy kubevirt diff --git a/.gitignore b/.gitignore index 10d32f0..d006fb0 100644 --- a/.gitignore +++ b/.gitignore @@ -130,11 +130,6 @@ dmypy.json # Pyre type checker .pyre/ -# Allow specific vscode configuration -.vscode/* -!.vscode/extensions.json -!.vscode/settings.json - # Local files .idea/ bin/ diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..251af7d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,45 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Debug: Ansible Module", + "type": "debugpy", + "request": "launch", + "cwd": "${workspaceFolder}", + "module": "plugins.modules.${fileBasenameNoExtension}", + "console": "internalConsole", + "args": [ + "${workspaceFolder}/.vscode/module_args/${fileBasenameNoExtension}.json" + ], + "justMyCode": false + }, + { + "name": "Debug: Ansible Inventory", + "type": "debugpy", + "request": "launch", + "cwd": "${workspaceFolder}", + "module": "ansible.cli.inventory", + "console": "internalConsole", + "args": [ + "--inventory", + "${workspaceFolder}/examples/default.kubevirt.yml", + "--list" + ], + "justMyCode": false + }, + { + "name": "Debug: Unit Tests", + "type": "debugpy", + "request": "launch", + "program": "${file}", + "purpose": [ + "debug-test" + ], + "console": "internalConsole", + "justMyCode": false + } + ] +} diff --git a/.vscode/module_args/kubevirt_vm.json b/.vscode/module_args/kubevirt_vm.json new file mode 100644 index 0000000..0db6bc9 --- /dev/null +++ b/.vscode/module_args/kubevirt_vm.json @@ -0,0 +1,11 @@ +{ + "ANSIBLE_MODULE_ARGS": { + "name": "testvm", + "namespace": "default", + "spec": { + "domain": { + "devices": {} + } + } + } +} diff --git a/.vscode/module_args/kubevirt_vm_info.json b/.vscode/module_args/kubevirt_vm_info.json new file mode 100644 index 0000000..755fae5 --- /dev/null +++ b/.vscode/module_args/kubevirt_vm_info.json @@ -0,0 +1,6 @@ +{ + "ANSIBLE_MODULE_ARGS": { + "name": "testvm", + "namespace": "default" + } +} diff --git a/Makefile b/Makefile index a8348f7..d32b507 100644 --- a/Makefile +++ b/Makefile @@ -27,4 +27,4 @@ test-unit: .PHONY: test-integration test-integration: - tox -f integration --ansible --conf tox-ansible.ini + ansible-test integration diff --git a/galaxy.yml b/galaxy.yml index 646f2ff..7a4ac7f 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -6,7 +6,7 @@ readme: README.md authors: - KubeVirt Project (kubevirt.io) dependencies: - kubernetes.core: '>=3.1.0,<4.1.0' + kubernetes.core: '>=3.1.0,<6.0.0' description: Lean Ansible bindings for KubeVirt license_file: LICENSE tags: diff --git a/tests/unit/plugins/modules/test_kubevirt_vm.py b/tests/unit/plugins/modules/test_kubevirt_vm.py index ea8fda1..0310285 100644 --- a/tests/unit/plugins/modules/test_kubevirt_vm.py +++ b/tests/unit/plugins/modules/test_kubevirt_vm.py @@ -236,8 +236,8 @@ def k8s_module_params_delete(module_params_delete, vm_definition_running): } -def test_module_fails_when_required_args_missing(monkeypatch): - monkeypatch.setattr(AnsibleModule, "fail_json", fail_json) +def test_module_fails_when_required_args_missing(mocker): + mocker.patch.object(AnsibleModule, "fail_json", fail_json) with pytest.raises(AnsibleFailJson): set_module_args({}) kubevirt_vm.main() @@ -274,27 +274,29 @@ def test_module_fails_when_required_args_missing(monkeypatch): ) def test_module( request, - monkeypatch, mocker, module_params, k8s_module_params, vm_definition, method, ): - monkeypatch.setattr(AnsibleModule, "exit_json", exit_json) - monkeypatch.setattr(runner, "get_api_client", lambda _: None) + mocker.patch.object(AnsibleModule, "exit_json", exit_json) + mocker.patch.object(runner, "get_api_client") - set_module_args(request.getfixturevalue(module_params)) - - perform_action = mocker.patch.object(runner, "perform_action") - perform_action.return_value = { - "method": method, - "changed": True, - "result": "success", - } + perform_action = mocker.patch.object( + runner, + "perform_action", + return_value={ + "method": method, + "changed": True, + "result": "success", + }, + ) with pytest.raises(AnsibleExitJson): + set_module_args(request.getfixturevalue(module_params)) kubevirt_vm.main() + perform_action.assert_called_once_with( mocker.ANY, request.getfixturevalue(vm_definition), @@ -422,15 +424,13 @@ def vm_template_labels(): "namespace": "default", "labels": { "test": "test", - } + }, }, "spec": { "running": True, "template": { "metadata": { - "labels": { - "test": "test" - }, + "labels": {"test": "test"}, }, "spec": { "domain": { @@ -451,15 +451,13 @@ def vm_template_annotations(): "namespace": "default", "annotations": { "test": "test", - } + }, }, "spec": { "running": True, "template": { "metadata": { - "annotations": { - "test": "test" - }, + "annotations": {"test": "test"}, }, "spec": { "domain": { @@ -481,9 +479,7 @@ def vm_template_instancetype(): }, "spec": { "running": True, - "instancetype": { - "name": "u1.medium" - }, + "instancetype": {"name": "u1.medium"}, "template": { "spec": { "domain": { @@ -505,9 +501,7 @@ def vm_template_preference(): }, "spec": { "running": True, - "preference": { - "name": "fedora" - }, + "preference": {"name": "fedora"}, "template": { "spec": { "domain": { @@ -638,9 +632,10 @@ def vm_template_specs(): ("render_template_params_datavolumetemplate", "vm_template_datavolumetemplate"), ("render_template_params_name", "vm_template_name"), ("render_template_params_generate_name", "vm_template_generate_name"), - ("render_template_params_specs", "vm_template_specs") + ("render_template_params_specs", "vm_template_specs"), ], ) def test_render_template(request, params, rendered_template): - result = kubevirt_vm.render_template(request.getfixturevalue(params)) - assert result == dump(request.getfixturevalue(rendered_template), sort_keys=False) + assert kubevirt_vm.render_template(request.getfixturevalue(params)) == dump( + request.getfixturevalue(rendered_template), sort_keys=False + ) diff --git a/tests/unit/plugins/modules/test_kubevirt_vm_info.py b/tests/unit/plugins/modules/test_kubevirt_vm_info.py index 436b205..38b5c7e 100644 --- a/tests/unit/plugins/modules/test_kubevirt_vm_info.py +++ b/tests/unit/plugins/modules/test_kubevirt_vm_info.py @@ -84,8 +84,8 @@ def find_args_stopped(find_args_default): {"running": False}, ], ) -def test_module_fails_when_required_args_missing(monkeypatch, module_args): - monkeypatch.setattr(AnsibleModule, "fail_json", fail_json) +def test_module_fails_when_required_args_missing(mocker, module_args): + mocker.patch.object(AnsibleModule, "fail_json", fail_json) with pytest.raises(AnsibleFailJson): set_module_args(module_args) kubevirt_vm_info.main() @@ -102,19 +102,22 @@ def test_module_fails_when_required_args_missing(monkeypatch, module_args): ({"wait": True, "running": False}, "find_args_stopped"), ], ) -def test_module(request, monkeypatch, mocker, module_args, find_args): - monkeypatch.setattr(AnsibleModule, "exit_json", exit_json) - monkeypatch.setattr(kubevirt_vm_info, "get_api_client", lambda _: None) +def test_module(request, mocker, module_args, find_args): + mocker.patch.object(AnsibleModule, "exit_json", exit_json) + mocker.patch.object(kubevirt_vm_info, "get_api_client") - set_module_args(module_args) - - find = mocker.patch.object(K8sService, "find") - find.return_value = { - "api_found": True, - "failed": False, - "resources": [], - } + find = mocker.patch.object( + K8sService, + "find", + return_value={ + "api_found": True, + "failed": False, + "resources": [], + }, + ) with pytest.raises(AnsibleExitJson): + set_module_args(module_args) kubevirt_vm_info.main() + find.assert_called_once_with(**request.getfixturevalue(find_args))