Merge pull request #115 from 0xFelix/small-improvements-2

Several minor improvements
This commit is contained in:
kubevirt-bot
2024-07-01 11:42:53 +02:00
committed by GitHub
10 changed files with 139 additions and 51 deletions

View File

@@ -16,6 +16,37 @@ on:
schedule: schedule:
- cron: "0 6 * * *" - cron: "0 6 * * *"
jobs: 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: linter:
runs-on: ubuntu-latest runs-on: ubuntu-latest
env: env:
@@ -55,6 +86,8 @@ jobs:
run: | run: |
find . -type f -iname '*.sh' -exec shellcheck {} \; find . -type f -iname '*.sh' -exec shellcheck {} \;
working-directory: ${{ env.collection_dir }} working-directory: ${{ env.collection_dir }}
needs:
- check-tree-clean
sanity: sanity:
uses: ansible-network/github_actions/.github/workflows/sanity.yml@main uses: ansible-network/github_actions/.github/workflows/sanity.yml@main

View File

@@ -112,7 +112,7 @@ jobs:
if: inputs.ansible_test_targets != '' if: inputs.ansible_test_targets != ''
uses: helm/kind-action@v1.9.0 uses: helm/kind-action@v1.9.0
with: with:
version: v0.22.0 version: v0.23.0
install_only: true install_only: true
- name: Deploy kubevirt - name: Deploy kubevirt

5
.gitignore vendored
View File

@@ -130,11 +130,6 @@ dmypy.json
# Pyre type checker # Pyre type checker
.pyre/ .pyre/
# Allow specific vscode configuration
.vscode/*
!.vscode/extensions.json
!.vscode/settings.json
# Local files # Local files
.idea/ .idea/
bin/ bin/

45
.vscode/launch.json vendored Normal file
View File

@@ -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
}
]
}

11
.vscode/module_args/kubevirt_vm.json vendored Normal file
View File

@@ -0,0 +1,11 @@
{
"ANSIBLE_MODULE_ARGS": {
"name": "testvm",
"namespace": "default",
"spec": {
"domain": {
"devices": {}
}
}
}
}

View File

@@ -0,0 +1,6 @@
{
"ANSIBLE_MODULE_ARGS": {
"name": "testvm",
"namespace": "default"
}
}

View File

@@ -27,4 +27,4 @@ test-unit:
.PHONY: test-integration .PHONY: test-integration
test-integration: test-integration:
tox -f integration --ansible --conf tox-ansible.ini ansible-test integration

View File

@@ -6,7 +6,7 @@ readme: README.md
authors: authors:
- KubeVirt Project (kubevirt.io) - KubeVirt Project (kubevirt.io)
dependencies: dependencies:
kubernetes.core: '>=3.1.0,<4.1.0' kubernetes.core: '>=3.1.0,<6.0.0'
description: Lean Ansible bindings for KubeVirt description: Lean Ansible bindings for KubeVirt
license_file: LICENSE license_file: LICENSE
tags: tags:

View File

@@ -236,8 +236,8 @@ def k8s_module_params_delete(module_params_delete, vm_definition_running):
} }
def test_module_fails_when_required_args_missing(monkeypatch): def test_module_fails_when_required_args_missing(mocker):
monkeypatch.setattr(AnsibleModule, "fail_json", fail_json) mocker.patch.object(AnsibleModule, "fail_json", fail_json)
with pytest.raises(AnsibleFailJson): with pytest.raises(AnsibleFailJson):
set_module_args({}) set_module_args({})
kubevirt_vm.main() kubevirt_vm.main()
@@ -274,27 +274,29 @@ def test_module_fails_when_required_args_missing(monkeypatch):
) )
def test_module( def test_module(
request, request,
monkeypatch,
mocker, mocker,
module_params, module_params,
k8s_module_params, k8s_module_params,
vm_definition, vm_definition,
method, method,
): ):
monkeypatch.setattr(AnsibleModule, "exit_json", exit_json) mocker.patch.object(AnsibleModule, "exit_json", exit_json)
monkeypatch.setattr(runner, "get_api_client", lambda _: None) mocker.patch.object(runner, "get_api_client")
set_module_args(request.getfixturevalue(module_params)) perform_action = mocker.patch.object(
runner,
perform_action = mocker.patch.object(runner, "perform_action") "perform_action",
perform_action.return_value = { return_value={
"method": method, "method": method,
"changed": True, "changed": True,
"result": "success", "result": "success",
} },
)
with pytest.raises(AnsibleExitJson): with pytest.raises(AnsibleExitJson):
set_module_args(request.getfixturevalue(module_params))
kubevirt_vm.main() kubevirt_vm.main()
perform_action.assert_called_once_with( perform_action.assert_called_once_with(
mocker.ANY, mocker.ANY,
request.getfixturevalue(vm_definition), request.getfixturevalue(vm_definition),
@@ -422,15 +424,13 @@ def vm_template_labels():
"namespace": "default", "namespace": "default",
"labels": { "labels": {
"test": "test", "test": "test",
} },
}, },
"spec": { "spec": {
"running": True, "running": True,
"template": { "template": {
"metadata": { "metadata": {
"labels": { "labels": {"test": "test"},
"test": "test"
},
}, },
"spec": { "spec": {
"domain": { "domain": {
@@ -451,15 +451,13 @@ def vm_template_annotations():
"namespace": "default", "namespace": "default",
"annotations": { "annotations": {
"test": "test", "test": "test",
} },
}, },
"spec": { "spec": {
"running": True, "running": True,
"template": { "template": {
"metadata": { "metadata": {
"annotations": { "annotations": {"test": "test"},
"test": "test"
},
}, },
"spec": { "spec": {
"domain": { "domain": {
@@ -481,9 +479,7 @@ def vm_template_instancetype():
}, },
"spec": { "spec": {
"running": True, "running": True,
"instancetype": { "instancetype": {"name": "u1.medium"},
"name": "u1.medium"
},
"template": { "template": {
"spec": { "spec": {
"domain": { "domain": {
@@ -505,9 +501,7 @@ def vm_template_preference():
}, },
"spec": { "spec": {
"running": True, "running": True,
"preference": { "preference": {"name": "fedora"},
"name": "fedora"
},
"template": { "template": {
"spec": { "spec": {
"domain": { "domain": {
@@ -638,9 +632,10 @@ def vm_template_specs():
("render_template_params_datavolumetemplate", "vm_template_datavolumetemplate"), ("render_template_params_datavolumetemplate", "vm_template_datavolumetemplate"),
("render_template_params_name", "vm_template_name"), ("render_template_params_name", "vm_template_name"),
("render_template_params_generate_name", "vm_template_generate_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): def test_render_template(request, params, rendered_template):
result = kubevirt_vm.render_template(request.getfixturevalue(params)) assert kubevirt_vm.render_template(request.getfixturevalue(params)) == dump(
assert result == dump(request.getfixturevalue(rendered_template), sort_keys=False) request.getfixturevalue(rendered_template), sort_keys=False
)

View File

@@ -84,8 +84,8 @@ def find_args_stopped(find_args_default):
{"running": False}, {"running": False},
], ],
) )
def test_module_fails_when_required_args_missing(monkeypatch, module_args): def test_module_fails_when_required_args_missing(mocker, module_args):
monkeypatch.setattr(AnsibleModule, "fail_json", fail_json) mocker.patch.object(AnsibleModule, "fail_json", fail_json)
with pytest.raises(AnsibleFailJson): with pytest.raises(AnsibleFailJson):
set_module_args(module_args) set_module_args(module_args)
kubevirt_vm_info.main() 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"), ({"wait": True, "running": False}, "find_args_stopped"),
], ],
) )
def test_module(request, monkeypatch, mocker, module_args, find_args): def test_module(request, mocker, module_args, find_args):
monkeypatch.setattr(AnsibleModule, "exit_json", exit_json) mocker.patch.object(AnsibleModule, "exit_json", exit_json)
monkeypatch.setattr(kubevirt_vm_info, "get_api_client", lambda _: None) mocker.patch.object(kubevirt_vm_info, "get_api_client")
set_module_args(module_args) find = mocker.patch.object(
K8sService,
find = mocker.patch.object(K8sService, "find") "find",
find.return_value = { return_value={
"api_found": True, "api_found": True,
"failed": False, "failed": False,
"resources": [], "resources": [],
} },
)
with pytest.raises(AnsibleExitJson): with pytest.raises(AnsibleExitJson):
set_module_args(module_args)
kubevirt_vm_info.main() kubevirt_vm_info.main()
find.assert_called_once_with(**request.getfixturevalue(find_args)) find.assert_called_once_with(**request.getfixturevalue(find_args))