private void describe_WriteTarStreamToPath() { string destinationArchiveFileName = null; Mock <IContainer> containerMock = null; LocalPrincipalManager userManager = null; string username = null; before = () => { Helpers.AssertAdministratorPrivileges(); userManager = new LocalPrincipalManager(); var guid = System.Guid.NewGuid().ToString("N"); username = "******" + guid.Substring(0, 6); var credentials = userManager.CreateUser(username); containerMock = new Mock <IContainer>(); containerMock.Setup(x => x.ImpersonateContainerUser(It.IsAny <Action>())).Callback((Action x) => x()); Directory.CreateDirectory(Path.Combine(inputDir, "fooDir")); File.WriteAllText(Path.Combine(inputDir, "content.txt"), "content"); File.WriteAllText(Path.Combine(inputDir, "fooDir", "content.txt"), "MOAR content"); new TarStreamService().CreateTarFromDirectory(inputDir, tarFile); tarStream = new FileStream(tarFile, FileMode.Open); }; context["when the tar stream contains files and directories"] = () => { act = () => tarStreamService.WriteTarStreamToPath(tarStream, containerMock.Object, outputDir); it["writes the file to disk"] = () => { File.ReadAllLines(Path.Combine(outputDir, "content.txt")).should_be("content"); File.ReadAllLines(Path.Combine(outputDir, "fooDir", "content.txt")).should_be("MOAR content"); }; }; after = () => { tarStream.Close(); File.Delete(tarFile); userManager.DeleteUser(username); }; }
private void describe_WriteTarStreamToPath() { Mock <IContainer> containerMock = null; string mappedPath = null; string tmpPath = null; before = () => { containerMock = new Mock <IContainer>(); mappedPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); var binPath = Path.Combine(mappedPath, "bin"); Directory.CreateDirectory(binPath); containerMock.Setup(x => x.Directory.MapBinPath(It.IsIn(new List <string>() { "tar.exe" }.AsEnumerable()))) .Returns(Path.Combine(binPath, "tar.exe")); containerMock.Setup(x => x.Directory.MapBinPath(It.IsIn(new List <string>() { "zlib1.dll" }.AsEnumerable()))) .Returns(Path.Combine(binPath, "zlib1.dll")); containerMock.Setup(x => x.Directory.MapBinPath(It.IsNotIn(new List <string>() { "tar.exe", "zlib1.dll" }.AsEnumerable()))) .Returns(Path.Combine(binPath, Path.GetRandomFileName())); tmpPath = Path.GetTempFileName(); tarStream = new FileStream(tmpPath, FileMode.Open); }; after = () => { tarStream.Close(); File.Delete(tmpPath); }; context["when copying tar.exe to localPath"] = () => { before = () => { var processMock = new Mock <IContainerProcess>(); processMock.Setup(x => x.WaitForExit()).Returns(0); containerMock.Setup(x => x.Run(It.IsAny <ProcessSpec>(), It.IsAny <IProcessIO>())) .Returns(processMock.Object); }; act = () => tarStreamService.WriteTarStreamToPath(tarStream, containerMock.Object, outputDir); it["expects the tar.exe to exists in container bin directory"] = () => { var tarPath = containerMock.Object.Directory.MapBinPath("tar.exe"); File.Exists(tarPath).should_be_true(); var zlibPath = containerMock.Object.Directory.MapBinPath("zlib1.dll"); File.Exists(zlibPath).should_be_true(); }; }; context["when the tar process returns a zero exit code"] = () => { before = () => { var processMock = new Mock <IContainerProcess>(); processMock.Setup(x => x.WaitForExit()).Returns(0); containerMock.Setup(x => x.Run(It.IsAny <ProcessSpec>(), It.IsAny <IProcessIO>())) .Returns(processMock.Object); }; act = () => tarStreamService.WriteTarStreamToPath(tarStream, containerMock.Object, outputDir); it["extracts the tar to the container"] = () => { var arguments = new[] { "xf", "-C", outputDir }; // Verify the processes arguments are a subset of the expected arguments. // We cannot check the temp file argument as it is random. containerMock.Verify(x => x.Run(It.Is <ProcessSpec>(p => !arguments.Except(p.Arguments).Any()), null)); }; }; context["when the tar process returns a non-zero exit code"] = () => { before = () => { var processMock = new Mock <IContainerProcess>(); processMock.Setup(x => x.WaitForExit()).Returns(1); containerMock.Setup(x => x.Run(It.IsAny <ProcessSpec>(), It.IsAny <IProcessIO>())) .Returns(processMock.Object); }; it["throws an exception"] = () => { var passed = false; try { tarStreamService.WriteTarStreamToPath(tarStream, containerMock.Object, outputDir); } catch (Exception) { passed = true; } passed.should_be_true(); }; }; }