4
4
from pathlib import Path
5
5
from typing import Callable , Optional
6
6
7
+ from scenarios_test import ScenariosTest
7
8
from test_base import TestBase
8
9
9
10
from warnet .constants import KUBECONFIG , WARGAMES_NAMESPACE_PREFIX
17
18
from warnet .process import run_command
18
19
19
20
20
- class NamespaceAdminTest (TestBase ):
21
+ class NamespaceAdminTest (ScenariosTest , TestBase ):
21
22
def __init__ (self ):
22
23
super ().__init__ ()
24
+
23
25
self .namespace_dir = (
24
26
Path (os .path .dirname (__file__ ))
25
27
/ "data"
26
28
/ "admin"
27
29
/ "namespaces"
28
30
/ "two_namespaces_two_users"
29
31
)
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" ]
33
43
34
44
def run_test (self ):
35
45
try :
36
46
os .chdir (self .tmpdir )
37
47
self .log .info (f"Running test in: { self .tmpdir } " )
38
48
self .establish_initial_context ()
39
- self .establish_names ()
40
49
self .setup_namespaces ()
41
50
self .setup_service_accounts ()
42
- self .deploy_network_in_team_namespaces ()
51
+ self .setup_network ()
43
52
self .authenticate_and_become_bob ()
44
- self .return_to_intial_context ()
53
+ self .bob_runs_scenario_tests ()
45
54
finally :
55
+ self .return_to_initial_context ()
46
56
try :
47
57
self .cleanup_kubeconfig ()
48
58
except K8sError as e :
@@ -52,27 +62,8 @@ def run_test(self):
52
62
def establish_initial_context (self ):
53
63
self .initial_context = get_kubeconfig_value ("{.current-context}" )
54
64
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 } " )
76
67
77
68
def setup_namespaces (self ):
78
69
self .log .info ("Setting up the namespaces" )
@@ -86,18 +77,28 @@ def setup_service_accounts(self):
86
77
self .wait_for_predicate (self .service_accounts_are_validated )
87
78
self .log .info ("Service accounts have been set up and validated" )
88
79
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" ))
92
89
self .wait_for_all_tanks_status ()
93
90
self .log .info ("Waiting for all edges" )
94
91
self .wait_for_all_edges ()
95
92
96
93
def authenticate_and_become_bob (self ):
97
94
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
98
97
assert get_kubeconfig_value ("{.current-context}" ) == self .initial_context
99
98
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 } " )
101
102
102
103
def service_accounts_are_validated (self ) -> bool :
103
104
self .log .info ("Checking service accounts" )
@@ -145,6 +146,17 @@ def two_namespaces_are_validated(self) -> bool:
145
146
return False
146
147
return self .red_namespace in maybe_namespaces
147
148
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
+
148
160
def cleanup_kubeconfig (self ):
149
161
try :
150
162
kubeconfig_data = open_kubeconfig (KUBECONFIG )
@@ -159,6 +171,11 @@ def cleanup_kubeconfig(self):
159
171
except Exception as e :
160
172
raise K8sError (f"Could not write to KUBECONFIG: { KUBECONFIG } " ) from e
161
173
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
+
162
179
163
180
def remove_user (kubeconfig_data : dict , username : str ) -> dict :
164
181
kubeconfig_data ["users" ] = [
0 commit comments