This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
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 #28 from avb1989/feature/tunnel
#27 Implementation for tunnel feature
- Loading branch information
Showing
2 changed files
with
158 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
|
||
from click.testing import CliRunner | ||
from unittest.mock import MagicMock | ||
import yaml | ||
import zign.api | ||
from piu.cli import cli | ||
|
||
|
@@ -23,7 +22,14 @@ def test_success(monkeypatch): | |
runner = CliRunner() | ||
|
||
with runner.isolated_filesystem(): | ||
result = runner.invoke(cli, ['[email protected]', '--lifetime=15', '--even-url=https://localhost/', '--odd-host=odd.example.org', '--password=foobar', 'my reason'], catch_exceptions=False) | ||
result = runner.invoke(cli, | ||
['[email protected]', | ||
'--lifetime=15', | ||
'--even-url=https://localhost/', | ||
'--odd-host=odd.example.org', | ||
'--password=foobar', | ||
'my reason'], | ||
catch_exceptions=False) | ||
|
||
assert response.text in result.output | ||
|
||
|
@@ -36,7 +42,14 @@ def test_bad_request(monkeypatch): | |
runner = CliRunner() | ||
|
||
with runner.isolated_filesystem(): | ||
result = runner.invoke(cli, ['req', '--lifetime=15', '--even-url=https://localhost/', '--password=foobar', 'myuser@odd-host', 'my reason'], catch_exceptions=False) | ||
result = runner.invoke(cli, | ||
['req', | ||
'--lifetime=15', | ||
'--even-url=https://localhost/', | ||
'--password=foobar', | ||
'myuser@odd-host', | ||
'my reason'], | ||
catch_exceptions=False) | ||
|
||
assert response.text in result.output | ||
assert 'Server returned status 400:' in result.output | ||
|
@@ -50,7 +63,13 @@ def test_auth_failure(monkeypatch): | |
runner = CliRunner() | ||
|
||
with runner.isolated_filesystem(): | ||
result = runner.invoke(cli, ['r', '--even-url=https://localhost/', '--password=invalid', 'myuser@odd-host', 'my reason'], catch_exceptions=False) | ||
result = runner.invoke(cli, | ||
['r', | ||
'--even-url=https://localhost/', | ||
'--password=invalid', | ||
'myuser@odd-host', | ||
'my reason'], | ||
catch_exceptions=False) | ||
|
||
assert response.text in result.output | ||
assert 'Server returned status 403:' in result.output | ||
|
@@ -67,11 +86,13 @@ def test_dialog(monkeypatch): | |
runner = CliRunner() | ||
|
||
with runner.isolated_filesystem(): | ||
result = runner.invoke(cli, ['--config-file=config.yaml', 'req', '[email protected]', 'my reason'], catch_exceptions=False, input='even\nodd\npassword\n\n') | ||
result = runner.invoke(cli, ['--config-file=config.yaml', 'req', '[email protected]', | ||
'my reason'], catch_exceptions=False, input='even\nodd\npassword\n\n') | ||
|
||
assert result.exit_code == 0 | ||
assert response.text in result.output | ||
|
||
|
||
def test_oauth_failure(monkeypatch): | ||
response = MagicMock(status_code=200, text='**MAGIC-SUCCESS**') | ||
monkeypatch.setattr('zign.api.get_named_token', MagicMock(side_effect=zign.api.ServerError('**MAGIC-FAIL**'))) | ||
|
@@ -83,11 +104,13 @@ def test_oauth_failure(monkeypatch): | |
runner = CliRunner() | ||
|
||
with runner.isolated_filesystem(): | ||
result = runner.invoke(cli, ['--config-file=config.yaml', 'req', '[email protected]', 'my reason'], catch_exceptions=False, input='even\nodd\npassword\n\n') | ||
result = runner.invoke(cli, ['--config-file=config.yaml', 'req', '[email protected]', | ||
'my reason'], catch_exceptions=False, input='even\nodd\npassword\n\n') | ||
|
||
assert result.exit_code == 500 | ||
assert 'Server error: **MAGIC-FAIL**' in result.output | ||
|
||
|
||
def test_login_arg_user(monkeypatch, tmpdir): | ||
arg_user = 'arg_user' | ||
zign_user = 'zign_user' | ||
|
@@ -98,7 +121,7 @@ def test_login_arg_user(monkeypatch, tmpdir): | |
runner = CliRunner() | ||
|
||
def mock__request_access(even_url, cacert, username, first_host, reason, | ||
remote_host, lifetime, user, password, clip): | ||
remote_host, lifetime, user, password, clip): | ||
assert arg_user == username | ||
|
||
monkeypatch.setattr('zign.api.get_config', lambda: {'user': zign_user}) | ||
|
@@ -107,8 +130,7 @@ def mock__request_access(even_url, cacert, username, first_host, reason, | |
monkeypatch.setattr('requests.get', lambda x, timeout: response) | ||
|
||
with runner.isolated_filesystem(): | ||
result = runner.invoke(cli, ['request-access', '-U', arg_user], | ||
catch_exceptions=False) | ||
runner.invoke(cli, ['request-access', '-U', arg_user], catch_exceptions=False) | ||
|
||
|
||
def test_login_zign_user(monkeypatch, tmpdir): | ||
|
@@ -120,7 +142,7 @@ def test_login_zign_user(monkeypatch, tmpdir): | |
runner = CliRunner() | ||
|
||
def mock__request_access(even_url, cacert, username, first_host, reason, | ||
remote_host, lifetime, user, password, clip): | ||
remote_host, lifetime, user, password, clip): | ||
assert zign_user == username | ||
|
||
monkeypatch.setattr('zign.api.get_config', lambda: {'user': zign_user}) | ||
|
@@ -129,7 +151,7 @@ def mock__request_access(even_url, cacert, username, first_host, reason, | |
monkeypatch.setattr('requests.get', lambda x, timeout: response) | ||
|
||
with runner.isolated_filesystem(): | ||
result = runner.invoke(cli, ['request-access'], catch_exceptions=False) | ||
runner.invoke(cli, ['request-access'], catch_exceptions=False) | ||
|
||
|
||
def test_login_env_user(monkeypatch, tmpdir): | ||
|
@@ -140,7 +162,7 @@ def test_login_env_user(monkeypatch, tmpdir): | |
runner = CliRunner() | ||
|
||
def mock__request_access(even_url, cacert, username, first_host, reason, | ||
remote_host, lifetime, user, password, clip): | ||
remote_host, lifetime, user, password, clip): | ||
assert env_user == username | ||
|
||
monkeypatch.setattr('zign.api.get_config', lambda: {'user': ''}) | ||
|
@@ -149,26 +171,41 @@ def mock__request_access(even_url, cacert, username, first_host, reason, | |
monkeypatch.setattr('requests.get', lambda x, timeout: response) | ||
|
||
with runner.isolated_filesystem(): | ||
result = runner.invoke(cli, ['request-access'], catch_exceptions=False) | ||
runner.invoke(cli, ['request-access'], catch_exceptions=False) | ||
|
||
|
||
def test_interactive_success(monkeypatch): | ||
ec2 = MagicMock() | ||
request_access = MagicMock() | ||
|
||
response = [] | ||
response.append(MagicMock(**{'instance_id': 'i-123456', 'private_ip_address': '172.31.10.10', 'tags': [{'Key': 'Name', 'Value': 'stack1-0o1o0'}, {'Key': 'StackVersion', 'Value': '0o1o0'}, {'Key': 'StackName', 'Value': 'stack1'}]})) | ||
response.append(MagicMock(**{'instance_id': 'i-789012', 'private_ip_address': '172.31.10.20', 'tags': [{'Key': 'Name', 'Value': 'stack2-0o1o0'}, {'Key': 'StackVersion', 'Value': '0o2o0'}, {'Key': 'StackName', 'Value': 'stack2'}]})) | ||
response.append(MagicMock(**{'instance_id': 'i-123456', | ||
'private_ip_address': '172.31.10.10', | ||
'tags': [{'Key': 'Name', 'Value': 'stack1-0o1o0'}, | ||
{'Key': 'StackVersion', 'Value': '0o1o0'}, | ||
{'Key': 'StackName', 'Value': 'stack1'}] | ||
})) | ||
response.append(MagicMock(**{'instance_id': 'i-789012', | ||
'private_ip_address': '172.31.10.20', | ||
'tags': [{'Key': 'Name', 'Value': 'stack2-0o1o0'}, | ||
{'Key': 'StackVersion', 'Value': '0o2o0'}, | ||
{'Key': 'StackName', 'Value': 'stack2'}] | ||
})) | ||
ec2.instances.filter = MagicMock(return_value=response) | ||
boto3 = MagicMock() | ||
monkeypatch.setattr('boto3.resource', MagicMock(return_value=ec2)) | ||
monkeypatch.setattr('piu.cli._request_access', MagicMock(side_effect=request_access)) | ||
|
||
runner = CliRunner() | ||
input_stream = '\n'.join(['eu-west-1', '1', 'Troubleshooting']) + '\n' | ||
|
||
with runner.isolated_filesystem(): | ||
result = runner.invoke(cli, ['request-access', '--interactive', '--even-url=https://localhost/', '--odd-host=odd.example.org'], input=input_stream, catch_exceptions=False) | ||
runner.invoke(cli, | ||
['request-access', | ||
'--interactive', | ||
'--even-url=https://localhost/', | ||
'--odd-host=odd.example.org'], | ||
input=input_stream, | ||
catch_exceptions=False) | ||
|
||
assert request_access.called | ||
|
||
|
@@ -178,37 +215,109 @@ def test_interactive_single_instance_success(monkeypatch): | |
request_access = MagicMock() | ||
|
||
response = [] | ||
response.append(MagicMock(**{'instance_id': 'i-123456', 'private_ip_address': '172.31.10.10', 'tags': [{'Key': 'Name', 'Value': 'stack1-0o1o0'}, {'Key': 'StackVersion', 'Value': '0o1o0'}, {'Key': 'StackName', 'Value': 'stack1'}]})) | ||
response.append(MagicMock(**{'instance_id': 'i-123456', | ||
'private_ip_address': '172.31.10.10', | ||
'tags': [{'Key': 'Name', 'Value': 'stack1-0o1o0'}, | ||
{'Key': 'StackVersion', 'Value': '0o1o0'}, | ||
{'Key': 'StackName', 'Value': 'stack1'}] | ||
})) | ||
ec2.instances.filter = MagicMock(return_value=response) | ||
boto3 = MagicMock() | ||
monkeypatch.setattr('boto3.resource', MagicMock(return_value=ec2)) | ||
monkeypatch.setattr('piu.cli._request_access', MagicMock(side_effect=request_access)) | ||
|
||
runner = CliRunner() | ||
input_stream = '\n'.join(['eu-west-1', '', 'Troubleshooting']) + '\n' | ||
|
||
with runner.isolated_filesystem(): | ||
result = runner.invoke(cli, ['request-access', '--interactive', '--even-url=https://localhost/', '--odd-host=odd.example.org'], input=input_stream, catch_exceptions=False) | ||
runner.invoke(cli, | ||
['request-access', | ||
'--interactive', | ||
'--even-url=https://localhost/', | ||
'--odd-host=odd.example.org'], | ||
input=input_stream, | ||
catch_exceptions=False) | ||
|
||
assert request_access.called | ||
|
||
|
||
|
||
def test_interactive_no_instances_failure(monkeypatch): | ||
ec2 = MagicMock() | ||
request_access = MagicMock() | ||
|
||
response = [] | ||
ec2.instances.filter = MagicMock(return_value=response) | ||
boto3 = MagicMock() | ||
monkeypatch.setattr('boto3.resource', MagicMock(return_value=ec2)) | ||
monkeypatch.setattr('piu.cli._request_access', MagicMock(side_effect=request_access)) | ||
|
||
runner = CliRunner() | ||
input_stream = '\neu-west-1\n' | ||
|
||
with runner.isolated_filesystem(): | ||
result = runner.invoke(cli, ['request-access', '--interactive', '--even-url=https://localhost/', '--odd-host=odd.example.org'], input=input_stream, catch_exceptions=False) | ||
result = runner.invoke(cli, | ||
['request-access', | ||
'--interactive', | ||
'--even-url=https://localhost/', | ||
'--odd-host=odd.example.org'], | ||
input=input_stream, | ||
catch_exceptions=False) | ||
|
||
assert result.exception | ||
assert 'Error: No running instances were found.' in result.output | ||
|
||
|
||
def test_tunnel_either_connect_or_tunnel(): | ||
input_stream = '\neu-central-1\n' | ||
|
||
runner = CliRunner() | ||
with runner.isolated_filesystem(): | ||
result = runner.invoke(cli, | ||
['request-access', | ||
'--connect', | ||
'--tunnel', | ||
'[email protected]', | ||
'Testing'], | ||
input=input_stream, | ||
catch_exceptions=False) | ||
assert result.exception | ||
assert 'Cannot specify both "connect" and "tunnel"' | ||
|
||
|
||
def test_tunnel_should_have_correct_format(): | ||
|
||
runner = CliRunner() | ||
with runner.isolated_filesystem(): | ||
result = runner.invoke(cli, ['request-access', '--tunnel', 'a2345:234a', | ||
'[email protected]', 'Testing'], catch_exceptions=False) | ||
assert result.exception | ||
|
||
with runner.isolated_filesystem(): | ||
result = runner.invoke(cli, ['request-access', '--tunnel', '23434', | ||
'[email protected]', 'Testing'], catch_exceptions=False) | ||
assert result.exception | ||
|
||
with runner.isolated_filesystem(): | ||
result = runner.invoke(cli, ['request-access', '--tunnel', 'a2345:2343', | ||
'[email protected]', 'Testing'], catch_exceptions=False) | ||
assert result.exception | ||
|
||
|
||
def test_tunnel_success(monkeypatch): | ||
|
||
response = MagicMock(status_code=200, text='**MAGIC-SUCCESS**') | ||
|
||
monkeypatch.setattr('zign.api.get_named_token', MagicMock(return_value={'access_token': '123'})) | ||
monkeypatch.setattr('requests.post', MagicMock(return_value=response)) | ||
monkeypatch.setattr('subprocess.call', MagicMock()) | ||
|
||
runner = CliRunner() | ||
with runner.isolated_filesystem(): | ||
result = runner.invoke(cli, ['request-access', | ||
'--tunnel', '2380:2379', | ||
'--even-url=https://localhost/', | ||
'--odd-host=odd.example.org', | ||
'[email protected]', | ||
'Testing'], | ||
catch_exceptions=False) | ||
|
||
assert response.text in result.output | ||
assert '-L 2380:somehost.example.org:2379' in result.output |