Exemple #1
0
def test_output_to_new_file(_file_requests, _file_requests_config, tmp_path):

    target_file = tmp_path / "ssh_test.conf"

    make_ssh_config("-o", str(target_file))

    assert target_file.read_text() == _file_requests_config
Exemple #2
0
def test_output_to_existing_file(_file_requests, _file_requests_config,
                                 tmp_path):

    target_file = tmp_path / "ssh_test.conf"
    target_file.write_text("foo\nbar\n")

    make_ssh_config("-o", str(target_file))

    assert target_file.read_text() == f"foo\nbar\n{_file_requests_config}"
Exemple #3
0
def test_default_config_to_stdout(ec2_stub, ec2_region_name, capsys,
                                  monkeypatch):

    monkeypatch.setenv("AWS_PROFILE", "testprofile")
    monkeypatch.setenv("AWS_REGION", ec2_region_name)

    ec2_stub.add_response("describe_instances",
                          expected_params={
                              "Filters": [{
                                  "Name": "instance-state-name",
                                  "Values": ["running"]
                              }]
                          },
                          service_response={
                              "Reservations": [{
                                  "Instances": [{
                                      "InstanceId": "i-1",
                                      "PrivateIpAddress": "192.168.0.1",
                                      "PublicIpAddress": "42.42.42.42",
                                      "LaunchTime":
                                      "2019-01-01 09:00:00+00:00",
                                      "Tags": []
                                  }, {
                                      "InstanceId":
                                      "i-2",
                                      "PrivateIpAddress":
                                      "192.168.0.2",
                                      "LaunchTime":
                                      "2018-01-01 09:00:00+00:00",
                                      "Tags": [{
                                          "Key": "Name",
                                          "Value": "clusterfoo"
                                      }]
                                  }, {
                                      "InstanceId":
                                      "i-3",
                                      "PrivateIpAddress":
                                      "192.168.0.3",
                                      "LaunchTime":
                                      "2017-01-01 09:00:00+00:00",
                                      "Tags": [{
                                          "Key": "Name",
                                          "Value": "clusterfoo"
                                      }]
                                  }]
                              }]
                          })

    make_ssh_config()

    out, err = capsys.readouterr()

    assert err == ""
    assert out == f"""\
Exemple #4
0
def test_help_to_stdout(capsys):

    with pytest.raises(SystemExit) as wrapped_exception:
        make_ssh_config("-h")

    assert wrapped_exception.type == SystemExit
    assert wrapped_exception.value.code == 0

    out, err = capsys.readouterr()

    assert err == ""
    assert "--help" in out
Exemple #5
0
def test_version_to_stdout(capsys):

    version_pattern = re.compile("^[0-9]+.[0-9]+.[0-9]+$")

    with pytest.raises(SystemExit) as wrapped_exception:
        make_ssh_config("-v")

    assert wrapped_exception.type == SystemExit
    assert wrapped_exception.value.code == 0

    out, err = capsys.readouterr()

    assert err == ""
    assert version_pattern.match(out)
Exemple #6
0
def test_output_to_existing_file_with_section(_file_requests,
                                              _file_requests_config, tmp_path):

    target_file = tmp_path / "ssh_test.conf"
    target_file.write_text("""\
foo

# BEGIN [testprofile]

Some old stuff, that should be replaced.

# END [testprofile]

bar
""")

    make_ssh_config("-o", str(target_file))

    assert target_file.read_text() == f"""\
Exemple #7
0
def test_no_instances_to_stdout(ec2_stub, ec2_region_name, capsys,
                                monkeypatch):

    monkeypatch.setenv("AWS_PROFILE", "testprofile")
    monkeypatch.setenv("AWS_REGION", ec2_region_name)

    ec2_stub.add_response("describe_instances",
                          expected_params={
                              "Filters": [{
                                  "Name": "instance-state-name",
                                  "Values": ["running"]
                              }]
                          },
                          service_response={"Reservations": []})

    make_ssh_config()

    out, err = capsys.readouterr()

    assert err == ""
    assert out == f"""\
Exemple #8
0
def test_extended_config_to_stdout(ec2_stub, ec2_region_name, capsys):
    ec2_stub.add_response("describe_instances",
                          expected_params={
                              "Filters": [{
                                  "Name": "instance-state-name",
                                  "Values": ["running"]
                              }, {
                                  'Name': 'tag:Name',
                                  'Values': ['*node*']
                              }]
                          },
                          service_response={
                              "Reservations": [{
                                  "Instances": [
                                      {
                                          "InstanceId":
                                          "i-1",
                                          "PrivateIpAddress":
                                          "192.168.0.1",
                                          "PublicIpAddress":
                                          "11.11.11.11",
                                          "LaunchTime":
                                          "2018-01-01 09:00:00+00:00",
                                          "Tags": [{
                                              "Key": "Name",
                                              "Value": "publicnode"
                                          }]
                                      },
                                      {
                                          "InstanceId":
                                          "i-2",
                                          "PrivateIpAddress":
                                          "192.168.0.2",
                                          "PublicIpAddress":
                                          "22.22.22.22",
                                          "LaunchTime":
                                          "2019-01-01 09:00:00+00:00",
                                          "Tags": [{
                                              "Key": "Name",
                                              "Value": "publicnode"
                                          }]
                                      },
                                      {
                                          "InstanceId":
                                          "i-3",
                                          "PrivateIpAddress":
                                          "192.168.0.3",
                                          "LaunchTime":
                                          "2017-01-01 09:00:00+00:00",
                                          "Tags": [{
                                              "Key": "Name",
                                              "Value": "privatenode"
                                          }]
                                      },
                                  ]
                              }]
                          })

    make_ssh_config("--profile", "testprofile", "--region", ec2_region_name,
                    "--ec2-filter-name", "*node*", "--address", "public",
                    "--config-key", "test_key", "--region-prefix",
                    "--name-prefix", "test-prefix-", "--port", "22", "--user",
                    "tester", "--identity-file", "~/.ssh/id_rsa.test",
                    "--server-alive-interval", "100", "--no-identities-only",
                    "--skip-strict-host-checking", "--proxy-command",
                    "ssh ec2-user@jumphost nc %h %p")

    out, err = capsys.readouterr()

    assert err == ""
    assert out == f"""\