Skip to content

Commit 548ea20

Browse files
committed
Added tests with mocks
Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent 09d516f commit 548ea20

5 files changed

Lines changed: 141 additions & 3 deletions

File tree

src/cfengine_cli/validate.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import os
1616
import subprocess
1717

18+
cmd_cache = {}
19+
1820

1921
def validate_state_bootstrap(bootstrap):
2022
state = read_json(CLOUD_STATE_FPATH)
@@ -51,9 +53,16 @@ def validate_version(version, edition):
5153

5254

5355
def validate_vagrant_box(box):
54-
ret = subprocess.run(["vagrant", "box", "list"], capture_output=True, text=True)
56+
cmd = ("vagrant", "box", "list")
57+
if cmd in cmd_cache:
58+
cmd_output = cmd_cache[cmd]
59+
else:
60+
ret = subprocess.run(cmd, capture_output=True, text=True)
61+
cmd_output = ret.stdout
62+
cmd_cache[cmd] = cmd_output
63+
5564
box_list = [
56-
line.split()[0] for line in ret.stdout.split("\n") if len(line.split()) > 0
65+
line.split()[0] for line in cmd_output.split("\n") if len(line.split()) > 0
5766
]
5867

5968
if box not in box_list:
@@ -124,7 +133,7 @@ def validate_gcp_credentials():
124133
creds_data["gcp"]["key_path"],
125134
)
126135
except KeyError:
127-
raise UserError("Incomplete AWS credential info") # TODO: report missing keys
136+
raise UserError("Incomplete GCP credential info") # TODO: report missing keys
128137

129138
region = creds_data["gcp"].get("region", "europe-west1-b")
130139

tests/run-up-validate-tests.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#/usr/bin/env bash
2+
3+
set -e
4+
5+
echo "These tests expect cfengine CLI to be installed globally or in venv"
6+
7+
echo "Looking for CFEngine CLI"
8+
cfengine --version
9+
10+
echo "Check that test files are in expected location:"
11+
ls -al tests/up-validate/*.yaml
12+
13+
rm -rf tmp
14+
mkdir -p tmp/bin
15+
mkdir -p tmp/home/.cfengine/cf-remote
16+
17+
# Mocking vagrant
18+
cp tests/up-validate/mocks/vagrant tmp/bin/.
19+
export PATH="$(pwd)/tmp/bin:$PATH"
20+
21+
# Mocking cloud_config.json
22+
cp tests/up-validate/mocks/cloud_config.json tmp/home/.cfengine/cf-remote/.
23+
MOCK_HOME="$(pwd)/tmp/home"
24+
25+
for file in tests/up-validate/*.yaml; do
26+
HOME="$MOCK_HOME" cfengine up "$file" --validate
27+
done
28+
29+
echo "All cfengine up --validate tests successful!"
30+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
templates:
2+
cfengine3.27:
3+
version: 3.27.0
4+
edition: enterprise
5+
bootstrap: hub1
6+
demo: false
7+
8+
cfengine-master:
9+
remote_download: true
10+
hub_package: "http://buildcache.cfengine.com/packages/testing-pr/jenkins-master-nightly-pipeline-333/PACKAGES_HUB_x86_64_linux_debian_11/cfengine-nova-hub_3.28.0a.1433c0c2f~133.debian11_amd64.deb"
11+
client_package: "http://buildcache.cfengine.com/packages/testing-pr/jenkins-master-nightly-pipeline-333/PACKAGES_x86_64_linux_debian_11/cfengine-nova_3.28.0a.1433c0c2f~133.debian11_amd64.deb"
12+
bootstrap: hub2
13+
14+
groups:
15+
- hub1:
16+
role: hub
17+
cfengine: cfengine3.27
18+
source:
19+
mode: spawn
20+
spawn:
21+
provider: vagrant
22+
vagrant:
23+
box: ubuntu/focal64
24+
memory: 512
25+
cpus: 1
26+
sync_folder: null
27+
provision: null
28+
count: 1
29+
30+
- hub2:
31+
role: hub
32+
cfengine: cfengine-master
33+
source:
34+
mode: save
35+
hosts: ["8.8.8.8"]
36+
37+
- client2:
38+
role: client
39+
cfengine: cfengine3.27
40+
source:
41+
mode: spawn
42+
spawn:
43+
provider: aws
44+
aws:
45+
image: debian-13
46+
size: xlarge
47+
count: 3
48+
49+
- client3:
50+
role: client
51+
cfengine: cfengine-master
52+
source:
53+
mode: spawn
54+
spawn:
55+
provider: gcp
56+
gcp:
57+
image: debian-13
58+
network: null
59+
public_ip: true
60+
size: n1-standard-1
61+
count: 2
62+
63+
- client4:
64+
role: client
65+
source:
66+
mode: spawn
67+
spawn:
68+
provider: vagrant
69+
vagrant:
70+
box: generic/centos7
71+
count: 1
72+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"aws": {
3+
"key": "TBD",
4+
"secret": "TBD",
5+
"key_pair": "TDB",
6+
"security_groups": [
7+
"TDB"
8+
],
9+
"region": "eu-west-2"
10+
},
11+
"gcp" : {
12+
"project_id" : "TDB",
13+
"service_account_id" : "TDB",
14+
"key_path" : "TDB"
15+
}
16+
}

tests/up-validate/mocks/vagrant

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
if [[ "$1" == "box" && "$2" == "list" ]]; then
4+
echo "generic/centos7 (virtualbox, 4.3.12, (amd64))"
5+
echo "ubuntu/focal64 (virtualbox, 20240821.0.1)"
6+
exit 0
7+
fi
8+
9+
echo "Mock Vagrant: Unknown command $@" >&2
10+
exit 1
11+

0 commit comments

Comments
 (0)