| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | TestImageRegistry.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains a per-process registry that tracks which test images are loaded in a |
| 12 | session, so that repeated setup across test classes does not reload the same image tar. |
| 13 | --*/ |
| 14 | |
| 15 | #pragma once |
| 16 | |
| 17 | #include <set> |
| 18 | #include <string> |
| 19 | #include <tuple> |
| 20 | |
| 21 | namespace WSLCE2ETests { |
| 22 | |
| 23 | struct TestImage; |
| 24 | |
| 25 | // The cache is only correct as long as every removal is either routed through Delete or followed |
| 26 | // by Restore, so a test that removes images another way has to put the session back itself. |
| 27 | class TestImageRegistry |
| 28 | { |
| 29 | public: |
| 30 | static TestImageRegistry& Instance(); |
| 31 | |
| 32 | NON_COPYABLE(TestImageRegistry); |
| 33 | NON_MOVABLE(TestImageRegistry); |
| 34 | |
| 35 | // Loads the image only if it is not already present in the session. |
| 36 | void EnsureLoaded(const TestImage& image, const std::wstring& sessionName = L""); |
| 37 | |
| 38 | // Deletes the image if present, along with any containers using it, and drops it from the cache. |
| 39 | // The image inventory is queried directly rather than through the cache, so images created outside |
| 40 | // the registry, such as built or imported ones, are still removed. |
| 41 | void Delete(const TestImage& image, const std::wstring& sessionName = L""); |
| 42 | |
| 43 | // Loads the image back without consulting the cache. Tests that remove images without going |
| 44 | // through Delete, such as those exercising image prune, call this to restore the session. |
| 45 | void Restore(const TestImage& image, const std::wstring& sessionName = L""); |
| 46 | |
| 47 | // Discards cached image state for a session after an operation that can remove multiple images. |
| 48 | void InvalidateSession(const std::wstring& sessionName = L""); |
| 49 | |
| 50 | private: |
| 51 | TestImageRegistry() = default; |
| 52 | |
| 53 | // Loads the image without consulting the cache. |
| 54 | void Load(const TestImage& image, const std::wstring& sessionName); |
| 55 | |
| 56 | struct ImageKey |
| 57 | { |
| 58 | std::wstring SessionName; |
| 59 | std::wstring Name; |
| 60 | std::wstring Tag; |
| 61 | |
| 62 | bool operator<(const ImageKey& other) const |
| 63 | { |
| 64 | return std::tie(SessionName, Name, Tag) < std::tie(other.SessionName, other.Name, other.Tag); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | static ImageKey MakeKey(const TestImage& image, const std::wstring& sessionName); |
| 69 | |
| 70 | // Populates the cache for a session from a live image list query the first time it is used. |
| 71 | void EnsureSeeded(const std::wstring& sessionName); |
| 72 | |
| 73 | // Prefixes a command with the session flag when the command targets a named session. |
| 74 | static std::wstring FormatCommand(const std::wstring& sessionName, const std::wstring& command); |
| 75 | |
| 76 | // Test methods run one at a time in a single process, so the cache needs no synchronization. |
| 77 | std::set<ImageKey> m_loaded; |
| 78 | std::set<std::wstring> m_seededSessions; |
| 79 | }; |
| 80 | |
| 81 | } // namespace WSLCE2ETests |