Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Expanduser for sharing things
Browse files Browse the repository at this point in the history
This allows one to share things like .ssh/.gnupg and other stuff easier
into the container.
  • Loading branch information
shuhaowu committed May 6, 2018
1 parent e0a3ebc commit 82815c6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lxdock/conf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def from_base_dir(cls, base_dir='.'):
config.interpolate()

try:
schema(config._dict)
config._dict = schema(config._dict)
except Invalid as e:
# Formats the voluptuous error
path = ' @ %s' % '.'.join(map(str, e.path)) if e.path else ''
Expand Down
8 changes: 3 additions & 5 deletions lxdock/conf/schema.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from voluptuous import (ALLOW_EXTRA, All, Any, Coerce, Extra, In, IsDir, Length, Required, Schema,
Url)
from voluptuous import ALLOW_EXTRA, All, Any, Coerce, Extra, In, Length, Required, Schema, Url

from ..provisioners import Provisioner
from .validators import Hostname, LXDIdentifier
from .validators import Hostname, IsDirAfterExpandUser, LXDIdentifier


def get_schema():
Expand All @@ -18,8 +17,7 @@ def get_schema():
'provisioning': [], # will be set dynamically using provisioner classes...
'server': Url(),
'shares': [{
# The existence of the source directory will be checked!
'source': IsDir(),
'source': IsDirAfterExpandUser,
'dest': str,
'set_host_acl': bool, # TODO: need a way to deprecate this
}],
Expand Down
13 changes: 13 additions & 0 deletions lxdock/conf/validators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os.path
import re

from voluptuous import Invalid
from voluptuous.schema_builder import message
from voluptuous.validators import truth

Expand Down Expand Up @@ -29,3 +31,14 @@ def LXDIdentifier(v):
if len(v) > 63:
return False
return lxd_identifier_re.match(v)


def IsDirAfterExpandUser(path):
if type(path) != str:
raise Invalid("expected a string path")

path = os.path.expanduser(path)
if not os.path.isdir(path):
raise Invalid("expected path {} to exists".format(path))

return path
15 changes: 14 additions & 1 deletion tests/unit/conf/test_validators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os.path

import pytest
from voluptuous import Invalid
from voluptuous.error import ValueInvalid

from lxdock.conf.validators import Hostname, LXDIdentifier
from lxdock.conf.validators import Hostname, IsDirAfterExpandUser, LXDIdentifier


class TestHostnameValidator:
Expand Down Expand Up @@ -43,3 +46,13 @@ def test_cannot_allow_identifiers_longer_than_63_characters(self):
assert id_validator('i' * 63)
with pytest.raises(ValueInvalid):
id_validator('i' * 64)


class TestIsDirAfterExpandUser:
def test_converts_dir_if_encountering_tilde(self):
expanded_path = IsDirAfterExpandUser("~/.ssh")
assert expanded_path == os.path.expanduser("~/.ssh")

def test_raise_invalid_if_not_directory(self):
with pytest.raises(Invalid):
IsDirAfterExpandUser("/adkfjak/adfkjakf/adkfjakjdf")

0 comments on commit 82815c6

Please sign in to comment.