Skip to content

Commit 14efa29

Browse files
authored
Merge pull request #11 from nuclearcat/add-test-retry
feat(testretry): Add testretry command
2 parents b64b8b9 + 14b4ef6 commit 14efa29

File tree

4 files changed

+146
-1
lines changed

4 files changed

+146
-1
lines changed

docs/index.md

+70
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,73 @@
11
# kci-dev
22

33
kci-dev is a cmdline tool for interact with a enabled KernelCI server
4+
Purpose of this tool to provide a easy way to use features of KernelCI Pipeline instance.
5+
6+
## Installation
7+
8+
Using poetry and virtualenv
9+
```sh
10+
virtualenv .venv
11+
source .venv/bin/activate
12+
pip install poetry
13+
poetry install
14+
poetry run kci-dev
15+
```
16+
17+
## Configuration
18+
19+
kci-dev uses a configuration file .kci-dev.toml in the program directory.
20+
```toml
21+
default_instance="staging"
22+
[local]
23+
host="https://127.0.0.1"
24+
token="example"
25+
26+
[staging]
27+
host="https://staging.kernelci.org:9100/"
28+
token="SOMEVERYSECRETTOKEN"
29+
30+
[production]
31+
host="https://kernelci-pipeline.westus3.cloudapp.azure.com/"
32+
token="example"
33+
```
34+
35+
Where `default_instance` is the default instance to use, if not provided in the command line.
36+
In section `local`, `staging`, `production` you can provide the host and token for the available instances.
37+
host is the URL of the KernelCI Pipeline API endpoint, and token is the API token to use for authentication.
38+
If you are using KernelCI Pipeline instance, you can get the token from the project maintainers.
39+
If it is a local instance, you can generate your token using kernelci-pipeline/tools/jwt_generator.py script.
40+
41+
## Options
42+
43+
### instance
44+
You can provide the instance name to use for the command.
45+
46+
Example:
47+
```sh
48+
kci-dev --instance staging
49+
```
50+
51+
### settings
52+
53+
You can provide the configuration file path to use for the command.
54+
55+
Example:
56+
```sh
57+
kci-dev --settings /path/to/.kci-dev.toml
58+
```
59+
60+
## Commands
61+
62+
### testretry
63+
64+
This command will retry the failed tests. In some cases tests may fail due to network issues, hardware problems,
65+
nature of test (flaky), etc. This command will retry the failed tests, and create additional test jobs for the failed tests.
66+
After observing the results, you can decide if test results were reliable, not, or maybe even test need improvement.
67+
68+
Example:
69+
```sh
70+
kci-dev testretry --nodeid <testnodeid>
71+
```
72+
73+
testnodeid is the node id of the test job, which you can get from the KernelCI dashboard. Usually it is hexadecimal string.

kci-dev/kci-dev.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import click
55
from libs.common import *
6-
from subcommands import commit, patch, results
6+
from subcommands import commit, patch, results, testretry
77

88

99
@click.group(
@@ -32,6 +32,7 @@ def run():
3232
cli.add_command(commit.commit)
3333
cli.add_command(patch.patch)
3434
cli.add_command(results.results)
35+
cli.add_command(testretry.testretry)
3536
cli()
3637

3738

kci-dev/subcommands/testretry.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import json
5+
6+
import click
7+
import requests
8+
from git import Repo
9+
from libs.common import *
10+
11+
12+
def api_connection(host):
13+
click.secho("api connect: " + host, fg="green")
14+
return host
15+
16+
17+
def display_api_error(response):
18+
click.secho(f"API response error code: {response.status_code}", fg="red")
19+
try:
20+
click.secho(response.json(), fg="red")
21+
except json.decoder.JSONDecodeError:
22+
click.secho(f"No JSON response. Plain text: {response.text}", fg="yellow")
23+
return
24+
25+
26+
def send_jobretry(baseurl, jobid, token):
27+
url = baseurl + "api/jobretry"
28+
headers = {
29+
"Content-Type": "application/json; charset=utf-8",
30+
"Authorization": f"{token}",
31+
}
32+
data = {"nodeid": jobid}
33+
jdata = json.dumps(data)
34+
try:
35+
response = requests.post(url, headers=headers, data=jdata)
36+
except requests.exceptions.RequestException as e:
37+
click.secho(f"API connection error: {e}", fg="red")
38+
return
39+
40+
if response.status_code != 200:
41+
display_api_error(response)
42+
return None
43+
return response.json()
44+
45+
46+
@click.command(help="Retry a test(job) on KernelCI")
47+
@click.option(
48+
"--nodeid",
49+
help="define the node id of the (test)job to retry",
50+
required=True,
51+
)
52+
@click.pass_context
53+
def testretry(ctx, nodeid):
54+
cfg = ctx.obj.get("CFG")
55+
instance = ctx.obj.get("INSTANCE")
56+
url = api_connection(cfg[instance]["host"])
57+
resp = send_jobretry(url, nodeid, cfg[instance]["token"])
58+
if resp and "message" in resp:
59+
click.secho(resp["message"], fg="green")
60+
61+
62+
if __name__ == "__main__":
63+
main_kcidev()

tests/test_kcidev.py

+11
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ def test_kcidev_results_help():
5656
assert result.returncode == 0
5757

5858

59+
def test_kcidev_testretry_help():
60+
command = ["poetry", "run", "kci-dev", "testretry", "--help"]
61+
result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True)
62+
print("returncode: " + str(result.returncode))
63+
print("#### stdout ####")
64+
print(result.stdout)
65+
print("#### stderr ####")
66+
print(result.stderr)
67+
assert result.returncode == 0
68+
69+
5970
def test_kcidev_results_tests():
6071
command = [
6172
"poetry",

0 commit comments

Comments
 (0)