Skip to content

Commit d1d28f1

Browse files
committed
selftest: Add selftests
With the latest updates to testhelper, the complexity of the helper code has increased and there is now a good case for adding selftests to ensure that the helper code works as expected. We start by creating a few simple tests for the code in testhelper/testhelper.py. Signed-off-by: Sachin Prabhu <[email protected]>
1 parent d45636b commit d1d28f1

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ define runtox
1010
@cd "$(ROOT_DIR)" && tox -e $1
1111
endef
1212

13+
.PHONY: selftest
14+
selftest:
15+
$(call runtox, "selftest")
16+
1317
.PHONY: test
1418
test:
1519
$(call runtox, "pytest")

selftest/test-info1.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Server hostname to be tested
2+
server: server_name
3+
4+
# Users to use for authentication
5+
users:
6+
user1: user1password
7+
user2: user2password
8+
9+
# Backend filesystem of the exported shares
10+
backend: glusterfs
11+
12+
# shares: List of dict of exported shares
13+
shares:
14+
# share export1
15+
export1:
16+
# If present, it means the share is pre-mounted
17+
# at the location given
18+
path: /mnt/share/export1-cephfs-vfs
19+
backend:
20+
# If present override default backend filesystem
21+
name: cephfs.vfs
22+
# If present, use these credentials to perform the
23+
# tests for this share
24+
users:
25+
test2: x
26+
# If present, override the server to test for this share
27+
server: hostname1
28+
# share name export2
29+
export2:
30+
# Use default values set for this share

selftest/test-info2.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public_interfaces:
2+
- "192.168.123.10"
3+
- "192.168.123.11"
4+
5+
exported_sharenames:
6+
- "gluster-vol"
7+
8+
test_users:
9+
- {"username": "test1", "password": "x"}
10+
- {"username": "test2", "password": "x"}
11+
12+
test_backend: glusterfs

selftest/test_testhelper.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import testhelper
2+
from pathlib import Path
3+
4+
5+
def test_read_yaml1():
6+
testinfo = testhelper.read_yaml("test-info1.yml")
7+
8+
export1 = testinfo["shares"]["export1"]
9+
assert export1["server"] == "hostname1"
10+
assert export1["path"] == "/mnt/share/export1-cephfs-vfs"
11+
assert export1["backend"]["name"] == "cephfs.vfs"
12+
assert "user1" not in export1["users"]
13+
assert "test2" in export1["users"]
14+
assert export1["users"]["test2"] == "x"
15+
16+
export2 = testinfo["shares"]["export2"]
17+
assert export2["server"] == "server_name"
18+
assert "path" not in export2
19+
assert export2["backend"]["name"] == "glusterfs"
20+
assert "test2" not in export2["users"]
21+
assert "user2" in export2["users"]
22+
assert export2["users"]["user2"] == "user2password"
23+
24+
25+
def test_read_yaml2():
26+
testinfo = testhelper.read_yaml("test-info2.yml")
27+
28+
export = testinfo["shares"]["gluster-vol"]
29+
assert export["server"] == "192.168.123.10"
30+
assert "path" not in export
31+
assert export["backend"]["name"] == "glusterfs"
32+
assert "user1" not in export["users"]
33+
assert "test1" in export["users"]
34+
assert export["users"]["test1"] == "x"
35+
36+
37+
def test_get_share():
38+
testinfo = testhelper.read_yaml("test-info1.yml")
39+
export2 = testhelper.get_share(testinfo, "export2")
40+
assert export2["server"] == "server_name"
41+
assert "path" not in export2
42+
assert export2["backend"]["name"] == "glusterfs"
43+
assert "test2" not in export2["users"]
44+
assert "user2" in export2["users"]
45+
assert export2["users"]["user2"] == "user2password"
46+
47+
48+
def test_list_premounted():
49+
testinfo = testhelper.read_yaml("test-info1.yml")
50+
premounted = testhelper.get_premounted_shares(testinfo)
51+
assert len(premounted) == 1
52+
assert Path("/mnt/share/export1-cephfs-vfs") in premounted
53+
54+
55+
def test_generate_exported_shares():
56+
testinfo = testhelper.read_yaml("test-info1.yml")
57+
arr = []
58+
for sharename in testhelper.get_exported_shares(testinfo):
59+
share = testhelper.get_share(testinfo, sharename)
60+
arr.append((share["server"], share["name"]))
61+
assert len(arr) == 1
62+
assert ("server_name", "export2") in arr

tox.ini

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ deps =
3232
changedir = {toxinidir}
3333
commands = pytest -vrfEsxXpP testcases/consistency
3434

35+
[testenv:selftest]
36+
deps =
37+
pytest
38+
pyyaml
39+
changedir = {toxinidir}/selftest
40+
commands = pytest -vrfEsxXpP .
41+
3542
[testenv:flake8]
3643
deps = flake8
3744
changedir = {toxinidir}

0 commit comments

Comments
 (0)