chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using NUnit.Framework;
using OpenTap.EngineUnitTestUtils;
using OpenTap.Package;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
 
namespace OpenTap.Image.Tests
{
    public class Workflows
    {
        [Test]
        public void DeployImage()
        {
            using var install1 = new TempInstall();
            using var install2 = new TempInstall();
 
            ImageSpecifier imageSpecifier = MockRepository.CreateSpecifier();
            imageSpecifier.Packages = new List<PackageSpecifier>
            {
                new PackageSpecifier("Demonstration"),
                new PackageSpecifier("TUI")
            };
 
            Stopwatch stopwatch = Stopwatch.StartNew();
            var image = imageSpecifier.Resolve(CancellationToken.None);
            Console.WriteLine($"Resolve: {stopwatch.ElapsedMilliseconds} ms");
            Assert.IsNotNull(image);
            Assert.IsTrue(image.Packages.Any(s => s.Name == "OpenTAP"));
            Assert.IsTrue(image.Packages.Any(s => s.Name == "Demonstration"));
            Assert.IsTrue(image.Packages.Any(s => s.Name == "TUI"));
 
            stopwatch.Restart();
            image.Cache();
            Console.WriteLine($"Cache: {stopwatch.ElapsedMilliseconds} ms");
 
 
            stopwatch.Restart();
            image.Deploy(install1.Directory, CancellationToken.None);
            Console.WriteLine($"First deploy: {stopwatch.ElapsedMilliseconds} ms");
 
            Installation installation = install1.Installation;
            var packages = installation.GetPackages();
            Assert.IsTrue(packages.Any(s => s.Name == "OpenTAP"));
            Assert.IsTrue(packages.Any(s => s.Name == "Demonstration"));
            Assert.IsTrue(packages.Any(s => s.Name == "TUI"));
 
            stopwatch.Restart();
            image.Deploy(install2.Directory, CancellationToken.None);
            Console.WriteLine($"Second deploy: {stopwatch.ElapsedMilliseconds} ms");
        }
 
        [Test]
        public void DeployImageOnExistingInstallation()
        {
            using var tempInstall = new TempInstall();
 
            ImageSpecifier imageSpecifier = MockRepository.CreateSpecifier();
            imageSpecifier.Packages = new List<PackageSpecifier>
            {
                new PackageSpecifier("Demonstration"),
                new PackageSpecifier("TUI")
            };
 
            Stopwatch stopwatch = Stopwatch.StartNew();
            var image = imageSpecifier.Resolve(CancellationToken.None);
            Console.WriteLine($"Resolve: {stopwatch.ElapsedMilliseconds} ms");
            Assert.IsNotNull(image);
            Assert.IsTrue(image.Packages.Any(s => s.Name == "OpenTAP"));
            Assert.IsTrue(image.Packages.Any(s => s.Name == "Demonstration"));
            Assert.IsTrue(image.Packages.Any(s => s.Name == "TUI"));
 
            stopwatch.Restart();
            image.Cache();
            Console.WriteLine($"Cache: {stopwatch.ElapsedMilliseconds} ms");
            foreach (var pkg in image.Packages)
                Assert.IsTrue(File.Exists(PackageCacheHelper.GetCacheFilePath(pkg)));
 
            stopwatch.Restart();
            image.Deploy(tempInstall.Directory, CancellationToken.None);
            Console.WriteLine($"First deploy: {stopwatch.ElapsedMilliseconds} ms");
 
            Installation installation = tempInstall.Installation;
            var packages = installation.GetPackages();
            Assert.IsTrue(packages.Any(s => s.Name == "OpenTAP"));
            Assert.IsTrue(packages.Any(s => s.Name == "Demonstration"));
            Assert.IsTrue(packages.Any(s => s.Name == "TUI"));
 
 
            imageSpecifier.Packages.Remove(imageSpecifier.Packages.FirstOrDefault(s => s.Name == "Demonstration"));
            stopwatch.Restart();
            image = imageSpecifier.Resolve(CancellationToken.None);
            Console.WriteLine($"Second resolve: {stopwatch.ElapsedMilliseconds} ms");
            stopwatch.Restart();
            image.Deploy(tempInstall.Directory, CancellationToken.None);
            Console.WriteLine($"Second deploy: {stopwatch.ElapsedMilliseconds} ms");
 
            installation = tempInstall.Installation;
            packages = installation.GetPackages();
            Assert.IsTrue(packages.Any(s => s.Name == "OpenTAP"));
            Assert.IsTrue(packages.Any(s => s.Name == "TUI"));
            Assert.IsFalse(packages.Any(s => s.Name == "Demonstration"));
        }
 
        [Test]
        public void DoNotDownloadPackagesThatAreAlreadyInstalled()
        {
            using var tempInstall = new TempInstall();
 
            ImageSpecifier imageSpecifier = MockRepository.CreateSpecifier();
            imageSpecifier.Packages = new List<PackageSpecifier>
            {
                new PackageSpecifier("OpenTAP"),
                new PackageSpecifier("Demonstration"),
                new PackageSpecifier("TUI")
            };
 
            Stopwatch stopwatch = Stopwatch.StartNew();
            var image = imageSpecifier.Resolve(CancellationToken.None);
            image.Deploy(tempInstall.Directory, CancellationToken.None);
            PackageCacheHelper.ClearCache(); // Remove downloaded packages from cache
            Console.WriteLine($"Test installation deployed: {stopwatch.ElapsedMilliseconds} ms");
 
 
            imageSpecifier.Packages.Add(new PackageSpecifier("SDK"));
            stopwatch.Restart();
            image = imageSpecifier.Resolve(CancellationToken.None);
            image.Deploy(tempInstall.Directory, CancellationToken.None);
            Console.WriteLine($"Second deploy: {stopwatch.ElapsedMilliseconds} ms");
 
            // Now we should only have the SDK package in the cache.
            // As the other packages in the image were already in stalled in the correct version, they should not have been downloaded at all
            Assert.AreEqual(1, Directory.GetFiles(PackageCacheHelper.PackageCacheDirectory).Count(), "Unexpected number of files in cache.");
            Assert.IsTrue(File.Exists(PackageCacheHelper.GetCacheFilePath(image.Packages.First(p => p.Name == "SDK"))));
 
            var installation = tempInstall.Installation;
            var packages = installation.GetPackages().Where(s => s.Class != "system-wide").ToList();
            Assert.AreEqual(4, packages.Count, "Unexpected number of packages in installation.");
            Assert.IsTrue(packages.Any(s => s.Name == "OpenTAP"));
            Assert.IsTrue(packages.Any(s => s.Name == "TUI"));
            Assert.IsTrue(packages.Any(s => s.Name == "Demonstration"));
            Assert.IsTrue(packages.Any(s => s.Name == "SDK"));
        }
 
        [Test]
        public void UninstallInOrder()
        {
            using var tempInstall = new TempInstall();
 
            ImageSpecifier imageSpecifier = MockRepository.CreateSpecifier();
            imageSpecifier.Packages = new List<PackageSpecifier>
            {
                new PackageSpecifier("OpenTAP"),
                new PackageSpecifier("OSIntegration")
            };
 
            Stopwatch stopwatch = Stopwatch.StartNew();
            var image = imageSpecifier.Resolve(CancellationToken.None);
            image.Deploy(tempInstall.Directory, CancellationToken.None);
            PackageCacheHelper.ClearCache(); // Remove downloaded packages from cache
            Console.WriteLine($"Test installation deployed: {stopwatch.ElapsedMilliseconds} ms");
 
            imageSpecifier.Packages.Clear();
            stopwatch.Restart();
            image = imageSpecifier.Resolve(CancellationToken.None);
 
            TestTraceListener logListener = new TestTraceListener();
            Log.AddListener(logListener);
 
            image.Deploy(tempInstall.Directory, CancellationToken.None);
            Console.WriteLine($"Second deploy (uninstall): {stopwatch.ElapsedMilliseconds} ms");
 
            var uninstallLog = logListener.allLog.ToString();
            Assert.That(logListener.ErrorMessage, Is.Empty);
            StringAssert.Contains("Starting uninstall step 'tap package list -i'", uninstallLog, $"Errors in uninstall log:\n {uninstallLog}");
 
            var installation = tempInstall.Installation;
            var packages = installation.GetPackages().Where(s => s.Class != "system-wide");
            Assert.AreEqual(0, packages.Count(), "Unexpected packages left in installation after uninstall.");
        }
    }
}