Ask Your Question
4

In what way can I perform unit testing on the setting of a role in the IamInstanceProfile with cdktf?

asked 2022-08-08 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-12-29 01:00:00 +0000

lakamha gravatar image

To perform unit testing on the setting of a role in the IamInstanceProfile with cdktf, you can create a mock IamInstanceProfile resource using the "mockedIamInstanceProfile" function provided by the cdktf-testing library. Then, you can set the expected Role using the "withRole" function provided by the mock resource. Finally, you can use the "expect" function to compare the actual and expected values. Here is an example:

import { mockedIamInstanceProfile } from 'cdktf-testing';
import { MyStack } from './my-stack';

describe('MyStack', () => {
  it('should create IamInstanceProfile with role', () => {
    const stack = new MyStack();

    const expectedRole = 'my-role';

    const profile = mockedIamInstanceProfile({
      withRole: expectedRole
    });

    stack.createIamInstanceProfile(profile);

    expect(stack).toHaveResourceLike('AWS::IAM::InstanceProfile', {
      Roles: [
        { Ref: 'MyRole' }
      ]
    });
  });
});

In this example, "MyStack" is the stack that creates the IamInstanceProfile resource. The "createIamInstanceProfile" function is called with the mock resource as an argument. Then, the "toHaveResourceLike" function from the "jest-cdk/assertions" library is used to check that the actual Roles value of the IamInstanceProfile resource matches the expected value. If the actual value does not match the expected value, the test will fail.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-08-08 11:00:00 +0000

Seen: 1 times

Last updated: Dec 29 '22