By using AWS re:Post, you agree to the Terms of Use

AWS Greengrass - Unit test components

0

Hi there,

I am trying to develop unit tests in python language for simple AWS Greengrass components, using pytest framework and simulating a temperature sensor.

The point here is that I have a SonarQube process running right now locally on my computer, and its main purpose is to check both the code quality and the testing coverage. The first task was easy to make it work, but for testing task, I am able to achieve it only if I ignore the AWS parts, but that is not the way I would like to follow, the whole code should be checked.

Is there any guide I might follow in order to achieve it?

Thanks beforehand, looking forward to hearing from you.

Kind regards Daniel.

asked 3 months ago85 views
2 Answers
0

Hi Greg_B,

First of all, thank you very much for your reply, I was able to achieve one unit test publishing a message to AWS Cloudwatch metric by using the AWS Greengrass component SDK, yet I have many doubts maybe you can solve me, since I cannot see how this procedure is actually working.

I am getting a passed test for the one previously indicated, which its code is attached below:

    def test_ipc_connect(self, mocker):
        ipc_connect = mocker.patch('awsiot.greengrasscoreipc.connect')
        mocker.patch('json.loads', return_value='1')

        init_ipc_client()

        ret_cpu = publish_message(ipc_connect, aws_cloudwatch_metric_payload("Greengrass-CoreDevice",  
                "System.Cpu.Usage", "hostname", "co2sink-dev-CoreDevice02", get_cpu_usage()), 
                "cloudwatch/metric/put")
        ipc_connect.assert_called_once()

        assert ret_cpu == 1

But what really worries me is how it works and how the credentials are internally managed by the mock framework. As fas as I concern, the components that are running within the AWS Greengrass software work since it manages everything by using its own credentials giving at the installation process. What is it really being tested? Is it some kind of simulation?

I have a 75% coverage, which is absolutely rather good, but I am quite demanding, so I would like to have a 90% at least, the problem here is I am not able to test all exceptions that the AWS Greengrasas component SDK might throw.

https://imgur.com/k4k0mX0

How can I test those cases? That is to say, the wrong ones (the publish message method is one from your example codes, but modified to fit in my application).

I have tried something like this, but I am sure that is not ok, because in order to achieve the test properly, the exception should be thrown first within the publish_message method:

    def test_get_secret_exception(self, mocker):
        ipc_connect = mocker.patch('awsiot.greengrasscoreipc.connect', side_effect=Exception('mocked exception'))

        with pytest.raises(Exception) as e:
            init_ipc_client()

            publish_message(ipc_connect, aws_cloudwatch_metric_payload("Greengrass-CoreDevice",
                            "System.Cpu.Usage", "hostname", "co2sink-dev-CoreDevice02", get_cpu_usage()),
                            "cloudwatch/metric/put")
            assert e.type == SystemExit
            assert e.code == 1

        ipc_connect.assert_called_once()

https://imgur.com/WectiAf

Thanks beforehand, looking forward to hearing from you.

Kind regards.

Daniel.

answered 3 months ago
  • What is it really being tested? Is it some kind of simulation?

    Generally speaking, all of the GG library calls should be mocked so you only test the logic of your code. There's no real credentials being used in your test case.

    How can I test those cases?

    For each error, you have both a return value and a raise. But the raise will prevent the return value being returned.

    To test the raises, you would need to mock the future_response.result() call and use a side_effect to throw the right exception.

  • Side effect example here: https://github.com/awslabs/aws-greengrass-labs-component-for-home-assistant/blob/0056ab6f23ac9949cf6d1b561ac6635e756da663/tests/test_artifacts_secret.py#L20

    However, you should possibly read more widely on Pytest and mocker as this is all outside of AWS scope.

  • Hi Greg_B

    Thank you very much for your reply, I will try the exception approach you have just mentioned before, but first I have to replace everything developed by pytest to nosetests, since it is the tool used by my work colleagues, so I will let you know how it goes.

    Kind regards. Daniel.

0
profile picture
EXPERT
answered 3 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions