Skip to content

Commit

Permalink
Merge pull request #10 from dlopes7/tests
Browse files Browse the repository at this point in the history
Add tests and github actions
  • Loading branch information
dlopes7 authored May 1, 2021
2 parents 4167f97 + f879ac4 commit f25f9f6
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 4 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Build and publish to PyPi
on:
release:
types: [created]

jobs:
build-n-publish:
name: Build and publish to PyPi
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@master
- uses: actions/setup-python@v2
- name: Install dependencies
run: |
python -m pip install --upgrade setuptools wheel
- name: Build
run: |
python setup.py sdist bdist_wheel
- name: Publish
uses: pypa/gh-action-pypi-publish@master
with:
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
30 changes: 30 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Tests

on: [push, pull_request]

jobs:
build:
runs-on: ${{ matrix.platform }}
strategy:
max-parallel: 4
matrix:
# https://help.github.com/articles/virtual-environments-for-github-actions
platform:
- ubuntu-16.04
- ubuntu-latest # ubuntu-18.04
- macos-latest # macOS-10.14
- windows-latest # windows-2019
python-version: [3.6, 3.7, 3.8, 3.9]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade setuptools pip wheel pytest mock requests
- name: Test with pytest
run: pytest -rp

2 changes: 1 addition & 1 deletion dynatrace/environment_v1/custom_device.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime, timedelta, timezone
from collections import MutableSequence
from collections.abc import MutableSequence
from typing import Optional, List, Dict, Tuple


Expand Down
2 changes: 1 addition & 1 deletion dynatrace/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __init__(
total=retries,
backoff_factor=retry_delay_s,
status_forcelist=[400, 401, 403, 413, 429, 500, 502, 503, 504],
method_whitelist=["TRACE", "PUT", "DELETE", "OPTIONS", "HEAD", "GET", "POST"],
allowed_methods=["TRACE", "PUT", "DELETE", "OPTIONS", "HEAD", "GET", "POST"],
raise_on_status=False,
)

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
requests>=2.24
requests>=2.21
4 changes: 4 additions & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
requests>=2.21
pytest>=6.2.3
mock
tox
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

setup(
name="dtapi",
version="1.1.10",
version="1.1.11",
packages=find_packages(),
install_requires=["requests>=2.21"],
tests_require=["pytest", "mock", "tox"],
python_requires=">=3.6",
author="David Lopes",
author_email="[email protected]",
Expand Down
Empty file added test/__init__.py
Empty file.
Empty file.
30 changes: 30 additions & 0 deletions test/configuration_v1/test_alerting_profiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from dynatrace import Dynatrace
from dynatrace.configuration_v1.alerting_profiles import AlertingProfileStub, AlertingProfile, AlertingProfileSeverityRule
from dynatrace.pagination import PaginatedList


def test_list(dt: Dynatrace):
alert_profiles = dt.alerting_profiles.list()
assert isinstance(alert_profiles, PaginatedList)

list_alert_profiles = list(alert_profiles)
assert len(list_alert_profiles) == 6

first = list_alert_profiles[0]
assert isinstance(first, AlertingProfileStub)

assert first.id == "b1f379d9-98b4-4efe-be38-0289609c9295"
assert first.name == "deployment_change_autoremediation"


def test_get_full_configuration(dt: Dynatrace):
alert_profiles = dt.alerting_profiles.list()
list_alert_profiles = list(alert_profiles)
first = list_alert_profiles[0]

full = first.get_full_configuration()
assert isinstance(full, AlertingProfile)
assert full.id == "b1f379d9-98b4-4efe-be38-0289609c9295"
assert full.display_name == "deployment_change_autoremediation"
assert isinstance(full.rules, list)
assert isinstance(full.rules[0], AlertingProfileSeverityRule)
36 changes: 36 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
from pathlib import Path
from typing import Optional, Dict
from unittest import mock
import json

import pytest

from dynatrace import Dynatrace
from dynatrace.http_client import HttpClient

current_file_path = os.path.dirname(os.path.realpath(__file__))


class MockResponse:
def __init__(self, json_data):
self.json_data = json_data
self.headers = {}

def json(self):
return self.json_data


def local_make_request(self, path: str, params: Optional[Dict] = None, headers: Optional[Dict] = None, method="GET", data=None) -> MockResponse:
file_name = f'{path.replace("/", "_").lstrip("_")}.json'
file_path = Path(current_file_path, "mock_data", file_name)
with open(file_path) as f:
json_data = json.load(f)
return MockResponse(json_data)


@pytest.fixture(autouse=True)
def dt():
with mock.patch.object(HttpClient, "make_request", new=local_make_request):
dt = Dynatrace("mock_tenant", "mock_token")
yield dt
28 changes: 28 additions & 0 deletions test/mock_data/api_config_v1_alertingProfiles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"values": [
{
"id": "b1f379d9-98b4-4efe-be38-0289609c9295",
"name": "deployment_change_autoremediation"
},
{
"id": "c21f969b-5f03-333d-83e0-4f8f136e7682",
"name": "Default"
},
{
"id": "142e6edb-280a-4e9c-85b1-e01f5d2ee829",
"name": "dbslowdwon_autoremediation"
},
{
"id": "b68e03c2-8a28-45a1-a516-4abce33317e7",
"name": "process_crash_autoremediation"
},
{
"id": "caa901e6-8163-413c-ac64-5de723a400ab",
"name": "rguerrero"
},
{
"id": "ebb53f45-6cc6-4efa-a271-1a9657a433c0",
"name": "Keptn"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"metadata": {
"currentConfigurationVersions": [
"0"
],
"configurationVersions": [],
"clusterVersion": "1.214.112.20210409-064503"
},
"id": "b1f379d9-98b4-4efe-be38-0289609c9295",
"displayName": "deployment_change_autoremediation",
"rules": [
{
"severityLevel": "PERFORMANCE",
"tagFilter": {
"includeMode": "NONE",
"tagFilters": []
},
"delayInMinutes": 15
},
{
"severityLevel": "AVAILABILITY",
"tagFilter": {
"includeMode": "NONE",
"tagFilters": []
},
"delayInMinutes": 10
}
],
"managementZoneId": null,
"mzId": null,
"eventTypeFilters": []
}
9 changes: 9 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[tox]
envlist = py{36,37,38,39}

[testenv]
deps =
pytest
mock
commands =
pytest -rp

0 comments on commit f25f9f6

Please sign in to comment.