Skip to content

Commit

Permalink
Add --output option to pbench-generate-token
Browse files Browse the repository at this point in the history
This option makes it a bit easier to script the invocation of
`pbench-generate-token` where username and/or password prompts are
required (the prompt does not get mixed up into the token output).
  • Loading branch information
portante committed Feb 20, 2023
1 parent fca6338 commit e496ad7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/pbench/cli/agent/commands/generate_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def execute(self):
payload = {"message": response.text}

if response.ok and "auth_token" in payload:
click.echo(payload["auth_token"])
if not self.context.output:
click.echo(payload["auth_token"])
else:
with open(self.context.output, "w") as ofp:
ofp.write(f"{payload['auth_token']}\n")
return 0

click.echo(
Expand Down Expand Up @@ -70,11 +74,17 @@ def execute(self):
show_default=True,
help="number of seconds",
)
@click.option(
"--output",
required=False,
help="Output file to which to write the generated token",
)
@pass_cli_context
def main(context, username, password, token_duration):
def main(context, username, password, token_duration, output):
context.username = username
context.password = password
context.token_duration = token_duration
context.output = output

try:
rv = GenerateToken(context).execute()
Expand Down
53 changes: 53 additions & 0 deletions lib/pbench/test/unit/agent/task/test_generate_token.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from http import HTTPStatus
from pathlib import Path

from click.testing import CliRunner
import requests
Expand All @@ -11,6 +12,7 @@ class TestGenerateToken:

USER_SWITCH = "--username"
PSWD_SWITCH = "--password"
OUTPUT_SWITCH = "--output"
USER_PROMPT = "Username: "
PSWD_PROMPT = "Password: "
USER_TEXT = "test_user"
Expand All @@ -37,6 +39,15 @@ def add_badlogin_mock_response():
json={"message": "Bad login"},
)

@staticmethod
def add_notjson_mock_response():
responses.add(
responses.POST,
TestGenerateToken.URL + TestGenerateToken.ENDPOINT,
status=HTTPStatus.FORBIDDEN,
body="NOT Jason",
)

@staticmethod
def add_connectionerr_mock_response():
responses.add(
Expand Down Expand Up @@ -149,6 +160,24 @@ def test_bad_login():
assert not result.stdout
assert result.stderr == "Bad login\n"

@staticmethod
@responses.activate
def test_not_json():
TestGenerateToken.add_notjson_mock_response()
runner = CliRunner(mix_stderr=False)
result = runner.invoke(
generate_token.main,
args=[
TestGenerateToken.USER_SWITCH,
TestGenerateToken.USER_TEXT,
TestGenerateToken.PSWD_SWITCH,
TestGenerateToken.PSWD_TEXT,
],
)
assert result.exit_code == 1
assert not result.stdout
assert result.stderr == "NOT Jason\n"

@staticmethod
@responses.activate
def test_connection_failed():
Expand All @@ -169,3 +198,27 @@ def test_connection_failed():
assert -1 != str(result.stderr).find(
"Cannot connect to 'http://pbench.example.com/api/v1/login'"
)

@staticmethod
@responses.activate
def test_output_file(tmp_path):
output_d = tmp_path / "test_output_file"
output_d.mkdir()
output_file = output_d / "token.file"
TestGenerateToken.add_success_mock_response()
runner = CliRunner(mix_stderr=False)
result = runner.invoke(
generate_token.main,
args=[
TestGenerateToken.USER_SWITCH,
TestGenerateToken.USER_TEXT,
TestGenerateToken.PSWD_SWITCH,
TestGenerateToken.PSWD_TEXT,
TestGenerateToken.OUTPUT_SWITCH,
str(output_file),
],
)
assert result.exit_code == 0
assert result.stdout == ""
assert not result.stderr_bytes
assert Path(output_file).read_text() == f"{TestGenerateToken.TOKEN_TEXT}\n"

0 comments on commit e496ad7

Please sign in to comment.