Skip to content

Commit 66df89e

Browse files
Add integration tests (#240)
## 📝 Description Adding integration tests - more details at:https://jira.linode.com/browse/TPT-1831 ## ✔️ How to Test **What are the steps to reproduce the issue or verify the changes?** **How do I run the relevant unit/integration tests?** ## 📷 Preview **If applicable, include a screenshot or code snippet of this change. Otherwise, please remove this section.** --------- Co-authored-by: Zhiwei Liang <121905282+zliang-akamai@users.noreply.github.com>
1 parent 2142e4d commit 66df89e

24 files changed

Lines changed: 2453 additions & 2 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: AccTest Command
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
jobs:
8+
acctest-command:
9+
runs-on: ubuntu-latest
10+
if: ${{ github.event.issue.pull_request }}
11+
steps:
12+
- name: Slash Command Dispatch
13+
uses: peter-evans/slash-command-dispatch@v1.2.0
14+
with:
15+
token: ${{ secrets.GITHUB_TOKEN }}
16+
issue-type: pull-request
17+
commands: acctest
18+
named-args: true
19+
permission: write

.github/workflows/e2e-test-pr.yml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
on:
2+
pull_request:
3+
repository_dispatch:
4+
types: [acctest-command]
5+
6+
name: PR E2E Tests
7+
8+
jobs:
9+
# Maintainer has commented /acctest on a pull request
10+
integration-fork-ubuntu:
11+
runs-on: ubuntu-latest
12+
if:
13+
github.event_name == 'repository_dispatch' &&
14+
github.event.client_payload.slash_command.sha != '' &&
15+
github.event.client_payload.pull_request.head.sha == github.event.client_payload.slash_command.sha
16+
17+
steps:
18+
- uses: actions-ecosystem/action-regex-match@v2
19+
id: validate-tests
20+
with:
21+
text: ${{ github.event.client_payload.slash_command.tests }}
22+
regex: '[^a-z0-9-:.\/_]' # Tests validation
23+
flags: gi
24+
25+
# Check out merge commit
26+
- name: Checkout PR
27+
uses: actions/checkout@v3
28+
with:
29+
ref: ${{ github.event.client_payload.slash_command.sha }}
30+
31+
- name: Update system packages
32+
run: sudo apt-get update -y
33+
34+
- name: Install system deps
35+
run: sudo apt-get install -y build-essential
36+
37+
- name: Setup Python
38+
uses: actions/setup-python@v4
39+
with:
40+
python-version: '3.x'
41+
42+
- name: Install Python deps
43+
run: pip install -r requirements.txt -r requirements-dev.txt wheel boto3
44+
45+
- name: Install Python SDK
46+
run: make install
47+
env:
48+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
49+
50+
- run: make testint
51+
if: ${{ steps.validate-tests.outputs.match == '' }}
52+
env:
53+
LINODE_CLI_TOKEN: ${{ secrets.LINODE_TOKEN }}
54+
55+
- uses: actions/github-script@v5
56+
id: update-check-run
57+
if: ${{ always() }}
58+
env:
59+
number: ${{ github.event.client_payload.pull_request.number }}
60+
job: ${{ github.job }}
61+
conclusion: ${{ job.status }}
62+
with:
63+
github-token: ${{ secrets.GITHUB_TOKEN }}
64+
script: |
65+
const { data: pull } = await github.rest.pulls.get({
66+
...context.repo,
67+
pull_number: process.env.number
68+
});
69+
const ref = pull.head.sha;
70+
const { data: checks } = await github.rest.checks.listForRef({
71+
...context.repo,
72+
ref
73+
});
74+
const check = checks.check_runs.filter(c => c.name === process.env.job);
75+
const { data: result } = await github.rest.checks.update({
76+
...context.repo,
77+
check_run_id: check[0].id,
78+
status: 'completed',
79+
conclusion: process.env.conclusion
80+
});
81+
return result;

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
PYTHON ?= python3
22

3+
INTEGRATION_TEST_PATH :=
4+
35
@PHONEY: clean
46
clean:
57
mkdir -p dist
@@ -45,3 +47,7 @@ lint:
4547
autoflake --check linode_api4 test
4648
black --check --verbose linode_api4 test
4749
pylint linode_api4
50+
51+
@PHONEY: testint
52+
testint:
53+
python3 -m pytest test/integration/

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ tox>=4.4.0
77
Sphinx>=6.0.0
88
sphinx-autobuild>=2021.3.14
99
sphinxcontrib-fulltoc>=1.2.0
10+
pytest>=7.3.1
1011
httpretty>=1.1.4

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
httplib2
22
enum34
3-
requests
3+
requests

test/integration/__init__.py

Whitespace-only changes.

test/integration/conftest.py

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
import os
2+
import time
3+
4+
import pytest
5+
6+
from linode_api4.linode_client import LinodeClient, LongviewSubscription
7+
8+
ENV_TOKEN_NAME = "LINODE_TOKEN"
9+
RUN_LONG_TESTS = "RUN_LONG_TESTS"
10+
11+
12+
def get_token():
13+
return os.environ.get(ENV_TOKEN_NAME, None)
14+
15+
16+
def run_long_tests():
17+
return os.environ.get(RUN_LONG_TESTS, None)
18+
19+
20+
@pytest.fixture(scope="session")
21+
def create_linode(get_client):
22+
client = get_client
23+
available_regions = client.regions()
24+
chosen_region = available_regions[0]
25+
timestamp = str(int(time.time()))
26+
label = "TestSDK-" + timestamp
27+
28+
linode_instance, password = client.linode.instance_create(
29+
"g5-standard-4", chosen_region, image="linode/debian9", label=label
30+
)
31+
32+
yield linode_instance
33+
34+
linode_instance.delete()
35+
36+
37+
@pytest.fixture
38+
def create_linode_for_pass_reset(get_client):
39+
client = get_client
40+
available_regions = client.regions()
41+
chosen_region = available_regions[0]
42+
timestamp = str(int(time.time()))
43+
label = "TestSDK-" + timestamp
44+
45+
linode_instance, password = client.linode.instance_create(
46+
"g5-standard-4", chosen_region, image="linode/debian9", label=label
47+
)
48+
49+
yield linode_instance, password
50+
51+
linode_instance.delete()
52+
53+
54+
@pytest.fixture(scope="session")
55+
def ssh_key_gen():
56+
output = os.popen("ssh-keygen -q -t rsa -f ./sdk-sshkey -q -N ''")
57+
58+
time.sleep(1)
59+
60+
pub_file = open("./sdk-sshkey.pub", "r")
61+
pub_key = pub_file.read().rstrip()
62+
63+
priv_file = open("./sdk-sshkey", "r")
64+
priv_key = priv_file.read().rstrip()
65+
66+
yield pub_key, priv_key
67+
68+
os.popen("rm ./sdk-sshkey*")
69+
70+
71+
@pytest.fixture(scope="session")
72+
def get_client():
73+
token = get_token()
74+
client = LinodeClient(token)
75+
return client
76+
77+
78+
@pytest.fixture
79+
def set_account_settings(get_client):
80+
client = get_client
81+
account_settings = client.account.settings()
82+
account_settings._populated = True
83+
account_settings.network_helper = True
84+
85+
account_settings.save()
86+
87+
88+
@pytest.fixture(scope="session")
89+
def create_domain(get_client):
90+
client = get_client
91+
92+
timestamp = str(int(time.time()))
93+
domain_addr = timestamp + "-example.com"
94+
soa_email = "pathiel-test123@linode.com"
95+
96+
domain = client.domain_create(
97+
domain=domain_addr, soa_email=soa_email, tags=["test-tag"]
98+
)
99+
100+
# Create a SRV record
101+
domain.record_create(
102+
"SRV",
103+
target="rc_test",
104+
priority=10,
105+
weight=5,
106+
port=80,
107+
service="service_test",
108+
)
109+
110+
yield domain
111+
112+
domain.delete()
113+
114+
115+
@pytest.fixture(scope="session")
116+
def create_volume(get_client):
117+
client = get_client
118+
timestamp = str(int(time.time()))
119+
label = "TestSDK-" + timestamp
120+
121+
volume = client.volume_create(label=label, region="ap-west")
122+
123+
yield volume
124+
125+
volume.delete()
126+
127+
128+
@pytest.fixture
129+
def create_tag(get_client):
130+
client = get_client
131+
132+
timestamp = str(int(time.time()))
133+
label = "TestSDK-" + timestamp
134+
135+
tag = client.tag_create(label=label)
136+
137+
yield tag
138+
139+
tag.delete()
140+
141+
142+
@pytest.fixture
143+
def create_nodebalancer(get_client):
144+
client = get_client
145+
146+
timestamp = str(int(time.time()))
147+
label = "TestSDK-" + timestamp
148+
149+
nodebalancer = client.nodebalancer_create(region="us-east", label=label)
150+
151+
yield nodebalancer
152+
153+
nodebalancer.delete()
154+
155+
156+
@pytest.fixture
157+
def create_longview_client(get_client):
158+
client = get_client
159+
timestamp = str(int(time.time()))
160+
label = "TestSDK-" + timestamp
161+
longview_client = client.longview.client_create(label=label)
162+
163+
yield longview_client
164+
165+
longview_client.delete()
166+
167+
168+
@pytest.fixture
169+
def upload_sshkey(get_client, ssh_key_gen):
170+
pub_key = ssh_key_gen[0]
171+
client = get_client
172+
key = client.profile.ssh_key_upload(pub_key, "IntTestSDK-sshkey")
173+
174+
yield key
175+
176+
key.delete()
177+
178+
179+
@pytest.fixture
180+
def create_ssh_keys_object_storage(get_client):
181+
client = get_client
182+
label = "TestSDK-obj-storage-key"
183+
key = client.object_storage.keys_create(label)
184+
185+
yield key
186+
187+
key.delete()
188+
189+
190+
@pytest.fixture(scope="session")
191+
def create_firewall(get_client):
192+
client = get_client
193+
rules = {
194+
"outbound": [],
195+
"outbound_policy": "DROP",
196+
"inbound": [],
197+
"inbound_policy": "ACCEPT",
198+
}
199+
200+
firewall = client.networking.firewall_create(
201+
"test-firewall", rules=rules, status="enabled"
202+
)
203+
204+
yield firewall
205+
206+
firewall.delete()
207+
208+
209+
@pytest.fixture
210+
def create_oauth_client(get_client):
211+
client = get_client
212+
oauth_client = client.account.oauth_client_create(
213+
"test-oauth-client", "https://localhost/oauth/callback"
214+
)
215+
216+
yield oauth_client
217+
218+
oauth_client.delete()

0 commit comments

Comments
 (0)