Skip to content

Commit 651059a

Browse files
committed
add 3rd party integration tests
1 parent 5476126 commit 651059a

File tree

6 files changed

+290
-4
lines changed

6 files changed

+290
-4
lines changed

README.md

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,69 @@ Log-in to Rancher and create a new workload:
9393
* `CLOUDCLI_API_SERVER` = `https://api.example.com`
9494
* `CLOUDCLI_PROVIDER` = `proxy`
9595

96-
## Running libcloud tests
96+
## Running 3rd party integration tests
9797

9898
Create a Python virtualenv
9999

100100
```
101101
mkdir tests/venv
102102
python3 -m virtualenv -p python3 tests/venv
103-
tests/venv/bin/pip install https://github.com/OriHoch/libcloud/archive/kamatera-compute.zip
104103
```
105104

106-
Run the cloudcli-server on localhost and then run the tests:
105+
Start cloudcli-server localy
106+
107+
#### Running Libcloud tests
108+
109+
Install Libcloud development version
110+
111+
```
112+
tests/venv/bin/python3 -m pip install -e git+https://git-wip-us.apache.org/repos/asf/libcloud.git@trunk#egg=apache-libcloud
113+
```
114+
115+
Run the tests:
107116

108117
```
109118
export API_CLIENT_ID=XXX
110119
export API_SECRET=YYY
111120
export API_SERVER=localhost
112-
tests/venv/bin/python tests/libcloud_create_node.py && tests/venv/bin/python tests/libcloud_node_operations.py
121+
tests/venv/bin/python3 tests/libcloud_create_node.py && tests/venv/bin/python3 tests/libcloud_node_operations.py
122+
```
123+
124+
#### Running Ansible tests
125+
126+
[Install latest Ansible](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html)
127+
128+
Verify version: `ansible-galaxy --version | grep "ansible-galaxy 2.9."`
129+
130+
Install the Kamatera collection:
131+
132+
```
133+
ansible-galaxy collection install --force https://github.com/OriHoch/ansible-collection-kamatera/releases/download/v0.0.1/kamatera-kamatera-0.0.0.tar.gz
134+
```
135+
136+
Run the tests:
137+
138+
```
139+
export KAMATERA_API_CLIENT_ID=
140+
export KAMATERA_API_SECRET=
141+
export KAMATERA_API_URL=http://localhost:8000
142+
tests/venv/bin/python3 tests/ansible.py
143+
```
144+
145+
#### Running Salt tests
146+
147+
Install Salt with Kamatera
148+
149+
```
150+
tests/venv/bin/python3 -m pip install https://github.com/OriHoch/salt/archive/kamatera-cloud.zip
151+
```
152+
153+
Run the tests:
154+
155+
```
156+
export KAMATERA_API_CLIENT_ID=
157+
export KAMATERA_API_SECRET=
158+
export KAMATERA_API_URL=http://localhost:8000
159+
export SALT_BIN_DIR=`pwd`/tests/venv/bin
160+
tests/venv/bin/python3 tests/salt.py
113161
```

tests/ansible.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import subprocess
2+
import tempfile
3+
import json
4+
import os
5+
6+
7+
testsdir = os.path.dirname(__file__)
8+
outputdir = os.path.abspath(os.path.join(testsdir, "output"))
9+
os.makedirs(outputdir, exist_ok=True)
10+
11+
subprocess.check_call(["ansible-playbook", "tests/ansible_playbooks/compute_datacenters_playbook.yml", "-e", "output_dir=%s" % outputdir])
12+
13+
with open(os.path.join(outputdir, 'kamatera_datacenters.json')) as f:
14+
datacenters = json.load(f)
15+
assert set(datacenters[0].keys()) == set(['category', 'subCategory', 'name', 'id']), set(datacenters[0].keys())
16+
for k,v in datacenters[0].items():
17+
assert len(v) > 1, '%s=%s' % (k, v)
18+
19+
subprocess.check_call(["ansible-playbook", "tests/ansible_playbooks/compute_options_playbook.yml", "-e", "output_dir=%s" % outputdir, "-e", "datacenter=EU-LO"])
20+
21+
with open(os.path.join(outputdir, 'kamatera_datacenter_capabilities.json')) as f:
22+
capabilities = json.load(f)
23+
assert set(capabilities.keys()) == set(['cpuTypes', 'defaultMonthlyTrafficPackage', 'diskSizeGB', 'monthlyTrafficPackage']), set(capabilities.keys())
24+
for k,v in capabilities.items():
25+
assert len(v) > 0, '%s=%s' % (k,v)
26+
27+
with open(os.path.join(outputdir, 'kamatera_datacenter_images.json')) as f:
28+
images = json.load(f)
29+
assert set(images[0].keys()) == set(['datacenter', 'code', 'name', 'osDiskSizeGB', 'ramMBMin', 'os', 'id']), set(images[0].keys())
30+
for k,v in images[0].items():
31+
if k == 'osDiskSizeGB':
32+
assert v > 0, '%s=%s' % (k,v)
33+
else:
34+
assert len(v) > 0, '%s=%s' % (k,v)
35+
for image in images:
36+
if image['os'] == 'Ubuntu' and image['code'] == '18.04 64bit':
37+
image_id = image['id']
38+
39+
create_args = [
40+
"-e", "datacenter=EU-LO",
41+
"-e", "image=%s" % image_id,
42+
"-e", "cpu_type=%s" % capabilities['cpuTypes'][0]['id'],
43+
"-e", "cpu_cores=%s" % capabilities['cpuTypes'][0]['cpuCores'][2],
44+
"-e", "ram_mb=%s" % capabilities['cpuTypes'][0]['ramMB'][4],
45+
"-e", "disk_size_gb=%s" % capabilities['diskSizeGB'][4],
46+
"-e", "disk_size_2_gb=%s" % capabilities['diskSizeGB'][2]
47+
]
48+
49+
subprocess.check_call(["ansible-playbook", "tests/ansible_playbooks/compute_playbook.yml", "-e", "output_dir=%s" % outputdir, "-e", "server_name=ansibletest", *create_args])
50+
51+
print("Great Success!")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- name: Get capabilities
2+
hosts: localhost
3+
tasks:
4+
- kamatera.kamatera.kamatera_compute_options: {}
5+
register: datacenters
6+
- copy:
7+
content: "{{ datacenters.datacenters | to_nice_json }}"
8+
dest: "{{ output_dir | default('/tmp') }}/kamatera_datacenters.json"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
- name: Get capabilities
2+
hosts: localhost
3+
tasks:
4+
- kamatera.kamatera.kamatera_compute_options:
5+
datacenter: "{{ datacenter }}"
6+
register: capabilities
7+
- copy:
8+
content: "{{ capabilities.images | to_nice_json }}"
9+
dest: "{{ output_dir | default('/tmp') }}/kamatera_datacenter_images.json"
10+
- copy:
11+
content: "{{ capabilities.capabilities | to_nice_json }}"
12+
dest: "{{ output_dir | default('/tmp') }}/kamatera_datacenter_capabilities.json"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
- name: Provisioning and operations example
2+
hosts: localhost
3+
tasks:
4+
- kamatera.kamatera.kamatera_compute:
5+
server_names: ['{{ server_name }}_1','{{ server_name }}_2']
6+
state: present
7+
datacenter: "{{ datacenter }}"
8+
image: "{{ image }}"
9+
cpu_type: "{{ cpu_type }}"
10+
cpu_cores: "{{ cpu_cores }}"
11+
ram_mb: "{{ ram_mb }}"
12+
disk_size_gb: "{{ disk_size_gb }}"
13+
extra_disk_sizes_gb:
14+
- "{{ disk_size_2_gb }}"
15+
register: res
16+
- copy:
17+
content: "{{ res | to_nice_json }}"
18+
dest: "{{ output_dir | default('/tmp') }}/kamatera_create_servers.json"
19+
- kamatera.kamatera.kamatera_compute:
20+
server_names: '{{ res.server_names }}'
21+
state: restarted
22+
register: res
23+
- copy:
24+
content: "{{ res | to_nice_json }}"
25+
dest: "{{ output_dir | default('/tmp') }}/kamatera_restart_servers.json"
26+
- kamatera.kamatera.kamatera_compute:
27+
server_names: '{{ res.server_names }}'
28+
state: stopped
29+
register: res
30+
- copy:
31+
content: "{{ res | to_nice_json }}"
32+
dest: "{{ output_dir | default('/tmp') }}/kamatera_stop_servers.json"
33+
- kamatera.kamatera.kamatera_compute:
34+
server_names: '{{ res.server_names }}'
35+
state: running
36+
register: res
37+
- copy:
38+
content: "{{ res | to_nice_json }}"
39+
dest: "{{ output_dir | default('/tmp') }}/kamatera_start_servers.json"
40+
- kamatera.kamatera.kamatera_compute:
41+
server_names: '{{ res.server_names }}'
42+
state: absent
43+
register: res
44+
- copy:
45+
content: "{{ res | to_nice_json }}"
46+
dest: "{{ output_dir | default('/tmp') }}/kamatera_delete_servers.json"

tests/salt.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import json
2+
import os
3+
import subprocess
4+
5+
testsdir = os.path.dirname(__file__)
6+
outputdir = os.path.abspath(os.path.join(testsdir, "output"))
7+
os.makedirs(outputdir, exist_ok=True)
8+
9+
os.makedirs("/etc/salt/cloud.providers.d", exist_ok=True)
10+
if os.path.exists("/etc/salt/cloud.providers.d/cloudcli-server-test-kamatera.conf"):
11+
os.unlink("/etc/salt/cloud.providers.d/cloudcli-server-test-kamatera.conf")
12+
with open("/etc/salt/cloud.providers.d/cloudcli-server-test-kamatera.conf", "w") as f:
13+
f.write("""
14+
cloudcli-server-test-kamatera:
15+
driver: kamatera
16+
api_client_id: {KAMATERA_API_CLIENT_ID}
17+
api_secret: {KAMATERA_API_SECRET}
18+
api_url: {KAMATERA_API_URL}
19+
""".format(**os.environ))
20+
21+
print("listing locations...")
22+
with open(os.path.join(outputdir, "salt-locations.json"), "wb") as f:
23+
f.write(subprocess.check_output(["%s/salt-cloud" % os.environ["SALT_BIN_DIR"], "--out=json", "--list-locations", "cloudcli-server-test-kamatera"]))
24+
25+
with open(os.path.join(outputdir, "salt-locations.json")) as f:
26+
locations = json.load(f)["cloudcli-server-test-kamatera"]["kamatera"]
27+
assert locations["IL"] == "Rosh Haayin, Israel (Middle East)"
28+
assert len(locations) > 10
29+
print("OK")
30+
31+
print("listing sizes...")
32+
with open(os.path.join(outputdir, "salt-sizes.json"), "wb") as f:
33+
f.write(subprocess.check_output(["%s/salt-cloud" % os.environ["SALT_BIN_DIR"], "--out=json", "--location=IL", "--list-sizes", "cloudcli-server-test-kamatera"]))
34+
35+
with open(os.path.join(outputdir, "salt-sizes.json")) as f:
36+
sizes = json.load(f)["cloudcli-server-test-kamatera"]["kamatera"]
37+
assert len(sizes) > 3
38+
assert sizes["B"]["name"] == "Type B - General Purpose"
39+
print("OK")
40+
41+
print("listing available server options...")
42+
with open(os.path.join(outputdir, "salt-server-options.json"), "wb") as f:
43+
f.write(subprocess.check_output(["%s/salt-cloud" % os.environ["SALT_BIN_DIR"], "--out=json", "--location=IL", "-f", "avail_server_options", "cloudcli-server-test-kamatera"]))
44+
45+
with open(os.path.join(outputdir, "salt-server-options.json")) as f:
46+
server_options = json.load(f)["cloudcli-server-test-kamatera"]["kamatera"]
47+
assert len(server_options) > 1
48+
assert server_options["monthlyTrafficPackage"]["t5000"] == "5000GB/month on 10Gbit/sec port"
49+
print("OK")
50+
51+
print("listing images...")
52+
with open(os.path.join(outputdir, "salt-images.json"), "wb") as f:
53+
f.write(subprocess.check_output(["%s/salt-cloud" % os.environ["SALT_BIN_DIR"], "--out=json", "--location=IL", "--list-images", "cloudcli-server-test-kamatera"]))
54+
55+
with open(os.path.join(outputdir, "salt-images.json")) as f:
56+
images = json.load(f)["cloudcli-server-test-kamatera"]["kamatera"]
57+
assert len(images) > 10
58+
assert images["IL:6000C29a5a7220dcf84716e7bba74215"] == "Ubuntu Server version 18.04 LTS (bionic) 64-bit"
59+
print("OK")
60+
61+
os.makedirs("/etc/salt/cloud.profiles.d", exist_ok=True)
62+
if os.path.exists("/etc/salt/cloud.profiles.d/cloudcli-server-test.conf"):
63+
os.unlink("/etc/salt/cloud.profiles.d/cloudcli-server-test.conf")
64+
with open("/etc/salt/cloud.profiles.d/cloudcli-server-test.conf", "w") as f:
65+
f.write("""
66+
cloudcli-server-test:
67+
provider: cloudcli-server-test-kamatera
68+
location: IL
69+
cpu_type: B
70+
cpu_cores: 2
71+
ram_mb: 2048
72+
disk_size_gb: 20
73+
extra_disk_sizes_gb:
74+
- 15
75+
billing_cycle: hourly
76+
monthly_traffic_package: t5000
77+
image: IL:6000C29a5a7220dcf84716e7bba74215
78+
networks:
79+
- name: wan
80+
ip: auto
81+
daily_backup: false
82+
managed: false
83+
""")
84+
85+
print("creating server...")
86+
with open(os.path.join(outputdir, "salt-create-server.json"), "wb") as f:
87+
f.write(subprocess.check_output(["%s/salt-cloud" % os.environ["SALT_BIN_DIR"], "--out=json", "-p", "cloudcli-server-test", "salttest"]))
88+
89+
with open(os.path.join(outputdir, "salt-create-server.json")) as f:
90+
jsonlines = None
91+
for line in f.readlines():
92+
if line.strip() == "{":
93+
jsonlines = ["{"]
94+
elif jsonlines is not None:
95+
jsonlines.append(line.strip())
96+
created_server = json.loads("\n".join(jsonlines))["salttest"]
97+
assert created_server["deployed"] == True
98+
created_server_name = created_server["name"]
99+
print("created_server_name = " + created_server_name)
100+
print("OK")
101+
102+
print("listing servers..")
103+
with open(os.path.join(outputdir, "salt-query.json"), "wb") as f:
104+
f.write(subprocess.check_output(["%s/salt-cloud" % os.environ["SALT_BIN_DIR"], "--out=json", "-Q"]))
105+
106+
with open(os.path.join(outputdir, "salt-query.json")) as f:
107+
servers = json.load(f)["cloudcli-server-test-kamatera"]["kamatera"]
108+
assert len(servers) > 0
109+
assert servers[created_server_name]["state"] == "running"
110+
print("OK")
111+
112+
print("deleting server...")
113+
with open(os.path.join(outputdir, "salt-delete.json"), "wb") as f:
114+
f.write(subprocess.check_output(["%s/salt-cloud" % os.environ["SALT_BIN_DIR"], "--out=json", "-y", "-d", created_server_name]))
115+
116+
with open(os.path.join(outputdir, "salt-delete.json")) as f:
117+
servers = json.load(f)["cloudcli-server-test-kamatera"]["kamatera"]
118+
assert len(servers) > 0
119+
assert servers[created_server_name]["state"] == "destroyed"
120+
assert servers[created_server_name]["success"] == True
121+
print("OK")

0 commit comments

Comments
 (0)