Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remote config validation #3628

Merged
merged 8 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions dvc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,7 @@ def load(self, validate=True):
Raises:
ConfigError: thrown if config has an invalid format.
"""
conf = {}
for level in self.LEVELS:
if level in self.files:
_merge(conf, self.load_one(level))
conf = self._load_config_to_level()

if validate:
conf = self.validate(conf)
Expand Down Expand Up @@ -333,6 +330,15 @@ def _map_dirs(conf, func):
dirs_schema = {"cache": {"dir": func}, "remote": {str: {"url": func}}}
return Schema(dirs_schema, extra=ALLOW_EXTRA)(conf)

def _load_config_to_level(self, level=None):
merged_conf = {}
for merge_level in self.LEVELS:
if merge_level == level:
break
if merge_level in self.files:
_merge(merged_conf, self.load_one(merge_level))
return merged_conf

@contextmanager
def edit(self, level="repo"):
if level in {"repo", "local"} and self.dvc_dir is None:
Expand All @@ -342,6 +348,11 @@ def edit(self, level="repo"):
yield conf

conf = self._save_paths(conf, self.files[level])

merged_conf = self._load_config_to_level(level)
karajan1001 marked this conversation as resolved.
Show resolved Hide resolved
_merge(merged_conf, conf)
self.validate(merged_conf)

_save_config(self.files[level], conf)
self.load()

Expand Down
6 changes: 6 additions & 0 deletions tests/func/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ def test_merging_two_levels(dvc):
with dvc.config.edit() as conf:
conf["remote"]["test"] = {"url": "ssh://example.com"}

with pytest.raises(
ConfigError, match=r"expected 'url' for dictionary value"
):
with dvc.config.edit("global") as conf:
conf["remote"]["test"] = {"password": "1"}

with dvc.config.edit("local") as conf:
conf["remote"]["test"] = {"password": "1"}

Expand Down
16 changes: 16 additions & 0 deletions tests/func/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,19 @@ def test_push_order(tmp_dir, dvc, tmp_path_factory, mocker):
dvc.push()
# last uploaded file should be dir checksum
assert mocked_upload.call_args[0][0].endswith(".dir")


def test_remote_modify_validation(dvc):
remote_name = "drive"
unsupported_config = "unsupported_config"
assert (
main(["remote", "add", "-d", remote_name, "gdrive://test/test"]) == 0
)
assert (
main(
["remote", "modify", remote_name, unsupported_config, "something"]
)
== 251
)
config = configobj.ConfigObj(dvc.config.files["repo"])
assert unsupported_config not in config['remote "{}"'.format(remote_name)]