Skip to content

Commit f879ac4

Browse files
committed
Add tests and github actions
1 parent 4167f97 commit f879ac4

14 files changed

+196
-4
lines changed

.github/workflows/publish.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Build and publish to PyPi
2+
on:
3+
release:
4+
types: [created]
5+
6+
jobs:
7+
build-n-publish:
8+
name: Build and publish to PyPi
9+
runs-on: ubuntu-20.04
10+
steps:
11+
- uses: actions/checkout@master
12+
- uses: actions/setup-python@v2
13+
- name: Install dependencies
14+
run: |
15+
python -m pip install --upgrade setuptools wheel
16+
- name: Build
17+
run: |
18+
python setup.py sdist bdist_wheel
19+
- name: Publish
20+
uses: pypa/gh-action-pypi-publish@master
21+
with:
22+
password: ${{ secrets.TEST_PYPI_API_TOKEN }}

.github/workflows/tests.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ${{ matrix.platform }}
8+
strategy:
9+
max-parallel: 4
10+
matrix:
11+
# https://help.github.com/articles/virtual-environments-for-github-actions
12+
platform:
13+
- ubuntu-16.04
14+
- ubuntu-latest # ubuntu-18.04
15+
- macos-latest # macOS-10.14
16+
- windows-latest # windows-2019
17+
python-version: [3.6, 3.7, 3.8, 3.9]
18+
19+
steps:
20+
- uses: actions/checkout@v2
21+
- name: Set up Python ${{ matrix.python-version }}
22+
uses: actions/setup-python@v2
23+
with:
24+
python-version: ${{ matrix.python-version }}
25+
- name: Install dependencies
26+
run: |
27+
python -m pip install --upgrade setuptools pip wheel pytest mock requests
28+
- name: Test with pytest
29+
run: pytest -rp
30+

dynatrace/environment_v1/custom_device.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import datetime, timedelta, timezone
2-
from collections import MutableSequence
2+
from collections.abc import MutableSequence
33
from typing import Optional, List, Dict, Tuple
44

55

dynatrace/http_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def __init__(
5656
total=retries,
5757
backoff_factor=retry_delay_s,
5858
status_forcelist=[400, 401, 403, 413, 429, 500, 502, 503, 504],
59-
method_whitelist=["TRACE", "PUT", "DELETE", "OPTIONS", "HEAD", "GET", "POST"],
59+
allowed_methods=["TRACE", "PUT", "DELETE", "OPTIONS", "HEAD", "GET", "POST"],
6060
raise_on_status=False,
6161
)
6262

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
requests>=2.24
1+
requests>=2.21

requirements_dev.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
requests>=2.21
2+
pytest>=6.2.3
3+
mock
4+
tox

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
setup(
44
name="dtapi",
5-
version="1.1.10",
5+
version="1.1.11",
66
packages=find_packages(),
77
install_requires=["requests>=2.21"],
8+
tests_require=["pytest", "mock", "tox"],
89
python_requires=">=3.6",
910
author="David Lopes",
1011
author_email="[email protected]",

test/__init__.py

Whitespace-only changes.

test/configuration_v1/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from dynatrace import Dynatrace
2+
from dynatrace.configuration_v1.alerting_profiles import AlertingProfileStub, AlertingProfile, AlertingProfileSeverityRule
3+
from dynatrace.pagination import PaginatedList
4+
5+
6+
def test_list(dt: Dynatrace):
7+
alert_profiles = dt.alerting_profiles.list()
8+
assert isinstance(alert_profiles, PaginatedList)
9+
10+
list_alert_profiles = list(alert_profiles)
11+
assert len(list_alert_profiles) == 6
12+
13+
first = list_alert_profiles[0]
14+
assert isinstance(first, AlertingProfileStub)
15+
16+
assert first.id == "b1f379d9-98b4-4efe-be38-0289609c9295"
17+
assert first.name == "deployment_change_autoremediation"
18+
19+
20+
def test_get_full_configuration(dt: Dynatrace):
21+
alert_profiles = dt.alerting_profiles.list()
22+
list_alert_profiles = list(alert_profiles)
23+
first = list_alert_profiles[0]
24+
25+
full = first.get_full_configuration()
26+
assert isinstance(full, AlertingProfile)
27+
assert full.id == "b1f379d9-98b4-4efe-be38-0289609c9295"
28+
assert full.display_name == "deployment_change_autoremediation"
29+
assert isinstance(full.rules, list)
30+
assert isinstance(full.rules[0], AlertingProfileSeverityRule)

test/conftest.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
from pathlib import Path
3+
from typing import Optional, Dict
4+
from unittest import mock
5+
import json
6+
7+
import pytest
8+
9+
from dynatrace import Dynatrace
10+
from dynatrace.http_client import HttpClient
11+
12+
current_file_path = os.path.dirname(os.path.realpath(__file__))
13+
14+
15+
class MockResponse:
16+
def __init__(self, json_data):
17+
self.json_data = json_data
18+
self.headers = {}
19+
20+
def json(self):
21+
return self.json_data
22+
23+
24+
def local_make_request(self, path: str, params: Optional[Dict] = None, headers: Optional[Dict] = None, method="GET", data=None) -> MockResponse:
25+
file_name = f'{path.replace("/", "_").lstrip("_")}.json'
26+
file_path = Path(current_file_path, "mock_data", file_name)
27+
with open(file_path) as f:
28+
json_data = json.load(f)
29+
return MockResponse(json_data)
30+
31+
32+
@pytest.fixture(autouse=True)
33+
def dt():
34+
with mock.patch.object(HttpClient, "make_request", new=local_make_request):
35+
dt = Dynatrace("mock_tenant", "mock_token")
36+
yield dt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"values": [
3+
{
4+
"id": "b1f379d9-98b4-4efe-be38-0289609c9295",
5+
"name": "deployment_change_autoremediation"
6+
},
7+
{
8+
"id": "c21f969b-5f03-333d-83e0-4f8f136e7682",
9+
"name": "Default"
10+
},
11+
{
12+
"id": "142e6edb-280a-4e9c-85b1-e01f5d2ee829",
13+
"name": "dbslowdwon_autoremediation"
14+
},
15+
{
16+
"id": "b68e03c2-8a28-45a1-a516-4abce33317e7",
17+
"name": "process_crash_autoremediation"
18+
},
19+
{
20+
"id": "caa901e6-8163-413c-ac64-5de723a400ab",
21+
"name": "rguerrero"
22+
},
23+
{
24+
"id": "ebb53f45-6cc6-4efa-a271-1a9657a433c0",
25+
"name": "Keptn"
26+
}
27+
]
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"metadata": {
3+
"currentConfigurationVersions": [
4+
"0"
5+
],
6+
"configurationVersions": [],
7+
"clusterVersion": "1.214.112.20210409-064503"
8+
},
9+
"id": "b1f379d9-98b4-4efe-be38-0289609c9295",
10+
"displayName": "deployment_change_autoremediation",
11+
"rules": [
12+
{
13+
"severityLevel": "PERFORMANCE",
14+
"tagFilter": {
15+
"includeMode": "NONE",
16+
"tagFilters": []
17+
},
18+
"delayInMinutes": 15
19+
},
20+
{
21+
"severityLevel": "AVAILABILITY",
22+
"tagFilter": {
23+
"includeMode": "NONE",
24+
"tagFilters": []
25+
},
26+
"delayInMinutes": 10
27+
}
28+
],
29+
"managementZoneId": null,
30+
"mzId": null,
31+
"eventTypeFilters": []
32+
}

tox.ini

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[tox]
2+
envlist = py{36,37,38,39}
3+
4+
[testenv]
5+
deps =
6+
pytest
7+
mock
8+
commands =
9+
pytest -rp

0 commit comments

Comments
 (0)