| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | WSLCE2EImageDeleteTests.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 | |
| 20 | namespace WSLCE2ETests { |
| 21 | using namespace wsl::shared; |
| 22 | |
| 23 | class WSLCE2EImageDeleteTests |
| 24 | { |
| 25 | WSLC_TEST_CLASS(WSLCE2EImageDeleteTests) |
| 26 | |
| 27 | TEST_METHOD_SETUP(MethodSetup) |
| 28 | { |
| 29 | EnsureContainerDoesNotExist(WslcContainerName); |
| 30 | TestImageRegistry::Instance().Delete(DebianImage); |
| 31 | TestImageRegistry::Instance().Delete(AlpineImage); |
| 32 | TestImageRegistry::Instance().Delete(NoPruneTaggedImage); |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | TEST_CLASS_CLEANUP(ClassCleanup) |
| 37 | { |
| 38 | EnsureContainerDoesNotExist(WslcContainerName); |
| 39 | TestImageRegistry::Instance().Delete(DebianImage); |
| 40 | TestImageRegistry::Instance().Delete(AlpineImage); |
| 41 | TestImageRegistry::Instance().Delete(NoPruneTaggedImage); |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | WSLC_TEST_METHOD(WSLCE2E_Image_Delete_HelpCommand) |
| 46 | { |
| 47 | auto result = RunWslc(L"image delete --help"); |
| 48 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 49 | VERIFY_IS_FALSE(result.Stdout.value().empty()); |
| 50 | } |
| 51 | |
| 52 | WSLC_TEST_METHOD(WSLCE2E_Image_Delete_ImageNotFound) |
| 53 | { |
| 54 | auto result = RunWslc(std::format(L"image delete {}", InvalidImage.Name)); |
| 55 | auto errorMessage = |
| 56 | FormatErrorMessage(std::format(L"No such image: {}", InvalidImage.NameAndTag()), L"WSLC_E_IMAGE_NOT_FOUND"); |
| 57 | result.Verify({.Stdout = L"", .Stderr = errorMessage, .ExitCode = 1}); |
| 58 | } |
| 59 | |
| 60 | WSLC_TEST_METHOD(WSLCE2E_Image_Delete_MissingImageName) |
| 61 | { |
| 62 | auto result = RunWslc(L"image delete"); |
| 63 | result.Verify({.Stdout = L"", .ExitCode = 1}); |
| 64 | VERIFY_IS_TRUE(result.StderrContainsSubstring(L"Required argument not provided: 'image'")); |
| 65 | } |
| 66 | |
| 67 | WSLC_TEST_METHOD(WSLCE2E_Image_Delete_UnusedImage_Success) |
| 68 | { |
| 69 | TestImageRegistry::Instance().EnsureLoaded(DebianImage); |
| 70 | VerifyImageIsNotUsed(DebianImage); |
| 71 | |
| 72 | auto result = RunWslc(std::format(L"image delete {}", DebianImage.Name)); |
| 73 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 74 | VERIFY_IS_TRUE(result.StdoutContainsSubstring(std::format(L"Untagged: {}", DebianImage.NameAndTag()))); |
| 75 | VERIFY_IS_TRUE(result.StdoutContainsSubstring(L"Deleted: sha256:")); |
| 76 | } |
| 77 | |
| 78 | WSLC_TEST_METHOD(WSLCE2E_Image_Delete_MultipleUnusedImages_Success) |
| 79 | { |
| 80 | TestImageRegistry::Instance().EnsureLoaded(DebianImage); |
| 81 | TestImageRegistry::Instance().EnsureLoaded(AlpineImage); |
| 82 | VerifyImageIsNotUsed(DebianImage); |
| 83 | VerifyImageIsNotUsed(AlpineImage); |
| 84 | |
| 85 | auto result = RunWslc(std::format(L"image delete {} {}", DebianImage.Name, AlpineImage.Name)); |
| 86 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 87 | VERIFY_IS_TRUE(result.StdoutContainsSubstring(std::format(L"Untagged: {}", DebianImage.NameAndTag()))); |
| 88 | VERIFY_IS_TRUE(result.StdoutContainsSubstring(std::format(L"Untagged: {}", AlpineImage.NameAndTag()))); |
| 89 | } |
| 90 | |
| 91 | WSLC_TEST_METHOD(WSLCE2E_Image_Delete_UsedImage_Failure) |
| 92 | { |
| 93 | TestImageRegistry::Instance().EnsureLoaded(DebianImage); |
| 94 | VerifyImageIsNotUsed(DebianImage); |
| 95 | |
| 96 | auto createResult = RunWslc(std::format(L"container create --name {} {}", WslcContainerName, DebianImage.NameAndTag())); |
| 97 | createResult.Verify({.Stderr = L"", .ExitCode = 0}); |
| 98 | |
| 99 | VerifyImageIsUsed(DebianImage); |
| 100 | |
| 101 | auto inspectContainer = InspectContainer(WslcContainerName); |
| 102 | auto containerId = GetHashId(inspectContainer.Id); |
| 103 | auto inspectImage = InspectImage(DebianImage.NameAndTag()); |
| 104 | auto imageId = GetHashId(inspectImage.Id); |
| 105 | |
| 106 | auto result = RunWslc(std::format(L"image delete {}", DebianImage.Name)); |
| 107 | auto errorMessage = FormatErrorMessage( |
| 108 | std::format( |
| 109 | L"conflict: unable to remove repository reference \"{}\" (must force) - container {} is using its referenced " |
| 110 | L"image {}", |
| 111 | DebianImage.Name, |
| 112 | containerId, |
| 113 | imageId), |
| 114 | L"ERROR_SHARING_VIOLATION"); |
| 115 | result.Verify({.Stdout = L"", .Stderr = errorMessage, .ExitCode = 1}); |
| 116 | } |
| 117 | |
| 118 | WSLC_TEST_METHOD(WSLCE2E_Image_DeleteForce_UsedImage_Success) |
| 119 | { |
| 120 | TestImageRegistry::Instance().EnsureLoaded(DebianImage); |
| 121 | VerifyImageIsNotUsed(DebianImage); |
| 122 | |
| 123 | auto createResult = RunWslc(std::format(L"container create --name {} {}", WslcContainerName, DebianImage.NameAndTag())); |
| 124 | createResult.Verify({.Stderr = L"", .ExitCode = 0}); |
| 125 | |
| 126 | VerifyImageIsUsed(DebianImage); |
| 127 | |
| 128 | auto result = RunWslc(std::format(L"image delete --force {}", DebianImage.Name)); |
| 129 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 130 | VERIFY_IS_TRUE(result.StdoutContainsSubstring(std::format(L"Untagged: {}", DebianImage.NameAndTag()))); |
| 131 | } |
| 132 | |
| 133 | WSLC_TEST_METHOD(WSLCE2E_Image_DeleteNoPrune) |
| 134 | { |
| 135 | // Tag debian a second time, then remove via the alias with --no-prune. |
| 136 | // The alias must disappear while the original tag stays resolvable. |
| 137 | TestImageRegistry::Instance().EnsureLoaded(DebianImage); |
| 138 | TestImageRegistry::Instance().Delete(NoPruneTaggedImage); |
| 139 | |
| 140 | auto tagResult = RunWslc(std::format(L"image tag {} {}", DebianImage.NameAndTag(), NoPruneTaggedImage.NameAndTag())); |
| 141 | tagResult.Verify({.Stderr = L"", .ExitCode = 0}); |
| 142 | |
| 143 | auto removeResult = RunWslc(std::format(L"image delete --no-prune {}", NoPruneTaggedImage.NameAndTag())); |
| 144 | removeResult.Verify({.Stderr = L"", .ExitCode = 0}); |
| 145 | |
| 146 | VerifyImageIsListed(DebianImage); |
| 147 | |
| 148 | auto listAfter = RunWslc(L"image list"); |
| 149 | listAfter.Verify({.Stderr = L"", .ExitCode = 0}); |
| 150 | for (const auto& line : listAfter.GetStdoutLines()) |
| 151 | { |
| 152 | VERIFY_IS_FALSE( |
| 153 | line.find(NoPruneTaggedImage.Name) != std::wstring::npos && line.find(NoPruneTaggedImage.Tag) != std::wstring::npos, |
| 154 | L"Secondary tag should have been removed by `image delete --no-prune`"); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | private: |
| 159 | const std::wstring WslcContainerName = L"wslc-test-container"; |
| 160 | const TestImage& DebianImage = DebianTestImage(); |
| 161 | const TestImage& AlpineImage = AlpineTestImage(); |
| 162 | const TestImage& InvalidImage = InvalidTestImage(); |
| 163 | const TestImage NoPruneTaggedImage{L"wslc-test-noprune", L"alias", L""}; |
| 164 | }; |
| 165 | } // namespace WSLCE2ETests |