| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | WSLCE2EContainerExportTests.cpp |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains end-to-end tests for WSLC container export. |
| 12 | --*/ |
| 13 | |
| 14 | #include "precomp.h" |
| 15 | #include "windows/Common.h" |
| 16 | #include "WSLCExecutor.h" |
| 17 | #include "WSLCE2EHelpers.h" |
| 18 | #include "TestImageRegistry.h" |
| 19 | |
| 20 | namespace WSLCE2ETests { |
| 21 | using namespace wsl::shared; |
| 22 | |
| 23 | class WSLCE2EContainerExportTests |
| 24 | { |
| 25 | WSLC_TEST_CLASS(WSLCE2EContainerExportTests) |
| 26 | |
| 27 | TEST_CLASS_SETUP(ClassSetup) |
| 28 | { |
| 29 | TestImageRegistry::Instance().EnsureLoaded(DebianImage); |
| 30 | return true; |
| 31 | } |
| 32 | |
| 33 | TEST_CLASS_CLEANUP(ClassCleanup) |
| 34 | { |
| 35 | EnsureContainerDoesNotExist(WslcContainerName); |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | TEST_METHOD_SETUP(MethodSetup) |
| 40 | { |
| 41 | EnsureContainerDoesNotExist(WslcContainerName); |
| 42 | ExportPath = wsl::windows::common::filesystem::GetTempFilename(); |
| 43 | DeleteFileW(ExportPath.c_str()); |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | TEST_METHOD_CLEANUP(MethodCleanup) |
| 48 | { |
| 49 | EnsureContainerDoesNotExist(WslcContainerName); |
| 50 | DeleteFileW(ExportPath.c_str()); |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | WSLC_TEST_METHOD(WSLCE2E_Container_Export_HelpCommand) |
| 55 | { |
| 56 | auto result = RunWslc(L"container export --help"); |
| 57 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 58 | VERIFY_IS_FALSE(result.Stdout.value().empty()); |
| 59 | } |
| 60 | |
| 61 | WSLC_TEST_METHOD(WSLCE2E_Container_Export_MissingContainerId) |
| 62 | { |
| 63 | const auto result = RunWslc(std::format(L"container export --output \"{}\"", ExportPath.wstring())); |
| 64 | result.Verify({.Stdout = L"", .ExitCode = 1}); |
| 65 | VERIFY_IS_TRUE(result.StderrContainsSubstring(L"Required argument not provided: 'container-id'")); |
| 66 | } |
| 67 | |
| 68 | WSLC_TEST_METHOD(WSLCE2E_Container_Export_ContainerNotFound) |
| 69 | { |
| 70 | const auto result = RunWslc(std::format(L"container export --output \"{}\" {}", ExportPath.wstring(), InvalidContainerName)); |
| 71 | VERIFY_IS_TRUE(result.ExitCode.has_value()); |
| 72 | VERIFY_ARE_EQUAL(1u, result.ExitCode.value()); |
| 73 | VERIFY_IS_TRUE(result.Stderr.has_value()); |
| 74 | VERIFY_ARE_NOT_EQUAL(0u, result.Stderr.value().size()); |
| 75 | } |
| 76 | |
| 77 | WSLC_TEST_METHOD(WSLCE2E_Container_Export_ToFile_Success) |
| 78 | { |
| 79 | // Create a stopped container so it has a filesystem to export. |
| 80 | const auto createResult = RunWslc(std::format(L"container create --name {} {}", WslcContainerName, DebianImage.NameAndTag())); |
| 81 | createResult.Verify({.Stderr = L"", .ExitCode = 0}); |
| 82 | |
| 83 | const auto result = RunWslc(std::format(L"container export --output \"{}\" {}", ExportPath.wstring(), WslcContainerName)); |
| 84 | result.Verify({.Stdout = L"", .Stderr = L"", .ExitCode = 0}); |
| 85 | |
| 86 | VERIFY_IS_TRUE(std::filesystem::exists(ExportPath)); |
| 87 | VERIFY_ARE_NOT_EQUAL(0u, std::filesystem::file_size(ExportPath)); |
| 88 | } |
| 89 | |
| 90 | WSLC_TEST_METHOD(WSLCE2E_Container_Export_ToStdout_Success) |
| 91 | { |
| 92 | const auto createResult = RunWslc(std::format(L"container create --name {} {}", WslcContainerName, DebianImage.NameAndTag())); |
| 93 | createResult.Verify({.Stderr = L"", .ExitCode = 0}); |
| 94 | |
| 95 | const auto result = RunWslcAndRedirectToFile(std::format(L"container export {}", WslcContainerName), ExportPath); |
| 96 | result.Verify({.Stdout = L"", .Stderr = L"", .ExitCode = 0}); |
| 97 | |
| 98 | VERIFY_IS_TRUE(std::filesystem::exists(ExportPath)); |
| 99 | VERIFY_ARE_NOT_EQUAL(0u, std::filesystem::file_size(ExportPath)); |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | const std::wstring WslcContainerName = L"wslc-test-container-export"; |
| 104 | const std::wstring InvalidContainerName = L"wslc-nonexistent-container-for-export"; |
| 105 | const TestImage& DebianImage = DebianTestImage(); |
| 106 | |
| 107 | std::filesystem::path ExportPath{}; |
| 108 | }; |
| 109 | } // namespace WSLCE2ETests |