Skip to content

Commit

Permalink
Updates to CLI and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Astitva committed Apr 7, 2024
1 parent 7b9b827 commit 5395d07
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cli/src/pkg/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""This file supports the DTaaS config class"""

import click
from . import utils
from src.pkg import utils

class Config:
"""The Config class for DTaaS"""
Expand Down
27 changes: 22 additions & 5 deletions cli/src/pkg/users.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""This file has functions that handle the user cli commands"""

import subprocess
from . import utils
from src.pkg import utils

def getComposeConfig(username, server, path):
"""Makes and returns the config for the user"""
Expand Down Expand Up @@ -46,7 +46,10 @@ def startUserContainers(users):
cmd.append(username)

cmdStr = " ".join(cmd)
subprocess.run(cmdStr, shell=True, check=False)
result = subprocess.run(cmdStr, shell=True, check=False)
if result.returncode !=0:
return Exception("failed to start containers")
return None

def stopUserContainers(users):
"""Stops all the user containers in the 'users' list"""
Expand All @@ -56,7 +59,10 @@ def stopUserContainers(users):
cmd.append(username)

cmdStr = " ".join(cmd)
subprocess.run(cmdStr, shell=True, check=False)
result = subprocess.run(cmdStr, shell=True, check=False)
if result.returncode != 0:
return Exception("failed to stop containers")
return None

def addUsers(configObj):
"""add cli command handler"""
Expand All @@ -78,6 +84,13 @@ def addUsers(configObj):
compose['version'] = '3'
if 'services' not in compose:
compose['services'] = {}
if 'networks' not in compose:
compose['networks'] = {
'users': {
'name': 'dtaas-users',
'external': True
}
}


err = addUsersToCompose(userList, compose, server, path)
Expand All @@ -88,7 +101,9 @@ def addUsers(configObj):
if err is not None:
return err

startUserContainers(userList)
err = startUserContainers(userList)
if err is not None:
return err

return None

Expand All @@ -102,7 +117,9 @@ def deleteUser(configObj):
if err is not None:
return err

stopUserContainers(userList)
err = stopUserContainers(userList)
if err is not None:
return err

for username in userList:
if 'services' not in compose:
Expand Down
22 changes: 22 additions & 0 deletions cli/tests/compose.users.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '3'
services:
astitvasehgal05:
image: mltooling/ml-workspace-minimal:0.13.2
volumes:
- /home/astitva/Desktop/yamlstuff/files/common:/workspace/common
- /home/astitva/Desktop/yamlstuff/files/astitvasehgal05:/workspace
environment:
- AUTHENTICATE_VIA_JUPYTER=
- WORKSPACE_BASE_URL=astitvasehgal05
shm_size: 512m
labels:
- traefik.enable=true
- traefik.http.routers.astitvasehgal05.entryPoints=web
- traefik.http.routers.astitvasehgal05.rule=PathPrefix(`/astitvasehgal05`)
- traefik.http.routers.astitvasehgal05.middlewares=traefik-forward-auth
networks:
- users
networks:
users:
name: dtaas-users
external: true
10 changes: 10 additions & 0 deletions cli/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import subprocess


def test_add_user_cli():
result =subprocess.run(["dtaas admin user add"], shell=True)
assert result.returncode == 0

def test_delete_user_cli():
result = subprocess.run(["dtaas admin user delete"], shell=True)
assert result.returncode == 0
217 changes: 217 additions & 0 deletions cli/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
from src.pkg import utils
import filecmp
from pprint import pprint

def test_import_yaml_users():

expected = {
"image": "mltooling/ml-workspace-minimal:0.13.2",
"volumes": [
"${DTAAS_DIR}/files/common:/workspace/common",
"${DTAAS_DIR}/files/${username}:/workspace"
],
"environment":[
"AUTHENTICATE_VIA_JUPYTER=",
"WORKSPACE_BASE_URL=${username}"
],
"shm_size": "512m",
"labels": [
"traefik.enable=true",
"traefik.http.routers.${username}.entryPoints=web",
"traefik.http.routers.${username}.rule=PathPrefix(`/${username}`)",
"traefik.http.routers.${username}.middlewares=traefik-forward-auth"
],
"networks":[
"users"
]
}

template, err= utils.importYaml("users.local.yml")
if err!=None:
raise Exception(err)

assert template==expected

def test_import_yaml_compose():
expected = {
"version": '3',
"services":{
"astitvasehgal05":{
"image": "mltooling/ml-workspace-minimal:0.13.2",
"volumes": [
"/home/astitva/Desktop/yamlstuff/files/common:/workspace/common",
"/home/astitva/Desktop/yamlstuff/files/astitvasehgal05:/workspace"
],
"environment":[
"AUTHENTICATE_VIA_JUPYTER=",
"WORKSPACE_BASE_URL=astitvasehgal05"
],
"shm_size": "512m",
"labels": [
"traefik.enable=true",
"traefik.http.routers.astitvasehgal05.entryPoints=web",
"traefik.http.routers.astitvasehgal05.rule=PathPrefix(`/astitvasehgal05`)",
"traefik.http.routers.astitvasehgal05.middlewares=traefik-forward-auth"
],
"networks":[
"users"
]
}

},
"networks":{
"users":{
"name":"dtaas-users",
"external": True,
}
}
}

compose, err = utils.importYaml('src/test_units/compose.users.test.yml')
if err is not None:
raise Exception(err)
assert expected==compose

def test_import_toml():
toml, err = utils.importToml('dtaas.toml')
if err is not None:
raise Exception(err)

expected = {
"name" : "Digital Twin as a Service (DTaaS)",
"version" : "0.1",
"owner" : "The INTO-CPS-Association",
"git-repo" : "https://github.com/into-cps-association/DTaaS.git",

"common":{
# absolute path to the DTaaS application directory
"server-dns" : "localhost",
"path" : "/home/astitva/Desktop/yamlstuff"
},
"users":{
# matching user info must present in this config file
"add" : ["astitvasehgal05","astitvasehgal19", "prasad"],
"delete" : ["astitvasehgal19", "prasad"],

"astitvasehgal05" :{
"email" : "[email protected]"
},
"astitvasehgal19" :{
"email" : "[email protected]"
},
"prasad" : {
"email" : "[email protected]"
}
},

"client":{
"web":
{
"config" : "/home/astitva/Desktop/yamlstuff/env.local.js"
}
}
}

assert expected==toml

def test_replace_all():

template = {
"key1": "stringval1",
"key2": ["listval1", "listval2", "listval3"],
"dictkey1":{
"dict1key1": "stringval2",
"dict2key2": ["listval1","listval3"],
"dict3key3": {
"key3":"stringval1",
"key4":{
"listkey": ["listval2"]
}
}
},
"dictkey2": {
"dict2key1" : {
"key5": "stringval3",
"key6": "listval1",
"key7": ["listval2", "listval3"]
},
"dict2key2": "stringval2",
"dict2key3": "listval2"
}
}

expected = {
"key1": "one",
"key2": ["foo", "bar", "qux"],
"dictkey1":{
"dict1key1": "two",
"dict2key2": ["foo","qux"],
"dict3key3": {
"key3":"one",
"key4":{
"listkey": ["bar"]
}
}
},
"dictkey2": {
"dict2key1" : {
"key5": "three",
"key6": "foo",
"key7": ["bar", "qux"]
},
"dict2key2": "two",
"dict2key3": "bar"
}
}

mapping = {
"stringval1": "one",
"stringval2": "two",
"stringval3": "three",
"listval1": "foo",
"listval2": "bar",
"listval3": "qux",
}

ans, err = utils.replaceAll(template, mapping)

assert ans==expected

def test_export_yaml():

data = {
"version": '3',
"services":{
"astitvasehgal05":{
"image": "mltooling/ml-workspace-minimal:0.13.2",
"volumes": [
"/home/astitva/Desktop/yamlstuff/files/common:/workspace/common",
"/home/astitva/Desktop/yamlstuff/files/astitvasehgal05:/workspace"
],
"environment":[
"AUTHENTICATE_VIA_JUPYTER=",
"WORKSPACE_BASE_URL=astitvasehgal05"
],
"shm_size": "512m",
"labels": [
"traefik.enable=true",
"traefik.http.routers.astitvasehgal05.entryPoints=web",
"traefik.http.routers.astitvasehgal05.rule=PathPrefix(`/astitvasehgal05`)",
"traefik.http.routers.astitvasehgal05.middlewares=traefik-forward-auth"
],
"networks":["users"]
}
},
"networks":{
"users":{
"name":"dtaas-users",
"external": True,
}
}
}

err = utils.exportYaml(data, 'src/test_units/compose.users.exp.yml')
if err is not None:
raise Exception(err)

assert filecmp.cmp('src/test_units/compose.users.test.yml', 'src/test_units/compose.users.exp.yml')
6 changes: 4 additions & 2 deletions cli/users.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ shm_size: 512m
labels:
- "traefik.enable=true"
- "traefik.http.routers.${username}.entryPoints=web"
- "traefik.http.routers.${username}.rule=PathPrefix(`/${username}`) "
- "traefik.http.routers.${username}.middlewares=traefik-forward-auth"
- "traefik.http.routers.${username}.rule=PathPrefix(`/${username}`)"
- "traefik.http.routers.${username}.middlewares=traefik-forward-auth"
networks:
- users
4 changes: 3 additions & 1 deletion cli/users.server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ labels:
- "traefik.enable=true"
- "traefik.http.routers.${username}.entryPoints=web"
- "traefik.http.routers.${username}.rule=Host(`${SERVER_DNS}`)&&PathPrefix(`/${username}`)"
- "traefik.http.routers.${username}.middlewares=traefik-forward-auth"
- "traefik.http.routers.${username}.middlewares=traefik-forward-auth"
networks:
- users

0 comments on commit 5395d07

Please sign in to comment.