919
925
self.mocker.result(1000)
920
926
self.mocker.replay()
921
927
sys_exit = self.assertRaises(SystemExit,
922
main, ["-c", self.make_working_config()])
928
main, ["-c", self.make_working_config()])
923
929
self.assertIn("landscape-config must be run as root", str(sys_exit))
931
def test_import_from_file(self):
932
sysvconfig_mock = self.mocker.patch(SysVConfig)
933
sysvconfig_mock.set_start_on_boot(True)
934
sysvconfig_mock.restart_landscape()
935
self.mocker.result(True)
940
"computer_title = New Title\n"
941
"account_name = New Name\n"
942
"registration_password = New Password\n"
943
"http_proxy = http://new.proxy\n"
944
"https_proxy = https://new.proxy\n"
945
"url = http://new.url\n")
947
import_filename = self.makeFile(configuration, basename="import_config")
948
config_filename = self.makeFile("", basename="final_config")
950
config = self.get_config(["--config", config_filename, "--silent",
951
"--import", import_filename])
954
options = ConfigParser()
955
options.read(config_filename)
957
self.assertEquals(dict(options.items("client")),
958
{"computer_title": "New Title",
959
"account_name": "New Name",
960
"registration_password": "New Password",
961
"http_proxy": "http://new.proxy",
962
"https_proxy": "https://new.proxy",
963
"url": "http://new.url"})
965
def test_import_from_empty_file(self):
968
config_filename = self.makeFile("", basename="final_config")
969
import_filename = self.makeFile("", basename="import_config")
971
# Use a command line option as well to test the precedence.
973
self.get_config(["--config", config_filename, "--silent",
974
"--import", import_filename])
975
except ImportOptionError, error:
976
self.assertEquals(str(error),
977
"Nothing to import at %s." % import_filename)
979
self.fail("ImportOptionError not raised")
981
def test_import_from_non_existent_file(self):
984
config_filename = self.makeFile("", basename="final_config")
985
import_filename = self.makeFile(basename="import_config")
987
# Use a command line option as well to test the precedence.
989
self.get_config(["--config", config_filename, "--silent",
990
"--import", import_filename])
991
except ImportOptionError, error:
992
self.assertEquals(str(error),
993
"File %s doesn't exist." % import_filename)
995
self.fail("ImportOptionError not raised")
997
def test_import_from_file_with_empty_client_section(self):
1000
old_configuration = "[client]\n"
1002
config_filename = self.makeFile("", old_configuration,
1003
basename="final_config")
1004
import_filename = self.makeFile("", basename="import_config")
1006
# Use a command line option as well to test the precedence.
1008
self.get_config(["--config", config_filename, "--silent",
1009
"--import", import_filename])
1010
except ImportOptionError, error:
1011
self.assertEquals(str(error),
1012
"Nothing to import at %s." % import_filename)
1014
self.fail("ImportOptionError not raised")
1016
def test_import_from_bogus_file(self):
1017
self.mocker.replay()
1019
config_filename = self.makeFile("", basename="final_config")
1020
import_filename = self.makeFile("<strong>BOGUS!</strong>",
1021
basename="import_config")
1023
# Use a command line option as well to test the precedence.
1025
self.get_config(["--config", config_filename, "--silent",
1026
"--import", import_filename])
1027
except ImportOptionError, error:
1028
self.assertIn("File contains no section headers.", str(error))
1030
self.fail("ImportOptionError not raised")
1032
def test_import_from_file_preserves_old_options(self):
1033
sysvconfig_mock = self.mocker.patch(SysVConfig)
1034
sysvconfig_mock.set_start_on_boot(True)
1035
sysvconfig_mock.restart_landscape()
1036
self.mocker.result(True)
1037
self.mocker.replay()
1039
old_configuration = (
1041
"computer_title = Old Title\n"
1042
"account_name = Old Name\n"
1043
"registration_password = Old Password\n"
1044
"http_proxy = http://old.proxy\n"
1045
"https_proxy = https://old.proxy\n"
1046
"url = http://old.url\n")
1048
new_configuration = (
1050
"account_name = New Name\n"
1051
"registration_password = New Password\n"
1052
"url = http://new.url\n")
1054
config_filename = self.makeFile(old_configuration,
1055
basename="final_config")
1056
import_filename = self.makeFile(new_configuration,
1057
basename="import_config")
1059
# Use a command line option as well to test the precedence.
1060
config = self.get_config(["--config", config_filename, "--silent",
1061
"--import", import_filename,
1062
"-p", "Command Line Password"])
1065
options = ConfigParser()
1066
options.read(config_filename)
1068
self.assertEquals(dict(options.items("client")),
1069
{"computer_title": "Old Title",
1070
"account_name": "New Name",
1071
"registration_password": "Command Line Password",
1072
"http_proxy": "http://old.proxy",
1073
"https_proxy": "https://old.proxy",
1074
"url": "http://new.url"})
1076
def test_import_from_file_may_reset_old_options(self):
1078
This test ensures that setting an empty option in an imported
1079
configuration file will actually set the local value to empty
1080
too, rather than being ignored.
1082
sysvconfig_mock = self.mocker.patch(SysVConfig)
1083
sysvconfig_mock.set_start_on_boot(True)
1084
sysvconfig_mock.restart_landscape()
1085
self.mocker.result(True)
1086
self.mocker.replay()
1088
old_configuration = (
1090
"computer_title = Old Title\n"
1091
"account_name = Old Name\n"
1092
"registration_password = Old Password\n"
1093
"url = http://old.url\n")
1095
new_configuration = (
1097
"registration_password =\n")
1099
config_filename = self.makeFile(old_configuration,
1100
basename="final_config")
1101
import_filename = self.makeFile(new_configuration,
1102
basename="import_config")
1104
config = self.get_config(["--config", config_filename, "--silent",
1105
"--import", import_filename])
1108
options = ConfigParser()
1109
options.read(config_filename)
1111
self.assertEquals(dict(options.items("client")),
1112
{"computer_title": "Old Title",
1113
"account_name": "Old Name",
1114
"registration_password": "", # <==
1115
"url": "http://old.url"})
1117
def test_import_from_url(self):
1118
sysvconfig_mock = self.mocker.patch(SysVConfig)
1119
sysvconfig_mock.set_start_on_boot(True)
1120
sysvconfig_mock.restart_landscape()
1121
self.mocker.result(True)
1125
"computer_title = New Title\n"
1126
"account_name = New Name\n"
1127
"registration_password = New Password\n"
1128
"http_proxy = http://new.proxy\n"
1129
"https_proxy = https://new.proxy\n"
1130
"url = http://new.url\n")
1132
fetch_mock = self.mocker.replace("landscape.lib.fetch.fetch")
1133
fetch_mock("https://config.url")
1134
self.mocker.result(configuration)
1136
print_text_mock = self.mocker.replace(print_text)
1137
print_text_mock("Fetching configuration from https://config.url...")
1139
self.mocker.replay()
1141
config_filename = self.makeFile("", basename="final_config")
1143
config = self.get_config(["--config", config_filename, "--silent",
1144
"--import", "https://config.url"])
1147
options = ConfigParser()
1148
options.read(config_filename)
1150
self.assertEquals(dict(options.items("client")),
1151
{"computer_title": "New Title",
1152
"account_name": "New Name",
1153
"registration_password": "New Password",
1154
"http_proxy": "http://new.proxy",
1155
"https_proxy": "https://new.proxy",
1156
"url": "http://new.url"})
1158
def test_import_from_url_with_http_code_fetch_error(self):
1159
fetch_mock = self.mocker.replace("landscape.lib.fetch.fetch")
1160
fetch_mock("https://config.url")
1161
self.mocker.throw(HTTPCodeError(501, ""))
1163
print_text_mock = self.mocker.replace(print_text)
1164
print_text_mock("Fetching configuration from https://config.url...")
1166
self.mocker.replay()
1168
config_filename = self.makeFile("", basename="final_config")
1171
self.get_config(["--config", config_filename, "--silent",
1172
"--import", "https://config.url"])
1173
except ImportOptionError, error:
1174
self.assertEquals(str(error),
1175
"Couldn't download configuration from "
1176
"https://config.url: Server "
1177
"returned HTTP code 501")
1179
self.fail("ImportOptionError not raised")
1181
def test_import_from_url_with_pycurl_error(self):
1182
fetch_mock = self.mocker.replace("landscape.lib.fetch.fetch")
1183
fetch_mock("https://config.url")
1184
self.mocker.throw(pycurl.error(60, "pycurl message"))
1186
print_text_mock = self.mocker.replace(print_text)
1187
print_text_mock("Fetching configuration from https://config.url...")
1189
self.mocker.replay()
1191
config_filename = self.makeFile("", basename="final_config")
1194
self.get_config(["--config", config_filename, "--silent",
1195
"--import", "https://config.url"])
1196
except ImportOptionError, error:
1197
self.assertEquals(str(error),
1198
"Couldn't download configuration from "
1199
"https://config.url: pycurl message")
1201
self.fail("ImportOptionError not raised")
1203
def test_import_from_url_with_empty_content(self):
1204
fetch_mock = self.mocker.replace("landscape.lib.fetch.fetch")
1205
fetch_mock("https://config.url")
1206
self.mocker.result("")
1208
print_text_mock = self.mocker.replace(print_text)
1209
print_text_mock("Fetching configuration from https://config.url...")
1211
self.mocker.replay()
1213
# Use a command line option as well to test the precedence.
1215
self.get_config(["--silent", "--import", "https://config.url"])
1216
except ImportOptionError, error:
1217
self.assertEquals(str(error),
1218
"Nothing to import at https://config.url.")
1220
self.fail("ImportOptionError not raised")
1222
def test_import_from_url_with_bogus_content(self):
1223
fetch_mock = self.mocker.replace("landscape.lib.fetch.fetch")
1224
fetch_mock("https://config.url")
1225
self.mocker.result("<strong>BOGUS!</strong>")
1227
print_text_mock = self.mocker.replace(print_text)
1228
print_text_mock("Fetching configuration from https://config.url...")
1230
self.mocker.replay()
1232
# Use a command line option as well to test the precedence.
1234
self.get_config(["--silent", "--import", "https://config.url"])
1235
except ImportOptionError, error:
1236
self.assertIn("File contains no section headers.", str(error))
1238
self.fail("ImportOptionError not raised")
1240
def test_import_error_is_handled_nicely_by_main(self):
1241
fetch_mock = self.mocker.replace("landscape.lib.fetch.fetch")
1242
fetch_mock("https://config.url")
1243
self.mocker.throw(HTTPCodeError(404, ""))
1245
print_text_mock = self.mocker.replace(print_text)
1246
print_text_mock("Fetching configuration from https://config.url...")
1247
print_text_mock(CONTAINS("Server returned HTTP code 404"), error=True)
1249
self.mocker.replay()
1251
system_exit = self.assertRaises(
1252
SystemExit, main, ["--import", "https://config.url"])
1253
self.assertEquals(system_exit.code, 1)
1255
def test_base64_ssl_public_key_is_exported_to_file(self):
1256
sysvconfig_mock = self.mocker.patch(SysVConfig)
1257
sysvconfig_mock.set_start_on_boot(True)
1258
sysvconfig_mock.restart_landscape()
1259
self.mocker.result(True)
1261
config_filename = self.makeFile("")
1262
key_filename = config_filename + ".ssl_public_key"
1264
print_text_mock = self.mocker.replace(print_text)
1265
print_text_mock("Writing SSL public key to %s..." % key_filename)
1267
self.mocker.replay()
1269
config = self.get_config(["--silent", "-c", config_filename,
1270
"-u", "url", "-a", "account", "-t", "title",
1271
"--ssl-public-key", "base64:SGkgdGhlcmUh"])
1274
self.assertTrue(os.path.isfile(key_filename))
1275
self.assertEquals(open(key_filename).read(), "Hi there!")
1277
options = ConfigParser()
1278
options.read(config_filename)
1279
self.assertEquals(options.get("client", "ssl_public_key"),
1282
def test_normal_ssl_public_key_is_not_exported_to_file(self):
1283
sysvconfig_mock = self.mocker.patch(SysVConfig)
1284
sysvconfig_mock.set_start_on_boot(True)
1285
sysvconfig_mock.restart_landscape()
1286
self.mocker.result(True)
1287
self.mocker.replay()
1289
config_filename = self.makeFile("")
1291
config = self.get_config(["--silent", "-c", config_filename,
1292
"-u", "url", "-a", "account", "-t", "title",
1293
"--ssl-public-key", "/some/filename"])
1296
key_filename = config_filename + ".ssl_public_key"
1297
self.assertFalse(os.path.isfile(key_filename))
1299
options = ConfigParser()
1300
options.read(config_filename)
1301
self.assertEquals(options.get("client", "ssl_public_key"),
1304
# We test them individually since they must work individually.
1305
def test_import_from_url_honors_http_proxy(self):
1306
self.ensure_import_from_url_honors_proxy_options("http_proxy")
1308
def test_import_from_url_honors_https_proxy(self):
1309
self.ensure_import_from_url_honors_proxy_options("https_proxy")
1311
def ensure_import_from_url_honors_proxy_options(self, proxy_option):
1312
def check_proxy(url):
1313
self.assertEquals(os.environ.get(proxy_option), "http://proxy")
1315
fetch_mock = self.mocker.replace("landscape.lib.fetch.fetch")
1316
fetch_mock("https://config.url")
1317
self.mocker.call(check_proxy)
1319
# Doesn't matter. We just want to check the context around it.
1320
self.mocker.result("")
1322
print_text_mock = self.mocker.replace(print_text)
1323
print_text_mock("Fetching configuration from https://config.url...")
1325
self.mocker.replay()
1327
config_filename = self.makeFile("", basename="final_config")
1330
self.get_config(["--config", config_filename, "--silent",
1331
"--" + proxy_option.replace("_", "-"),
1333
"--import", "https://config.url"])
1334
except ImportOptionError:
1335
pass # The returned content is empty. We don't really
1336
# care for this test. Mocker will ensure the tests
1337
# we care about are done.
926
1340
class RegisterFunctionTest(LandscapeIsolatedTest):