Skip to content

Commit e12dc8e

Browse files
authored
Merge pull request #644 from mplsgrant/2024-10-podstatus-run-debug
fix run --debug issue for service accounts
2 parents 26b1910 + 101cd6e commit e12dc8e

File tree

6 files changed

+71
-97
lines changed

6 files changed

+71
-97
lines changed

resources/charts/namespaces/values.yaml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,25 @@ roles:
1919
resources: ["persistentvolumeclaims", "namespaces"]
2020
verbs: ["get", "list"]
2121
- apiGroups: [""]
22-
resources: ["events"]
22+
resources: ["events", "pods/status"]
2323
verbs: ["get"]
2424
- name: pod-manager
2525
rules:
2626
- apiGroups: [""]
2727
resources: ["pods", "services"]
2828
verbs: ["get", "list", "watch", "create", "delete", "update"]
29+
- apiGroups: [""]
30+
resources: ["pods"]
31+
verbs: ["get", "list", "watch", "create", "delete", "update", "patch"]
2932
- apiGroups: [""]
3033
resources: ["pods/log", "pods/exec", "pods/attach", "pods/portforward"]
3134
verbs: ["get", "create"]
3235
- apiGroups: [""]
3336
resources: ["configmaps", "secrets"]
34-
verbs: ["get", "list", "create"]
37+
verbs: ["get", "list", "create", "update"]
3538
- apiGroups: [""]
3639
resources: ["persistentvolumeclaims", "namespaces"]
3740
verbs: ["get", "list"]
3841
- apiGroups: [""]
39-
resources: ["events"]
40-
verbs: ["get"]
42+
resources: ["events", "pods/status"]
43+
verbs: ["get"]

resources/scenarios/test_scenarios/p2p_interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def run_test(self):
4949
good_getdata = msg_getdata()
5050
good_getdata.inv.append(CInv(t=2, h=best_block))
5151
p2p_block_store.send_and_ping(good_getdata)
52-
p2p_block_store.wait_until(lambda: p2p_block_store.blocks[best_block] == 1)
52+
p2p_block_store.wait_until(lambda: p2p_block_store.blocks[best_block] >= 1)
5353

5454

5555
def main():

test/data/admin/networks/6_node_bitcoin/network.yaml

Lines changed: 0 additions & 34 deletions
This file was deleted.

test/data/admin/networks/6_node_bitcoin/node-defaults.yaml

Lines changed: 0 additions & 26 deletions
This file was deleted.

test/namespace_admin_test.py

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pathlib import Path
55
from typing import Callable, Optional
66

7+
from scenarios_test import ScenariosTest
78
from test_base import TestBase
89

910
from warnet.constants import KUBECONFIG, WARGAMES_NAMESPACE_PREFIX
@@ -17,32 +18,41 @@
1718
from warnet.process import run_command
1819

1920

20-
class NamespaceAdminTest(TestBase):
21+
class NamespaceAdminTest(ScenariosTest, TestBase):
2122
def __init__(self):
2223
super().__init__()
24+
2325
self.namespace_dir = (
2426
Path(os.path.dirname(__file__))
2527
/ "data"
2628
/ "admin"
2729
/ "namespaces"
2830
/ "two_namespaces_two_users"
2931
)
30-
self.network_dir = (
31-
Path(os.path.dirname(__file__)) / "data" / "admin" / "networks" / "6_node_bitcoin"
32-
)
32+
33+
self.initial_context = None
34+
self.current_context = None
35+
self.bob_user = "bob-warnettest"
36+
self.bob_auth_file = "bob-warnettest-wargames-red-team-warnettest-kubeconfig"
37+
self.bob_context = "bob-warnettest-wargames-red-team-warnettest"
38+
39+
self.blue_namespace = "wargames-blue-team-warnettest"
40+
self.red_namespace = "wargames-red-team-warnettest"
41+
self.blue_users = ["carol-warnettest", "default", "mallory-warnettest"]
42+
self.red_users = ["alice-warnettest", self.bob_user, "default"]
3343

3444
def run_test(self):
3545
try:
3646
os.chdir(self.tmpdir)
3747
self.log.info(f"Running test in: {self.tmpdir}")
3848
self.establish_initial_context()
39-
self.establish_names()
4049
self.setup_namespaces()
4150
self.setup_service_accounts()
42-
self.deploy_network_in_team_namespaces()
51+
self.setup_network()
4352
self.authenticate_and_become_bob()
44-
self.return_to_intial_context()
53+
self.bob_runs_scenario_tests()
4554
finally:
55+
self.return_to_initial_context()
4656
try:
4757
self.cleanup_kubeconfig()
4858
except K8sError as e:
@@ -52,27 +62,8 @@ def run_test(self):
5262
def establish_initial_context(self):
5363
self.initial_context = get_kubeconfig_value("{.current-context}")
5464
self.log.info(f"Initial context: {self.initial_context}")
55-
56-
def establish_names(self):
57-
self.bob_user = "bob-warnettest"
58-
self.bob_auth_file = "bob-warnettest-wargames-red-team-warnettest-kubeconfig"
59-
self.bob_context = "bob-warnettest-wargames-red-team-warnettest"
60-
61-
self.blue_namespace = "wargames-blue-team-warnettest"
62-
self.red_namespace = "wargames-red-team-warnettest"
63-
self.blue_users = ["carol-warnettest", "default", "mallory-warnettest"]
64-
self.red_users = ["alice-warnettest", self.bob_user, "default"]
65-
66-
def return_to_intial_context(self):
67-
cmd = f"kubectl config use-context {self.initial_context}"
68-
self.log.info(run_command(cmd))
69-
self.wait_for_predicate(self.this_is_the_current_context(self.initial_context))
70-
71-
def this_is_the_current_context(self, context: str) -> Callable[[], bool]:
72-
cmd = "kubectl config current-context"
73-
current_context = run_command(cmd).strip()
74-
self.log.info(f"Current context: {current_context} {context == current_context}")
75-
return lambda: current_context == context
65+
self.current_context = self.initial_context
66+
self.log.info(f"Current context: {self.current_context}")
7667

7768
def setup_namespaces(self):
7869
self.log.info("Setting up the namespaces")
@@ -86,18 +77,28 @@ def setup_service_accounts(self):
8677
self.wait_for_predicate(self.service_accounts_are_validated)
8778
self.log.info("Service accounts have been set up and validated")
8879

89-
def deploy_network_in_team_namespaces(self):
90-
self.log.info("Deploy networks to team namespaces")
91-
self.log.info(self.warnet(f"deploy {self.network_dir} --to-all-users"))
80+
def setup_network(self):
81+
if self.current_context == self.bob_context:
82+
self.log.info(f"Allowing {self.current_context} to update the network...")
83+
assert self.this_is_the_current_context(self.bob_context)
84+
self.warnet(f"deploy {self.network_dir}")
85+
else:
86+
self.log.info("Deploy networks to team namespaces")
87+
assert self.this_is_the_current_context(self.initial_context)
88+
self.log.info(self.warnet(f"deploy {self.network_dir} --to-all-users"))
9289
self.wait_for_all_tanks_status()
9390
self.log.info("Waiting for all edges")
9491
self.wait_for_all_edges()
9592

9693
def authenticate_and_become_bob(self):
9794
self.log.info("Authenticating and becoming bob...")
95+
self.log.info(f"Current context: {self.current_context}")
96+
assert self.initial_context == self.current_context
9897
assert get_kubeconfig_value("{.current-context}") == self.initial_context
9998
self.warnet(f"auth kubeconfigs/{self.bob_auth_file}")
100-
assert get_kubeconfig_value("{.current-context}") == self.bob_context
99+
self.current_context = self.bob_context
100+
assert get_kubeconfig_value("{.current-context}") == self.current_context
101+
self.log.info(f"Current context: {self.current_context}")
101102

102103
def service_accounts_are_validated(self) -> bool:
103104
self.log.info("Checking service accounts")
@@ -145,6 +146,17 @@ def two_namespaces_are_validated(self) -> bool:
145146
return False
146147
return self.red_namespace in maybe_namespaces
147148

149+
def return_to_initial_context(self):
150+
cmd = f"kubectl config use-context {self.initial_context}"
151+
self.log.info(run_command(cmd))
152+
self.wait_for_predicate(self.this_is_the_current_context(self.initial_context))
153+
154+
def this_is_the_current_context(self, context: str) -> Callable[[], bool]:
155+
cmd = "kubectl config current-context"
156+
current_context = run_command(cmd).strip()
157+
self.log.info(f"Current context: {current_context} {context == current_context}")
158+
return lambda: current_context == context
159+
148160
def cleanup_kubeconfig(self):
149161
try:
150162
kubeconfig_data = open_kubeconfig(KUBECONFIG)
@@ -159,6 +171,11 @@ def cleanup_kubeconfig(self):
159171
except Exception as e:
160172
raise K8sError(f"Could not write to KUBECONFIG: {KUBECONFIG}") from e
161173

174+
def bob_runs_scenario_tests(self):
175+
assert self.this_is_the_current_context(self.bob_context)
176+
super().run_test()
177+
assert self.this_is_the_current_context(self.bob_context)
178+
162179

163180
def remove_user(kubeconfig_data: dict, username: str) -> dict:
164181
kubeconfig_data["users"] = [

test/scenarios_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
import os
4+
import re
45
from pathlib import Path
56

67
from test_base import TestBase
@@ -21,6 +22,7 @@ def run_test(self):
2122
self.setup_network()
2223
self.run_and_check_miner_scenario_from_file()
2324
self.run_and_check_scenario_from_file()
25+
self.run_and_check_scenario_from_file_debug()
2426
self.check_regtest_recon()
2527
self.check_active_count()
2628
finally:
@@ -82,6 +84,12 @@ def run_and_check_miner_scenario_from_file(self):
8284
assert "Active Scenarios: 1" in table
8385
self.stop_scenario()
8486

87+
def run_and_check_scenario_from_file_debug(self):
88+
scenario_file = self.scen_dir / "test_scenarios" / "p2p_interface.py"
89+
self.log.info(f"Running scenario from: {scenario_file}")
90+
output = self.warnet(f"run {scenario_file} --source_dir={self.scen_dir} --debug")
91+
self.check_for_pod_deletion_message(output)
92+
8593
def run_and_check_scenario_from_file(self):
8694
scenario_file = self.scen_dir / "test_scenarios" / "p2p_interface.py"
8795
self.log.info(f"Running scenario from: {scenario_file}")
@@ -109,6 +117,12 @@ def two_pass_one_fail():
109117
table = self.warnet("status")
110118
assert "Active Scenarios: 0" in table
111119

120+
def check_for_pod_deletion_message(self, input):
121+
message = "Deleting pod..."
122+
self.log.info(f"Checking for message: '{message}'")
123+
assert re.search(re.escape(message), input, flags=re.MULTILINE)
124+
self.log.info(f"Found message: '{message}'")
125+
112126

113127
if __name__ == "__main__":
114128
test = ScenariosTest()

0 commit comments

Comments
 (0)