| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | WSLCE2EContainerInspectTests.cpp |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains end-to-end tests for WSLC. |
| 12 | --*/ |
| 13 | |
| 14 | #include "precomp.h" |
| 15 | #include "windows/Common.h" |
| 16 | #include "WSLCExecutor.h" |
| 17 | #include "WSLCE2EHelpers.h" |
| 18 | #include "TestImageRegistry.h" |
| 19 | #include <wslc_schema.h> |
| 20 | |
| 21 | namespace WSLCE2ETests { |
| 22 | using namespace wsl::shared; |
| 23 | using namespace wsl::shared::string; |
| 24 | |
| 25 | class WSLCE2EContainerInspectTests |
| 26 | { |
| 27 | WSLC_TEST_CLASS(WSLCE2EContainerInspectTests) |
| 28 | |
| 29 | TEST_CLASS_SETUP(ClassSetup) |
| 30 | { |
| 31 | TestImageRegistry::Instance().EnsureLoaded(DebianImage); |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | TEST_CLASS_CLEANUP(ClassCleanup) |
| 36 | { |
| 37 | EnsureContainerDoesNotExist(TestContainerName1); |
| 38 | EnsureContainerDoesNotExist(TestContainerName2); |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | TEST_METHOD_SETUP(MethodSetup) |
| 43 | { |
| 44 | EnsureContainerDoesNotExist(TestContainerName1); |
| 45 | EnsureContainerDoesNotExist(TestContainerName2); |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | WSLC_TEST_METHOD(WSLCE2E_Container_Inspect_HelpCommand) |
| 50 | { |
| 51 | auto result = RunWslc(L"container inspect --help"); |
| 52 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 53 | VERIFY_IS_FALSE(result.Stdout.value().empty()); |
| 54 | } |
| 55 | |
| 56 | WSLC_TEST_METHOD(WSLCE2E_Container_Inspect_MissingContainerId) |
| 57 | { |
| 58 | auto result = RunWslc(L"container inspect"); |
| 59 | result.Verify({.Stdout = L"", .ExitCode = 1}); |
| 60 | VERIFY_IS_TRUE(result.StderrContainsSubstring(L"Required argument not provided: 'container-id'")); |
| 61 | } |
| 62 | |
| 63 | WSLC_TEST_METHOD(WSLCE2E_Container_Inspect_ContainerNotFound) |
| 64 | { |
| 65 | auto result = RunWslc(std::format(L"container inspect {}", TestContainerName1)); |
| 66 | result.Verify({.Stdout = L"[]\r\n", .Stderr = std::format(L"Container '{}' not found.\r\n", TestContainerName1), .ExitCode = 1}); |
| 67 | } |
| 68 | |
| 69 | WSLC_TEST_METHOD(WSLCE2E_Container_Inspect_Success) |
| 70 | { |
| 71 | auto createResult = RunWslc(std::format(L"container create --name {} {}", TestContainerName1, DebianImage.NameAndTag())); |
| 72 | createResult.Verify({.Stderr = L"", .ExitCode = 0}); |
| 73 | |
| 74 | auto result = RunWslc(std::format(L"container inspect {}", TestContainerName1)); |
| 75 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 76 | auto inspectData = |
| 77 | wsl::shared::FromJson<std::vector<wsl::windows::common::wslc_schema::InspectContainer>>(result.Stdout.value().c_str()); |
| 78 | VERIFY_ARE_EQUAL(1u, inspectData.size()); |
| 79 | VERIFY_ARE_EQUAL("/" + WideToMultiByte(TestContainerName1), inspectData[0].Name); |
| 80 | } |
| 81 | |
| 82 | WSLC_TEST_METHOD(WSLCE2E_Container_Inspect_SizeOption) |
| 83 | { |
| 84 | auto createResult = RunWslc(std::format(L"container create --name {} {}", TestContainerName1, DebianImage.NameAndTag())); |
| 85 | createResult.Verify({.Stderr = L"", .ExitCode = 0}); |
| 86 | |
| 87 | // Without --size the document must not carry the size fields. |
| 88 | auto plain = RunWslc(std::format(L"container inspect {}", TestContainerName1)); |
| 89 | plain.Verify({.Stderr = L"", .ExitCode = 0}); |
| 90 | auto plainDocument = nlohmann::json::parse(WideToMultiByte(plain.Stdout.value())); |
| 91 | VERIFY_ARE_EQUAL(1u, plainDocument.size()); |
| 92 | VERIFY_IS_FALSE(plainDocument[0].contains("SizeRw")); |
| 93 | VERIFY_IS_FALSE(plainDocument[0].contains("SizeRootFs")); |
| 94 | |
| 95 | const auto verifySized = [&](const std::wstring& command) { |
| 96 | auto result = RunWslc(command); |
| 97 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 98 | |
| 99 | auto document = nlohmann::json::parse(WideToMultiByte(result.Stdout.value())); |
| 100 | VERIFY_ARE_EQUAL(1u, document.size()); |
| 101 | VERIFY_IS_TRUE(document[0].contains("SizeRw")); |
| 102 | VERIFY_IS_TRUE(document[0].contains("SizeRootFs")); |
| 103 | VERIFY_IS_TRUE(document[0]["SizeRw"].is_number()); |
| 104 | VERIFY_IS_TRUE(document[0]["SizeRootFs"].is_number()); |
| 105 | |
| 106 | // The image layers always account for more than nothing. |
| 107 | VERIFY_IS_GREATER_THAN(document[0]["SizeRootFs"].get<int64_t>(), static_cast<int64_t>(0)); |
| 108 | }; |
| 109 | |
| 110 | verifySized(std::format(L"container inspect --size {}", TestContainerName1)); |
| 111 | verifySized(std::format(L"inspect --size {}", TestContainerName1)); |
| 112 | verifySized(std::format(L"inspect --size --type container {}", TestContainerName1)); |
| 113 | } |
| 114 | |
| 115 | WSLC_TEST_METHOD(WSLCE2E_Container_Inspect_SizeOption_ListedInHelp) |
| 116 | { |
| 117 | auto result = RunWslc(L"container inspect --help"); |
| 118 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 119 | VERIFY_IS_TRUE(result.StdoutContainsSubstring(L"--size")); |
| 120 | VERIFY_IS_TRUE(result.StdoutContainsSubstring(L"Display total file sizes if the type is container")); |
| 121 | |
| 122 | result = RunWslc(L"inspect --help"); |
| 123 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 124 | VERIFY_IS_TRUE(result.StdoutContainsSubstring(L"--size")); |
| 125 | } |
| 126 | |
| 127 | WSLC_TEST_METHOD(WSLCE2E_Container_Inspect_FormatJson_IsSingleLine) |
| 128 | { |
| 129 | auto createResult = RunWslc(std::format(L"container create --name {} {}", TestContainerName1, DebianImage.NameAndTag())); |
| 130 | createResult.Verify({.Stderr = L"", .ExitCode = 0}); |
| 131 | |
| 132 | auto result = RunWslc(std::format(L"container inspect --format json {}", TestContainerName1)); |
| 133 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 134 | |
| 135 | const auto document = VerifyCompactJsonOutput(result); |
| 136 | VERIFY_IS_TRUE(document.is_array()); |
| 137 | VERIFY_ARE_EQUAL(1u, document.size()); |
| 138 | VERIFY_ARE_EQUAL("/" + WideToMultiByte(TestContainerName1), document[0]["Name"].get<std::string>()); |
| 139 | } |
| 140 | |
| 141 | WSLC_TEST_METHOD(WSLCE2E_Container_InspectMultiple_Success) |
| 142 | { |
| 143 | // Create two containers to inspect at the same time |
| 144 | auto result = RunWslc(std::format(L"container create --name {} {}", TestContainerName1, DebianImage.NameAndTag())); |
| 145 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 146 | result = RunWslc(std::format(L"container create --name {} {}", TestContainerName2, DebianImage.NameAndTag())); |
| 147 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 148 | |
| 149 | // Inspect both containers in the same command |
| 150 | result = RunWslc(std::format(L"container inspect {} {}", TestContainerName1, TestContainerName2)); |
| 151 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 152 | auto inspectData = |
| 153 | wsl::shared::FromJson<std::vector<wsl::windows::common::wslc_schema::InspectContainer>>(result.Stdout.value().c_str()); |
| 154 | VERIFY_ARE_EQUAL(2u, inspectData.size()); |
| 155 | VERIFY_ARE_EQUAL("/" + WideToMultiByte(TestContainerName1), inspectData[0].Name); |
| 156 | VERIFY_ARE_EQUAL("/" + WideToMultiByte(TestContainerName2), inspectData[1].Name); |
| 157 | } |
| 158 | |
| 159 | WSLC_TEST_METHOD(WSLCE2E_Container_Inspect_MixedFoundNotFound) |
| 160 | { |
| 161 | // Create one container but not the other |
| 162 | auto result = RunWslc(std::format(L"container create --name {} {}", TestContainerName1, DebianImage.NameAndTag())); |
| 163 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 164 | |
| 165 | // Inspect both containers in the same command, expecting one to be found and the other to not be found |
| 166 | result = RunWslc(std::format(L"container inspect {} {}", TestContainerName1, TestContainerName2)); |
| 167 | result.Verify({.Stderr = std::format(L"Container '{}' not found.\r\n", TestContainerName2), .ExitCode = 1}); |
| 168 | |
| 169 | // Verify found container |
| 170 | auto inspectData = |
| 171 | wsl::shared::FromJson<std::vector<wsl::windows::common::wslc_schema::InspectContainer>>(result.Stdout.value().c_str()); |
| 172 | VERIFY_ARE_EQUAL(1u, inspectData.size()); |
| 173 | VERIFY_ARE_EQUAL("/" + WideToMultiByte(TestContainerName1), inspectData[0].Name); |
| 174 | } |
| 175 | |
| 176 | private: |
| 177 | const std::wstring TestContainerName1 = L"wslc-e2e-container-inspect-1"; |
| 178 | const std::wstring TestContainerName2 = L"wslc-e2e-container-inspect-2"; |
| 179 | const TestImage& DebianImage = DebianTestImage(); |
| 180 | }; |
| 181 | } // namespace WSLCE2ETests |