adhorn / aws-lambda-chaos-injection

Chaos Injection library for AWS Lambda
MIT License
99 stars 13 forks source link

Improve tests #13

Closed alexcasalboni closed 3 years ago

alexcasalboni commented 5 years ago

A few considerations about tests:

  1. I think we shouldn't test for printed output in our tests, as it doesn't allow you to easily change and/or add new logs.

  2. also, we could use a library such as moto to avoid actually API calls during unit tests, so anybody can run unit tests without configuring AWS credentials. Additionally, we can always run the same tests as "integration tests" on a real AWS account.

  3. we could speed up all the tests related to delays by patching time.sleep. This is not super critical now, but it'd allow us to add more tests and test much longer intervals without waiting minutes to unit tests to complete.

For example, this:

@ignore_warnings
def test_get_delay(self):
    with patch('sys.stdout', new=StringIO()) as fakeOutput:
        response = handler('foo', 'bar')
        assert (
            'Injecting 400 of delay with a rate of 1' in fakeOutput.getvalue().strip()
        )
    self.assertEqual(str(response), "{'statusCode': 200, 'body': 'Hello from Lambda!'}")

could simply become this:

def test_get_delay(self):
    with patch('time.sleep', return_value=None) as patched_time_sleep:
        response = handler('foo', 'bar')
        patched_time_sleep. assert_called()
    self.assertEqual(str(response), "{'statusCode': 200, 'body': 'Hello from Lambda!'}")

And we could mock SSM like this:

from moto import mock_ssm

@mock_ssm
class TestExceptionMethods(unittest.TestCase):
    ...
adhorn commented 5 years ago

All good ideas - feel free to PR ;)

adhorn commented 4 years ago

Support offline tests with placebo https://github.com/adhorn/aws-lambda-chaos-injection/pull/20 - Didn't go with moto since placebo was better for the intended use.