-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from devon-mar/scp-tests
Add SCP only tests
- Loading branch information
Showing
8 changed files
with
253 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import random | ||
import shutil | ||
import string | ||
import tempfile | ||
from pathlib import Path | ||
|
||
import pytest | ||
from nornir.core import Nornir | ||
|
||
from nornir_paramiko.plugins.tasks import paramiko_command, paramiko_sftp | ||
|
||
|
||
def random_str(len: int) -> str: | ||
return "".join( | ||
random.choice(string.ascii_lowercase + string.digits) for _ in range(len) | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def temp_dir(): | ||
tmp = tempfile.mkdtemp() | ||
yield Path(tmp) | ||
shutil.rmtree(tmp) | ||
|
||
|
||
def test_paramiko_sftp(host: str, nr: Nornir, temp_dir: Path) -> None: | ||
nr_filtered = nr.filter(name=host) | ||
|
||
test_content = random_str(20) | ||
|
||
host_file = f"/tmp/test_{random_str(5)}" | ||
|
||
test_put_file = temp_dir.joinpath("test_put") | ||
|
||
with open(test_put_file, "wt") as f: | ||
f.write(test_content) | ||
result = nr_filtered.run( | ||
task=paramiko_sftp, action="put", src=str(test_put_file), dst=host_file | ||
) | ||
|
||
assert result.failed is False | ||
assert len(result[host]) == 1 | ||
host_result = result[host][0] | ||
assert host_result.changed is True | ||
assert host_result.files_changed == [host_file] | ||
|
||
# Test idempotence | ||
result = nr_filtered.run( | ||
task=paramiko_sftp, action="put", src=str(test_put_file), dst=host_file | ||
) | ||
assert result.failed is False | ||
assert len(result[host]) == 1 | ||
host_result = result[host][0] | ||
assert host_result.changed is False | ||
assert host_result.files_changed == [] | ||
|
||
# Now change the content then put | ||
test_content += "a" | ||
with open(test_put_file, "wt") as f: | ||
f.write(test_content) | ||
result = nr_filtered.run( | ||
task=paramiko_sftp, action="put", src=str(test_put_file), dst=host_file | ||
) | ||
|
||
assert result.failed is False | ||
assert len(result[host]) == 1 | ||
host_result = result[host][0] | ||
assert host_result.changed is True | ||
assert host_result.files_changed == [host_file] | ||
|
||
# Now retrieve the file back from the host | ||
test_get_file = temp_dir.joinpath("test_get") | ||
|
||
result = nr_filtered.run( | ||
task=paramiko_sftp, action="get", src=host_file, dst=str(test_get_file) | ||
) | ||
assert result.failed is False | ||
assert len(result[host]) == 1 | ||
host_result = result[host][0] | ||
assert host_result.changed is True | ||
assert host_result.files_changed == [str(test_get_file)] | ||
|
||
with open(test_get_file, "rt") as f: | ||
have_content = f.read() | ||
assert have_content == test_content | ||
|
||
|
||
def test_paramiko_sftp_dry_run(host: str, nr_dry_run: Nornir, temp_dir: Path) -> None: | ||
nr_filtered = nr_dry_run.filter(name=host) | ||
|
||
test_content = random_str(20) | ||
|
||
host_file = f"/tmp/test_{random_str(5)}" | ||
|
||
test_put_file = temp_dir.joinpath("test_put") | ||
|
||
with open(test_put_file, "wt") as f: | ||
f.write(test_content) | ||
result = nr_filtered.run( | ||
task=paramiko_sftp, action="put", src=str(test_put_file), dst=host_file | ||
) | ||
|
||
assert result.failed is False | ||
assert len(result[host]) == 1 | ||
host_result = result[host][0] | ||
assert host_result.changed is True | ||
assert host_result.files_changed == [host_file] | ||
|
||
# Check that the file was not written | ||
result = nr_filtered.run(task=paramiko_command, command=f"test -f '{host_file}'") | ||
assert result.failed is True # because the file wasn't written | ||
|
||
|
||
def test_paramiko_scp_only_host(nr_dry_run: Nornir, temp_dir: Path) -> None: | ||
host = "alpinescp" | ||
nr_filtered = nr_dry_run.filter(name=host) | ||
|
||
test_content = random_str(20) | ||
|
||
host_file = f"/tmp/test_{random_str(5)}" | ||
|
||
test_put_file = temp_dir.joinpath("test_put") | ||
|
||
with open(test_put_file, "wt") as f: | ||
f.write(test_content) | ||
result = nr_filtered.run( | ||
task=paramiko_sftp, | ||
action="put", | ||
src=str(test_put_file), | ||
dst=host_file, | ||
compare=False, | ||
) | ||
|
||
assert result.failed is False | ||
assert len(result[host]) == 1 | ||
host_result = result[host][0] | ||
assert host_result.changed is True | ||
assert host_result.files_changed == [host_file] | ||
|
||
# Check that the file was not written | ||
result = nr_filtered.run(task=paramiko_command, command=f"test -f '{host_file}'") | ||
assert result.failed is False # because the file wasn't written |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FROM alpine:latest | ||
RUN apk add --no-cache dropbear dropbear-scp | ||
RUN mkdir /root/.ssh/ \ | ||
&& echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEq2cLqs++uwJh6xLTy4nX20UDfbgb6cDZl2nNkJEKud" > /root/.ssh/authorized_keys | ||
ENTRYPOINT ["dropbear", "-EFR"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters