1414 assert_alert_rules ,
1515 assert_logging ,
1616 assert_metrics_endpoint ,
17+ assert_security_context ,
1718 deploy_and_assert_grafana_agent ,
19+ generate_container_securitycontext_map ,
1820 get_alert_rules ,
21+ get_pod_names ,
1922)
2023from charms_dependencies import ISTIO_GATEWAY , ISTIO_PILOT , JUPYTER_UI
2124from httpx import HTTPStatusError
2932
3033METADATA = yaml .safe_load (Path ("./metadata.yaml" ).read_text ())
3134APP_NAME = METADATA ["name" ]
35+ CONTAINERS_SECURITY_CONTEXT_MAP = generate_container_securitycontext_map (METADATA )
3236ISTIO_GATEWAY_APP_NAME = "istio-ingressgateway"
3337
3438
39+ @pytest .fixture (scope = "session" )
40+ def lightkube_client () -> Client :
41+ """Returns lightkube Kubernetes client"""
42+ client = Client (field_manager = f"{ APP_NAME } " )
43+ return client
44+
45+
3546@pytest .mark .abort_on_fail
3647async def test_build_and_deploy (ops_test : OpsTest , request ):
3748 """Test build and deploy."""
@@ -142,9 +153,8 @@ def assert_replicas(client, resource_class, resource_name, namespace):
142153 assert replicas == 1 , f"Waited too long for { resource_class_kind } /{ resource_name } !"
143154
144155
145- async def test_create_notebook (ops_test : OpsTest ):
156+ async def test_create_notebook (ops_test : OpsTest , lightkube_client : Client ):
146157 """Test notebook creation."""
147- lightkube_client = Client ()
148158 this_ns = lightkube_client .get (res = Namespace , name = ops_test .model .name )
149159 lightkube_client .patch (res = Namespace , name = this_ns .metadata .name , obj = this_ns )
150160
@@ -172,8 +182,30 @@ async def test_create_notebook(ops_test: OpsTest):
172182 assert_replicas (lightkube_client , notebook_resource , "sample-notebook" , ops_test .model .name )
173183
174184
185+ @pytest .mark .parametrize ("container_name" , list (CONTAINERS_SECURITY_CONTEXT_MAP .keys ()))
186+ @pytest .mark .abort_on_fail
187+ async def test_container_security_context (
188+ ops_test : OpsTest ,
189+ lightkube_client : Client ,
190+ container_name : str ,
191+ ):
192+ """Test container security context is correctly set.
193+
194+ Verify that container spec defines the security context with correct
195+ user ID and group ID.
196+ """
197+ pod_name = get_pod_names (ops_test .model .name , APP_NAME )[0 ]
198+ assert_security_context (
199+ lightkube_client ,
200+ pod_name ,
201+ container_name ,
202+ CONTAINERS_SECURITY_CONTEXT_MAP ,
203+ ops_test .model .name ,
204+ )
205+
206+
175207@pytest .mark .abort_on_fail
176- async def test_remove_with_resources_present (ops_test : OpsTest ):
208+ async def test_remove_with_resources_present (ops_test : OpsTest , lightkube_client : Client ):
177209 """Test remove with all resources deployed.
178210
179211 Verify that all deployed resources that need to be removed are removed.
@@ -184,7 +216,6 @@ async def test_remove_with_resources_present(ops_test: OpsTest):
184216 assert APP_NAME not in ops_test .model .applications
185217
186218 # verify that all resources that were deployed are removed
187- lightkube_client = Client ()
188219
189220 # verify all CRDs in namespace are removed
190221 crd_list = lightkube_client .list (
0 commit comments