Skip to content

Commit 481a5bd

Browse files
committed
testing: add e2e namespace/admin test
1 parent 87d5300 commit 481a5bd

File tree

5 files changed

+221
-0
lines changed

5 files changed

+221
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
users:
2+
- name: warnet-user
3+
roles:
4+
- pod-viewer
5+
- pod-manager
6+
# the pod-viewer and pod-manager roles are the default
7+
# roles defined in values.yaml for the namespaces charts
8+
#
9+
# if you need a different set of roles for a particular namespaces
10+
# deployment, you can override values.yaml by providing your own
11+
# role definitions below
12+
#
13+
# roles:
14+
# - name: my-custom-role
15+
# rules:
16+
# - apiGroups: ""
17+
# resources: ""
18+
# verbs: ""
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespaces:
2+
- name: wargames-red-team
3+
users:
4+
- name: alice
5+
roles:
6+
- pod-viewer
7+
- name: bob
8+
roles:
9+
- pod-viewer
10+
- pod-manager
11+
- name: wargames-blue-team
12+
users:
13+
- name: mallory
14+
roles:
15+
- pod-viewer
16+
- name: carol
17+
roles:
18+
- pod-viewer
19+
- pod-manager
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
nodes:
2+
- name: tank-0001
3+
image:
4+
tag: "26.0"
5+
connect:
6+
- tank-0002.wargames-red-team.svc.cluster.local
7+
- tank-0003.wargames-blue-team.svc.cluster.local
8+
- name: tank-0002
9+
resources:
10+
limits:
11+
cpu: 100m
12+
memory: 128Mi
13+
requests:
14+
cpu: 100m
15+
memory: 128Mi
16+
connect:
17+
- tank-0003.wargames-red-team.svc.cluster.local
18+
- tank-0004.wargames-blue-team.svc.cluster.local
19+
- name: tank-0003
20+
connect:
21+
- tank-0004.wargames-red-team.svc.cluster.local
22+
- tank-0005.wargames-blue-team.svc.cluster.local
23+
- name: tank-0004
24+
connect:
25+
- tank-0005.wargames-red-team.svc.cluster.local
26+
- tank-0006.wargames-blue-team.svc.cluster.local
27+
- name: tank-0005
28+
connect:
29+
- tank-0006.wargames-red-team.svc.cluster.local
30+
- name: tank-0006
31+
fork_observer:
32+
enabled: false
33+
caddy:
34+
enabled: false
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
chain: regtest
2+
3+
collectLogs: false
4+
metricsExport: false
5+
6+
resources: {}
7+
# We usually recommend not to specify default resources and to leave this as a conscious
8+
# choice for the user. This also increases chances charts run on environments with little
9+
# resources, such as Minikube. If you do want to specify resources, uncomment the following
10+
# lines, adjust them as necessary, and remove the curly braces after 'resources:'.
11+
# limits:
12+
# cpu: 100m
13+
# memory: 128Mi
14+
# requests:
15+
# cpu: 100m
16+
# memory: 128Mi
17+
18+
image:
19+
repository: bitcoindevproject/bitcoin
20+
pullPolicy: IfNotPresent
21+
# Overrides the image tag whose default is the chart appVersion.
22+
tag: "27.0"
23+
24+
config: |
25+
dns=1
26+
debug=rpc

test/namespace_admin_test.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
from pathlib import Path
5+
from typing import Callable, Optional
6+
7+
from test_base import TestBase
8+
9+
from warnet.k8s import get_kubeconfig_value
10+
from warnet.process import run_command
11+
12+
13+
class NamespaceAdminTest(TestBase):
14+
def __init__(self):
15+
super().__init__()
16+
self.namespace_dir = (
17+
Path(os.path.dirname(__file__))
18+
/ "data"
19+
/ "admin"
20+
/ "namespaces"
21+
/ "two_namespaces_two_users"
22+
)
23+
self.network_dir = (
24+
Path(os.path.dirname(__file__)) / "data" / "admin" / "networks" / "6_node_bitcoin"
25+
)
26+
27+
def run_test(self):
28+
try:
29+
self.setup_namespaces()
30+
self.setup_service_accounts()
31+
self.deploy_network_in_team_namespaces()
32+
self.authenticate_and_become_bob()
33+
self.become_minikube_once_again()
34+
finally:
35+
self.cleanup()
36+
37+
def become_minikube_once_again(self):
38+
minikube = "minikube"
39+
cmd = f"kubectl config use-context {minikube}"
40+
self.log.info(run_command(cmd))
41+
self.wait_for_predicate(self.this_is_the_current_context(minikube))
42+
43+
def this_is_the_current_context(self, context: str) -> Callable[[], bool]:
44+
cmd = "kubectl config current-context"
45+
current_context = run_command(cmd).strip()
46+
self.log.info(f"Current context: {current_context} {context == current_context}")
47+
return lambda: current_context == context
48+
49+
def setup_namespaces(self):
50+
self.log.info("Setting up the namespaces")
51+
self.log.info(self.warnet(f"deploy {self.namespace_dir}"))
52+
self.wait_for_predicate(self.two_namespaces_are_validated)
53+
self.log.info("Namespace setup complete")
54+
55+
def setup_service_accounts(self):
56+
self.log.info("Creating service accounts...")
57+
self.log.info(self.warnet("admin create-kubeconfigs"))
58+
self.wait_for_predicate(self.service_accounts_are_validated)
59+
60+
def deploy_network_in_team_namespaces(self):
61+
self.log.info("Deploy networks to team namespaces")
62+
self.log.info(self.warnet(f"deploy {self.network_dir} --to-all-users"))
63+
self.wait_for_all_tanks_status()
64+
self.log.info("Waiting for all edges")
65+
self.wait_for_all_edges()
66+
67+
def authenticate_and_become_bob(self):
68+
self.log.info("Authenticating and becoming bob...")
69+
assert get_kubeconfig_value("{.current-context}") == "minikube"
70+
self.log.info(self.warnet("auth kubeconfigs/bob-wargames-red-team-kubeconfig"))
71+
assert get_kubeconfig_value("{.current-context}") == "bob-wargames-red-team"
72+
73+
def get_service_accounts(self) -> Optional[dict[str, str]]:
74+
self.log.info("Setting up service accounts")
75+
resp = self.warnet("admin service-accounts list")
76+
if resp == "Could not find any matching service accounts.":
77+
return None
78+
service_accounts: dict[str, [str]] = {}
79+
current_namespace = ""
80+
for line in resp.splitlines():
81+
if line.startswith("Service"):
82+
current_namespace = line.split(": ")[1]
83+
service_accounts[current_namespace] = []
84+
if line.startswith("- "):
85+
sa = line.lstrip("- ")
86+
service_accounts[current_namespace].append(sa)
87+
self.log.info(f"Service accounts: {service_accounts}")
88+
return service_accounts
89+
90+
def service_accounts_are_validated(self) -> bool:
91+
self.log.info("Checking service accounts")
92+
maybe_service_accounts = self.get_service_accounts()
93+
expected = {
94+
"wargames-blue-team": ["carol", "default", "mallory"],
95+
"wargames-red-team": ["alice", "bob", "default"],
96+
}
97+
return maybe_service_accounts == expected
98+
99+
def get_namespaces(self) -> Optional[list[str]]:
100+
self.log.info("Querying the namespaces...")
101+
resp = self.warnet("admin namespaces list")
102+
if resp == "No warnet namespaces found.":
103+
return None
104+
namespaces = []
105+
for line in resp.splitlines():
106+
if line.startswith("- "):
107+
namespaces.append(line.lstrip("- "))
108+
self.log.info(f"Namespaces: {namespaces}")
109+
return namespaces
110+
111+
def two_namespaces_are_validated(self) -> bool:
112+
maybe_namespaces = self.get_namespaces()
113+
if maybe_namespaces is None:
114+
return False
115+
if len(maybe_namespaces) != 2:
116+
return False
117+
if "wargames-blue-team" not in maybe_namespaces:
118+
return False
119+
return "wargames-red-team" in maybe_namespaces
120+
121+
122+
if __name__ == "__main__":
123+
test = NamespaceAdminTest()
124+
test.run_test()

0 commit comments

Comments
 (0)