def test_kwargs(self): """ We pass through any kwargs we don't recognise to docker. """ con = self.mkcontainer([(0, b'hi\n')], {'stdout': False}) with self.assertRaises(AssertionError): self.wflm(con, EqualsMatcher('hi')) with self.assertRaises(AssertionError): self.wflm(con, EqualsMatcher('hi'), stdout=False, stderr=False) self.wflm(con, EqualsMatcher('hi'), stdout=False)
def test_fake_and_real_logging_behaviour(self): """ Our fake logs container should exhibit similar behaviour to a real container. """ logger = self.start_logging_container() self.wflm(logger, EqualsMatcher('Log entry 1'), timeout=1) early_logs = logger.logs().decode('utf8').splitlines() self.assertIn('Log entry 1', early_logs) self.assertNotIn('Log entry 4', early_logs) streamed_logs = [] with self.assertRaises(TimeoutError): for line in self.stream_logs(logger, tail=0, timeout=0.7): streamed_logs.append(line.decode('utf8').rstrip()) self.assertNotIn('Log entry 1', streamed_logs) self.assertIn('Log entry 4', streamed_logs) self.assertNotIn('Log entry 9', streamed_logs) streamed_logs = [] for line in self.stream_logs(logger, timeout=2): streamed_logs.append(line.decode('utf8').rstrip()) self.assertIn('Log entry 1', streamed_logs) self.assertIn('Log entry 4', streamed_logs) self.assertIn('Log entry 9', streamed_logs)
def test_str(self): """ The string representation is readable and shows which matchers have been matched and which are still to be matched. """ matcher = UnorderedLinesMatcher(EqualsMatcher('foo'), RegexMatcher('^bar')) self.assertEqual( str(matcher), 'UnorderedLinesMatcher(matched=[], ' "unmatched=[EqualsMatcher('foo'), RegexMatcher('^bar')])") self.assertEqual(repr(matcher), str(matcher)) self.assertFalse(matcher('foo')) self.assertEqual( str(matcher), "UnorderedLinesMatcher(matched=[EqualsMatcher('foo')], " "unmatched=[RegexMatcher('^bar')])") self.assertEqual(repr(matcher), str(matcher)) self.assertTrue(matcher('barfoo')) self.assertEqual( str(matcher), "UnorderedLinesMatcher(matched=[EqualsMatcher('foo'), " "RegexMatcher('^bar')], unmatched=[])") self.assertEqual(repr(matcher), str(matcher))
def test_wait_for_logs_matching(self): """ This behaves like calling the underlying wait_for_logs_matching() function with the container object as the first parameter. """ script = self.run_logs_container([ 'echo "hi"', 'echo "heya" >&2', 'echo "hello"', ], delay=0.2, wait=False) script.wait_for_logs_matching(EqualsMatcher('hello')) with self.assertRaises(RuntimeError): script.wait_for_logs_matching(EqualsMatcher('goodbye'))
def test_matching(self): """ Applies each matcher sequentially and returns True on the final match. """ matcher = UnorderedLinesMatcher(EqualsMatcher('foo'), RegexMatcher('^bar')) self.assertFalse(matcher('barfoo')) self.assertFalse(matcher('baz')) self.assertTrue(matcher('foo')) matcher = UnorderedLinesMatcher(EqualsMatcher('foo'), RegexMatcher('^bar')) self.assertFalse(matcher('foo')) self.assertFalse(matcher('baz')) self.assertTrue(matcher('barfoo'))
def test_encoding(self): """ We can operate on logs that use excitingly horrible encodings. """ con = self.mkcontainer([ (0, b'\xfeorn\n'), ]) # If this doesn't raise an exception, the test passes. self.wflm(con, EqualsMatcher('\u00feorn'), encoding='latin1')
def test_default_encoding(self): """ By default, we assume logs are UTF-8. """ con = self.mkcontainer([ (0, b'\xc3\xbeorn\n'), ]) # If this doesn't raise an exception, the test passes. self.wflm(con, EqualsMatcher('\u00feorn'))
def test_one_matching_line(self): """ If one matching line is logged, all is happy. """ con = self.mkcontainer([ (0, b'hello\n'), ]) # If this doesn't raise an exception, the test passes. self.wflm(con, EqualsMatcher('hello'))
def test_one_non_matching_line(self): """ If there's no match by the time the logs end, we raise an exception. """ con = self.mkcontainer([ (0, b'goodbye\n'), ]) with self.assertRaises(RuntimeError) as cm: self.wflm(con, EqualsMatcher('hello')) self.assertIn("Logs matching EqualsMatcher('hello') not found.", str(cm.exception)) self.assertIn('goodbye\n', str(cm.exception))
def test_timeout_first_line(self): """ If we take too long to get the first line, we time out. """ con = self.mkcontainer([ (0.2, b'hello\n'), ]) with self.assertRaises(TimeoutError) as cm: self.wflm(con, EqualsMatcher('hello'), timeout=0.1) self.assertIn( "Timeout (0.1s) waiting for logs matching EqualsMatcher('hello').", str(cm.exception)) self.assertNotIn('hello\n', str(cm.exception))
def test_wait_for_logs_matching_timeout(self): """ If we take too long to get a match, we time out. """ script = self.run_logs_container([ 'echo "hi"', 'echo "heya" >&2', 'echo "hello"', ], delay=0.2, wait=False) with self.assertRaises(TimeoutError): script.wait_for_logs_matching(EqualsMatcher('hello'), timeout=0.1)
def test_str(self): """ The string representation is readable. """ matcher = EqualsMatcher('bar') self.assertEqual(str(matcher), "EqualsMatcher('bar')") self.assertEqual(repr(matcher), str(matcher))
def test_matching(self): """ Matches exactly equal strings and nothing else. """ matcher = EqualsMatcher('foo') self.assertTrue(matcher('foo')) self.assertFalse(matcher('foobar'))