~ubuntu-branches/ubuntu/trusty/landscape-client/trusty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
--- a/landscape/__init__.py
+++ b/landscape/__init__.py
@@ -1,5 +1,5 @@
 DEBIAN_REVISION = ""
-UPSTREAM_VERSION = "13.10+bzr73"
+UPSTREAM_VERSION = "14.01"
 VERSION = "%s%s" % (UPSTREAM_VERSION, DEBIAN_REVISION)
 
 # The "server-api" field of outgoing messages will be set to this value, and
--- a/landscape/broker/config.py
+++ b/landscape/broker/config.py
@@ -79,7 +79,7 @@
         """Get the path to the message store."""
         return os.path.join(self.data_path, "messages")
 
-    def load(self, args, accept_nonexistent_config=False):
+    def load(self, args):
         """
         Load options from command line arguments and a config file.
 
@@ -87,8 +87,7 @@
         C{http_proxy} and C{https_proxy} environment variables based on
         that config data.
         """
-        super(BrokerConfiguration, self).load(
-            args, accept_nonexistent_config=accept_nonexistent_config)
+        super(BrokerConfiguration, self).load(args)
         if self.http_proxy:
             os.environ["http_proxy"] = self.http_proxy
         elif self._original_http_proxy:
--- a/landscape/deployment.py
+++ b/landscape/deployment.py
@@ -148,18 +148,24 @@
         """
         self.load(self._command_line_args)
 
-    def load(self, args, accept_nonexistent_config=False):
+    def load(self, args, accept_nonexistent_default_config=False):
         """
         Load configuration data from command line arguments and a config file.
 
+        @param accept_nonexistent_default_config: If True, don't complain if
+            default configuration files aren't found
+
         @raise: A SystemExit if the arguments are bad.
+
         """
         self.load_command_line(args)
 
         if self.config:
             config_filenames = [self.config]
+            allow_missing = False
         else:
             config_filenames = self.default_config_filenames
+            allow_missing = accept_nonexistent_default_config
         # Parse configuration file, if found.
         for config_filename in config_filenames:
             if (os.path.isfile(config_filename)
@@ -169,7 +175,7 @@
                 break
 
         else:
-            if not accept_nonexistent_config:
+            if not allow_missing:
                 if len(config_filenames) == 1:
                     message = (
                         "error: config file %s can't be read" %
@@ -402,12 +408,11 @@
 
         return parser
 
-    def load(self, args, accept_nonexistent_config=False):
+    def load(self, args):
         """
         Load configuration data from command line arguments and a config file.
         """
-        super(Configuration, self).load(
-            args, accept_nonexistent_config=accept_nonexistent_config)
+        super(Configuration, self).load(args)
 
         if not isinstance(self.server_autodiscover, bool):
             autodiscover = str(self.server_autodiscover).lower()
--- a/landscape/sysinfo/deployment.py
+++ b/landscape/sysinfo/deployment.py
@@ -100,7 +100,9 @@
     if sysinfo is None:
         sysinfo = SysInfoPluginRegistry()
     config = SysInfoConfiguration()
-    config.load(args)
+    # landscape-sysinfo needs to work where there's no
+    # /etc/landscape/client.conf See lp:1293990
+    config.load(args, accept_nonexistent_default_config=True)
     for plugin in config.get_plugins():
         sysinfo.add(plugin)
 
--- a/landscape/sysinfo/tests/test_deployment.py
+++ b/landscape/sysinfo/tests/test_deployment.py
@@ -5,6 +5,8 @@
 
 from twisted.internet.defer import Deferred
 
+from landscape.lib.fs import create_file
+
 from landscape.sysinfo.deployment import (
     SysInfoConfiguration, ALL_PLUGINS, run, setup_logging,
     get_landscape_log_directory)
@@ -49,9 +51,7 @@
 
     def test_config_file(self):
         filename = self.makeFile()
-        f = open(filename, "w")
-        f.write("[sysinfo]\nsysinfo_plugins = TestPlugin\n")
-        f.close()
+        create_file(filename, "[sysinfo]\nsysinfo_plugins = TestPlugin\n")
         self.configuration.load(["--config", filename, "-d", self.makeDir()])
         plugins = self.configuration.get_plugins()
         self.assertEqual(len(plugins), 1)
@@ -155,6 +155,17 @@
 
         return result.addCallback(check_result)
 
+    def test_missing_config_file(self):
+        """The process doesn't fail if there is no config file."""
+        # Existing revert in tearDown will handle undoing this
+        SysInfoConfiguration.default_config_filenames = []
+        result = run([])
+
+        def check_result(result):
+            self.assertIn("System load", self.stdout.getvalue())
+
+        return result.addCallback(check_result)
+
     def test_plugins_called_after_reactor_starts(self):
         """
         Plugins are invoked after the reactor has started, so that they can
--- a/landscape/tests/test_deployment.py
+++ b/landscape/tests/test_deployment.py
@@ -4,7 +4,10 @@
 from StringIO import StringIO
 from textwrap import dedent
 
-from landscape.deployment import Configuration, get_versioned_persist
+from landscape.lib.fs import read_file, create_file
+
+from landscape.deployment import (
+    BaseConfiguration, Configuration, get_versioned_persist)
 from landscape.manager.config import ManagerConfiguration
 
 from landscape.tests.helpers import LandscapeTest, LogKeeperHelper
@@ -21,6 +24,37 @@
         return parser
 
 
+class BaseConfigurationTest(LandscapeTest):
+
+    def test_load_not_found_default_accept_missing(self):
+        """
+        C{config.load} doesn't exit the process if the default config file
+        is not found and C{accept_nonexistent_default_config} is C{True}.
+        """
+        class MyConfiguration(BaseConfiguration):
+            default_config_filenames = ["/not/here"]
+
+        config = MyConfiguration()
+        result = config.load([], accept_nonexistent_default_config=True)
+        self.assertIs(result, None)
+
+    def test_load_not_found_accept_missing(self):
+        """
+        C{config.load} exits the process if the specified config file
+        is not found and C{accept_nonexistent_default_config} is C{True}.
+        """
+        class MyConfiguration(BaseConfiguration):
+            default_config_filenames = []
+
+        config = MyConfiguration()
+        filename = "/not/here"
+        error = self.assertRaises(
+            SystemExit, config.load, ["--config", filename],
+            accept_nonexistent_default_config=True)
+        self.assertEqual(
+            "error: config file %s can't be read" % filename, str(error))
+
+
 class ConfigurationTest(LandscapeTest):
 
     helpers = [LogKeeperHelper]
@@ -160,7 +194,7 @@
         self.write_config_file(log_level="debug")
         self.config.log_level = "warning"
         self.config.write()
-        data = open(self.config_filename).read()
+        data = read_file(self.config_filename)
         self.assertConfigEqual(data, "[client]\nlog_level = warning")
 
     def test_write_configuration_with_section(self):
@@ -168,7 +202,7 @@
         self.write_config_file(section_name="babble", whatever="yay")
         self.config.whatever = "boo"
         self.config.write()
-        data = open(self.config_filename).read()
+        data = read_file(self.config_filename)
         self.assertConfigEqual(data, "[babble]\nwhatever = boo")
 
     def test_write_unrelated_configuration_back(self):
@@ -183,7 +217,7 @@
         self.config.load_configuration_file(config_filename)
         self.config.whatever = "boo"
         self.config.write()
-        data = open(config_filename).read()
+        data = read_file(config_filename)
         self.assertConfigEqual(
             data,
             "[babble]\nwhatever = boo\n\n[goojy]\nunrelated = yes")
@@ -195,9 +229,8 @@
         self.config.load([])
         self.config.log_level = "warning"
         self.config.write()
-        data = open(self.config_filename).read()
-        self.assertConfigEqual(data,
-            "[client]\nlog_level = warning\n")
+        data = read_file(self.config_filename)
+        self.assertConfigEqual(data, "[client]\nlog_level = warning\n")
 
     def test_write_empty_list_values_instead_of_double_quotes(self):
         """
@@ -209,7 +242,7 @@
         self.config.load([])
         self.config.include_manager_plugins = ""
         self.config.write()
-        data = open(self.config_filename).read()
+        data = read_file(self.config_filename)
         self.assertConfigEqual(data, "[client]\ninclude_manager_plugins = \n")
 
     def test_dont_write_config_specified_default_options(self):
@@ -220,7 +253,7 @@
         self.write_config_file(log_level="debug")
         self.config.log_level = "info"
         self.config.write()
-        data = open(self.config_filename).read()
+        data = read_file(self.config_filename)
         self.assertConfigEqual(data, "[client]")
 
     def test_dont_write_unspecified_default_options(self):
@@ -231,7 +264,7 @@
         self.write_config_file()
         self.config.log_level = "info"
         self.config.write()
-        data = open(self.config_filename).read()
+        data = read_file(self.config_filename)
         self.assertConfigEqual(data, "[client]")
 
     def test_dont_write_client_section_default_options(self):
@@ -242,7 +275,7 @@
         self.write_config_file(log_level="debug")
         self.config.log_level = "info"
         self.config.write()
-        data = open(self.config_filename).read()
+        data = read_file(self.config_filename)
         self.assertConfigEqual(data, "[client]")
 
     def test_do_write_preexisting_default_options(self):
@@ -255,7 +288,7 @@
         self.config.load_configuration_file(config_filename)
         self.config.log_level = "info"
         self.config.write()
-        data = open(config_filename).read()
+        data = read_file(config_filename)
         self.assertConfigEqual(data, "[client]\nlog_level = info\n")
 
     def test_dont_delete_explicitly_set_default_options(self):
@@ -266,21 +299,21 @@
         """
         self.write_config_file(log_level="info")
         self.config.write()
-        data = open(self.config_filename).read()
+        data = read_file(self.config_filename)
         self.assertConfigEqual(data, "[client]\nlog_level = info")
 
     def test_dont_write_config_option(self):
         self.write_config_file()
         self.config.config = self.config_filename
         self.config.write()
-        data = open(self.config_filename).read()
+        data = read_file(self.config_filename)
         self.assertConfigEqual(data, "[client]")
 
     def test_write_command_line_options(self):
         self.write_config_file()
         self.config.load(["--log-level", "warning"])
         self.config.write()
-        data = open(self.config_filename).read()
+        data = read_file(self.config_filename)
         self.assertConfigEqual(data, "[client]\nlog_level = warning\n")
 
     def test_write_command_line_precedence(self):
@@ -289,7 +322,7 @@
         self.write_config_file(log_level="debug")
         self.config.load(["--log-level", "warning"])
         self.config.write()
-        data = open(self.config_filename).read()
+        data = read_file(self.config_filename)
         self.assertConfigEqual(data, "[client]\nlog_level = warning\n")
 
     def test_write_manually_set_precedence(self):
@@ -299,17 +332,16 @@
         self.config.load(["--log-level", "warning"])
         self.config.log_level = "error"
         self.config.write()
-        data = open(self.config_filename).read()
+        data = read_file(self.config_filename)
         self.assertConfigEqual(data, "[client]\nlog_level = error\n")
 
     def test_write_to_given_config_file(self):
-        filename = self.makeFile()
+        filename = self.makeFile(content="")
         self.config.load(
-            ["--log-level", "warning", "--config", filename],
-            accept_nonexistent_config=True)
+            ["--log-level", "warning", "--config", filename])
         self.config.log_level = "error"
         self.config.write()
-        data = open(filename).read()
+        data = read_file(filename)
         self.assertConfigEqual(data, "[client]\nlog_level = error\n")
 
     def test_comments_are_maintained(self):
@@ -322,7 +354,7 @@
         self.config.load_configuration_file(filename)
         self.config.log_level = "error"
         self.config.write()
-        new_config = open(filename).read()
+        new_config = read_file(filename)
         self.assertConfigEqual(
             new_config,
             "[client]\n# Comment 1\nlog_level = error\n#Comment 2\n")
@@ -383,7 +415,7 @@
         """
         filename = self.makeFile("[client]\nhello = world1\n")
         self.config.load(["--config", filename])
-        open(filename, "w").write("[client]\nhello = world2\n")
+        create_file(filename, "[client]\nhello = world2\n")
         self.config.reload()
         self.assertEqual(self.config.hello, "world2")
 
@@ -459,8 +491,8 @@
 
     def test_url_option(self):
         """Ensure options.url option can be read by parse_args."""
-        options = self.parser.parse_args(["--url",
-                                       "http://mylandscape/message-system"])[0]
+        options = self.parser.parse_args(
+            ["--url", "http://mylandscape/message-system"])[0]
         self.assertEqual(options.url, "http://mylandscape/message-system")
 
     def test_url_default(self):
@@ -470,8 +502,8 @@
 
     def test_ping_url_option(self):
         """Ensure options.ping_url option can be read by parse_args."""
-        options = self.parser.parse_args(["--ping-url",
-                                       "http://mylandscape/ping"])[0]
+        options = self.parser.parse_args(
+            ["--ping-url", "http://mylandscape/ping"])[0]
         self.assertEqual(options.ping_url, "http://mylandscape/ping")
 
     def test_ping_url_default(self):
@@ -482,8 +514,8 @@
 
     def test_ssl_public_key_option(self):
         """Ensure options.ssl_public_key option can be read by parse_args."""
-        options = self.parser.parse_args(["--ssl-public-key",
-                                       "/tmp/somekeyfile.ssl"])[0]
+        options = self.parser.parse_args(
+            ["--ssl-public-key", "/tmp/somekeyfile.ssl"])[0]
         self.assertEqual(options.ssl_public_key, "/tmp/somekeyfile.ssl")
 
     def test_ssl_public_key_default(self):
@@ -508,8 +540,9 @@
         Ensure options.autodiscover_srv_query_string option can be read by
         parse_args.
         """
-        options = self.parser.parse_args(["--autodiscover-srv-query-string",
-                                       "_tcp._landscape.someotherdomain"])[0]
+        options = self.parser.parse_args(
+            ["--autodiscover-srv-query-string",
+             "_tcp._landscape.someotherdomain"])[0]
         self.assertEqual(options.autodiscover_srv_query_string,
                          "_tcp._landscape.someotherdomain")
 
@@ -527,8 +560,8 @@
         Ensure options.autodiscover_a_query_string option can be read by
         parse_args.
         """
-        options = self.parser.parse_args(["--autodiscover-a-query-string",
-                                       "customname.mydomain"])[0]
+        options = self.parser.parse_args(
+            ["--autodiscover-a-query-string", "customname.mydomain"])[0]
         self.assertEqual(options.autodiscover_a_query_string,
                          "customname.mydomain")
 
@@ -543,8 +576,8 @@
 
     def test_log_file_option(self):
         """Ensure options.log_dir option can be read by parse_args."""
-        options = self.parser.parse_args(["--log-dir",
-                                       "/var/log/my-awesome-log"])[0]
+        options = self.parser.parse_args(
+            ["--log-dir", "/var/log/my-awesome-log"])[0]
         self.assertEqual(options.log_dir, "/var/log/my-awesome-log")
 
     def test_log_level_default(self):