|
| 1 | +// aws-greengrass-component-sdk - Lightweight AWS IoT Greengrass SDK |
| 2 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +#include <gg/ipc/client.hpp> |
| 6 | +#include <gg/object.hpp> |
| 7 | +#include <cstddef> |
| 8 | +#include <cstdint> |
| 9 | +#include <array> |
| 10 | +#include <span> |
| 11 | +#include <string_view> |
| 12 | +#include <system_error> |
| 13 | + |
| 14 | +extern "C" { |
| 15 | +#include <gg/buffer.h> |
| 16 | +#include <gg/ipc/mock.h> |
| 17 | +#include <gg/ipc/packet_sequences.h> |
| 18 | +#include <gg/map.h> |
| 19 | +#include <gg/object.h> |
| 20 | +#include <gg/test.h> |
| 21 | +#include <sys/types.h> |
| 22 | +#include <unistd.h> |
| 23 | +#include <unity.h> |
| 24 | + |
| 25 | +GgError gg_process_wait(pid_t pid) noexcept; |
| 26 | +} |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +// Build { "aws.greengrass.NucleusLite": { "merge": { merge_key: merge_val } } } |
| 31 | +// programmatically. C compound-literal macros (GG_MAP/gg_kv) don't compile |
| 32 | +// cleanly in C++, so we construct the nested structure with direct calls. |
| 33 | +GgMap build_nucleus_merge_map( |
| 34 | + GgBuffer merge_key, |
| 35 | + GgBuffer merge_val, |
| 36 | + GgKV *kv_storage, |
| 37 | + GgObject *obj_storage |
| 38 | +) { |
| 39 | + // Innermost: { merge_key: merge_val } |
| 40 | + kv_storage[0] = gg_kv(merge_key, gg_obj_buf(merge_val)); |
| 41 | + obj_storage[0] = gg_obj_map(GgMap { .pairs = &kv_storage[0], .len = 1 }); |
| 42 | + |
| 43 | + // Middle: { "merge": <inner> } |
| 44 | + kv_storage[1] = gg_kv(GG_STR("merge"), obj_storage[0]); |
| 45 | + obj_storage[1] = gg_obj_map(GgMap { .pairs = &kv_storage[1], .len = 1 }); |
| 46 | + |
| 47 | + // Outer: { "aws.greengrass.NucleusLite": <middle> } |
| 48 | + kv_storage[2] = gg_kv(GG_STR("aws.greengrass.NucleusLite"), obj_storage[1]); |
| 49 | + return GgMap { .pairs = &kv_storage[2], .len = 1 }; |
| 50 | +} |
| 51 | + |
| 52 | +} // namespace |
| 53 | + |
| 54 | +GG_TEST_DEFINE(cpp_create_local_deployment_okay) { |
| 55 | + GgBuffer expected_id = GG_STR("cpp-deploy-id-123"); |
| 56 | + |
| 57 | + GgKV kv_storage[3]; |
| 58 | + GgObject obj_storage[2]; |
| 59 | + GgMap component_to_config = build_nucleus_merge_map( |
| 60 | + GG_STR("url"), GG_STR("http://127.0.0.1:3129"), kv_storage, obj_storage |
| 61 | + ); |
| 62 | + |
| 63 | + std::array<std::byte, 64> id_mem {}; |
| 64 | + |
| 65 | + pid_t pid = fork(); |
| 66 | + TEST_ASSERT_TRUE_MESSAGE(pid >= 0, "fork failed"); |
| 67 | + |
| 68 | + if (pid == 0) { |
| 69 | + auto &client = gg::ipc::Client::get(); |
| 70 | + GG_TEST_ASSERT_OK(client.connect().value()); |
| 71 | + |
| 72 | + GgCreateLocalDeploymentArgs args = {}; |
| 73 | + args.component_to_configuration = component_to_config; |
| 74 | + |
| 75 | + std::string_view deployment_id; |
| 76 | + GG_TEST_ASSERT_OK(client |
| 77 | + .create_local_deployment( |
| 78 | + args, std::span(id_mem), &deployment_id |
| 79 | + ) |
| 80 | + .value()); |
| 81 | + |
| 82 | + TEST_ASSERT_EQUAL_STRING_LEN( |
| 83 | + "cpp-deploy-id-123", deployment_id.data(), deployment_id.size() |
| 84 | + ); |
| 85 | + |
| 86 | + TEST_PASS(); |
| 87 | + } |
| 88 | + |
| 89 | + GG_TEST_ASSERT_OK(gg_test_connect_request_disconnect_sequence( |
| 90 | + gg_test_create_local_deployment_accepted_sequence( |
| 91 | + 1, gg_obj_map(component_to_config), expected_id |
| 92 | + ), |
| 93 | + 5 |
| 94 | + )); |
| 95 | + |
| 96 | + GG_TEST_ASSERT_OK(gg_process_wait(pid)); |
| 97 | +} |
| 98 | + |
| 99 | +GG_TEST_DEFINE(cpp_create_local_deployment_no_output) { |
| 100 | + GgKV kv_storage[3]; |
| 101 | + GgObject obj_storage[2]; |
| 102 | + GgMap component_to_config = build_nucleus_merge_map( |
| 103 | + GG_STR("url"), GG_STR("http://127.0.0.1:3129"), kv_storage, obj_storage |
| 104 | + ); |
| 105 | + |
| 106 | + pid_t pid = fork(); |
| 107 | + TEST_ASSERT_TRUE_MESSAGE(pid >= 0, "fork failed"); |
| 108 | + |
| 109 | + if (pid == 0) { |
| 110 | + auto &client = gg::ipc::Client::get(); |
| 111 | + GG_TEST_ASSERT_OK(client.connect().value()); |
| 112 | + |
| 113 | + GgCreateLocalDeploymentArgs args = {}; |
| 114 | + args.component_to_configuration = component_to_config; |
| 115 | + |
| 116 | + GG_TEST_ASSERT_OK(client.create_local_deployment(args).value()); |
| 117 | + |
| 118 | + TEST_PASS(); |
| 119 | + } |
| 120 | + |
| 121 | + GG_TEST_ASSERT_OK(gg_test_connect_request_disconnect_sequence( |
| 122 | + gg_test_create_local_deployment_accepted_sequence( |
| 123 | + 1, gg_obj_map(component_to_config), GG_STR("any-id") |
| 124 | + ), |
| 125 | + 5 |
| 126 | + )); |
| 127 | + |
| 128 | + GG_TEST_ASSERT_OK(gg_process_wait(pid)); |
| 129 | +} |
0 commit comments