Exemple #1
0
        public OmnibusSession Start()
        {
            Metainfo metainfo;

            byte[] data = Bytes.Random(20000);

            using (FileSandbox temp = new FileSandbox(new EmptyFileLocator()))
            {
                MetainfoBuilder builder = new MetainfoBuilder(temp.Directory);
                string          path    = temp.CreateFile("debian-8.5.0-amd64-CD-1.iso");

                File.WriteAllBytes(path, data);
                builder.AddFile(path);

                metainfo = builder.ToMetainfo();
            }

            OmnibusService service =
                new OmnibusBuilder()
                .WithHash(metainfo.Hash)
                .WithPipeline(new PipelineSimulator())
                .Build();

            return(new OmnibusSession(metainfo, service));
        }
Exemple #2
0
        public RepositorySession Start()
        {
            Metainfo       metainfo;
            RepositoryData data = new RepositoryData(20000);

            using (FileSandbox temp = new FileSandbox(new EmptyFileLocator()))
            {
                MetainfoBuilder builder = new MetainfoBuilder(temp.Directory);
                string          path    = temp.CreateFile("debian-8.5.0-amd64-CD-1.iso");

                File.WriteAllBytes(path, data.ToBytes());
                builder.AddFile(path);

                metainfo = builder.ToMetainfo();
            }

            FileSandbox sandbox     = new FileSandbox(new EmptyFileLocator());
            string      destination = Path.Combine(sandbox.Directory, metainfo.Hash.ToString());

            RepositoryService service =
                new RepositoryBuilder()
                .WithHash(metainfo.Hash)
                .WithDestination(destination)
                .WithFiles(files)
                .WithPipeline(pipeline)
                .WithMemory(new RepositoryMemoryImplementation())
                .Build();

            return(new RepositorySession(metainfo, service, sandbox, data));
        }
 public JobRepositoryTests()
 {
     sandbox    = new FileSandbox(new EmptyFileLocator());
     repository = JobRepositoryFactory.Create(with =>
     {
         with.Location = sandbox.Directory;
     });
 }
Exemple #4
0
        public static AusarModuleProxy LoadFromFile(string path)
        {
            path = FileSandbox.ResolvePath(path);
            var file = new FileInfo(path);

            using (var stream = file.OpenRead())
                return(new AusarModuleProxy(AusarModule.Open(stream), file));
        }
Exemple #5
0
        public MetashareSession Start()
        {
            Metainfo metainfo;

            byte[] bytes = Bytes.Random(20000);

            using (FileSandbox temp = new FileSandbox(new EmptyFileLocator()))
            {
                MetainfoBuilder builder = new MetainfoBuilder(temp.Directory);
                string          path    = temp.CreateFile("debian-8.5.0-amd64-CD-1.iso");

                File.WriteAllBytes(path, bytes);
                builder.AddFile(path);

                metainfo = builder.ToMetainfo(out bytes);
            }

            FileSandbox sandbox     = new FileSandbox(new EmptyFileLocator());
            string      destination = Path.Combine(sandbox.Directory, metainfo.Hash.ToString());

            File.WriteAllBytes(destination + ".metainfo", bytes);

            CoordinatorService glue =
                new CoordinatorBuilder()
                .WithHash(metainfo.Hash)
                .WithMemory(new MemoryBuilder().Build())
                .WithPlugin(new MetadataPlugin(new MetadataHooks()))
                .Build();

            MetafileService metafile =
                new MetafileBuilder()
                .WithHash(metainfo.Hash)
                .WithDestination(destination + ".metainfo")
                .WithFiles(files)
                .WithPipeline(pipeline)
                .Build();

            MetashareService service =
                new MetashareBuilder()
                .WithHash(metainfo.Hash)
                .WithPipeline(pipeline)
                .WithGlue(glue)
                .WithMetafile(metafile)
                .Build();

            TaskCompletionSource <bool> onVerified = new TaskCompletionSource <bool>();

            metafile.Hooks.OnMetafileVerified += _ => onVerified.SetResult(true);

            metafile.Start();
            metafile.Verify();
            onVerified.Task.Wait(5000);

            return(new MetashareSession(service));
        }
Exemple #6
0
            public void ShouldReturnPath()
            {
                Blob           blob = NewBlob();
                BlobDeployment deployment;

                using (FileSandbox sandbox = new FileSandbox(new EmptyFileLocator()))
                {
                    deployment = blob.DeployTo(sandbox.Directory);
                    deployment.Path.Should().Be(sandbox.Directory);
                }
            }
		public void ExistsFile_WithoutCreateFile_ShouldReturnFalse()
		{
			// Arrange
			var fileLocator = Fixture.Create<EmptyFileLocator>();
			var testName = Fixture.Create<string>();

			var sut = new FileSandbox(fileLocator);

			// Act && Assert
			sut.ExistsFile(testName).Should().BeFalse();
		}
Exemple #8
0
        public void ExtractEntry(ModuleEntry entry, string path, string sectionName)
        {
            if (!_module.ContainsEntry(entry))
            {
                throw new ScriptRuntimeException("Entry is not contained inside the module");
            }
            path = FileSandbox.ResolvePath(path);
            var section = TranslateSection(sectionName);

            using (var stream = _file.OpenRead())
                _module.ExtractEntry(stream, entry, section, path);
        }
		public void ExistsDirectory_AfterCreateDirectory_ShouldReturnTrue()
		{
			// Arrange
			var fileLocator = Fixture.Create<EmptyFileLocator>();
			var testName = Fixture.Create<string>();

			var sut = new FileSandbox(fileLocator);
			sut.CreateDirectory(testName);

			// Act && Assert
			sut.ExistsDirectory(testName).Should().BeTrue();
		}
Exemple #10
0
            public void ShouldReturnAllFiles()
            {
                Blob           blob = NewBlob();
                BlobDeployment deployment;

                using (FileSandbox sandbox = new FileSandbox(new EmptyFileLocator()))
                {
                    deployment = blob.DeployTo(sandbox.Directory);
                    deployment.Files.Should().ContainSingle(x =>
                                                            x.Name == "file" && x.Path == Path.Combine(deployment.Path, "file"));
                }
            }
		public void ResolvePath_ShouldRetunrnPathToFileInSandbox()
		{
			// Arrange
			var fileLocator = Fixture.Create<IFileLocator>();
			var fileName = Fixture.Create<string>();
			var sut = new FileSandbox(fileLocator);

			// Act
			var path = sut.ResolvePath(fileName);

			// Assert
			path.Should().Be(Path.Combine(sut.Directory, fileName));
		}
		public void Directory_ShouldReturnDirectoryInWindowsTempDirectory()
		{
			// Arrange
			var fileLocator = Fixture.Create<IFileLocator>();
			var sut = new FileSandbox(fileLocator);

			var tempDirectory = Path.GetTempPath();

			// Act
			var sandboxDirectory = sut.Directory;

			// Assert
			sandboxDirectory.StartsWith(tempDirectory).Should().BeTrue();
		}
Exemple #13
0
 public void MyFileSystemTest()
 {
     using (FileSandbox sandbox = new FileSandbox())
     {
         // Getting Temp file name to use
         string tempfile = sandbox.GetTempFileName("txt");
         // Get the current executing assembly (in this case it's the test dll)
         Assembly myassembly = Assembly.GetExecutingAssembly();
         // Get the stream (embedded resource) - be sure to wrap in a using block
         using (Stream stream = myassembly.GetManifestResourceStream("TestClass.TestFiles.TextFile1.txt"))
         {
             // In this case using an external method to write the stream to the file system
             tempfile = TestHelper.StreamToFile(stream, tempfile);
             string[] lines = File.ReadAllLines(tempfile);
             Assert.IsTrue(lines.Length > 0);
         }
     }
 }
Exemple #14
0
        public MetafileSession Start(bool completed = false)
        {
            Metainfo metainfo = null;

            byte[] data = null;

            using (IFileSandbox temp = new FileSandbox(new EmptyFileLocator()))
            {
                MetainfoBuilder builder = new MetainfoBuilder(temp.Directory);
                string          path    = temp.CreateFile("debian-8.5.0-amd64-CD-1.iso");

                File.WriteAllBytes(path, Bytes.Random(20000));
                builder.AddFile(path);

                metainfo = builder.ToMetainfo(out data);
            }

            IFileSandbox sandbox     = new FileSandbox(new EmptyFileLocator());
            string       destination = Path.Combine(sandbox.Directory, metainfo.Hash.ToString());

            if (completed)
            {
                File.WriteAllBytes(destination, data);
            }

            MetafileService service =
                new MetafileBuilder()
                .WithHash(metainfo.Hash)
                .WithDestination(destination)
                .WithFiles(files)
                .WithPipeline(pipeline)
                .Build();

            TaskCompletionSource <bool> onVerified = new TaskCompletionSource <bool>();

            service.Hooks.OnMetafileVerified += _ => onVerified.SetResult(true);

            return(new MetafileSession(sandbox, metainfo, destination, data, service, onVerified.Task));
        }
Exemple #15
0
        public MetagetSession Start(bool completed = false)
        {
            Metainfo metainfo;

            byte[] bytes = Bytes.Random(20000);

            using (FileSandbox temp = new FileSandbox(new EmptyFileLocator()))
            {
                MetainfoBuilder builder = new MetainfoBuilder(temp.Directory);
                string          path    = temp.CreateFile("debian-8.5.0-amd64-CD-1.iso");

                File.WriteAllBytes(path, bytes);
                builder.AddFile(path);

                metainfo = builder.ToMetainfo(out bytes);
            }

            FileSandbox sandbox     = new FileSandbox(new EmptyFileLocator());
            string      destination = Path.Combine(sandbox.Directory, metainfo.Hash.ToString());

            MetagetData data = new MetagetData(bytes);

            if (completed)
            {
                File.WriteAllBytes(destination + ".metainfo", data.ToBytes());
            }

            MetagetService metaget =
                new MetagetBuilder()
                .WithHash(metainfo.Hash)
                .WithPipeline(new PipelineSimulator())
                .WithGlue(A.Fake <MetagetGlue>())
                .WithMetafile(A.Fake <MetagetMetafile>())
                .Build();

            return(new MetagetSession(sandbox, metainfo.Hash, data, metaget));
        }
		public void CreateTempFile_ShouldCreateFile()
		{
			// Arrange
			var fileLocator = Fixture.Create<IFileLocator>();
			var sut = new FileSandbox(fileLocator);

			// Act
			var createdFilePath = sut.CreateTempFile();

			// Assert
			File.Exists(createdFilePath).Should().BeTrue();
		}
Exemple #17
0
 public static UInt64Proxy ReadId(string path)
 {
     path = FileSandbox.ResolvePath(path);
     using (var stream = File.OpenRead(path))
         return(new UInt64Proxy(AusarModule.ReadId(stream)));
 }
Exemple #18
0
 public void SetUp()
 {
     sandbox = new FileSandbox(new EmptyFileLocator());
 }
		public void Dispose_ShouldDeleteSanboxDirectory()
		{
			// Arrange
			var fileLocator = Fixture.Create<IFileLocator>();
			string sandboxDirectory;
			using (var sut = new FileSandbox(fileLocator))
			{
				sandboxDirectory = sut.Directory;
				Directory.Exists(sandboxDirectory).Should().BeTrue();
			}

			Directory.Exists(sandboxDirectory).Should().BeFalse();
		}
		public void ProvideDirectories_ShouldCreateDirectories()
		{
			// Arrange
			var fileLocator = Fixture.Create<IFileLocator>();
			var directories = Fixture.CreateMany<string>(3);
			var files = Fixture.CreateMany<string>(3);

			A.CallTo(() => fileLocator.EnumeratePath("")).Returns(files);
			A.CallTo(() => fileLocator.Exists(A<string>.Ignored)).Returns(true);

			var sut = new FileSandbox(fileLocator);

			// Act
			Action a = () => sut.CreateDirectories(directories.ToArray());
			a.Invoke();

			var directoryList = directories.ToList();

			// Assert
			Directory.Exists(Path.Combine(sut.Directory, directoryList[0])).Should().BeTrue();
			Directory.Exists(Path.Combine(sut.Directory, directoryList[1])).Should().BeTrue();
			Directory.Exists(Path.Combine(sut.Directory, directoryList[2])).Should().BeTrue();
		}
		public void ProvideDirectory_IfFilesDoNotExist_ShouldThrow()
		{
			// Arrange
			var directoryName = Fixture.Create<string>();
			var fileLocator = Fixture.Create<IFileLocator>();
			var files = Fixture.CreateMany<string>(3);

			A.CallTo(() => fileLocator.EnumeratePath(directoryName)).Returns(files);
			A.CallTo(() => fileLocator.Exists(A<string>.Ignored)).Returns(false);

			var sut = new FileSandbox(fileLocator);

			// Act
			Action a = () => sut.ProvideDirectory(directoryName);

			// Assert
			a.ShouldThrow<FileNotFoundException>();
		}
		public void ProvideDirectory_ShouldCreateSubDirectoryWithIncludedFiles()
		{
			// Arrange
			var directoryName = "testdata";
			var fileLocator = Fixture.Create<IFileLocator>();
			var files = new[] { "testdata/test/test2.txt", "testdata/abc/sample.txt", "testdata/heinz.doc" };

			A.CallTo(() => fileLocator.EnumeratePath(directoryName)).Returns(files);
			A.CallTo(() => fileLocator.Exists(A<string>.Ignored)).Returns(true);

			var sut = new FileSandbox(fileLocator);

			// Act
			sut.ProvideDirectory(directoryName);

			// Assert
			Directory.Exists(Path.Combine(sut.Directory, "testdata/test")).Should().BeTrue();
			Directory.Exists(Path.Combine(sut.Directory, "testdata/abc")).Should().BeTrue();
		}
		public void ProvideDirectory_IfEnumeratePathReturnFilledList_ShouldProvideDirectoryWithFilesToSandbox()
		{
			// Arrange
			var directoryName = Fixture.Create<string>();
			var fileLocator = Fixture.Create<IFileLocator>();
			var files = Fixture.CreateMany<string>(3);

			A.CallTo(() => fileLocator.EnumeratePath(directoryName)).Returns(files);
			A.CallTo(() => fileLocator.Exists(A<string>.Ignored)).Returns(true);

			var sut = new FileSandbox(fileLocator);

			// Act
			sut.ProvideDirectory(directoryName);

			// Assert
			A.CallTo(() => fileLocator.CopyTo(A<string>.Ignored, A<string>.Ignored)).MustHaveHappened(Repeated.Exactly.Times(3));
		}
		public void ProvideDirectory_IfEnumeratePathReturnEmptyList_ShouldProvideEmptyDirectoryToSandbox(string directoryName)
		{
			// Arrange
			var fileLocator = Fixture.Create<IFileLocator>();
			A.CallTo(() => fileLocator.EnumeratePath(directoryName)).Returns(Enumerable.Empty<string>());

			var sut = new FileSandbox(fileLocator);

			var expectedDirectoryPath = Path.Combine(sut.Directory, directoryName);

			// Act && Assert
			sut.ProvideDirectory(directoryName).Should().Be(expectedDirectoryPath);
			Directory.Exists(expectedDirectoryPath).Should().BeTrue();
		}
		public void ProvideFile_IfFileDoesNotExist_ShouldThrow()
		{
			// Arrange
			var fileName = Fixture.Create<string>();
			var fileLocator = Fixture.Create<IFileLocator>();
			A.CallTo(() => fileLocator.Exists(fileName)).Returns(false);

			var sut = new FileSandbox(fileLocator);

			// Act
			Action a = () => sut.ProvideFile(fileName);

			// Assert
			a.ShouldThrow<FileNotFoundException>();
		}
Exemple #26
0
 public void TearDown()
 {
     sandbox?.Dispose();
     sandbox = null;
 }
		public void ProvideFile_ShouldProvideFileToSandbox()
		{
			// Arrange
			var fileName = Fixture.Create<string>();
			var fileLocator = Fixture.Create<IFileLocator>();
			A.CallTo(() => fileLocator.Exists(fileName)).Returns(true);

			var sut = new FileSandbox(fileLocator);

			// Act && Assert
			sut.ProvideFile(fileName).ShouldBeEquivalentTo(sut.ResolvePath(fileName));
		}
		public void GetTempFile_ShouldNotCreateTempFile()
		{
			// Arrange
			var fileLocator = Fixture.Create<IFileLocator>();
			var sut = new FileSandbox(fileLocator);

			// Act
			var tempFile = sut.GetTempFile();

			// Assert
			File.Exists(tempFile).Should().BeFalse();
		}
		public void GetTempFile_ShouldReturnRandomFilePath()
		{
			// Arrange
			var fileLocator = Fixture.Create<IFileLocator>();
			var sut = new FileSandbox(fileLocator);

			// Act
			var tempFile = sut.GetTempFile();

			// Assert
			tempFile.Should().NotBeEmpty();
			tempFile.StartsWith(sut.Directory);
		}
		public void CreateDirectory_ShouldCreateDirectory(string directoryName)
		{
			// Arrange
			var fileLocator = Fixture.Create<IFileLocator>();
			var sut = new FileSandbox(fileLocator);
			var expectedPath = Path.Combine(sut.Directory, directoryName);

			// Act
			var path = sut.CreateDirectory(directoryName);

			// Assert
			path.Should().Be(expectedPath);
			Directory.Exists(path).Should().BeTrue();
		}