Skip to content

Commit

Permalink
Added flag to skip sending a registration request and minor fixes (#243)
Browse files Browse the repository at this point in the history
Added flag to skip sending a registration request and minor fixes
  • Loading branch information
silverdrake11 committed Jul 25, 2024
1 parent d221e62 commit bea9394
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 8 deletions.
15 changes: 14 additions & 1 deletion landscape/client/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io
import os
import pwd
import re
import shlex
import sys
import textwrap
Expand Down Expand Up @@ -111,6 +112,7 @@ class LandscapeSetupConfiguration(BrokerConfiguration):
"silent",
"ok_no_register",
"import_from",
"skip_registration",
)

encoding = "utf-8"
Expand Down Expand Up @@ -247,6 +249,11 @@ def make_parser(self):
"registered else returns {}. Displays "
"registration info.".format(EXIT_NOT_REGISTERED),
)
parser.add_option(
"--skip-registration",
action="store_true",
help="Don't send a new registration request",
)
return parser


Expand Down Expand Up @@ -473,7 +480,9 @@ def query_landscape_edition(self):
self.landscape_domain = self.prompt_get_input(
"Landscape Domain: ",
True,
)
).strip("/")
self.landscape_domain = re.sub(r"^https?://", "",
self.landscape_domain)
self.config.ping_url = f"http://{self.landscape_domain}/ping"
self.config.url = f"https://{self.landscape_domain}/message-system"
else:
Expand Down Expand Up @@ -946,6 +955,10 @@ def main(args, print=print):

# Attempt to register the client.
reactor = LandscapeReactor()

if config.skip_registration:
return

if config.silent:
result = register(
config,
Expand Down
87 changes: 80 additions & 7 deletions landscape/client/tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from twisted.internet.defer import Deferred
from twisted.internet.defer import fail
from twisted.internet.defer import succeed
from twisted.python.compat import iteritems

from landscape.client.broker.registration import Identity
from landscape.client.broker.registration import RegistrationError
Expand Down Expand Up @@ -384,6 +383,56 @@ def test_prompt_landscape_edition(
)
self.assertEqual(self.config.account_name, "standalone")

@mock.patch(
"landscape.client.configuration.input",
return_value="http://landscape.hello.com",
)
@mock.patch(
"landscape.client.configuration.prompt_yes_no",
return_value=True,
)
@mock.patch("landscape.client.configuration.show_help")
def test_prompt_landscape_edition_strip_http(
self,
mock_help,
prompt_yes_no,
mock_input,
):
self.script.query_landscape_edition()
self.assertEqual(
self.config.ping_url,
"http://landscape.hello.com/ping",
)
self.assertEqual(
self.config.url,
"https://landscape.hello.com/message-system",
)

@mock.patch(
"landscape.client.configuration.input",
return_value="https://landscape.hello.com",
)
@mock.patch(
"landscape.client.configuration.prompt_yes_no",
return_value=True,
)
@mock.patch("landscape.client.configuration.show_help")
def test_prompt_landscape_edition_strip_https(
self,
mock_help,
prompt_yes_no,
mock_input,
):
self.script.query_landscape_edition()
self.assertEqual(
self.config.ping_url,
"http://landscape.hello.com/ping",
)
self.assertEqual(
self.config.url,
"https://landscape.hello.com/message-system",
)

@mock.patch(
"landscape.client.configuration.input",
return_value="landscape.hello.com",
Expand Down Expand Up @@ -760,7 +809,9 @@ def get_content(self, config):
try:
config.config = config_file
config.write()
return open(config.config).read().strip() + "\n"
with open(config.config) as fh:
text = fh.read().strip() + "\n"
return text
finally:
config.config = original_config

Expand Down Expand Up @@ -788,7 +839,7 @@ def side_effect_input(prompt):
"Will you be using your own "
"Self-Hosted Landscape installation? [y/N]": "n",
}
for key, value in iteritems(fixtures):
for key, value in fixtures.items():
if key in prompt:
return value
raise KeyError(f"Couldn't find answer for {prompt}")
Expand All @@ -798,7 +849,7 @@ def side_effect_getpass(prompt):
"(Optional) Registration Key:": "New Password",
"Please confirm:": "New Password",
}
for key, value in iteritems(fixtures):
for key, value in fixtures.items():
if key in prompt:
return value
raise KeyError(f"Couldn't find answer for {prompt}")
Expand Down Expand Up @@ -870,7 +921,7 @@ def side_effect_input(prompt):
"Will you be using your own "
"Self-Hosted Landscape installation? [y/N]": "n",
}
for key, value in iteritems(fixtures):
for key, value in fixtures.items():
if key in prompt:
return value

Expand All @@ -879,7 +930,7 @@ def side_effect_getpass(prompt):
"(Optional) Registration Key:": "New Password",
"Please confirm:": "New Password",
}
for key, value in iteritems(fixtures):
for key, value in fixtures.items():
if key in prompt:
return value

Expand Down Expand Up @@ -1231,6 +1282,27 @@ def test_main_no_registration(self, mock_setup, mock_register, mock_input):
"\nRequest a new registration for this computer now? [Y/n]: ",
)

@mock.patch("landscape.client.configuration.input")
@mock.patch("landscape.client.configuration.register")
@mock.patch("landscape.client.configuration.setup")
def test_skip_registration(self, mock_setup, mock_register, mock_input):
"""
Registration and input asking user to register is not called
when flag on
"""
main(["-c", self.make_working_config(), "--skip-registration"],
print=noop_print)
mock_register.assert_not_called()
mock_input.assert_not_called()

@mock.patch("landscape.client.configuration.register")
@mock.patch("landscape.client.configuration.setup")
def test_main_no_registration_silent(self, mock_setup, mock_register):
"""Skip registration works in silent mode"""
main(["-c", self.make_working_config(), "--skip-registration",
"--silent"], print=noop_print)
mock_register.assert_not_called()

@mock.patch(
"landscape.client.configuration.register",
return_value="success",
Expand Down Expand Up @@ -2139,7 +2211,8 @@ def test_base64_ssl_public_key_is_exported_to_file(
mock_print_text.assert_called_once_with(
f"Writing SSL CA certificate to {key_filename}...",
)
self.assertEqual("Hi there!", open(key_filename).read())
with open(key_filename) as fh:
self.assertEqual("Hi there!", fh.read())

options = ConfigParser()
options.read(config_filename)
Expand Down

0 comments on commit bea9394

Please sign in to comment.