Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 173 additions & 0 deletions test/windows/wslc/e2e/WSLCE2EContainerAttachTests.cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*++

Copyright (c) Microsoft. All rights reserved.

Module Name:

WSLCE2EContainerAttachTests.cpp

Abstract:

This file contains end-to-end tests for WSLC container attach command.
--*/

#include "precomp.h"
#include "windows/Common.h"
#include "WSLCExecutor.h"
#include "WSLCE2EHelpers.h"

namespace WSLCE2ETests {

class WSLCE2EContainerAttachTests
{
WSLC_TEST_CLASS(WSLCE2EContainerAttachTests)

TEST_CLASS_SETUP(ClassSetup)
{
EnsureImageIsLoaded(DebianImage);
return true;
}

TEST_CLASS_CLEANUP(ClassCleanup)
{
EnsureContainerDoesNotExist(WslcContainerName);
EnsureImageIsDeleted(DebianImage);
return true;
}

TEST_METHOD_SETUP(TestMethodSetup)
{
EnsureContainerDoesNotExist(WslcContainerName);
return true;
}

TEST_METHOD(WSLCE2E_Container_Attach_HelpCommand)
{
WSL2_TEST_ONLY();

auto result = RunWslc(L"container attach --help");
result.Verify({.Stdout = GetHelpMessage(), .Stderr = L"", .ExitCode = 0});
}

TEST_METHOD(WSLCE2E_Container_Attach_TTY)
{
WSL2_TEST_ONLY();
VerifyContainerIsNotListed(WslcContainerName);

const auto& prompt = ">";
auto result = RunWslc(std::format(
L"container run -itd -e PS1={} --name {} {} bash --norc", prompt, WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = S_OK});
auto containerId = result.GetStdoutOneLine();

const auto& expectedAttachPrompt = VT::BuildContainerAttachPrompt(prompt);
const auto& expectedPrompt = VT::BuildContainerPrompt(prompt);

auto session = RunWslcInteractive(std::format(L"container attach {}", containerId));
VERIFY_IS_TRUE(session.IsRunning(), L"Container session should be running");

// The container attach prompt appears twice.
session.ExpectStdout(expectedAttachPrompt);
session.ExpectStdout(expectedAttachPrompt);

session.WriteLine("echo hello");
session.ExpectCommandEcho("echo hello");
session.ExpectStdout("hello\r\n");
session.ExpectStdout(expectedPrompt);

session.WriteLine("whoami");
session.ExpectCommandEcho("whoami");
session.ExpectStdout("root\r\n");
session.ExpectStdout(expectedPrompt);

session.ExitAndVerifyNoErrors();
auto exitCode = session.Wait();
VERIFY_ARE_EQUAL(0, exitCode);
}

TEST_METHOD(WSLCE2E_Container_Attach_NoTTY)
{
WSL2_TEST_ONLY();
VerifyContainerIsNotListed(WslcContainerName);
auto result = RunWslc(std::format(L"container run -id --name {} {} cat", WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = S_OK});
auto containerId = result.GetStdoutOneLine();

auto session = RunWslcInteractive(std::format(L"container attach {}", containerId));
VERIFY_IS_TRUE(session.IsRunning(), L"Container session should be running");

session.WriteLine("test line 1");
session.ExpectStdout("test line 1\n");
session.WriteLine("test line 2");
session.ExpectStdout("test line 2\n");

// Close stdin to signal EOF to cat
session.CloseStdin();

// Wait for cat to exit with code 0
auto exitCode = session.Wait(10000);
VERIFY_ARE_EQUAL(0, exitCode, L"Cat should exit with code 0 after receiving EOF");
session.VerifyNoErrors();
}

TEST_METHOD(WSLCE2E_Container_Attach_MissingContainerId)
{
WSL2_TEST_ONLY();

auto result = RunWslc(L"container attach");
result.Verify({.Stdout = GetHelpMessage(), .Stderr = L"Required argument not provided: 'container-id'\r\n", .ExitCode = 1});
}

TEST_METHOD(WSLCE2E_Container_Attach_ContainerNotFound)
{
WSL2_TEST_ONLY();

auto result = RunWslc(std::format(L"container attach {}", WslcContainerName));
result.Verify({.ExitCode = 1});
}

private:
const std::wstring WslcContainerName = L"wslc-test-container";
const TestImage& DebianImage = DebianTestImage();

std::wstring GetHelpMessage() const
{
std::wstringstream output;
output << GetWslcHeader() //
<< GetDescription() //
<< GetUsage() //
<< GetAvailableCommands() //
<< GetAvailableOptions();
return output.str();
}

std::wstring GetDescription() const
{
return L"Attaches to a container.\r\n\r\n";
}

std::wstring GetUsage() const
{
return L"Usage: wslc container attach [<options>] <container-id>\r\n\r\n";
}

std::wstring GetAvailableCommands() const
{
std::wstringstream commands;
commands << L"The following arguments are available:\r\n" //
<< L" container-id Container ID\r\n" //
<< L"\r\n";
return commands.str();
}

std::wstring GetAvailableOptions() const
{
std::wstringstream options;
options << L"The following options are available:\r\n" //
<< L" --session Specify the session to use\r\n" //
<< L" -h,--help Shows help about the selected command\r\n"
<< L"\r\n";
return options.str();
}
};
} // namespace WSLCE2ETests
Loading
Loading