44from pathlib import Path
55from typing import Callable , Optional
66
7+ from scenarios_test import ScenariosTest
78from test_base import TestBase
89
910from warnet .constants import KUBECONFIG , WARGAMES_NAMESPACE_PREFIX
1718from 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
163180def remove_user (kubeconfig_data : dict , username : str ) -> dict :
164181 kubeconfig_data ["users" ] = [
0 commit comments