Beispiel #1
0
 def test_duplicate_error(self):
     text = ("key1: value1\n" "key1: value2\n")
     with StringIO(text) as stream:
         with self.assertRaises(RFC822SyntaxError) as call:
             load_rfc822_records(stream)
         self.assertEqual(
             call.exception.msg,
             "Job has a duplicate key 'key1' with old value 'value1'"
             " and new value 'value2'")
Beispiel #2
0
 def test_duplicate_error(self):
     text = (
         "key1: value1\n"
         "key1: value2\n"
     )
     with StringIO(text) as stream:
         with self.assertRaises(RFC822SyntaxError) as call:
             load_rfc822_records(stream)
         self.assertEqual(call.exception.msg, (
             "Job has a duplicate key 'key1' with old value 'value1'"
             " and new value 'value2'"))
Beispiel #3
0
 def _plugin_resource(self, job):
     proc = self._run_command(job)
     if proc.returncode == 127:
         logging.warning("Unable to find command: %s", job.command)
         return
     line_list = []
     for byte_line in proc.stdout.splitlines():
         try:
             line = byte_line.decode("UTF-8")
         except UnicodeDecodeError as exc:
             logger.warning("resource script %s returned invalid UTF-8 data"
                            " %r: %s", job, byte_line, exc)
         else:
             line_list.append(line)
     with StringIO("\n".join(line_list)) as stream:
         try:
             record_list = load_rfc822_records(stream)
         except RFC822SyntaxError as exc:
             logger.warning("resource script %s returned invalid RFC822"
                            " data: %s", job, exc)
         else:
             for record in record_list:
                 logger.info("Storing resource record %s: %s",
                             job.name, record)
                 resource = Resource(record)
                 self._context.add_resource(job.name, resource)
Beispiel #4
0
 def test_many_records(self):
     text = ("key1:value1\n" "\n" "key2:value2\n" "\n" "key3:value3\n")
     with StringIO(text) as stream:
         records = load_rfc822_records(stream)
     self.assertEqual(len(records), 3)
     self.assertEqual(records[0], {'key1': 'value1'})
     self.assertEqual(records[1], {'key2': 'value2'})
     self.assertEqual(records[2], {'key3': 'value3'})
Beispiel #5
0
 def test_relevant_whitespace(self):
     text = (
         "key:\n"
         " value\n"
     )
     with StringIO(text) as stream:
         records = load_rfc822_records(stream)
     self.assertEqual(len(records), 1)
     self.assertEqual(records[0].data, {'key': 'value'})
Beispiel #6
0
 def test_relevant_whitespace(self):
     text = (
         "key:\n"
         " value\n"
     )
     with StringIO(text) as stream:
         records = load_rfc822_records(stream)
     self.assertEqual(len(records), 1)
     self.assertEqual(records[0], {'key': 'value'})
Beispiel #7
0
 def test_multiline_value(self):
     text = (
         "key:\n"
         " longer\n"
         " value\n"
     )
     with StringIO(text) as stream:
         records = load_rfc822_records(stream)
     self.assertEqual(len(records), 1)
     self.assertEqual(records[0].data, {'key': 'longer\nvalue'})
Beispiel #8
0
 def test_multiline_value(self):
     text = (
         "key:\n"
         " longer\n"
         " value\n"
     )
     with StringIO(text) as stream:
         records = load_rfc822_records(stream)
     self.assertEqual(len(records), 1)
     self.assertEqual(records[0], {'key': 'longer\nvalue'})
Beispiel #9
0
 def test_many_multiline_values(self):
     text = ("key1:initial\n"
             " longer\n"
             " value 1\n"
             "\n"
             "key2:\n"
             " longer\n"
             " value 2\n")
     with StringIO(text) as stream:
         records = load_rfc822_records(stream)
     self.assertEqual(len(records), 2)
     self.assertEqual(records[0], {'key1': 'initial\nlonger\nvalue 1'})
     self.assertEqual(records[1], {'key2': 'longer\nvalue 2'})
Beispiel #10
0
 def test_many_records(self):
     text = (
         "key1:value1\n"
         "\n"
         "key2:value2\n"
         "\n"
         "key3:value3\n"
     )
     with StringIO(text) as stream:
         records = load_rfc822_records(stream)
     self.assertEqual(len(records), 3)
     self.assertEqual(records[0].data, {'key1': 'value1'})
     self.assertEqual(records[1].data, {'key2': 'value2'})
     self.assertEqual(records[2].data, {'key3': 'value3'})
Beispiel #11
0
 def test_many_multiline_values(self):
     text = (
         "key1:initial\n"
         " longer\n"
         " value 1\n"
         "\n"
         "key2:\n"
         " longer\n"
         " value 2\n"
     )
     with StringIO(text) as stream:
         records = load_rfc822_records(stream)
     self.assertEqual(len(records), 2)
     self.assertEqual(records[0].data, {'key1': 'initial\nlonger\nvalue 1'})
     self.assertEqual(records[1].data, {'key2': 'longer\nvalue 2'})
Beispiel #12
0
 def load(self, somewhere):
     if isinstance(somewhere, str):
         # Load data from a file with the given name
         filename = somewhere
         with open(filename, 'rt', encoding='UTF-8') as stream:
             return load(stream)
     if isinstance(somewhere, TextIOWrapper):
         stream = somewhere
         logger.debug("Loading jobs definitions from %r...", stream.name)
         record_list = load_rfc822_records(stream)
         job_list = []
         for record in record_list:
             job = JobDefinition.from_rfc822_record(record)
             logger.debug("Loaded %r", job)
             job_list.append(job)
         return job_list
     else:
         raise TypeError("Unsupported type of 'somewhere': {!r}".format(
             type(somewhere)))
Beispiel #13
0
 def load(self, somewhere):
     if isinstance(somewhere, str):
         # Load data from a file with the given name
         filename = somewhere
         with open(filename, 'rt', encoding='UTF-8') as stream:
             return load(stream)
     if isinstance(somewhere, TextIOWrapper):
         stream = somewhere
         logger.debug("Loading jobs definitions from %r...", stream.name)
         record_list = load_rfc822_records(stream)
         job_list = []
         for record in record_list:
             job = JobDefinition.from_rfc822_record(record)
             logger.debug("Loaded %r", job)
             job_list.append(job)
         return job_list
     else:
         raise TypeError(
             "Unsupported type of 'somewhere': {!r}".format(
                 type(somewhere)))
Beispiel #14
0
 def test_empty(self):
     with StringIO("") as stream:
         records = load_rfc822_records(stream)
     self.assertEqual(len(records), 0)
Beispiel #15
0
 def test_syntax_error(self):
     text = "key1 = value1"
     with StringIO(text) as stream:
         with self.assertRaises(RFC822SyntaxError) as call:
             load_rfc822_records(stream)
         self.assertEqual(call.exception.msg, "Unexpected non-empty line")
Beispiel #16
0
 def test_bad_multiline(self):
     text = " extra value"
     with StringIO(text) as stream:
         with self.assertRaises(RFC822SyntaxError) as call:
             load_rfc822_records(stream)
         self.assertEqual(call.exception.msg, "Unexpected multi-line value")
Beispiel #17
0
 def test_empty(self):
     with StringIO("") as stream:
         records = load_rfc822_records(stream)
     self.assertEqual(len(records), 0)
Beispiel #18
0
 def test_garbage(self):
     text = "garbage"
     with StringIO(text) as stream:
         with self.assertRaises(RFC822SyntaxError) as call:
             load_rfc822_records(stream)
         self.assertEqual(call.exception.msg, "Unexpected non-empty line")
Beispiel #19
0
 def test_bad_multilie(self):
     text = " extra value"
     with StringIO(text) as stream:
         with self.assertRaises(RFC822SyntaxError) as call:
             load_rfc822_records(stream)
         self.assertEqual(call.exception.msg, "Unexpected multi-line value")
Beispiel #20
0
 def test_single_record(self):
     with StringIO("key:value") as stream:
         records = load_rfc822_records(stream)
     self.assertEqual(len(records), 1)
     self.assertEqual(records[0].data, {'key': 'value'})
Beispiel #21
0
 def test_single_record(self):
     with StringIO("key:value") as stream:
         records = load_rfc822_records(stream)
     self.assertEqual(len(records), 1)
     self.assertEqual(records[0], {'key': 'value'})