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:
- 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

View File

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

5
.gitignore vendored
View File

@@ -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/

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
test-integration:
tox -f integration --ansible --conf tox-ansible.ini
ansible-test integration

View File

@@ -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:

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):
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
)

View File

@@ -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))