Skip to content

Commit 4f1cd88

Browse files
authored
adding tests for config (#562)
* adding tests for config Signed-off-by: vsoch <[email protected]> * formatting Signed-off-by: vsoch <[email protected]> * add more derivatives true/false/none Signed-off-by: vsoch <[email protected]> * allowing support for providing set/add with key value (with space) Signed-off-by: vsoch <[email protected]> * add another test Signed-off-by: vsoch <[email protected]> * add tests for both formats of add/remove and update shpc config client docs Signed-off-by: vsoch <[email protected]> Co-authored-by: vsoch <[email protected]>
1 parent 81e50e2 commit 4f1cd88

File tree

8 files changed

+198
-36
lines changed

8 files changed

+198
-36
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ and **Merged pull requests**. Critical items to know are:
1414
The versions coincide with releases on pip. Only major versions will be released as tags on Github.
1515

1616
## [0.0.x](https://github.com/singularityhub/singularity-hpc/tree/main) (0.0.x)
17+
- fixing bug to config set/get nested fields, default for wrapper scripts true (0.0.57)
18+
- change in behavior, instead of `key:value` for set/add `key value` now also works.
1719
- adding support for container.yaml overrides (0.0.56)
1820
- `SINGULARITY_CONTAINER` and `PODMAN_CONTAINER` support (0.0.55)
1921
- support for views create, delete, edit, install, uninstall (0.0.54)

docs/getting_started/installation.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,17 @@ variables in the config:
8080
.. code-block:: console
8181
8282
83-
$ shpc config add registry:/<DIR>
84-
$ shpc config set module_base:/<DIR>
85-
$ shpc config set container_base:/<DIR>
83+
$ shpc config add registry /<DIR>
84+
$ shpc config set module_base /<DIR>
85+
$ shpc config set container_base /<DIR>
8686
8787
8888
Also importantly, if you are using environment modules (Tcl) and not LMOD, you need
8989
to tell shpc about this (as it defaults to LMOD):
9090

9191
.. code-block:: console
9292
93-
$ shpc config set module_sys:tcl
93+
$ shpc config set module_sys tcl
9494
9595
You can also easily (manually) update any settings in the ``shpc/settings.yaml`` file:
9696

docs/getting_started/user-guide.rst

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ default).
4040

4141
.. code-block:: console
4242
43-
$ shpc config set module_sys:tcl
44-
$ shpc config set module_sys:lmod # default
43+
$ shpc config set module_sys tcl
44+
$ shpc config set module_sys lmod # default
4545
4646
4747
You can then easily install, load, and use modules:
@@ -81,8 +81,8 @@ software, which is typicall lmod or tcl. Here is how to change from the default
8181

8282
.. code-block:: console
8383
84-
$ shpc config set module_sys:tcl
85-
$ shpc config set module_sys:lmod # this is the default, which we change back to!
84+
$ shpc config set module_sys tcl
85+
$ shpc config set module_sys lmod # this is the default, which we change back to!
8686
8787
8888
Once you have the correct module software indicated, try installing a container:
@@ -315,10 +315,10 @@ your install:
315315
.. code-block:: console
316316
317317
# an absolute path
318-
$ shpc config set module_base:/opt/lmod/modules
318+
$ shpc config set module_base /opt/lmod/modules
319319
320320
# or a path relative to a variable location remember to escape the "$"
321-
$ shpc config set module_base:\$root_dir/modules
321+
$ shpc config set module_base \$root_dir/modules
322322
323323
324324
This directory will be the base where lua files are added, and containers are stored
@@ -351,7 +351,7 @@ different. For example:
351351
.. code-block:: console
352352
353353
$ mkdir -p /tmp/containers
354-
$ shpc config set container_base:/tmp/containers
354+
$ shpc config set container_base /tmp/containers
355355
356356
357357
The same hierarchy will be preserved as to not put all containers in the same
@@ -464,7 +464,7 @@ or you can set the global variable to what you want to use (it defaults to lmod)
464464

465465
.. code-block:: console
466466
467-
$ shpc config set module_sys:tcl
467+
$ shpc config set module_sys tcl
468468
469469
470470
The command line argument, if provided, always over-rides the default.
@@ -488,7 +488,7 @@ or for a global setting:
488488

489489
.. code-block:: console
490490
491-
$ shpc config set container_tech:podman
491+
$ shpc config set container_tech podman
492492
493493
494494
If you would like support for a different container technology that has not been
@@ -520,6 +520,26 @@ Container-specific scripts you'll want to include in the container.yaml are desc
520520
# use for singularity aliases
521521
singularity: singularity.sh
522522
523+
Since these are nested values, to get the current value you can use a ``:`` to separate
524+
the fields, e.g.,:
525+
526+
.. code-block:: console
527+
528+
$ shpc config get wrapper_scripts:enabled
529+
wrapper_scripts:enabled False
530+
531+
And if you want to change the default, just add another level:
532+
533+
.. code-block:: console
534+
535+
$ shpc config set wrapper_scripts:enabled true
536+
Updated wrapper_scripts:enabled to be true
537+
538+
And don't forget you can manually update the file in an editor:
539+
540+
.. code-block:: console
541+
542+
$ shpc config edit
523543
524544
Since different container technologies might expose different environment variables (e.g., ``SINGULARITY_OPTS`` vs ``PODMAN_OPTS``)
525545
they are organized above based on the container technology. If you want to customize the wrapper script, simply replace the relative paths
@@ -688,7 +708,7 @@ If you always want to install to an (existing) named view, simply set the ``defa
688708

689709
.. code-block:: console
690710
691-
$ shpc config set default_view:mpi
711+
$ shpc config set default_view mpi
692712
693713
You should obviously create the view first or you'll get an error message that it does not exist!
694714
When you have a default view set, any install that you do will install to the module base and also your view.
@@ -717,7 +737,7 @@ for a short period of time to install to it. If you want to disable this, then j
717737

718738
.. code-block:: console
719739
720-
$ shpc config set default_view:null
740+
$ shpc config set default_view null
721741
722742
And note you can also ask to install to a view "one off":
723743

@@ -908,10 +928,10 @@ The following example shows changing the default module_base path from the insta
908928
.. code-block:: console
909929
910930
# an absolute path
911-
$ shpc config set module_base:/opt/lmod/modules
931+
$ shpc config set module_base /opt/lmod/modules
912932
913933
# or a path relative to the install directory, remember to escape the "$"
914-
$ shpc config set module_base:\$install_dir/modules
934+
$ shpc config set module_base \$install_dir/modules
915935
916936
917937
And then to get values:
@@ -925,8 +945,8 @@ And to add and remove a value to a list:
925945

926946
.. code-block:: console
927947
928-
$ shpc config add registry:/tmp/registry
929-
$ shpc config remove registry:/tmp/registry
948+
$ shpc config add registry /tmp/registry
949+
$ shpc config remove registry /tmp/registry
930950
931951
932952
You can also open the config in the editor defined in settings at ``config_editor``
@@ -1081,7 +1101,7 @@ Note that you can also set the namespace as any other setting:
10811101

10821102
.. code-block:: console
10831103
1084-
$ shpc config set namespace:ghcr.io/autamus
1104+
$ shpc config set namespace ghcr.io/autamus
10851105
10861106
Namespaces currently work with:
10871107

shpc/client/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,13 @@ def get_parser():
217217
"params",
218218
nargs="*",
219219
help="""Set or get a config value, edit the config, add or remove a list variable, or create a user-specific config.
220-
shpc config set key:value
220+
shpc config set key value
221+
shpc config set key:subkey value
221222
shpc config get key
222223
shpc edit
223224
shpc config inituser
224-
shpc config add registry:/tmp/registry
225-
shpc config remove registry:/tmp/registry""",
225+
shpc config add registry /tmp/registry
226+
shpc config remove registry /tmp/registry""",
226227
type=str,
227228
)
228229
# Generate markdown docs for a container registry entry

shpc/client/config.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ def main(args, parser, extra, subparser):
3535
return cli.settings.edit()
3636

3737
if command in ["set", "add", "remove"]:
38-
39-
# Update each param
40-
for param in args.params:
41-
cli.settings.update_param(command, param)
38+
cli.settings.update_param(command, args.params)
4239

4340
# Save settings
4441
cli.settings.save()
@@ -47,7 +44,7 @@ def main(args, parser, extra, subparser):
4744
elif command == "get":
4845
for key in args.params:
4946
value = cli.settings.get(key)
50-
value = value or "is unset"
47+
value = "is unset" if value is None else value
5148
logger.info("%s %s" % (key.ljust(30), value))
5249

5350
else:

shpc/main/settings.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,20 +202,25 @@ def set(self, key, value):
202202
"""
203203
Set a setting based on key and value. If the key has :, it's nested
204204
"""
205-
value = self.parse_boolean(value)
205+
while ":" in key:
206+
value = str(value)
207+
key, extra = key.split(":", 1)
208+
value = f"{extra}:{value}"
206209

207210
# List values not allowed for set
208211
current = self._settings.get(key)
209212
if current and isinstance(current, list):
210213
logger.exit("You cannot use 'set' for a list. Use add/remove instead.")
211214

212215
# This is a reference to a dictionary (object) setting
216+
# We assume only one level of nesting allowed
213217
if isinstance(value, str) and ":" in value:
214218
subkey, value = value.split(":")
215219
value = self.parse_boolean(value)
216220
value = self.parse_null(value)
217221
self._settings[key][subkey] = value
218222
else:
223+
value = self.parse_boolean(value)
219224
value = self.parse_null(value)
220225
self._settings[key] = value
221226

@@ -296,13 +301,27 @@ def update_param(self, command, param):
296301
"""
297302
Given a parameter, update the configuration on the fly if it's in set/add/remove
298303
"""
299-
if ":" not in param:
300-
logger.warning(
301-
"Param %s is missing a :, should be key:value pair. Skipping." % param
302-
)
303-
return
304+
# If we are given a list, assume is key and value at end
305+
if isinstance(param, list):
306+
# If one given, assume old format
307+
if len(param) == 1:
308+
param = param[0]
309+
elif len(param) == 2:
310+
key, value = param
311+
elif len(param) != 2:
312+
logger.exit(
313+
f"When providing a list, it must be a [key, value]. Found {param}"
314+
)
315+
316+
# With a string, assume splittling by :
317+
if isinstance(param, str):
318+
if ":" not in param:
319+
logger.exit(
320+
"Param %s is missing a :, should be key:value pair. Skipping."
321+
% param
322+
)
323+
key, value = param.rsplit(":", 1)
304324

305-
key, value = param.split(":", 1)
306325
if command == "set":
307326
self.set(key, value)
308327
logger.info("Updated %s to be %s" % (key, value))

shpc/settings.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ wrapper_shell: /bin/bash
7070
# If you want a custom wrapper script for a one-off container, define it there with the script
7171
# stored alongside the container.yaml.
7272
wrapper_scripts:
73-
enabled: false
73+
enabled: true
7474

7575
# use for docker aliases (set to null to disable)
7676
docker: docker.sh
@@ -99,3 +99,4 @@ container_features:
9999
# defaults to ~/.Xauthority if set to true and the container has x11: true
100100
home: # one of null, or a single path or src:dest path.
101101
# home: true in a container.yaml will use this path, if defines
102+
updated_at: '2022-07-08T11:31:49Z'

0 commit comments

Comments
 (0)