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

remove automatic resolver #112

Merged
merged 3 commits into from
Apr 10, 2018
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
4 changes: 0 additions & 4 deletions brigade/core/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,3 @@ def merge_two_dicts(x, y):
z = dict(x)
z.update(y)
return z


def format_string(text, task, **kwargs):
return text.format(host=task.host, **merge_two_dicts(task.host.items(), kwargs))
2 changes: 0 additions & 2 deletions brigade/plugins/tasks/commands/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


from brigade.core.exceptions import CommandError
from brigade.core.helpers import format_string
from brigade.core.task import Result


Expand All @@ -22,7 +21,6 @@ def command(task, command):
Raises:
:obj:`brigade.core.exceptions.CommandError`: when there is a command error
"""
command = format_string(command, task, **task.host)
cmd = subprocess.Popen(
shlex.split(command),
stdout=subprocess.PIPE,
Expand Down
2 changes: 0 additions & 2 deletions brigade/plugins/tasks/data/load_json.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json

from brigade.core.helpers import format_string
from brigade.core.task import Result


Expand All @@ -15,7 +14,6 @@ def load_json(task, file):
:obj:`brigade.core.task.Result`:
* result (``dict``): dictionary with the contents of the file
"""
file = format_string(file, task)
with open(file, "r") as f:
data = json.loads(f.read())

Expand Down
2 changes: 0 additions & 2 deletions brigade/plugins/tasks/data/load_yaml.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from brigade.core.helpers import format_string
from brigade.core.task import Result


Expand All @@ -16,7 +15,6 @@ def load_yaml(task, file):
:obj:`brigade.core.task.Result`:
* result (``dict``): dictionary with the contents of the file
"""
file = format_string(file, task)
with open(file, "r") as f:
data = yaml.load(f.read())

Expand Down
3 changes: 0 additions & 3 deletions brigade/plugins/tasks/files/sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import stat

from brigade.core.exceptions import CommandError
from brigade.core.helpers import format_string
from brigade.core.task import Result
from brigade.plugins.tasks import commands

Expand Down Expand Up @@ -124,8 +123,6 @@ def sftp(task, src, dst, action, dry_run=None):
* changed (``bool``):
* files_changed (``list``): list of files that changed
"""
src = format_string(src, task, **task.host)
dst = format_string(dst, task, **task.host)
dry_run = task.is_dry_run(dry_run)
actions = {"put": put, "get": get}
client = task.host.get_connection("paramiko")
Expand Down
4 changes: 0 additions & 4 deletions brigade/plugins/tasks/networking/napalm_configure.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from brigade.core.helpers import format_string
from brigade.core.task import Result


Expand All @@ -20,9 +19,6 @@ def napalm_configure(
* diff (``string``): change in the system
"""
device = task.host.get_connection("napalm")
filename = format_string(
filename, task, **task.host
) if filename is not None else None

if replace:
device.load_replace_candidate(filename=filename, config=configuration)
Expand Down
6 changes: 2 additions & 4 deletions brigade/plugins/tasks/text/template_file.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from brigade.core.helpers import format_string, jinja_helper, merge_two_dicts
from brigade.core.helpers import jinja_helper, merge_two_dicts
from brigade.core.task import Result


Expand All @@ -8,16 +8,14 @@ def template_file(task, template, path, **kwargs):

Arguments:
template (string): filename
path (string): path to dir with templates (will be rendered with format)
path (string): path to dir with templates
**kwargs: additional data to pass to the template

Returns:
:obj:`brigade.core.task.Result`:
* result (``string``): rendered string
"""
merged = merge_two_dicts(task.host, kwargs)
path = format_string(path, task, **kwargs)
template = format_string(template, task, **kwargs)
text = jinja_helper.render_from_file(
template=template, path=path, host=task.host, **merged
)
Expand Down
9 changes: 7 additions & 2 deletions tests/plugins/tasks/commands/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
from brigade.plugins.tasks import commands


def echo_hostname(task):
command = "echo {host.name}".format(host=task.host)
task.run(task=commands.command, command=command)


class Test(object):

def test_command(self, brigade):
result = brigade.run(commands.command, command="echo {host.name}")
result = brigade.run(echo_hostname)
assert result
for h, r in result.items():
assert h == r.stdout.strip()
assert h == r[1].stdout.strip()
Copy link
Collaborator

@ktbyers ktbyers Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seemed a bit strange...why the addition of the [1] i.e. why did it go from r.stdout.strip() to r[1].stdout.strip().

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I understand this behavior now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for reference; the reason is because instead of calling a task directly we are calling now a grouped task. Grouped tasks return a dict of results (key is host) with a list of results of which the first element is the result for the whole group.


def test_command_error(self, brigade):
result = brigade.run(commands.command, command="ech")
Expand Down
94 changes: 46 additions & 48 deletions tests/plugins/tasks/files/test_sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,48 @@
# import pytest


def get_file(task):
filename = "/tmp/{uuid}-{host.name}".format(uuid=uuid.uuid4(), host=task.host)
r = task.run(
task=files.sftp, dry_run=True, action="get", src="/etc/hostname", dst=filename
)
assert r
assert r.changed, r.files_changed

r = task.run(
task=files.sftp, dry_run=False, action="get", src="/etc/hostname", dst=filename
)
assert r
assert r.changed, r.files_changed

r = task.run(
task=files.sftp, dry_run=False, action="get", src="/etc/hostname", dst=filename
)
assert r
assert not r.changed


def get_directory(task):
filename = "/tmp/{uuid}-{host.name}".format(uuid=uuid.uuid4(), host=task.host)
r = task.run(
task=files.sftp, dry_run=True, action="get", src="/etc/terminfo/", dst=filename
)
assert r
assert r.changed, r.files_changed

r = task.run(
task=files.sftp, dry_run=False, action="get", src="/etc/terminfo/", dst=filename
)
assert r
assert r.changed, r.files_changed

r = task.run(
task=files.sftp, dry_run=True, action="get", src="/etc/terminfo/", dst=filename
)
assert r
assert not r.changed


class Test(object):

def test_sftp_put(self, brigade):
Expand Down Expand Up @@ -46,30 +88,8 @@ def test_sftp_put(self, brigade):
assert not r.changed

def test_sftp_get(self, brigade):
filename = "/tmp/" + str(uuid.uuid4()) + "-{host}"
result = brigade.run(
files.sftp, dry_run=True, action="get", src="/etc/hostname", dst=filename
)

assert result
for h, r in result.items():
assert r.changed, r.files_changed

result = brigade.run(
files.sftp, dry_run=False, action="get", src="/etc/hostname", dst=filename
)

assert result
for h, r in result.items():
assert r.changed, r.files_changed

result = brigade.run(
files.sftp, dry_run=False, action="get", src="/etc/hostname", dst=filename
)

assert result
for h, r in result.items():
assert not r.changed
result = brigade.run(get_file)
assert not result.failed

def test_sftp_put_directory(self, brigade):
result = brigade.run(
Expand Down Expand Up @@ -97,27 +117,5 @@ def test_sftp_put_directory(self, brigade):
assert not r.changed

def test_sftp_get_directory(self, brigade):
filename = "/tmp/" + str(uuid.uuid4()) + "-{host}"
result = brigade.run(
files.sftp, dry_run=True, action="get", src="/etc/terminfo/", dst=filename
)

assert result
for h, r in result.items():
assert r.changed, r.files_changed

result = brigade.run(
files.sftp, dry_run=False, action="get", src="/etc/terminfo/", dst=filename
)

assert result
for h, r in result.items():
assert r.changed, r.files_changed

result = brigade.run(
files.sftp, dry_run=True, action="get", src="/etc/terminfo/", dst=filename
)

assert result
for h, r in result.items():
assert not r.changed
result = brigade.run(get_directory)
assert not result.failed
3 changes: 0 additions & 3 deletions tests/plugins/tasks/networking/test_napalm_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class Test(object):

def test_napalm_validate_src_ok(self, brigade):
opt = {"path": THIS_DIR + "/mocked/napalm_get/test_napalm_getters"}
print(opt["path"])
d = brigade.filter(name="dev3.group_2")
d.run(connections.napalm_connection, optional_args=opt)
result = d.run(
Expand All @@ -22,7 +21,6 @@ def test_napalm_validate_src_ok(self, brigade):

def test_napalm_validate_src_error(self, brigade):
opt = {"path": THIS_DIR + "/mocked/napalm_get/test_napalm_getters"}
print(opt["path"])
d = brigade.filter(name="dev3.group_2")
d.run(connections.napalm_connection, optional_args=opt)

Expand All @@ -36,7 +34,6 @@ def test_napalm_validate_src_error(self, brigade):

def test_napalm_validate_src_validate_source(self, brigade):
opt = {"path": THIS_DIR + "/mocked/napalm_get/test_napalm_getters"}
print(opt["path"])
d = brigade.filter(name="dev3.group_2")
d.run(connections.napalm_connection, optional_args=opt)

Expand Down