AWS IoT Device SDK C: Fleet Provisioning
AWS IoT Fleet Provisioning
Return to main page ↑
Tests

Tests written for the Provisioning library.

The Provisioning tests reside in the provisioning/test directory. They are divided into the following subdirectories:

  • system: Provisioning system tests. These tests require a network connection and AWS IoT credentials. They also need an AWS account and a registered Provisioning Template in the account; see Setting up Provisioning system tests for instructions to configure an AWS account to run these tests. The command line option -n may be passed to the test executable to disable these tests.
  • unit: Provisioning unit tests. These tests do not require a network connection or credentials. These tests use the MQTT mocks.

See Test Configuration for configuration settings that change the behavior of the Provisioning system tests. The Provisioning unit tests require no special configuration.

The current Provisioning tests use the Unity test framework. See Building and running the tests for a guide on running the tests.

Setting up Provisioning system tests

How to set up the Provisioning system tests.

Precondition
The steps below assume basic familiarity with AWS and AWS CLI.

The Provisioning system tests require a Provisioning Template to be created with an AWS account. AWS does not provide the functionality to create a fleet provisioning template using the Device API; therefore, a fleet provisioning template will have to be created using another method.

See Additional Travis CI setup for Jobs and Provisioning system tests for additional setup for running the Provisioning tests on Travis CI.

  1. Create an IAM user for the tests. This needs to be done only once.
    Follow this guide to create an IAM user for the Provisioning tests.
    • Only programmatic access is required.
    • Save the access key and secret access key for this user.
    • The following policy grants the necessary permissions for this user. Replace <region> and <account> with your AWS region and account number, respectively; replace <TemplateName> with the name of the Provisioning Template that will be created in the next step (step 2).
      {
      "Version": "2012-10-17",
      "Statement": {
      "Effect": "Allow",
      "Action": [
      "iot:CreateProvisioningTemplate",
      "iot:UpdateProvisioningTemplate",
      "iot:DescribeProvisioningTemplate",
      "iot:DeleteProvisioningTemplate",
      "iot:ListProvisioningTemplates"
      ],
      "Resource": [
      "arn:aws:iot:<region>:<account>:provisioningtemplate/*",
      "arn:aws:iot:<region>:<account>:service-role/*"
      ]
      }
      }
  2. Create an IAM role that will be needed by a fleet provisioning template. Replace RoleName with a name of the role you want to create.
    aws iam create-role \
    --role-name <RoleName> \
    --assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Action":"sts:AssumeRole","Effect":"Allow","Principal":{"Service":"iot.amazonaws.com"}}]}'
  3. Attach a policy to the role created in step2. Replace RoleName with the name of the role you created in Step 2.
    aws iam attach-role-policy \
    --role-name <RoleName> \
    --policy-arn arn:aws:iam::aws:policy/service-role/AWSIoTThingsRegistration
  4. Create the template resource which will be used for provisioning the demo application. This needs to be done only once.
    To create a template, the following AWS CLI command may be used. Replace TemplateName with the name of the fleet provisioning template you want to create. Replace RoleName with the name of the role you created in Step 2. Replace TemplateJSON with the template body as a JSON string (containing escape characters). Replace account with your AWS account number.
    aws iot create-provisioning-template \
    --template-name <TemplateName> \
    --provisioning-role-arn arn:aws:iam::<account>:service-role/<RoleName> \
    --template-body "<TemplateJSON>" \
    --enabled
    For more information on fleet provisioning template, refer to this guide Following is an example template JSON string (that uses "SerialNumber" for Thing resource name and accepts an additional "DeviceLocation" parameter) which can be viewed on AWS IoT Console after the AWS CLI command using it is executed
    {\"Parameters\":{\"DeviceLocation\":{\"Type\":\"String\"},\"AWS::IoT::Certificate::Id\":{\"Type\":\"String\"},\"SerialNumber\":{\"Type\":\"String\"}},\"Mappings\":{\"LocationTable\":{\"Seattle\":{\"LocationUrl\":\"https://example.aws\"}}},\"Resources\":{\"thing\":{\"Type\":\"AWS::IoT::Thing\",\"Properties\":{\"ThingName\":{\"Fn::Join\":[\"\",[\"ThingPrefix_\",{\"Ref\":\"SerialNumber\"}]]},\"AttributePayload\":{\"version\":\"v1\",\"serialNumber\":\"serialNumber\"}},\"OverrideSettings\":{\"AttributePayload\":\"MERGE\",\"ThingTypeName\":\"REPLACE\",\"ThingGroups\":\"DO_NOTHING\"}},\"certificate\":{\"Type\":\"AWS::IoT::Certificate\",\"Properties\":{\"CertificateId\":{\"Ref\":\"AWS::IoT::Certificate::Id\"},\"Status\":\"Active\"},\"OverrideSettings\":{\"Status\":\"REPLACE\"}},\"policy\":{\"Type\":\"AWS::IoT::Policy\",\"Properties\":{\"PolicyDocument\":{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":[\"iot:Connect\",\"iot:Subscribe\",\"iot:Publish\",\"iot:Receive\"],\"Resource\":\"*\"}]}}}},\"DeviceConfiguration\":{\"FallbackUrl\":\"https://www.example.com/test-site\",\"LocationUrl\":{\"Fn::FindInMap\":[\"LocationTable\",{\"Ref\":\"DeviceLocation\"},\"LocationUrl\"]}}}
    You can query the created template using the following CLI command. Replace the TemplateName with the fleet provisioning template you created.
    aws iot describe-provisioning-template --template-name <TemplateName>
  5. Set the name of provisioning template, created in step 4, in the config file for the AWS_IOT_TEST_PROVISIONING_TEMPLATE_NAME macro. Build the tests for the aws_iot_tests_provisioning target.
  6. When the Provisioning tests are complete, the provisioning template it used is no longer needed. Provisioning templates can be deleted with the following command, where TemplateName is the name of the template that is to be deleted. Replace the <TemplateName> with the name of the provisioning template created in Step 4.
    aws iot delete-provisioning-template --template-name <TemplateName>