1309 lines
60 KiB
HTML
1309 lines
60 KiB
HTML
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
|
|
<meta name="generator" content="pdoc3 0.11.5">
|
|
<title>connpy.tests.test_core API documentation</title>
|
|
<meta name="description" content="Tests for connpy.core module — node and nodes classes.">
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/13.0.0/sanitize.min.css" integrity="sha512-y1dtMcuvtTMJc1yPgEqF0ZjQbhnc/bFhyvIyVNb9Zk5mIGtqVaAB1Ttl28su8AvFMOY0EwRbAe+HCLqj6W7/KA==" crossorigin>
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/13.0.0/typography.min.css" integrity="sha512-Y1DYSb995BAfxobCkKepB1BqJJTPrOp3zPL74AWFugHHmmdcvO+C48WLrUOlhGMc0QG7AE3f7gmvvcrmX2fDoA==" crossorigin>
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css" crossorigin>
|
|
<style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:1.5em;overflow:hidden}#sidebar > *:last-child{margin-bottom:2cm}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:2em 0 .50em 0}h3{font-size:1.4em;margin:1.6em 0 .7em 0}h4{margin:0;font-size:105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:.2em 0}a{color:#058;text-decoration:none;transition:color .2s ease-in-out}a:visited{color:#503}a:hover{color:#b62}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900;font-weight:bold}pre code{font-size:.8em;line-height:1.4em;padding:1em;display:block}code{background:#f3f3f3;font-family:"DejaVu Sans Mono",monospace;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{margin-top:.6em;font-weight:bold}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source > summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible;min-width:max-content}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}td{padding:0 .5em}.admonition{padding:.1em 1em;margin:1em 0}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style>
|
|
<style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%;height:100vh;overflow:auto;position:sticky;top:0}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul ul{padding-left:1em}.toc > ul > li{margin-top:.5em}}</style>
|
|
<style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style>
|
|
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js" integrity="sha512-D9gUyxqja7hBtkWpPWGt9wfbfaMGVt9gnyCvYa+jojwwPHLCzUm5i8rpk7vD7wNee9bA35eYIjobYPaQuKS1MQ==" crossorigin></script>
|
|
<script>window.addEventListener('DOMContentLoaded', () => {
|
|
hljs.configure({languages: ['bash', 'css', 'diff', 'graphql', 'ini', 'javascript', 'json', 'plaintext', 'python', 'python-repl', 'rust', 'shell', 'sql', 'typescript', 'xml', 'yaml']});
|
|
hljs.highlightAll();
|
|
/* Collapse source docstrings */
|
|
setTimeout(() => {
|
|
[...document.querySelectorAll('.hljs.language-python > .hljs-string')]
|
|
.filter(el => el.innerHTML.length > 200 && ['"""', "'''"].includes(el.innerHTML.substring(0, 3)))
|
|
.forEach(el => {
|
|
let d = document.createElement('details');
|
|
d.classList.add('hljs-string');
|
|
d.innerHTML = '<summary>"""</summary>' + el.innerHTML.substring(3);
|
|
el.replaceWith(d);
|
|
});
|
|
}, 100);
|
|
})</script>
|
|
</head>
|
|
<body>
|
|
<main>
|
|
<article id="content">
|
|
<header>
|
|
<h1 class="title">Module <code>connpy.tests.test_core</code></h1>
|
|
</header>
|
|
<section id="section-intro">
|
|
<p>Tests for connpy.core module — node and nodes classes.</p>
|
|
</section>
|
|
<section>
|
|
</section>
|
|
<section>
|
|
</section>
|
|
<section>
|
|
</section>
|
|
<section>
|
|
<h2 class="section-title" id="header-classes">Classes</h2>
|
|
<dl>
|
|
<dt id="connpy.tests.test_core.TestCommandGeneration"><code class="flex name class">
|
|
<span>class <span class="ident">TestCommandGeneration</span></span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">class TestCommandGeneration:
|
|
def _make_node(self, **kwargs):
|
|
from connpy.core import node
|
|
defaults = {
|
|
"unique": "test", "host": "10.0.0.1", "protocol": "ssh",
|
|
"user": "admin", "password": "", "port": "", "options": "",
|
|
"jumphost": "", "tags": "", "logs": ""
|
|
}
|
|
defaults.update(kwargs)
|
|
return node(defaults.pop("unique"), defaults.pop("host"), **defaults)
|
|
|
|
def test_ssh_cmd_basic(self):
|
|
n = self._make_node()
|
|
cmd = n._get_cmd()
|
|
assert "ssh" in cmd
|
|
assert "admin@10.0.0.1" in cmd
|
|
|
|
def test_ssh_cmd_port(self):
|
|
n = self._make_node(port="2222")
|
|
cmd = n._get_cmd()
|
|
assert "-p 2222" in cmd
|
|
|
|
def test_ssh_cmd_options(self):
|
|
n = self._make_node(options="-o StrictHostKeyChecking=no")
|
|
cmd = n._get_cmd()
|
|
assert "-o StrictHostKeyChecking=no" in cmd
|
|
|
|
def test_sftp_cmd_port(self):
|
|
n = self._make_node(protocol="sftp", port="2222")
|
|
cmd = n._get_cmd()
|
|
assert "-P 2222" in cmd # SFTP uses uppercase P
|
|
|
|
def test_telnet_cmd(self):
|
|
n = self._make_node(protocol="telnet", port="23")
|
|
cmd = n._get_cmd()
|
|
assert "telnet 10.0.0.1" in cmd
|
|
assert "23" in cmd
|
|
|
|
def test_kubectl_cmd(self):
|
|
n = self._make_node(protocol="kubectl", host="my-pod", tags={"kube_command": "/bin/sh"})
|
|
cmd = n._get_cmd()
|
|
assert "kubectl exec" in cmd
|
|
assert "my-pod" in cmd
|
|
assert "/bin/sh" in cmd
|
|
|
|
def test_kubectl_cmd_default_command(self):
|
|
n = self._make_node(protocol="kubectl", host="my-pod")
|
|
cmd = n._get_cmd()
|
|
assert "/bin/bash" in cmd
|
|
|
|
def test_docker_cmd(self):
|
|
n = self._make_node(protocol="docker", host="my-container",
|
|
tags={"docker_command": "/bin/sh"})
|
|
cmd = n._get_cmd()
|
|
assert "docker" in cmd
|
|
assert "my-container" in cmd
|
|
assert "/bin/sh" in cmd
|
|
|
|
def test_invalid_protocol_raises(self):
|
|
n = self._make_node(protocol="invalid_proto")
|
|
with pytest.raises(SystemExit) as exc:
|
|
n._get_cmd()
|
|
assert exc.value.code == 1
|
|
|
|
def test_ssh_cmd_no_user(self):
|
|
n = self._make_node(user="")
|
|
cmd = n._get_cmd()
|
|
assert "10.0.0.1" in cmd
|
|
assert "@" not in cmd # No user@ prefix</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
<h3>Methods</h3>
|
|
<dl>
|
|
<dt id="connpy.tests.test_core.TestCommandGeneration.test_docker_cmd"><code class="name flex">
|
|
<span>def <span class="ident">test_docker_cmd</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_docker_cmd(self):
|
|
n = self._make_node(protocol="docker", host="my-container",
|
|
tags={"docker_command": "/bin/sh"})
|
|
cmd = n._get_cmd()
|
|
assert "docker" in cmd
|
|
assert "my-container" in cmd
|
|
assert "/bin/sh" in cmd</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestCommandGeneration.test_invalid_protocol_raises"><code class="name flex">
|
|
<span>def <span class="ident">test_invalid_protocol_raises</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_invalid_protocol_raises(self):
|
|
n = self._make_node(protocol="invalid_proto")
|
|
with pytest.raises(SystemExit) as exc:
|
|
n._get_cmd()
|
|
assert exc.value.code == 1</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestCommandGeneration.test_kubectl_cmd"><code class="name flex">
|
|
<span>def <span class="ident">test_kubectl_cmd</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_kubectl_cmd(self):
|
|
n = self._make_node(protocol="kubectl", host="my-pod", tags={"kube_command": "/bin/sh"})
|
|
cmd = n._get_cmd()
|
|
assert "kubectl exec" in cmd
|
|
assert "my-pod" in cmd
|
|
assert "/bin/sh" in cmd</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestCommandGeneration.test_kubectl_cmd_default_command"><code class="name flex">
|
|
<span>def <span class="ident">test_kubectl_cmd_default_command</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_kubectl_cmd_default_command(self):
|
|
n = self._make_node(protocol="kubectl", host="my-pod")
|
|
cmd = n._get_cmd()
|
|
assert "/bin/bash" in cmd</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestCommandGeneration.test_sftp_cmd_port"><code class="name flex">
|
|
<span>def <span class="ident">test_sftp_cmd_port</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_sftp_cmd_port(self):
|
|
n = self._make_node(protocol="sftp", port="2222")
|
|
cmd = n._get_cmd()
|
|
assert "-P 2222" in cmd # SFTP uses uppercase P</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestCommandGeneration.test_ssh_cmd_basic"><code class="name flex">
|
|
<span>def <span class="ident">test_ssh_cmd_basic</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_ssh_cmd_basic(self):
|
|
n = self._make_node()
|
|
cmd = n._get_cmd()
|
|
assert "ssh" in cmd
|
|
assert "admin@10.0.0.1" in cmd</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestCommandGeneration.test_ssh_cmd_no_user"><code class="name flex">
|
|
<span>def <span class="ident">test_ssh_cmd_no_user</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_ssh_cmd_no_user(self):
|
|
n = self._make_node(user="")
|
|
cmd = n._get_cmd()
|
|
assert "10.0.0.1" in cmd
|
|
assert "@" not in cmd # No user@ prefix</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestCommandGeneration.test_ssh_cmd_options"><code class="name flex">
|
|
<span>def <span class="ident">test_ssh_cmd_options</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_ssh_cmd_options(self):
|
|
n = self._make_node(options="-o StrictHostKeyChecking=no")
|
|
cmd = n._get_cmd()
|
|
assert "-o StrictHostKeyChecking=no" in cmd</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestCommandGeneration.test_ssh_cmd_port"><code class="name flex">
|
|
<span>def <span class="ident">test_ssh_cmd_port</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_ssh_cmd_port(self):
|
|
n = self._make_node(port="2222")
|
|
cmd = n._get_cmd()
|
|
assert "-p 2222" in cmd</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestCommandGeneration.test_telnet_cmd"><code class="name flex">
|
|
<span>def <span class="ident">test_telnet_cmd</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_telnet_cmd(self):
|
|
n = self._make_node(protocol="telnet", port="23")
|
|
cmd = n._get_cmd()
|
|
assert "telnet 10.0.0.1" in cmd
|
|
assert "23" in cmd</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
</dd>
|
|
</dl>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestLogHandling"><code class="flex name class">
|
|
<span>class <span class="ident">TestLogHandling</span></span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">class TestLogHandling:
|
|
def test_logfile_variable_substitution(self):
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", user="admin", protocol="ssh", port="22",
|
|
logs="/logs/${unique}_${host}_${user}")
|
|
result = n._logfile()
|
|
assert result == "/logs/router1_10.0.0.1_admin"
|
|
|
|
def test_logfile_date_substitution(self):
|
|
from connpy.core import node
|
|
import datetime
|
|
n = node("router1", "10.0.0.1", logs="/logs/${date '%Y'}")
|
|
result = n._logfile()
|
|
assert datetime.datetime.now().strftime("%Y") in result
|
|
|
|
def test_logclean_removes_ansi(self):
|
|
from connpy.core import node
|
|
n = node("test", "10.0.0.1")
|
|
dirty = "\x1B[32mgreen text\x1B[0m"
|
|
clean = n._logclean(dirty, var=True)
|
|
assert "\x1B" not in clean
|
|
assert "green text" in clean
|
|
|
|
def test_logclean_removes_backspaces(self):
|
|
from connpy.core import node
|
|
n = node("test", "10.0.0.1")
|
|
dirty = "type\bo"
|
|
clean = n._logclean(dirty, var=True)
|
|
assert "\b" not in clean</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
<h3>Methods</h3>
|
|
<dl>
|
|
<dt id="connpy.tests.test_core.TestLogHandling.test_logclean_removes_ansi"><code class="name flex">
|
|
<span>def <span class="ident">test_logclean_removes_ansi</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_logclean_removes_ansi(self):
|
|
from connpy.core import node
|
|
n = node("test", "10.0.0.1")
|
|
dirty = "\x1B[32mgreen text\x1B[0m"
|
|
clean = n._logclean(dirty, var=True)
|
|
assert "\x1B" not in clean
|
|
assert "green text" in clean</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestLogHandling.test_logclean_removes_backspaces"><code class="name flex">
|
|
<span>def <span class="ident">test_logclean_removes_backspaces</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_logclean_removes_backspaces(self):
|
|
from connpy.core import node
|
|
n = node("test", "10.0.0.1")
|
|
dirty = "type\bo"
|
|
clean = n._logclean(dirty, var=True)
|
|
assert "\b" not in clean</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestLogHandling.test_logfile_date_substitution"><code class="name flex">
|
|
<span>def <span class="ident">test_logfile_date_substitution</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_logfile_date_substitution(self):
|
|
from connpy.core import node
|
|
import datetime
|
|
n = node("router1", "10.0.0.1", logs="/logs/${date '%Y'}")
|
|
result = n._logfile()
|
|
assert datetime.datetime.now().strftime("%Y") in result</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestLogHandling.test_logfile_variable_substitution"><code class="name flex">
|
|
<span>def <span class="ident">test_logfile_variable_substitution</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_logfile_variable_substitution(self):
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", user="admin", protocol="ssh", port="22",
|
|
logs="/logs/${unique}_${host}_${user}")
|
|
result = n._logfile()
|
|
assert result == "/logs/router1_10.0.0.1_admin"</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
</dd>
|
|
</dl>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestNodeInit"><code class="flex name class">
|
|
<span>class <span class="ident">TestNodeInit</span></span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">class TestNodeInit:
|
|
def test_basic_init(self):
|
|
"""Creates node with basic attributes."""
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", user="admin", password="pass1", protocol="ssh")
|
|
assert n.unique == "router1"
|
|
assert n.host == "10.0.0.1"
|
|
assert n.user == "admin"
|
|
assert n.protocol == "ssh"
|
|
assert n.password == ["pass1"]
|
|
|
|
def test_default_protocol(self):
|
|
"""Default protocol is ssh."""
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1")
|
|
assert n.protocol == "ssh"
|
|
|
|
def test_password_as_list_of_profiles(self, populated_config):
|
|
"""Password list with @profile references resolves correctly."""
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", password=["@office-user"],
|
|
config=populated_config)
|
|
assert n.password == ["officepass"]
|
|
|
|
def test_password_plain_string(self):
|
|
"""Plain string password is wrapped in a list."""
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", password="mypass")
|
|
assert n.password == ["mypass"]
|
|
|
|
def test_node_with_profile(self, populated_config):
|
|
"""Resolves @profile references for user."""
|
|
from connpy.core import node
|
|
n = node("test1", "10.0.0.1", user="@office-user", password="plain",
|
|
config=populated_config)
|
|
assert n.user == "officeadmin"
|
|
|
|
def test_node_tags(self):
|
|
"""Tags are stored correctly."""
|
|
from connpy.core import node
|
|
tags = {"os": "cisco_ios", "prompt": r"Router#"}
|
|
n = node("router1", "10.0.0.1", tags=tags)
|
|
assert n.tags["os"] == "cisco_ios"</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
<h3>Methods</h3>
|
|
<dl>
|
|
<dt id="connpy.tests.test_core.TestNodeInit.test_basic_init"><code class="name flex">
|
|
<span>def <span class="ident">test_basic_init</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_basic_init(self):
|
|
"""Creates node with basic attributes."""
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", user="admin", password="pass1", protocol="ssh")
|
|
assert n.unique == "router1"
|
|
assert n.host == "10.0.0.1"
|
|
assert n.user == "admin"
|
|
assert n.protocol == "ssh"
|
|
assert n.password == ["pass1"]</code></pre>
|
|
</details>
|
|
<div class="desc"><p>Creates node with basic attributes.</p></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestNodeInit.test_default_protocol"><code class="name flex">
|
|
<span>def <span class="ident">test_default_protocol</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_default_protocol(self):
|
|
"""Default protocol is ssh."""
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1")
|
|
assert n.protocol == "ssh"</code></pre>
|
|
</details>
|
|
<div class="desc"><p>Default protocol is ssh.</p></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestNodeInit.test_node_tags"><code class="name flex">
|
|
<span>def <span class="ident">test_node_tags</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_node_tags(self):
|
|
"""Tags are stored correctly."""
|
|
from connpy.core import node
|
|
tags = {"os": "cisco_ios", "prompt": r"Router#"}
|
|
n = node("router1", "10.0.0.1", tags=tags)
|
|
assert n.tags["os"] == "cisco_ios"</code></pre>
|
|
</details>
|
|
<div class="desc"><p>Tags are stored correctly.</p></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestNodeInit.test_node_with_profile"><code class="name flex">
|
|
<span>def <span class="ident">test_node_with_profile</span></span>(<span>self, populated_config)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_node_with_profile(self, populated_config):
|
|
"""Resolves @profile references for user."""
|
|
from connpy.core import node
|
|
n = node("test1", "10.0.0.1", user="@office-user", password="plain",
|
|
config=populated_config)
|
|
assert n.user == "officeadmin"</code></pre>
|
|
</details>
|
|
<div class="desc"><p>Resolves @profile references for user.</p></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestNodeInit.test_password_as_list_of_profiles"><code class="name flex">
|
|
<span>def <span class="ident">test_password_as_list_of_profiles</span></span>(<span>self, populated_config)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_password_as_list_of_profiles(self, populated_config):
|
|
"""Password list with @profile references resolves correctly."""
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", password=["@office-user"],
|
|
config=populated_config)
|
|
assert n.password == ["officepass"]</code></pre>
|
|
</details>
|
|
<div class="desc"><p>Password list with @profile references resolves correctly.</p></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestNodeInit.test_password_plain_string"><code class="name flex">
|
|
<span>def <span class="ident">test_password_plain_string</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_password_plain_string(self):
|
|
"""Plain string password is wrapped in a list."""
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", password="mypass")
|
|
assert n.password == ["mypass"]</code></pre>
|
|
</details>
|
|
<div class="desc"><p>Plain string password is wrapped in a list.</p></div>
|
|
</dd>
|
|
</dl>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestNodeRun"><code class="flex name class">
|
|
<span>class <span class="ident">TestNodeRun</span></span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">class TestNodeRun:
|
|
def _make_connected_node(self, mock_pexpect_obj, **kwargs):
|
|
"""Create a node and mock its _connect to succeed."""
|
|
from connpy.core import node
|
|
defaults = {
|
|
"unique": "router1", "host": "10.0.0.1",
|
|
"protocol": "ssh", "user": "admin", "password": ""
|
|
}
|
|
defaults.update(kwargs)
|
|
n = node(defaults.pop("unique"), defaults.pop("host"), **defaults)
|
|
return n
|
|
|
|
def test_run_returns_output(self, mock_pexpect):
|
|
"""run() returns string output."""
|
|
child = mock_pexpect["child"]
|
|
pexp = mock_pexpect["pexpect"]
|
|
|
|
# Simulate: connect succeeds, command runs, prompt found
|
|
child.expect.return_value = 9 # prompt index for ssh
|
|
child.logfile_read = None
|
|
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", user="admin", password="")
|
|
|
|
# Mock _connect to return True and set up child
|
|
with patch.object(n, '_connect', return_value=True):
|
|
n.child = child
|
|
log_buffer = io.BytesIO(b"show version\nRouter v1.0\nrouter#")
|
|
n.mylog = log_buffer
|
|
child.logfile_read = log_buffer
|
|
|
|
with patch.object(n, '_logclean', return_value="Router v1.0"):
|
|
output = n.run(["show version"])
|
|
|
|
assert n.status == 0
|
|
assert output == "Router v1.0"
|
|
|
|
def test_run_status_1_on_failure(self, mock_pexpect):
|
|
"""Status 1 when connection fails."""
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", user="admin", password="")
|
|
|
|
with patch.object(n, '_connect', return_value="Connection failed code: 1\nrefused"):
|
|
output = n.run(["show version"])
|
|
|
|
assert n.status == 1
|
|
assert "refused" in output
|
|
|
|
def test_run_with_variables(self, mock_pexpect):
|
|
"""Variables get substituted in commands."""
|
|
child = mock_pexpect["child"]
|
|
child.expect.return_value = 9
|
|
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", user="admin", password="")
|
|
|
|
sent_commands = []
|
|
child.sendline.side_effect = lambda cmd: sent_commands.append(cmd)
|
|
|
|
with patch.object(n, '_connect', return_value=True):
|
|
n.child = child
|
|
n.mylog = io.BytesIO(b"output")
|
|
with patch.object(n, '_logclean', return_value="output"):
|
|
n.run(["show ip route {subnet}"], vars={"subnet": "10.0.0.0/24"})
|
|
|
|
assert "show ip route 10.0.0.0/24" in sent_commands
|
|
|
|
def test_run_saves_to_folder(self, mock_pexpect, tmp_path):
|
|
"""folder param saves log file."""
|
|
child = mock_pexpect["child"]
|
|
child.expect.return_value = 9
|
|
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", user="admin", password="")
|
|
|
|
with patch.object(n, '_connect', return_value=True):
|
|
n.child = child
|
|
n.mylog = io.BytesIO(b"log output")
|
|
with patch.object(n, '_logclean', return_value="log output"):
|
|
n.run(["show version"], folder=str(tmp_path))
|
|
|
|
log_files = list(tmp_path.glob("router1_*.txt"))
|
|
assert len(log_files) == 1
|
|
assert "log output" in log_files[0].read_text()</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
<h3>Methods</h3>
|
|
<dl>
|
|
<dt id="connpy.tests.test_core.TestNodeRun.test_run_returns_output"><code class="name flex">
|
|
<span>def <span class="ident">test_run_returns_output</span></span>(<span>self, mock_pexpect)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_run_returns_output(self, mock_pexpect):
|
|
"""run() returns string output."""
|
|
child = mock_pexpect["child"]
|
|
pexp = mock_pexpect["pexpect"]
|
|
|
|
# Simulate: connect succeeds, command runs, prompt found
|
|
child.expect.return_value = 9 # prompt index for ssh
|
|
child.logfile_read = None
|
|
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", user="admin", password="")
|
|
|
|
# Mock _connect to return True and set up child
|
|
with patch.object(n, '_connect', return_value=True):
|
|
n.child = child
|
|
log_buffer = io.BytesIO(b"show version\nRouter v1.0\nrouter#")
|
|
n.mylog = log_buffer
|
|
child.logfile_read = log_buffer
|
|
|
|
with patch.object(n, '_logclean', return_value="Router v1.0"):
|
|
output = n.run(["show version"])
|
|
|
|
assert n.status == 0
|
|
assert output == "Router v1.0"</code></pre>
|
|
</details>
|
|
<div class="desc"><p>run() returns string output.</p></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestNodeRun.test_run_saves_to_folder"><code class="name flex">
|
|
<span>def <span class="ident">test_run_saves_to_folder</span></span>(<span>self, mock_pexpect, tmp_path)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_run_saves_to_folder(self, mock_pexpect, tmp_path):
|
|
"""folder param saves log file."""
|
|
child = mock_pexpect["child"]
|
|
child.expect.return_value = 9
|
|
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", user="admin", password="")
|
|
|
|
with patch.object(n, '_connect', return_value=True):
|
|
n.child = child
|
|
n.mylog = io.BytesIO(b"log output")
|
|
with patch.object(n, '_logclean', return_value="log output"):
|
|
n.run(["show version"], folder=str(tmp_path))
|
|
|
|
log_files = list(tmp_path.glob("router1_*.txt"))
|
|
assert len(log_files) == 1
|
|
assert "log output" in log_files[0].read_text()</code></pre>
|
|
</details>
|
|
<div class="desc"><p>folder param saves log file.</p></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestNodeRun.test_run_status_1_on_failure"><code class="name flex">
|
|
<span>def <span class="ident">test_run_status_1_on_failure</span></span>(<span>self, mock_pexpect)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_run_status_1_on_failure(self, mock_pexpect):
|
|
"""Status 1 when connection fails."""
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", user="admin", password="")
|
|
|
|
with patch.object(n, '_connect', return_value="Connection failed code: 1\nrefused"):
|
|
output = n.run(["show version"])
|
|
|
|
assert n.status == 1
|
|
assert "refused" in output</code></pre>
|
|
</details>
|
|
<div class="desc"><p>Status 1 when connection fails.</p></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestNodeRun.test_run_with_variables"><code class="name flex">
|
|
<span>def <span class="ident">test_run_with_variables</span></span>(<span>self, mock_pexpect)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_run_with_variables(self, mock_pexpect):
|
|
"""Variables get substituted in commands."""
|
|
child = mock_pexpect["child"]
|
|
child.expect.return_value = 9
|
|
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", user="admin", password="")
|
|
|
|
sent_commands = []
|
|
child.sendline.side_effect = lambda cmd: sent_commands.append(cmd)
|
|
|
|
with patch.object(n, '_connect', return_value=True):
|
|
n.child = child
|
|
n.mylog = io.BytesIO(b"output")
|
|
with patch.object(n, '_logclean', return_value="output"):
|
|
n.run(["show ip route {subnet}"], vars={"subnet": "10.0.0.0/24"})
|
|
|
|
assert "show ip route 10.0.0.0/24" in sent_commands</code></pre>
|
|
</details>
|
|
<div class="desc"><p>Variables get substituted in commands.</p></div>
|
|
</dd>
|
|
</dl>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestNodeTest"><code class="flex name class">
|
|
<span>class <span class="ident">TestNodeTest</span></span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">class TestNodeTest:
|
|
def test_test_returns_dict(self, mock_pexpect):
|
|
"""test() returns dict of results."""
|
|
child = mock_pexpect["child"]
|
|
child.expect.return_value = 0 # prompt found (index 0 in test expects)
|
|
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", user="admin", password="")
|
|
|
|
with patch.object(n, '_connect', return_value=True):
|
|
n.child = child
|
|
n.mylog = io.BytesIO(b"1.1.1.1 is up")
|
|
with patch.object(n, '_logclean', return_value="1.1.1.1 is up"):
|
|
result = n.test(["ping 1.1.1.1"], "1.1.1.1")
|
|
|
|
assert isinstance(result, dict)
|
|
assert result.get("1.1.1.1") == True
|
|
|
|
def test_test_expected_not_found(self, mock_pexpect):
|
|
"""Expected text not found returns False."""
|
|
child = mock_pexpect["child"]
|
|
child.expect.return_value = 0
|
|
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", user="admin", password="")
|
|
|
|
with patch.object(n, '_connect', return_value=True):
|
|
n.child = child
|
|
n.mylog = io.BytesIO(b"some other output")
|
|
with patch.object(n, '_logclean', return_value="some other output"):
|
|
result = n.test(["ping 1.1.1.1"], "1.1.1.1")
|
|
|
|
assert isinstance(result, dict)
|
|
assert result.get("1.1.1.1") == False</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
<h3>Methods</h3>
|
|
<dl>
|
|
<dt id="connpy.tests.test_core.TestNodeTest.test_test_expected_not_found"><code class="name flex">
|
|
<span>def <span class="ident">test_test_expected_not_found</span></span>(<span>self, mock_pexpect)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_test_expected_not_found(self, mock_pexpect):
|
|
"""Expected text not found returns False."""
|
|
child = mock_pexpect["child"]
|
|
child.expect.return_value = 0
|
|
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", user="admin", password="")
|
|
|
|
with patch.object(n, '_connect', return_value=True):
|
|
n.child = child
|
|
n.mylog = io.BytesIO(b"some other output")
|
|
with patch.object(n, '_logclean', return_value="some other output"):
|
|
result = n.test(["ping 1.1.1.1"], "1.1.1.1")
|
|
|
|
assert isinstance(result, dict)
|
|
assert result.get("1.1.1.1") == False</code></pre>
|
|
</details>
|
|
<div class="desc"><p>Expected text not found returns False.</p></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestNodeTest.test_test_returns_dict"><code class="name flex">
|
|
<span>def <span class="ident">test_test_returns_dict</span></span>(<span>self, mock_pexpect)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_test_returns_dict(self, mock_pexpect):
|
|
"""test() returns dict of results."""
|
|
child = mock_pexpect["child"]
|
|
child.expect.return_value = 0 # prompt found (index 0 in test expects)
|
|
|
|
from connpy.core import node
|
|
n = node("router1", "10.0.0.1", user="admin", password="")
|
|
|
|
with patch.object(n, '_connect', return_value=True):
|
|
n.child = child
|
|
n.mylog = io.BytesIO(b"1.1.1.1 is up")
|
|
with patch.object(n, '_logclean', return_value="1.1.1.1 is up"):
|
|
result = n.test(["ping 1.1.1.1"], "1.1.1.1")
|
|
|
|
assert isinstance(result, dict)
|
|
assert result.get("1.1.1.1") == True</code></pre>
|
|
</details>
|
|
<div class="desc"><p>test() returns dict of results.</p></div>
|
|
</dd>
|
|
</dl>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestNodes"><code class="flex name class">
|
|
<span>class <span class="ident">TestNodes</span></span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">class TestNodes:
|
|
def test_nodes_init(self):
|
|
"""Creates list of node objects."""
|
|
from connpy.core import nodes
|
|
nodes_dict = {
|
|
"r1": {"host": "10.0.0.1", "user": "admin", "password": ""},
|
|
"r2": {"host": "10.0.0.2", "user": "admin", "password": ""}
|
|
}
|
|
mynodes = nodes(nodes_dict)
|
|
assert len(mynodes.nodelist) == 2
|
|
assert hasattr(mynodes, "r1")
|
|
assert hasattr(mynodes, "r2")
|
|
|
|
def test_nodes_run_parallel(self):
|
|
"""run() executes on all nodes and returns dict."""
|
|
from connpy.core import nodes
|
|
|
|
nodes_dict = {
|
|
"r1": {"host": "10.0.0.1", "user": "admin", "password": ""},
|
|
"r2": {"host": "10.0.0.2", "user": "admin", "password": ""}
|
|
}
|
|
mynodes = nodes(nodes_dict)
|
|
|
|
# Mock run on each node — must set output AND status on the node
|
|
for n in mynodes.nodelist:
|
|
original_node = n # capture by value
|
|
def make_mock(node_ref):
|
|
def mock_run(commands, **kwargs):
|
|
node_ref.output = f"output from {node_ref.unique}"
|
|
node_ref.status = 0
|
|
return mock_run
|
|
n.run = make_mock(n)
|
|
|
|
result = mynodes.run(["show version"])
|
|
assert "r1" in result
|
|
assert "r2" in result
|
|
|
|
def test_nodes_splitlist(self):
|
|
"""_splitlist divides list correctly."""
|
|
from connpy.core import nodes
|
|
mynodes = nodes({"r1": {"host": "1.1.1.1", "user": "", "password": ""}})
|
|
chunks = list(mynodes._splitlist([1, 2, 3, 4, 5], 2))
|
|
assert chunks == [[1, 2], [3, 4], [5]]
|
|
|
|
def test_nodes_run_with_vars(self):
|
|
"""Variables per node and __global__ work."""
|
|
from connpy.core import nodes
|
|
|
|
nodes_dict = {
|
|
"r1": {"host": "10.0.0.1", "user": "admin", "password": ""},
|
|
}
|
|
mynodes = nodes(nodes_dict)
|
|
|
|
captured_vars = {}
|
|
|
|
def mock_run(commands, vars=None, **kwargs):
|
|
captured_vars.update(vars or {})
|
|
mynodes.r1.output = "ok"
|
|
mynodes.r1.status = 0
|
|
|
|
mynodes.r1.run = mock_run
|
|
|
|
variables = {
|
|
"__global__": {"mask": "255.255.255.0"},
|
|
"r1": {"ip": "10.0.0.1"}
|
|
}
|
|
mynodes.run(["show ip"], vars=variables)
|
|
assert captured_vars.get("mask") == "255.255.255.0"
|
|
assert captured_vars.get("ip") == "10.0.0.1"
|
|
|
|
def test_nodes_on_complete_callback(self):
|
|
"""on_complete callback fires per node."""
|
|
from connpy.core import nodes
|
|
|
|
nodes_dict = {
|
|
"r1": {"host": "10.0.0.1", "user": "admin", "password": ""},
|
|
}
|
|
mynodes = nodes(nodes_dict)
|
|
|
|
completed = []
|
|
|
|
def mock_run(commands, **kwargs):
|
|
mynodes.r1.output = "done"
|
|
mynodes.r1.status = 0
|
|
|
|
mynodes.r1.run = mock_run
|
|
|
|
def on_done(unique, output, status):
|
|
completed.append(unique)
|
|
|
|
mynodes.run(["show version"], on_complete=on_done)
|
|
assert "r1" in completed</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
<h3>Methods</h3>
|
|
<dl>
|
|
<dt id="connpy.tests.test_core.TestNodes.test_nodes_init"><code class="name flex">
|
|
<span>def <span class="ident">test_nodes_init</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_nodes_init(self):
|
|
"""Creates list of node objects."""
|
|
from connpy.core import nodes
|
|
nodes_dict = {
|
|
"r1": {"host": "10.0.0.1", "user": "admin", "password": ""},
|
|
"r2": {"host": "10.0.0.2", "user": "admin", "password": ""}
|
|
}
|
|
mynodes = nodes(nodes_dict)
|
|
assert len(mynodes.nodelist) == 2
|
|
assert hasattr(mynodes, "r1")
|
|
assert hasattr(mynodes, "r2")</code></pre>
|
|
</details>
|
|
<div class="desc"><p>Creates list of node objects.</p></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestNodes.test_nodes_on_complete_callback"><code class="name flex">
|
|
<span>def <span class="ident">test_nodes_on_complete_callback</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_nodes_on_complete_callback(self):
|
|
"""on_complete callback fires per node."""
|
|
from connpy.core import nodes
|
|
|
|
nodes_dict = {
|
|
"r1": {"host": "10.0.0.1", "user": "admin", "password": ""},
|
|
}
|
|
mynodes = nodes(nodes_dict)
|
|
|
|
completed = []
|
|
|
|
def mock_run(commands, **kwargs):
|
|
mynodes.r1.output = "done"
|
|
mynodes.r1.status = 0
|
|
|
|
mynodes.r1.run = mock_run
|
|
|
|
def on_done(unique, output, status):
|
|
completed.append(unique)
|
|
|
|
mynodes.run(["show version"], on_complete=on_done)
|
|
assert "r1" in completed</code></pre>
|
|
</details>
|
|
<div class="desc"><p>on_complete callback fires per node.</p></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestNodes.test_nodes_run_parallel"><code class="name flex">
|
|
<span>def <span class="ident">test_nodes_run_parallel</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_nodes_run_parallel(self):
|
|
"""run() executes on all nodes and returns dict."""
|
|
from connpy.core import nodes
|
|
|
|
nodes_dict = {
|
|
"r1": {"host": "10.0.0.1", "user": "admin", "password": ""},
|
|
"r2": {"host": "10.0.0.2", "user": "admin", "password": ""}
|
|
}
|
|
mynodes = nodes(nodes_dict)
|
|
|
|
# Mock run on each node — must set output AND status on the node
|
|
for n in mynodes.nodelist:
|
|
original_node = n # capture by value
|
|
def make_mock(node_ref):
|
|
def mock_run(commands, **kwargs):
|
|
node_ref.output = f"output from {node_ref.unique}"
|
|
node_ref.status = 0
|
|
return mock_run
|
|
n.run = make_mock(n)
|
|
|
|
result = mynodes.run(["show version"])
|
|
assert "r1" in result
|
|
assert "r2" in result</code></pre>
|
|
</details>
|
|
<div class="desc"><p>run() executes on all nodes and returns dict.</p></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestNodes.test_nodes_run_with_vars"><code class="name flex">
|
|
<span>def <span class="ident">test_nodes_run_with_vars</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_nodes_run_with_vars(self):
|
|
"""Variables per node and __global__ work."""
|
|
from connpy.core import nodes
|
|
|
|
nodes_dict = {
|
|
"r1": {"host": "10.0.0.1", "user": "admin", "password": ""},
|
|
}
|
|
mynodes = nodes(nodes_dict)
|
|
|
|
captured_vars = {}
|
|
|
|
def mock_run(commands, vars=None, **kwargs):
|
|
captured_vars.update(vars or {})
|
|
mynodes.r1.output = "ok"
|
|
mynodes.r1.status = 0
|
|
|
|
mynodes.r1.run = mock_run
|
|
|
|
variables = {
|
|
"__global__": {"mask": "255.255.255.0"},
|
|
"r1": {"ip": "10.0.0.1"}
|
|
}
|
|
mynodes.run(["show ip"], vars=variables)
|
|
assert captured_vars.get("mask") == "255.255.255.0"
|
|
assert captured_vars.get("ip") == "10.0.0.1"</code></pre>
|
|
</details>
|
|
<div class="desc"><p>Variables per node and <strong>global</strong> work.</p></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestNodes.test_nodes_splitlist"><code class="name flex">
|
|
<span>def <span class="ident">test_nodes_splitlist</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_nodes_splitlist(self):
|
|
"""_splitlist divides list correctly."""
|
|
from connpy.core import nodes
|
|
mynodes = nodes({"r1": {"host": "1.1.1.1", "user": "", "password": ""}})
|
|
chunks = list(mynodes._splitlist([1, 2, 3, 4, 5], 2))
|
|
assert chunks == [[1, 2], [3, 4], [5]]</code></pre>
|
|
</details>
|
|
<div class="desc"><p>_splitlist divides list correctly.</p></div>
|
|
</dd>
|
|
</dl>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestPasswordDecryption"><code class="flex name class">
|
|
<span>class <span class="ident">TestPasswordDecryption</span></span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">class TestPasswordDecryption:
|
|
def test_passtx_plaintext(self, config):
|
|
"""Plaintext passwords pass through unchanged."""
|
|
from connpy.core import node
|
|
n = node("test", "10.0.0.1", password="plainpass", config=config)
|
|
result = n._passtx(["plainpass"])
|
|
assert result == ["plainpass"]
|
|
|
|
def test_passtx_encrypted(self, config):
|
|
"""Encrypted passwords get decrypted."""
|
|
from connpy.core import node
|
|
encrypted = config.encrypt("mysecret")
|
|
n = node("test", "10.0.0.1", password=encrypted, config=config)
|
|
result = n._passtx([encrypted])
|
|
assert result == ["mysecret"]
|
|
|
|
def test_passtx_missing_key_raises(self):
|
|
"""Missing key file raises ValueError."""
|
|
from connpy.core import node
|
|
n = node("test", "10.0.0.1", password="pass")
|
|
# A password formatted as encrypted but no valid key
|
|
with pytest.raises((ValueError, Exception)):
|
|
n._passtx(["""b'corrupted_encrypted_data'"""], keyfile="/nonexistent")</code></pre>
|
|
</details>
|
|
<div class="desc"></div>
|
|
<h3>Methods</h3>
|
|
<dl>
|
|
<dt id="connpy.tests.test_core.TestPasswordDecryption.test_passtx_encrypted"><code class="name flex">
|
|
<span>def <span class="ident">test_passtx_encrypted</span></span>(<span>self, config)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_passtx_encrypted(self, config):
|
|
"""Encrypted passwords get decrypted."""
|
|
from connpy.core import node
|
|
encrypted = config.encrypt("mysecret")
|
|
n = node("test", "10.0.0.1", password=encrypted, config=config)
|
|
result = n._passtx([encrypted])
|
|
assert result == ["mysecret"]</code></pre>
|
|
</details>
|
|
<div class="desc"><p>Encrypted passwords get decrypted.</p></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestPasswordDecryption.test_passtx_missing_key_raises"><code class="name flex">
|
|
<span>def <span class="ident">test_passtx_missing_key_raises</span></span>(<span>self)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_passtx_missing_key_raises(self):
|
|
"""Missing key file raises ValueError."""
|
|
from connpy.core import node
|
|
n = node("test", "10.0.0.1", password="pass")
|
|
# A password formatted as encrypted but no valid key
|
|
with pytest.raises((ValueError, Exception)):
|
|
n._passtx(["""b'corrupted_encrypted_data'"""], keyfile="/nonexistent")</code></pre>
|
|
</details>
|
|
<div class="desc"><p>Missing key file raises ValueError.</p></div>
|
|
</dd>
|
|
<dt id="connpy.tests.test_core.TestPasswordDecryption.test_passtx_plaintext"><code class="name flex">
|
|
<span>def <span class="ident">test_passtx_plaintext</span></span>(<span>self, config)</span>
|
|
</code></dt>
|
|
<dd>
|
|
<details class="source">
|
|
<summary>
|
|
<span>Expand source code</span>
|
|
</summary>
|
|
<pre><code class="python">def test_passtx_plaintext(self, config):
|
|
"""Plaintext passwords pass through unchanged."""
|
|
from connpy.core import node
|
|
n = node("test", "10.0.0.1", password="plainpass", config=config)
|
|
result = n._passtx(["plainpass"])
|
|
assert result == ["plainpass"]</code></pre>
|
|
</details>
|
|
<div class="desc"><p>Plaintext passwords pass through unchanged.</p></div>
|
|
</dd>
|
|
</dl>
|
|
</dd>
|
|
</dl>
|
|
</section>
|
|
</article>
|
|
<nav id="sidebar">
|
|
<div class="toc">
|
|
<ul></ul>
|
|
</div>
|
|
<ul id="index">
|
|
<li><h3>Super-module</h3>
|
|
<ul>
|
|
<li><code><a title="connpy.tests" href="index.html">connpy.tests</a></code></li>
|
|
</ul>
|
|
</li>
|
|
<li><h3><a href="#header-classes">Classes</a></h3>
|
|
<ul>
|
|
<li>
|
|
<h4><code><a title="connpy.tests.test_core.TestCommandGeneration" href="#connpy.tests.test_core.TestCommandGeneration">TestCommandGeneration</a></code></h4>
|
|
<ul class="">
|
|
<li><code><a title="connpy.tests.test_core.TestCommandGeneration.test_docker_cmd" href="#connpy.tests.test_core.TestCommandGeneration.test_docker_cmd">test_docker_cmd</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestCommandGeneration.test_invalid_protocol_raises" href="#connpy.tests.test_core.TestCommandGeneration.test_invalid_protocol_raises">test_invalid_protocol_raises</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestCommandGeneration.test_kubectl_cmd" href="#connpy.tests.test_core.TestCommandGeneration.test_kubectl_cmd">test_kubectl_cmd</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestCommandGeneration.test_kubectl_cmd_default_command" href="#connpy.tests.test_core.TestCommandGeneration.test_kubectl_cmd_default_command">test_kubectl_cmd_default_command</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestCommandGeneration.test_sftp_cmd_port" href="#connpy.tests.test_core.TestCommandGeneration.test_sftp_cmd_port">test_sftp_cmd_port</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestCommandGeneration.test_ssh_cmd_basic" href="#connpy.tests.test_core.TestCommandGeneration.test_ssh_cmd_basic">test_ssh_cmd_basic</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestCommandGeneration.test_ssh_cmd_no_user" href="#connpy.tests.test_core.TestCommandGeneration.test_ssh_cmd_no_user">test_ssh_cmd_no_user</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestCommandGeneration.test_ssh_cmd_options" href="#connpy.tests.test_core.TestCommandGeneration.test_ssh_cmd_options">test_ssh_cmd_options</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestCommandGeneration.test_ssh_cmd_port" href="#connpy.tests.test_core.TestCommandGeneration.test_ssh_cmd_port">test_ssh_cmd_port</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestCommandGeneration.test_telnet_cmd" href="#connpy.tests.test_core.TestCommandGeneration.test_telnet_cmd">test_telnet_cmd</a></code></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<h4><code><a title="connpy.tests.test_core.TestLogHandling" href="#connpy.tests.test_core.TestLogHandling">TestLogHandling</a></code></h4>
|
|
<ul class="">
|
|
<li><code><a title="connpy.tests.test_core.TestLogHandling.test_logclean_removes_ansi" href="#connpy.tests.test_core.TestLogHandling.test_logclean_removes_ansi">test_logclean_removes_ansi</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestLogHandling.test_logclean_removes_backspaces" href="#connpy.tests.test_core.TestLogHandling.test_logclean_removes_backspaces">test_logclean_removes_backspaces</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestLogHandling.test_logfile_date_substitution" href="#connpy.tests.test_core.TestLogHandling.test_logfile_date_substitution">test_logfile_date_substitution</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestLogHandling.test_logfile_variable_substitution" href="#connpy.tests.test_core.TestLogHandling.test_logfile_variable_substitution">test_logfile_variable_substitution</a></code></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<h4><code><a title="connpy.tests.test_core.TestNodeInit" href="#connpy.tests.test_core.TestNodeInit">TestNodeInit</a></code></h4>
|
|
<ul class="">
|
|
<li><code><a title="connpy.tests.test_core.TestNodeInit.test_basic_init" href="#connpy.tests.test_core.TestNodeInit.test_basic_init">test_basic_init</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestNodeInit.test_default_protocol" href="#connpy.tests.test_core.TestNodeInit.test_default_protocol">test_default_protocol</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestNodeInit.test_node_tags" href="#connpy.tests.test_core.TestNodeInit.test_node_tags">test_node_tags</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestNodeInit.test_node_with_profile" href="#connpy.tests.test_core.TestNodeInit.test_node_with_profile">test_node_with_profile</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestNodeInit.test_password_as_list_of_profiles" href="#connpy.tests.test_core.TestNodeInit.test_password_as_list_of_profiles">test_password_as_list_of_profiles</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestNodeInit.test_password_plain_string" href="#connpy.tests.test_core.TestNodeInit.test_password_plain_string">test_password_plain_string</a></code></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<h4><code><a title="connpy.tests.test_core.TestNodeRun" href="#connpy.tests.test_core.TestNodeRun">TestNodeRun</a></code></h4>
|
|
<ul class="">
|
|
<li><code><a title="connpy.tests.test_core.TestNodeRun.test_run_returns_output" href="#connpy.tests.test_core.TestNodeRun.test_run_returns_output">test_run_returns_output</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestNodeRun.test_run_saves_to_folder" href="#connpy.tests.test_core.TestNodeRun.test_run_saves_to_folder">test_run_saves_to_folder</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestNodeRun.test_run_status_1_on_failure" href="#connpy.tests.test_core.TestNodeRun.test_run_status_1_on_failure">test_run_status_1_on_failure</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestNodeRun.test_run_with_variables" href="#connpy.tests.test_core.TestNodeRun.test_run_with_variables">test_run_with_variables</a></code></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<h4><code><a title="connpy.tests.test_core.TestNodeTest" href="#connpy.tests.test_core.TestNodeTest">TestNodeTest</a></code></h4>
|
|
<ul class="">
|
|
<li><code><a title="connpy.tests.test_core.TestNodeTest.test_test_expected_not_found" href="#connpy.tests.test_core.TestNodeTest.test_test_expected_not_found">test_test_expected_not_found</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestNodeTest.test_test_returns_dict" href="#connpy.tests.test_core.TestNodeTest.test_test_returns_dict">test_test_returns_dict</a></code></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<h4><code><a title="connpy.tests.test_core.TestNodes" href="#connpy.tests.test_core.TestNodes">TestNodes</a></code></h4>
|
|
<ul class="">
|
|
<li><code><a title="connpy.tests.test_core.TestNodes.test_nodes_init" href="#connpy.tests.test_core.TestNodes.test_nodes_init">test_nodes_init</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestNodes.test_nodes_on_complete_callback" href="#connpy.tests.test_core.TestNodes.test_nodes_on_complete_callback">test_nodes_on_complete_callback</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestNodes.test_nodes_run_parallel" href="#connpy.tests.test_core.TestNodes.test_nodes_run_parallel">test_nodes_run_parallel</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestNodes.test_nodes_run_with_vars" href="#connpy.tests.test_core.TestNodes.test_nodes_run_with_vars">test_nodes_run_with_vars</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestNodes.test_nodes_splitlist" href="#connpy.tests.test_core.TestNodes.test_nodes_splitlist">test_nodes_splitlist</a></code></li>
|
|
</ul>
|
|
</li>
|
|
<li>
|
|
<h4><code><a title="connpy.tests.test_core.TestPasswordDecryption" href="#connpy.tests.test_core.TestPasswordDecryption">TestPasswordDecryption</a></code></h4>
|
|
<ul class="">
|
|
<li><code><a title="connpy.tests.test_core.TestPasswordDecryption.test_passtx_encrypted" href="#connpy.tests.test_core.TestPasswordDecryption.test_passtx_encrypted">test_passtx_encrypted</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestPasswordDecryption.test_passtx_missing_key_raises" href="#connpy.tests.test_core.TestPasswordDecryption.test_passtx_missing_key_raises">test_passtx_missing_key_raises</a></code></li>
|
|
<li><code><a title="connpy.tests.test_core.TestPasswordDecryption.test_passtx_plaintext" href="#connpy.tests.test_core.TestPasswordDecryption.test_passtx_plaintext">test_passtx_plaintext</a></code></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</nav>
|
|
</main>
|
|
<footer id="footer">
|
|
<p>Generated by <a href="https://pdoc3.github.io/pdoc" title="pdoc: Python API documentation generator"><cite>pdoc</cite> 0.11.5</a>.</p>
|
|
</footer>
|
|
</body>
|
|
</html>
|