-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor updates, add tests for special case
- Loading branch information
Showing
7 changed files
with
293 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
import pytest | ||
import yaml | ||
from keycloak.exceptions import KeycloakGetError | ||
|
||
from nebari_workflow_controller.models import KeycloakGroup, KeycloakUser | ||
|
||
|
@@ -41,6 +42,28 @@ def all_request_paths(valid_request_paths, invalid_request_paths): | |
return valid_request_paths + invalid_request_paths | ||
|
||
|
||
def _valid_service_account_request_paths(): | ||
return sorted( | ||
[ | ||
str(p) | ||
for p in Path("./tests/test_data/requests/valid-service-account").glob( | ||
"*.yaml" | ||
) | ||
] | ||
) | ||
|
||
|
||
def _invalid_service_account_request_paths(): | ||
return sorted( | ||
[ | ||
str(p) | ||
for p in Path("./tests/test_data/requests/invalid-service-account").glob( | ||
"*.yaml" | ||
) | ||
] | ||
) | ||
|
||
|
||
def load_request(request_path): | ||
with open(request_path) as f: | ||
return yaml.load(f, Loader=yaml.FullLoader) | ||
|
@@ -102,3 +125,57 @@ def mocked_get_user_pod_spec(mocker): | |
"nebari_workflow_controller.app.get_user_pod_spec", | ||
return_value=jupyterlab_pod_spec, | ||
) | ||
|
||
|
||
class MockedKeycloakAdmin: | ||
def get_user(self, *args, **kwargs): | ||
raise KeycloakGetError("mocked error") | ||
|
||
def get_users(self, *args, **kwargs): | ||
return [ | ||
{ | ||
"id": "a1234567-abcd-89ef-0123-456789abcdef", | ||
"username": "[email protected]", | ||
"email": "[email protected]", | ||
"firstName": "Valid", | ||
"lastName": "User", | ||
"enabled": True, | ||
"groups": ["admin", "users"], | ||
}, | ||
] | ||
|
||
def get_user_groups(self, *args, **kwargs): | ||
return [ | ||
{ | ||
"id": "group123-id", | ||
"name": "admin", | ||
"path": "/admin", | ||
"attributes": {}, | ||
}, | ||
{ | ||
"id": "group456-id", | ||
"name": "users", | ||
"path": "/users", | ||
"attributes": {}, | ||
}, | ||
] | ||
|
||
|
||
VALID_ARGO_ROLES = ["argo-admin", "argo-developer"] | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def mock_special_case(mocker): | ||
mocker.patch( | ||
"nebari_workflow_controller.utils.create_keycloak_admin", | ||
return_value=MockedKeycloakAdmin(), | ||
) | ||
mocker.patch("nebari_workflow_controller.utils.config", return_value=None) | ||
mocker.patch( | ||
"nebari_workflow_controller.utils.sent_by_argo", | ||
return_value="workflows.argoproj.io/creator", | ||
) | ||
mocker.patch( | ||
"nebari_workflow_controller.utils.valid_argo_roles", | ||
return_value=VALID_ARGO_ROLES, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
tests/test_data/requests/invalid-service-account/jupyterflow-override-analyst.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
apiVersion: admission.k8s.io/v1 | ||
kind: AdmissionReview | ||
request: | ||
dryRun: false | ||
kind: | ||
group: argoproj.io | ||
kind: Workflow | ||
version: v1alpha1 | ||
name: hello-world | ||
namespace: dev | ||
object: | ||
api: argoproj.io/v1alpha1 | ||
kind: Workflow | ||
metadata: | ||
name: sparkly-python | ||
namespace: dev | ||
labels: | ||
example: 'true' | ||
jupyterflow-override: 'true' | ||
workflows.argoproj.io/creator-preferred-username: valid-2duser-40mail-2ecom | ||
workflows.argoproj.io/creator: system-serviceaccount-dev-argo-analyst # valid user but wrong permissions | ||
spec: | ||
templates: | ||
- name: argosay | ||
container: | ||
name: notebook | ||
image: quay.io/nebari/nebari-jupyterlab:2023.1.1 | ||
command: | ||
- sh | ||
- '-c' | ||
args: | ||
- conda run -n nebari-git-dask python script.py | ||
resources: | ||
limits: | ||
cpu: '3000m' | ||
nodeSelector: | ||
mylabel: myValue | ||
entrypoint: argosay | ||
uid: c1bba5c6-2189-41ff-9487-be504c04487b | ||
userInfo: | ||
groups: | ||
- system:serviceaccounts | ||
- system:serviceaccounts:dev | ||
- system:authenticated | ||
uid: eac0d7ab-af84-4c3f-a5fd-71845ff9e8c9 | ||
username: system:serviceaccount:dev:argo-admin |
46 changes: 46 additions & 0 deletions
46
...s/test_data/requests/invalid-service-account/jupyterflow-override-preferred-username.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
apiVersion: admission.k8s.io/v1 | ||
kind: AdmissionReview | ||
request: | ||
dryRun: false | ||
kind: | ||
group: argoproj.io | ||
kind: Workflow | ||
version: v1alpha1 | ||
name: hello-world | ||
namespace: dev | ||
object: | ||
api: argoproj.io/v1alpha1 | ||
kind: Workflow | ||
metadata: | ||
name: sparkly-python | ||
namespace: dev | ||
labels: | ||
example: 'true' | ||
jupyterflow-override: 'true' | ||
workflows.argoproj.io/creator-preferred-username: invalid-2duser-40mail-2ecom # username not in keycloak | ||
workflows.argoproj.io/creator: system-serviceaccount-dev-argo-admin | ||
spec: | ||
templates: | ||
- name: argosay | ||
container: | ||
name: notebook | ||
image: quay.io/nebari/nebari-jupyterlab:2023.1.1 | ||
command: | ||
- sh | ||
- '-c' | ||
args: | ||
- conda run -n nebari-git-dask python script.py | ||
resources: | ||
limits: | ||
cpu: '3000m' | ||
nodeSelector: | ||
mylabel: myValue | ||
entrypoint: argosay | ||
uid: c1bba5c6-2189-41ff-9487-be504c04487b | ||
userInfo: | ||
groups: | ||
- system:serviceaccounts | ||
- system:serviceaccounts:dev | ||
- system:authenticated | ||
uid: eac0d7ab-af84-4c3f-a5fd-71845ff9e8c9 | ||
username: system:serviceaccount:dev:argo-admin |
46 changes: 46 additions & 0 deletions
46
tests/test_data/requests/valid-service-account/jupyterflow-override-admin.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
apiVersion: admission.k8s.io/v1 | ||
kind: AdmissionReview | ||
request: | ||
dryRun: false | ||
kind: | ||
group: argoproj.io | ||
kind: Workflow | ||
version: v1alpha1 | ||
name: hello-world | ||
namespace: dev | ||
object: | ||
api: argoproj.io/v1alpha1 | ||
kind: Workflow | ||
metadata: | ||
name: sparkly-python | ||
namespace: dev | ||
labels: | ||
example: 'true' | ||
jupyterflow-override: 'true' | ||
workflows.argoproj.io/creator-preferred-username: valid-2duser-40mail-2ecom | ||
workflows.argoproj.io/creator: system-serviceaccount-dev-argo-admin | ||
spec: | ||
templates: | ||
- name: argosay | ||
container: | ||
name: notebook | ||
image: quay.io/nebari/nebari-jupyterlab:2023.1.1 | ||
command: | ||
- sh | ||
- '-c' | ||
args: | ||
- conda run -n nebari-git-dask python script.py | ||
resources: | ||
limits: | ||
cpu: '3000m' | ||
nodeSelector: | ||
mylabel: myValue | ||
entrypoint: argosay | ||
uid: c1bba5c6-2189-41ff-9487-be504c04487b | ||
userInfo: | ||
groups: | ||
- system:serviceaccounts | ||
- system:serviceaccounts:dev | ||
- system:authenticated | ||
uid: eac0d7ab-af84-4c3f-a5fd-71845ff9e8c9 | ||
username: system:serviceaccount:dev:argo-admin |
46 changes: 46 additions & 0 deletions
46
tests/test_data/requests/valid-service-account/jupyterflow-override-developer.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
apiVersion: admission.k8s.io/v1 | ||
kind: AdmissionReview | ||
request: | ||
dryRun: false | ||
kind: | ||
group: argoproj.io | ||
kind: Workflow | ||
version: v1alpha1 | ||
name: hello-world | ||
namespace: dev | ||
object: | ||
api: argoproj.io/v1alpha1 | ||
kind: Workflow | ||
metadata: | ||
name: sparkly-python | ||
namespace: dev | ||
labels: | ||
example: 'true' | ||
jupyterflow-override: 'true' | ||
workflows.argoproj.io/creator-preferred-username: valid-2duser-40mail-2ecom | ||
workflows.argoproj.io/creator: system-serviceaccount-dev-argo-developer | ||
spec: | ||
templates: | ||
- name: argosay | ||
container: | ||
name: notebook | ||
image: quay.io/nebari/nebari-jupyterlab:2023.1.1 | ||
command: | ||
- sh | ||
- '-c' | ||
args: | ||
- conda run -n nebari-git-dask python script.py | ||
resources: | ||
limits: | ||
cpu: '3000m' | ||
nodeSelector: | ||
mylabel: myValue | ||
entrypoint: argosay | ||
uid: c1bba5c6-2189-41ff-9487-be504c04487b | ||
userInfo: | ||
groups: | ||
- system:serviceaccounts | ||
- system:serviceaccounts:dev | ||
- system:authenticated | ||
uid: eac0d7ab-af84-4c3f-a5fd-71845ff9e8c9 | ||
username: system:serviceaccount:dev:argo-admin |