Esempio n. 1
0
        protected void DecodeCapture(int owner, byte[] bytes)
        {
            //string path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            this._decodeDisposeDir = new DisposableDirectory();
            this.DebugEvent("New decodeDDir: " + this._decodeDisposeDir);
            string fileName = Path.Combine(this._decodeDisposeDir.DirectoryPath, Path.GetRandomFileName())+".7z";

            File.WriteAllBytes(fileName, bytes);
            this._bufferedOperator.AddTask(owner, false, this._decodeDisposeDir, new string[] { fileName });
            this.DebugEvent("Decompress: " + fileName);
            /*Compression7z cmp = new Compression7z(this._decodeDisposeDir.DirectoryPath, true);
            cmp.OnComplete += new Compression7z.ProcessEventHandler(cmp_OnComplete);
            cmp.ExtractFile(false, fileName, Compression7z.Type._7z, Compression7z.Compression.Ultra, "h4x0r");*/
        }
Esempio n. 2
0
        public void EncodeCapture(CapturePacket cPacket)
        {
            cPacket.ParentDirectory = this._encodeDisposeDir.DirectoryPath;
            this.__staticCompressionQueue.Enqueue(cPacket.WriteEncoded(null));

            // Compress if limit
            if (this.__staticCompressionQueue.Count == this.CaptureBufferCount)
            {
                this._bufferedOperator.AddTask(0, true, this._encodeDisposeDir, this.__staticCompressionQueue.ToArray());
                this._encodeDisposeDir = new DisposableDirectory();
                this.DebugEvent("New encodeDDir: " + this._encodeDisposeDir);
                /*Compression7z cmp = new Compression7z(true);
                cmp.OnComplete += new Compression7z.ProcessEventHandler(cmp_OnComplete);
                cmp.CompressFiles(true, this.__staticCompressionQueue.Peek(), Compression7z.Type._7z, Compression7z.Compression.Ultra, this.__staticCompressionQueue.ToArray(), null, "h4x0r");
                 */
                this.__staticCompressionQueue = new Queue<string>();
            }
        }
Esempio n. 3
0
        public void TestDistributedCacheServiceTracker()
        {
            var context = new OperationContext(new Context(Logger));

            using var testDirectory = new DisposableDirectory(FileSystem);
            var logFilePath = testDirectory.CreateRandomFileName();

            var logIntervalSeconds = 10;
            var testClock          = new MemoryClock();

            // Create a new service tracker, in the constructor we will try to get previous online time
            // Because the file was never created previously, we can not determine offlineTime
            using var testServiceTracker = new Service.ServiceOfflineDurationTracker(context, testClock, FileSystem, logIntervalSeconds, logFilePath);

            // Offline time is not available.
            testServiceTracker.GetOfflineDuration(context).ShouldBeError();
            testServiceTracker.LogTimeStampToFile(context, testClock.UtcNow.ToString());

            testClock.UtcNow += TimeSpan.FromSeconds(logIntervalSeconds);

            // From the previous simulated interval, we created a new file and wrote a timestamp to it.
            // Now we should be able to determine previous offlineTIme
            testServiceTracker.GetOfflineDuration(context).ShouldBeSuccess();
        }
Esempio n. 4
0
        public Task PutFileWithExistingHashHardLinks()
        {
            var context = new Context(Logger);
            return TestStore(context, Clock, async store =>
            {
                using (var tempDirectory = new DisposableDirectory(FileSystem))
                {
                    var bytes = ThreadSafeRandom.GetBytes(100);
                    AbsolutePath sourcePath = tempDirectory.CreateRandomFileName();
                    FileSystem.WriteAllBytes(sourcePath, bytes);
                    await store.PutFileAsync(context, sourcePath, FileRealizationMode.HardLink, ContentHashType, null).ShouldBeSuccess();

                    AbsolutePath sourcePath2 = tempDirectory.CreateRandomFileName();
                    FileSystem.WriteAllBytes(sourcePath2, bytes);
                    await store.PutFileAsync(
                        context,
                        sourcePath2,
                        FileRealizationMode.HardLink,
                        bytes.CalculateHash(ContentHashType),
                        null).ShouldBeSuccess();
                    FileSystem.GetHardLinkCount(sourcePath2).Should().Be(3);
                }
            });
        }
Esempio n. 5
0
        public async Task CopyRetries(int retries)
        {
            var context = new Context(Logger);

            using (var directory = new DisposableDirectory(FileSystem))
            {
                var(distributedCopier, mockFileCopier) = await CreateAsync(context, directory.Path, retries);

                var hash = ContentHash.Random();
                var hashWithLocations = new ContentHashWithSizeAndLocations(
                    hash,
                    size: 99,
                    new MachineLocation[] { new MachineLocation("") });

                mockFileCopier.CopyFileAsyncResult = new CopyFileResult(CopyFileResult.ResultCode.SourcePathError);
                var result = await distributedCopier.TryCopyAndPutAsync(
                    new OperationContext(context),
                    hashWithLocations,
                    handleCopyAsync : tpl => Task.FromResult(new PutResult(hash, 99)));

                result.ShouldBeError();
                mockFileCopier.CopyAttempts.Should().Be(retries);
            }
        }
Esempio n. 6
0
        private async Task TestStore(Context context, Func <TStore, Task> func, IContentChangeAnnouncer announcer = null)
        {
            using (var testDirectory = new DisposableDirectory(FileSystem))
            {
                using (var store = CreateStore(testDirectory))
                {
                    if (announcer != null)
                    {
                        store.Announcer = announcer;
                    }

                    store.Should().NotBeNull();
                    try
                    {
                        await store.StartupAsync(context).ShouldBeSuccess();
                        await func(store);
                    }
                    finally
                    {
                        await store.ShutdownAsync(context).ShouldBeSuccess();
                    }
                }
            }
        }
        public async Task PlaceFileSkipsIfExists()
        {
            using (var placeDirectory = new DisposableDirectory(FileSystem))
            {
                var path = placeDirectory.Path / "file.dat";
                FileSystem.WriteAllBytes(path, new byte[0]);

                await RunTestAsync(ImplicitPin.None, null, async (context, session) =>
                {
                    var putResult = await session.PutRandomAsync(
                        context, ContentHashType, false, ContentByteCount, Token);
                    var result = await session.PlaceFileAsync(
                        context,
                        putResult.ContentHash,
                        path,
                        FileAccessMode.ReadOnly,
                        FileReplacementMode.SkipIfExists,
                        FileRealizationMode.Any,
                        Token);
                    Assert.False(result.Succeeded);
                    Assert.Equal(PlaceFileResult.ResultCode.NotPlacedAlreadyExists, result.Code);
                });
            }
        }
        public void CreateHardLinkResultsBasedOnDestinationDirectoryExistence()
        {
            using (var testDirectory = new DisposableDirectory(FileSystem))
            {
                var sourcePath = testDirectory.Path / "source.txt";
                FileSystem.WriteAllBytes(sourcePath, ThreadSafeRandom.GetBytes(10));
                FileUtilities.SetFileAccessControl(sourcePath.Path, FileSystemRights.Write | FileSystemRights.ReadAndExecute, false);

                // Covering short path case
                CreateHardLinkResultsBasedOnDestinationDirectoryExistenceCore(testDirectory.Path, sourcePath, new string('a', 10));

                // Covering long path case
                CreateHardLinkResultsBasedOnDestinationDirectoryExistenceCore(testDirectory.Path, sourcePath, new string('a', 200));
            }

            void CreateHardLinkResultsBasedOnDestinationDirectoryExistenceCore(AbsolutePath root, AbsolutePath sourcePath, string pathSegment)
            {
                var destinationPath = root / pathSegment / pathSegment / "destination.txt";
                
                // Creating hard link fails if the destination directory does not exist.
                // But this is the case only when the path is not a long path on a system with no long path support.
                if (destinationPath.IsLongPath && !AbsolutePath.LongPathsSupported)
                {
                    // In this case, the test case is skipped.
                    return;
                }

                var result = FileSystem.CreateHardLink(sourcePath, destinationPath, false);
                var expectedResult = CreateHardLinkResult.FailedDestinationDirectoryDoesNotExist;
                Assert.Equal(expectedResult, result);

                FileSystem.CreateDirectory(destinationPath.Parent);
                result = FileSystem.CreateHardLink(sourcePath, destinationPath, false);
                Assert.Equal(CreateHardLinkResult.Success, result);
            }
        }
Esempio n. 9
0
        public void ReadyBeforeStartup()
        {
            var logger  = TestGlobal.Logger;
            var context = new Context(logger);

            using var testDirectory = new DisposableDirectory(_fileSystem);
            try
            {
                LifetimeTracker.ServiceStarting(context, serviceRunningLogInterval: TimeSpan.FromMinutes(10), logFilePath: testDirectory.Path);

                LifetimeTracker.ServiceReadyToProcessRequests(context);

                GetFullOutput().Should().NotContain(FullyInitializedMessage);

                LifetimeTracker.ServiceStarted(context);
                var output = GetFullOutput();
                output.Should().Contain(FullyInitializedMessage);
                output.Should().Contain(UnavailableOfflineTime);
            }
            finally
            {
                LifetimeTracker.ServiceStopped(context, BoolResult.Success);
            }
        }
Esempio n. 10
0
        public void PropagateCancellationException()
        {
            string text =
                @"class C
{
}";

            using (var directory = new DisposableDirectory(Temp))
            {
                var file = directory.CreateFile("c.cs");
                file.WriteAllText(text);
                var diagnostics = DiagnosticBag.GetInstance();
                Assert.Throws <OperationCanceledException>(() =>
                                                           RunCompiler(
                                                               directory.Path,
                                                               file.Path,
                                                               diagnostics,
                                                               ImmutableArray.Create <SourceGenerator>(
                                                                   new SimpleSourceGenerator(c => { throw new InvalidCastException(); }),
                                                                   new SimpleSourceGenerator(c => { throw new OperationCanceledException(); }))));
                diagnostics.Verify();
                diagnostics.Free();
            }
        }
Esempio n. 11
0
        public DistributedContentStore(
            MachineLocation localMachineLocation,
            AbsolutePath localCacheRoot,
            Func <IDistributedLocationStore, IContentStore> innerContentStoreFunc,
            IContentLocationStoreFactory contentLocationStoreFactory,
            DistributedContentStoreSettings settings,
            DistributedContentCopier distributedCopier,
            ColdStorage coldStorage,
            IClock?clock = null)
        {
            Contract.Requires(settings != null);

            LocalMachineLocation        = localMachineLocation;
            ContentLocationStoreFactory = contentLocationStoreFactory;
            _clock                  = clock ?? SystemClock.Instance;
            _distributedCopier      = distributedCopier;
            _copierWorkingDirectory = new DisposableDirectory(distributedCopier.FileSystem, localCacheRoot / "Temp");

            _settings = settings;

            ColdStorage = coldStorage;

            InnerContentStore = innerContentStoreFunc(this);
        }
Esempio n. 12
0
        protected override IContentStore CreateStore(DisposableDirectory testDirectory, ContentStoreConfiguration configuration)
        {
            var rootPath = testDirectory.Path;

            configuration.Write(FileSystem, rootPath);

            var grpcPortFileName = Guid.NewGuid().ToString();

            var serviceConfig = new ServiceConfiguration(
                new Dictionary <string, AbsolutePath> {
                { CacheName, rootPath }
            },
                rootPath,
                GracefulShutdownSeconds,
                PortExtensions.GetNextAvailablePort(),
                grpcPortFileName);

            return(new TestServiceClientContentStore(
                       Logger,
                       FileSystem,
                       new ServiceClientContentStoreConfiguration(CacheName, rpcConfiguration: null, scenario: Scenario),
                       heartbeatInterval: null,
                       serviceConfiguration: serviceConfig));
        }
Esempio n. 13
0
        public async Task HistoryTimestampIsIncreasingMonotonically()
        {
            using (var testDirectory = new DisposableDirectory(FileSystem))
            {
                var pinSizeHistory = await PinSizeHistory.LoadOrCreateNewAsync(FileSystem, _clock, testDirectory.Path);

                var emptyHistory = pinSizeHistory.ReadHistory(1);
                AssertEmptyHistory(emptyHistory.Window);

                AddIntoPinSizeHistory(pinSizeHistory, 7);

                var history1 = pinSizeHistory.ReadHistory(1);
                AssertEqualHistory(new long[] { 7 }, history1.Window);
                Assert.True(history1.TimestampInTick > emptyHistory.TimestampInTick);

                await pinSizeHistory.SaveAsync(FileSystem);

                pinSizeHistory = await PinSizeHistory.LoadOrCreateNewAsync(FileSystem, _clock, testDirectory.Path);

                var history1AfterSave = pinSizeHistory.ReadHistory(1);
                AssertEqualHistory(new long[] { 7 }, history1AfterSave.Window);
                Assert.Equal(history1.TimestampInTick, history1AfterSave.TimestampInTick);
            }
        }
Esempio n. 14
0
        public async Task UpgradeStartup()
        {
            const int iterations = 5;
            var       stopwatch  = new Stopwatch();
            var       context    = new Context(Logger);

            // ReSharper disable once UnusedVariable
            foreach (var x in Enumerable.Range(0, iterations))
            {
                using (var testDirectory = new DisposableDirectory(FileSystem))
                {
                    await EstablishStartingDatabaseAsync(testDirectory);
                    await RemoveSerializedDeterminismColumnAsync(testDirectory);

                    using (var memoizationStore = CreateStoreFunc(testDirectory))
                    {
                        try
                        {
                            stopwatch.Start();
                            await memoizationStore.StartupAsync(context).ShouldBeSuccess();

                            stopwatch.Stop();
                        }
                        finally
                        {
                            await memoizationStore.ShutdownAsync(context).ShouldBeSuccess();
                        }
                    }
                }
            }

            var averageTime = stopwatch.ElapsedMilliseconds / iterations;
            var name        = GetType().Name + ".UpgradeStartup";

            ResultsFixture.Results.Add(name, averageTime, "milliseconds");
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="BlobReadOnlyContentSession"/> class.
        /// </summary>
        /// <param name="fileSystem">Filesystem used to read/write files.</param>
        /// <param name="name">Session name.</param>
        /// <param name="implicitPin">Policy determining whether or not content should be automatically pinned on adds or gets.</param>
        /// <param name="blobStoreHttpClient">Backing BlobStore http client.</param>
        /// <param name="timeToKeepContent">Minimum time-to-live for accessed content.</param>
        /// <param name="downloadBlobsThroughBlobStore">If true, gets blobs through BlobStore. If false, gets blobs from the Azure Uri.</param>
        public BlobReadOnlyContentSession(
            IAbsFileSystem fileSystem,
            string name,
            ImplicitPin implicitPin,
            IBlobStoreHttpClient blobStoreHttpClient,
            TimeSpan timeToKeepContent,
            bool downloadBlobsThroughBlobStore)
            : base(name)
        {
            Contract.Requires(fileSystem != null);
            Contract.Requires(name != null);
            Contract.Requires(blobStoreHttpClient != null);

            ImplicitPin                    = implicitPin;
            BlobStoreHttpClient            = blobStoreHttpClient;
            TimeToKeepContent              = timeToKeepContent;
            _downloadBlobsThroughBlobStore = downloadBlobsThroughBlobStore;
            _parallelSegmentDownloadConfig = new ParallelHttpDownload
                                             .Configuration(
                segmentDownloadTimeout: TimeSpan.FromMinutes(int.Parse(
                                                                 Environment.GetEnvironmentVariable(EnvironmentVariablePrefix + "SegmentDownloadTimeoutInMinutes") ??
                                                                 DefaultSegmentDownloadTimeoutInMinutes.ToString())),
                segmentSizeInBytes: int.Parse(
                    Environment.GetEnvironmentVariable(EnvironmentVariablePrefix + "ParallelDownloadSegmentSizeInBytes") ??
                    DefaultParallelDownloadSegmentSizeInBytes.ToString()),
                maxParallelSegmentDownloadsPerFile: int.Parse(
                    Environment.GetEnvironmentVariable(EnvironmentVariablePrefix + "MaxParallelSegmentDownloadsPerFile") ??
                    DefaultMaxParallelSegmentDownloadsPerFile.ToString()),
                maxSegmentDownloadRetries:
                int.Parse(
                    Environment.GetEnvironmentVariable(EnvironmentVariablePrefix + "MaxSegmentDownloadRetries") ??
                    DefaultMaxSegmentDownloadRetries.ToString()));

            TempDirectory = new DisposableDirectory(fileSystem);
            BuildXL.Native.IO.FileUtilities.CreateDirectory(TempDirectory.Path.Path);
        }
        public async Task CopyFailsWithNoLocations()
        {
            var context = new Context(Logger);

            using (var directory = new DisposableDirectory(FileSystem))
            {
                var(distributedCopier, mockFileCopier) = CreateMocks(FileSystem, directory.Path, TimeSpan.Zero);
                await using var _ = await distributedCopier.StartupWithAutoShutdownAsync(context);

                var hash = VsoHashInfo.Instance.EmptyHash;
                var hashWithLocations = new ContentHashWithSizeAndLocations(
                    hash,
                    size: 42,
                    new MachineLocation[0]);

                mockFileCopier.CopyToAsyncResult = CopyFileResult.SuccessWithSize(42);
                var result = await distributedCopier.TryCopyAndPutAsync(
                    new OperationContext(context),
                    hashWithLocations,
                    handleCopyAsync : tpl => Task.FromResult(new PutResult(hash, 42)));

                result.ShouldBeError();
            }
        }
        public BlobReadOnlyContentSession(
            BackingContentStoreConfiguration configuration,
            string name,
            ImplicitPin implicitPin,
            IBlobStoreHttpClient blobStoreHttpClient,
            CounterTracker?counterTracker = null)
            : base(name, counterTracker)
        {
            Contract.Requires(configuration != null);
            Contract.Requires(configuration.FileSystem != null);
            Contract.Requires(name != null);
            Contract.Requires(blobStoreHttpClient != null);

            Configuration = configuration;

            ImplicitPin                    = implicitPin;
            BlobStoreHttpClient            = blobStoreHttpClient;
            _parallelSegmentDownloadConfig = ParallelHttpDownload.DownloadConfiguration.ReadFromEnvironment(EnvironmentVariablePrefix);

            TempDirectory = new DisposableDirectory(configuration.FileSystem);

            _counters     = CounterTracker.CreateCounterCollection <BackingContentStore.SessionCounters>(counterTracker);
            _blobCounters = CounterTracker.CreateCounterCollection <Counters>(counterTracker);
        }
Esempio n. 18
0
        public async Task OpenStreamImplicitlyPins()
        {
            using (var directory = new DisposableDirectory(FileSystem))
            {
                // Put some random content into the store in one session.
                var contentHash = ContentHash.Random(ContentHashType);
                await RunTestAsync(ImplicitPin.None, directory, async (context, session) =>
                {
                    var r       = await session.PutRandomAsync(context, ContentHashType, false, ContentByteCount, Token).ShouldBeSuccess();
                    contentHash = r.ContentHash;
                });

                await RunTestAsync(ImplicitPin.PutAndGet, directory, async (context, session) =>
                {
                    // Open should pin this previously existing content, preventing eviction.
                    var r = await session.OpenStreamAsync(context, contentHash, Token).ShouldBeSuccess();
                    using (r.Stream)
                    {
                        // This put should fail as the previous open is holding content that cannot be evicted.
                        var pr1 = await session.PutRandomAsync(context, ContentHashType, false, MaxSize, Token).ShouldBeError();
                    }
                });
            }
        }
Esempio n. 19
0
        public void GetFilesByGlobPattern_Is_Dot_Directory()
        {
            using var tmpDirectory = new DisposableDirectory(Path.Combine(Directory.GetCurrentDirectory(), Guid.NewGuid().ToString("N")));

            var nupkgAbsoluteFilename  = Path.Combine(tmpDirectory, "test.nupkg");
            var snupkgAbsoluteFilename = Path.Combine(tmpDirectory, "test.snupkg");

            File.WriteAllText(nupkgAbsoluteFilename, string.Empty);
            File.WriteAllText(snupkgAbsoluteFilename, string.Empty);

            var packages = tmpDirectory.WorkingDirectory.GetFilesByGlobPattern(".", out var glob).ToList();

            var globPattern = glob.ToString().Substring(tmpDirectory.WorkingDirectory.Length).Replace("\\", "/");

            if (globPattern[0] == '/' || globPattern[0] == '\\')
            {
                globPattern = globPattern.Substring(1);
            }

            Assert.That(globPattern, Is.EqualTo(globPattern));
            Assert.That(packages.Count, Is.EqualTo(2));
            Assert.That(packages, Does.Contain(nupkgAbsoluteFilename));
            Assert.That(packages, Does.Contain(snupkgAbsoluteFilename));
        }
        protected async Task RunStoreTestAsync(Func <Context, IContentStore, Task> funcAsync, LocalServerConfiguration localContentServerConfiguration = null, TimeSpan?heartbeatOverride = null)
        {
            var context = new Context(Logger);

            // Using unique scenario to avoid flakiness when running the tests in parallel
            Scenario += Guid.NewGuid();
            using (var directory = new DisposableDirectory(FileSystem))
            {
                var config = new ContentStoreConfiguration(new MaxSizeQuota($"{DefaultMaxSize}"));

                using (var store = CreateStore(directory.Path, config, localContentServerConfiguration ?? TestConfigurationHelper.LocalContentServerConfiguration, heartbeatOverride))
                {
                    try
                    {
                        await store.StartupAsync(context).ShouldBeSuccess();
                        await funcAsync(context, store);
                    }
                    finally
                    {
                        await store.ShutdownAsync(context).ShouldBeSuccess();
                    }
                }
            }
        }
Esempio n. 21
0
        public void GetFilesByGlobPattern_Is_Relative_Directory(string globPattern, string expectedGlobPattern, int expectedFilesCount)
        {
            using var tmpDirectory = new DisposableDirectory(Path.Combine(Directory.GetCurrentDirectory(), Guid.NewGuid().ToString("N")));

            var nupkgDirectory = Path.Combine(tmpDirectory, "nupkg");

            Directory.CreateDirectory(nupkgDirectory);

            var nupkgAbsoluteFilename  = Path.Combine(nupkgDirectory, "test.nupkg");
            var snupkgAbsoluteFilename = Path.Combine(nupkgDirectory, "test.snupkg");

            File.WriteAllText(nupkgAbsoluteFilename, string.Empty);
            File.WriteAllText(snupkgAbsoluteFilename, string.Empty);

            var packages = tmpDirectory.WorkingDirectory.GetFilesByGlobPattern(globPattern, out var glob).ToList();
            var pattern  = glob.ToString().Substring(nupkgDirectory.Length).Replace("\\", "/");

            if (pattern[0] == '/' || pattern[0] == '\\')
            {
                pattern = pattern.Substring(1);
            }
            Assert.That(pattern, Is.EqualTo(expectedGlobPattern));
            Assert.That(packages.Count, Is.EqualTo(expectedFilesCount));
        }
Esempio n. 22
0
        public virtual async Task IdIsPersistent()
        {
            var context = new Context(Logger);

            using (var testDirectory = new DisposableDirectory(FileSystem))
            {
                Guid cacheGuid;
                using (var cache = CreateCache(testDirectory))
                {
                    await cache.StartupAsync(context).ShouldBeSuccess();

                    cacheGuid = cache.Id;
                    await cache.ShutdownAsync(context).ShouldBeSuccess();
                }

                using (var cache = CreateCache(testDirectory))
                {
                    await cache.StartupAsync(context).ShouldBeSuccess();

                    cache.Id.Should().Be(cacheGuid);
                    await cache.ShutdownAsync(context).ShouldBeSuccess();
                }
            }
        }
Esempio n. 23
0
        public Task PutFileTrustsHash()
        {
            var context = new Context(Logger);

            return(TestStore(context, Clock, async store =>
            {
                byte[] bytes = ThreadSafeRandom.GetBytes(ValueSize);
                ContentHash contentHash = bytes.CalculateHash(ContentHashType);

                ContentHash fakeHash = ThreadSafeRandom.GetBytes(ValueSize).CalculateHash(ContentHashType);

                // Verify content doesn't exist yet in store
                Assert.False(await store.ContainsAsync(context, contentHash, null));

                using (var tempDirectory = new DisposableDirectory(FileSystem))
                {
                    AbsolutePath pathToContent = tempDirectory.Path / "tempContent.txt";
                    FileSystem.WriteAllBytes(pathToContent, bytes);
                    ContentHash hashFromPut;
                    using (var pinContext = store.CreatePinContext())
                    {
                        // Put the content into the store w/ hard link
                        var r = await store.PutTrustedFileAsync(
                            context, pathToContent, FileRealizationMode.Any, new ContentHashWithSize(fakeHash, ValueSize), new PinRequest(pinContext));
                        hashFromPut = r.ContentHash;
                        Clock.Increment();
                        await store.EnsureContentIsPinned(context, Clock, hashFromPut);
                        Assert.True(pinContext.Contains(hashFromPut));
                    }

                    await store.EnsureContentIsNotPinned(context, Clock, hashFromPut);
                    Assert.Equal(fakeHash, hashFromPut);
                    Assert.NotEqual(contentHash, hashFromPut);
                }
            }));
        }
Esempio n. 24
0
        public Task PlaceFileHardLinkToLongFilePaths()
        {
            var context = new Context(Logger);

            return(TestStore(context, _clock, async store =>
            {
                byte[] bytes = ThreadSafeRandom.GetBytes(ValueSize);

                using (var tempDirectory = new DisposableDirectory(FileSystem))
                {
                    var pathToContent = tempDirectory.CreateRandomFileName();
                    FileSystem.WriteAllBytes(pathToContent, bytes);

                    // Put the content into the store via copy
                    var r = await store.PutFileAsync(context, pathToContent, FileRealizationMode.Copy, ContentHashType, null);

                    foreach (var length in Enumerable.Range(MaxShortPath - 10, 20))
                    {
                        var shouldError = length >= MaxShortPath && !AbsolutePath.LongPathsSupported;
                        await PlaceFileHardLinkToLongFilePathAsync(store, context, tempDirectory, r.ContentHash, length, shouldError);
                    }
                }
            }));
        }
 protected override ICache CreateCache(DisposableDirectory testDirectory)
 {
     return(CreateCache(testDirectory, Guid.NewGuid().ToString(), FileSystem, Logger, _backingOption, ItemStorageOption));
 }
 protected ICache CreateCache(DisposableDirectory testDirectory, string cacheNamespace, TimeSpan hashListLifetime, TimeSpan additionalHashListLifetime)
 {
     return(CreateCache(testDirectory, cacheNamespace, FileSystem, Logger, _backingOption, ItemStorageOption, hashListLifetime, additionalHashListLifetime));
 }
Esempio n. 27
0
        public async Task TestInstallAsync()
        {
            var snapAppsReleases = new SnapAppsReleases();
            var genesisSnapApp   = _baseFixture.BuildSnapApp();

            Assert.True(genesisSnapApp.Channels.Count >= 2);

            using var testDirectory             = new DisposableDirectory(_baseFixture.WorkingDirectory, _snapFilesystem);
            using var genesisSnapReleaseBuilder = _baseFixture.WithSnapReleaseBuilder(testDirectory, snapAppsReleases, genesisSnapApp, _snapReleaseBuilderContext);
            var mainAssemblyDefinition = _baseFixture.BuildSnapExecutable(genesisSnapApp);

            genesisSnapReleaseBuilder
            .AddNuspecItem(mainAssemblyDefinition)
            .AddNuspecItem(mainAssemblyDefinition.BuildRuntimeConfigFilename(_snapFilesystem), mainAssemblyDefinition.BuildRuntimeConfig())
            .AddNuspecItem(_baseFixture.BuildLibrary("test1"))
            .AddSnapDll();

            using var genesisPackageContext = await _baseFixture.BuildPackageAsync(genesisSnapReleaseBuilder);

            var anyOs = SnapOs.AnyOs;

            Assert.NotNull(anyOs);

            var loggerMock = new Mock <ILog>();

            var progressSource = new Mock <ISnapProgressSource>();

            progressSource.
            Setup(x => x.Raise(It.IsAny <int>()));

            var failedRunAsyncReturnValues = new List <(int exitCode, string stdOut)>();

            var snapOsProcessManager = new Mock <ISnapOsProcessManager>();

            snapOsProcessManager
            .Setup(x => x.RunAsync(It.IsAny <ProcessStartInfoBuilder>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync((ProcessStartInfoBuilder builder, CancellationToken cancellationToken) =>
            {
                var result = _snapOsProcessManager.RunAsync(builder, cancellationToken).GetAwaiter().GetResult();
                if (result.exitCode != 0)
                {
                    failedRunAsyncReturnValues.Add(result);
                }
                return(result);
            });
            snapOsProcessManager
            .Setup(x => x.StartNonBlocking(It.IsAny <ProcessStartInfoBuilder>()))
            .Returns((ProcessStartInfoBuilder builder) => _snapOsProcessManager.StartNonBlocking(builder));
            snapOsProcessManager
            .Setup(x => x.ChmodExecuteAsync(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .Returns((string filename, CancellationToken cancellationToken) => _snapOsProcessManager.ChmodExecuteAsync(filename, cancellationToken));
            _snapOsMock
            .Setup(x => x.Filesystem)
            .Returns(_snapFilesystem);
            _snapOsMock
            .Setup(x => x.ProcessManager)
            .Returns(snapOsProcessManager.Object);
            _snapOsMock
            .Setup(x => x.CreateShortcutsForExecutableAsync(
                       It.IsAny <SnapOsShortcutDescription>(),
                       It.IsAny <ILog>(),
                       It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            using var baseDirectory = _baseFixture.WithDisposableTempDirectory(_snapFilesystem);
            using var installCts    = new CancellationTokenSource();
            var snapCurrentChannel = genesisPackageContext.FullPackageSnapApp.GetCurrentChannelOrThrow();

            await _snapInstaller.InstallAsync(
                genesisPackageContext.FullPackageAbsolutePath,
                baseDirectory.WorkingDirectory,
                genesisPackageContext.FullPackageSnapRelease,
                snapCurrentChannel,
                progressSource.Object,
                loggerMock.Object,
                installCts.Token);

            var appDirectory          = _snapFilesystem.PathCombine(baseDirectory.WorkingDirectory, $"app-{genesisPackageContext.FullPackageSnapApp.Version}");
            var snapAppUpdated        = appDirectory.GetSnapAppFromDirectory(_snapFilesystem, _snapAppReader);
            var snapAppUpdatedChannel = snapAppUpdated.GetCurrentChannelOrThrow();

            Assert.Equal(snapCurrentChannel.Name, snapAppUpdatedChannel.Name);

#if !PLATFORM_WINDOWS || NETFULLFRAMEWORK
            // TODO: ENABLE ME! Unable to run a dotnet 2.2 (shared) executable on Windows.
            Assert.Empty(failedRunAsyncReturnValues);
#endif

            var coreRunExe = _snapFilesystem.PathCombine(baseDirectory.WorkingDirectory,
                                                         _snapEmbeddedResources.GetCoreRunExeFilenameForSnapApp(genesisPackageContext.FullPackageSnapApp));
            var appExe = _snapFilesystem.PathCombine(baseDirectory.WorkingDirectory,
                                                     $"app-{genesisSnapReleaseBuilder.SnapApp.Version}", genesisSnapReleaseBuilder.CoreRunExe);
            var snapInstalledArguments = $"--snapx-installed {genesisPackageContext.FullPackageSnapApp.Version.ToNormalizedString()}";
            var snapFirstRunArguments  = $"--snapx-first-run {genesisPackageContext.FullPackageSnapApp.Version.ToNormalizedString()}";

            progressSource.Verify(x => x.Raise(It.Is <int>(v => v == 100)), Times.Once);

            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                snapOsProcessManager.Verify(x => x.ChmodExecuteAsync(
                                                It.IsAny <string>(), It.IsAny <CancellationToken>()), Times.Exactly(2));
                snapOsProcessManager.Verify(x => x.ChmodExecuteAsync(
                                                It.Is <string>(v => v == coreRunExe), It.Is <CancellationToken>(v => v == installCts.Token)), Times.Once);
                snapOsProcessManager.Verify(x => x.ChmodExecuteAsync(
                                                It.Is <string>(v => v == appExe), It.Is <CancellationToken>(v => v == installCts.Token)), Times.Once);
            }

            _snapOsMock.Verify(x => x.KillAllProcessesInsideDirectory(
                                   It.Is <string>(v => v == baseDirectory.WorkingDirectory)), Times.Once);

            _snapOsMock.Verify(x => x.CreateShortcutsForExecutableAsync(
                                   It.IsAny <SnapOsShortcutDescription>(), It.IsAny <ILog>(),
                                   It.IsAny <CancellationToken>()), Times.Once);

            _snapOsMock.Verify(x => x.CreateShortcutsForExecutableAsync(
                                   It.Is <SnapOsShortcutDescription>(v => v.ExeAbsolutePath == coreRunExe),
                                   It.Is <ILog>(v => v != null), It.Is <CancellationToken>(v => v == installCts.Token)), Times.Once);

            snapOsProcessManager.Verify(x => x.RunAsync(
                                            It.IsAny <ProcessStartInfoBuilder>(), It.IsAny <CancellationToken>()), Times.Once);

            snapOsProcessManager.Verify(x => x.RunAsync(
                                            It.Is <ProcessStartInfoBuilder>(v => v.Filename == appExe &&
                                                                            v.Arguments == snapInstalledArguments),
                                            It.Is <CancellationToken>(v => v == installCts.Token)), Times.Once);

            snapOsProcessManager.Verify(x => x.StartNonBlocking(
                                            It.Is <ProcessStartInfoBuilder>(v => v.Filename == appExe
                                                                            & v.Arguments == snapFirstRunArguments)), Times.Once);
            snapOsProcessManager.Verify(x => x.StartNonBlocking(
                                            It.IsAny <ProcessStartInfoBuilder>()), Times.Once);
        }
Esempio n. 28
0
 protected abstract IContentStore CreateStore(DisposableDirectory testDirectory, ContentStoreConfiguration configuration);
Esempio n. 29
0
        public async Task TestInstallAsync_Different_Channel()
        {
            var snapAppsReleases = new SnapAppsReleases();
            var genesisSnapApp   = _baseFixture.BuildSnapApp();

            Assert.True(genesisSnapApp.Channels.Count >= 2);

            using var testDirectory             = new DisposableDirectory(_baseFixture.WorkingDirectory, _snapFilesystem);
            using var genesisSnapReleaseBuilder = _baseFixture.WithSnapReleaseBuilder(testDirectory, snapAppsReleases, genesisSnapApp, _snapReleaseBuilderContext);
            var mainAssemblyDefinition = _baseFixture.BuildSnapExecutable(genesisSnapApp);

            genesisSnapReleaseBuilder
            .AddNuspecItem(mainAssemblyDefinition)
            .AddSnapDll();

            using var genesisPackageContext = await _baseFixture.BuildPackageAsync(genesisSnapReleaseBuilder);

            var loggerMock = new Mock <ILog>();

            var snapOsProcessManager = new Mock <ISnapOsProcessManager>();

            snapOsProcessManager
            .Setup(x => x.RunAsync(It.IsAny <ProcessStartInfoBuilder>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync((ProcessStartInfoBuilder builder, CancellationToken cancellationToken) =>
            {
                var result = _snapOsProcessManager.RunAsync(builder, cancellationToken).GetAwaiter().GetResult();
                return(result);
            });
            snapOsProcessManager
            .Setup(x => x.StartNonBlocking(It.IsAny <ProcessStartInfoBuilder>()))
            .Returns((ProcessStartInfoBuilder builder) => _snapOsProcessManager.StartNonBlocking(builder));
            snapOsProcessManager
            .Setup(x => x.ChmodExecuteAsync(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .Returns((string filename, CancellationToken cancellationToken) => _snapOsProcessManager.ChmodExecuteAsync(filename, cancellationToken));
            _snapOsMock
            .Setup(x => x.Filesystem)
            .Returns(_snapFilesystem);
            _snapOsMock
            .Setup(x => x.ProcessManager)
            .Returns(snapOsProcessManager.Object);
            _snapOsMock
            .Setup(x => x.CreateShortcutsForExecutableAsync(
                       It.IsAny <SnapOsShortcutDescription>(),
                       It.IsAny <ILog>(),
                       It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            using var baseDirectory = _baseFixture.WithDisposableTempDirectory(_snapFilesystem);
            using var installCts    = new CancellationTokenSource();
            var nextSnapChannel = genesisSnapApp.GetNextChannel();

            await _snapInstaller.InstallAsync(
                genesisPackageContext.FullPackageAbsolutePath,
                baseDirectory.WorkingDirectory,
                genesisPackageContext.FullPackageSnapRelease,
                nextSnapChannel,
                null,
                loggerMock.Object,
                installCts.Token);

            var appDirectory          = _snapFilesystem.PathCombine(baseDirectory.WorkingDirectory, $"app-{genesisPackageContext.FullPackageSnapApp.Version}");
            var snapAppUpdated        = appDirectory.GetSnapAppFromDirectory(_snapFilesystem, _snapAppReader);
            var snapAppUpdatedChannel = snapAppUpdated.GetCurrentChannelOrThrow();

            Assert.Equal(nextSnapChannel.Name, snapAppUpdatedChannel.Name);
        }
        private async Task PutFileAcrossDrivesAsync(
            FileRealizationMode allowedFileRealizationMode,
            bool contentAlreadyCached,
            bool contentShouldBeCached,
            Func <PutResult, bool> checkResult)
        {
            // This only works when we have multiple drives.
            if (FileSystem is not MemoryFileSystem)
            {
                return;
            }

            using (var testDirectory = new DisposableDirectory(FileSystem))
            {
                var context = new Context(Logger);

                try
                {
                    using (var store = Create(testDirectory.Path, Clock))
                    {
                        await store.StartupAsync(context).ShouldBeSuccess();

                        byte[]      bytes       = ThreadSafeRandom.GetBytes(ValueSize);
                        ContentHash contentHash = bytes.CalculateHash(ContentHashType);

                        // Verify content doesn't exist yet in store
                        Assert.False(await store.ContainsAsync(context, contentHash, null));

                        var pathToContentDifferentVolume = new AbsolutePath(PathGeneratorUtilities.GetAbsolutePath("D", "foo.txt"));

                        try
                        {
                            FileSystem.WriteAllBytes(pathToContentDifferentVolume, bytes);
                            if (contentAlreadyCached)
                            {
                                await store.PutFileAsync(
                                    context, pathToContentDifferentVolume, FileRealizationMode.Copy, ContentHashType, null).ShouldBeSuccess();
                            }

                            var result = await store.PutFileAsync(
                                context, pathToContentDifferentVolume, allowedFileRealizationMode, ContentHashType, null);

                            Assert.True(checkResult(result));

                            (await store.ContainsAsync(context, contentHash, null)).Should()
                            .Be(contentShouldBeCached);
                        }
                        finally
                        {
                            FileSystem.DeleteFile(pathToContentDifferentVolume);
                        }

                        await store.ShutdownAsync(context).ShouldBeSuccess();
                    }
                }
                finally
                {
                    FileSystem.DeleteDirectory(testDirectory.Path, DeleteOptions.All);
                }
            }
        }
 protected virtual ICache CreateCache(
     DisposableDirectory testDirectory, string cacheNamespace, IAbsFileSystem fileSystem, ILogger logger, BackingOption backingOption, StorageOption storageOption, TimeSpan?expiryMinimum = null, TimeSpan?expiryRange = null)
 {
     return(CreateBareBuildCache(testDirectory, cacheNamespace, fileSystem, logger, backingOption, storageOption, expiryMinimum, expiryRange));
 }
Esempio n. 32
0
 public void AddTask(int owner, bool compress, DisposableDirectory ddir, string[] files)
 {
     lock (this._lock)
     {
         this._bufferQueue.Enqueue(new BufferedTask(owner, compress, ddir, files));
     }
     this._performer = new Thread(new ThreadStart(this.PerformTask));
     this._performer.Start();
 }
Esempio n. 33
0
 public BufferedTask(int owner, bool compress, DisposableDirectory ddir, string[] files)
 {
     this.Owner = owner;
     this.Compress = compress;
     this.Directory = ddir;
     this.Files = files;
 }
Esempio n. 34
0
        public async Task TestInterruption()
        {
            using var testDirectory = new DisposableDirectory(_fileSystem.Value);
            var manager = Create(testDirectory.Path, out var context);

            var interruptableServiceTask = manager.RunInterruptableServiceAsync(context, InterruptableServiceId, async token =>
            {
                try
                {
                    await Task.Delay(Timeout.InfiniteTimeSpan, token);
                    return(ServiceResult.Completed);
                }
                catch (TaskCanceledException)
                {
                    return(ServiceResult.Cancelled);
                }
            });

            await Task.Delay(5);

            interruptableServiceTask.IsCompleted.Should().BeFalse();

            bool isInterruptorCanceled = false;

            var interruptorStart      = new TaskCompletionSource <Unit>();
            var interruptorCompletion = new TaskCompletionSource <ServiceResult>();

            var interruptorTask = manager.RunInterrupterServiceAsync(context, InterrupterServiceId, InterruptableServiceId, async token =>
            {
                interruptableServiceTask.IsCompleted.Should().BeTrue();
                var interruptableServiceResult = await interruptableServiceTask;
                interruptableServiceResult.Should().Be(ServiceResult.Cancelled, "Service should be completed due to cancellation caused by interrupter");
                interruptorStart.SetResult(Unit.Void);

                try
                {
                    using var registration = token.Register(() =>
                    {
                        isInterruptorCanceled = true;
                        interruptorCompletion.SetCanceled();
                    });
                    return(await interruptorCompletion.Task);
                }
                catch (TaskCanceledException)
                {
                    return(ServiceResult.Cancelled);
                }
            });


            // Wait for start of interruptor before launching second interruptable. This ensure the first interruptable
            // has shutdown and that logic to prevent startup of interruptable has already been activated
            await interruptorStart.Task;

            // Start a second interruptable to verify it does not start until interruptor is completed
            var secondInterruptableStart = new TaskCompletionSource <Unit>();

            var secondInterruptableTask = manager.RunInterruptableServiceAsync(context, InterruptableServiceId, async token =>
            {
                try
                {
                    secondInterruptableStart.SetResult(Unit.Void);
                    await Task.Delay(Timeout.InfiniteTimeSpan, token);
                    return(false);
                }
                catch (TaskCanceledException)
                {
                    return(true);
                }
            });

            await Task.Delay(5);

            secondInterruptableTask.IsCompleted.Should().BeFalse("Interruptor should not be completed until signaled");
            secondInterruptableStart.Task.IsCompleted.Should().BeFalse("Interruptable should not start until interruptor is completed");
            interruptorCompletion.SetResult(ServiceResult.Completed);
            var interruptorResult = await interruptorTask;

            interruptorResult.Should().Be(ServiceResult.Completed, "Interruptor service should not be cancelled");

            await secondInterruptableStart.Task;

            // Check that cancellation was never triggered on the interruptor (either before or after
            // the completion is set).
            isInterruptorCanceled.Should().BeFalse("Interruptor service should not be cancelled");
        }
Esempio n. 35
0
 private void openToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (this.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
         Compression7z cmp7z = new Compression7z();
         cmp7z.OnComplete += new Compression7z.ProcessEventHandler(cmp7z_OnComplete);
         if (this._ddir != null)
         {
             this._ddir.Dispose();
             this._ddir = null;
         }
         this._ddir = new DisposableDirectory();
         this.progressBarLoading.Visible = true;
         cmp7z.ExtractFile(false, this.openFileDialog1.FileName, this._ddir.DirectoryPath, Compression7z.Type._7z, Compression7z.Compression.Ultra, null);
     }
 }