public void CleansUpWhenItFails()
            {
                var spec = new ContainerSpec
                {
                    Handle = "handle",
                };

                ContainerHostService.StartContainerHost(null, null, null, null)
                .ThrowsForAnyArgs(new Exception());

                try
                {
                    Service.CreateContainer(spec);
                }
                catch (Exception)
                {
                    // Expect this exception.
                }

                // Created and deleted the user
                UserManager.Received(1).CreateUser(Arg.Any <string>());
                UserManager.Received(1).DeleteUser(Arg.Any <string>());
                UserManager.Received(1).CreateProfile(Arg.Any <string>());
                UserManager.Received(1).DeleteProfile(Arg.Any <string>());

                containerDirectory.Received().Destroy();
            }
        public ContainerHostServiceTests()
        {
            ContainerId = Guid.NewGuid().ToString("N");

            Directory = Substitute.For <IContainerDirectory>();
            Directory.MapBinPath(null).ReturnsForAnyArgs(call => @"C:\Containers\handle\bin\" + call.Arg <string>());
            Directory.UserPath.Returns(@"C:\Containers\handle\user\");

            FileSystem = Substitute.For <FileSystemManager>();
            JobObject  = Substitute.For <JobObject>();

            Process = Substitute.For <IProcess>();
            Process.Id.Returns(100);
            Process.Handle.Returns(new IntPtr(100));
            Process.StandardError.Returns(new StringReader("OK\n"));

            ProcessRunner = Substitute.For <IProcessRunner>();
            ProcessRunner.Run(null).ReturnsForAnyArgs(Process);

            DependencyHelper = Substitute.For <ContainerHostDependencyHelper>();
            DependencyHelper.ContainerHostExe.Returns("IronFrame.Host.exe");
            DependencyHelper.ContainerHostExePath.Returns(@"C:\Path\To\IronFrame.Host.exe");
            DependencyHelper.GetContainerHostDependencies().Returns(new [] { @"C:\Path\To\IronFrame.Shared.dll" });

            FileSystem.FileExists(DependencyHelper.ContainerHostExeConfigPath).Returns(true);

            Service = new ContainerHostService(FileSystem, ProcessRunner, DependencyHelper);
        }
        public ContainerServiceTests()
        {
            ContainerBasePath  = @"C:\Containers";
            ContainerUserGroup = "ContainerUsers";

            ContainerPropertiesService = Substitute.For <IContainerPropertyService>();

            FileSystem = Substitute.For <IFileSystemManager>();

            Id = "DEADBEEF";

            HandleHelper = Substitute.For <ContainerHandleHelper>();
            HandleHelper.GenerateId(null).ReturnsForAnyArgs(Id);

            ProcessRunner  = Substitute.For <IProcessRunner>();
            TcpPortManager = Substitute.For <ILocalTcpPortManager>();
            UserManager    = Substitute.For <IUserManager>();

            ContainerHostClient = Substitute.For <IContainerHostClient>();

            ContainerHostService = Substitute.For <IContainerHostService>();
            ContainerHostService.StartContainerHost(null, null, null, null)
            .ReturnsForAnyArgs(ContainerHostClient);

            UserManager.CreateUser(null).ReturnsForAnyArgs(new NetworkCredential("username", "password"));
            sid = "S-1234";
            UserManager.GetSID(null).ReturnsForAnyArgs(sid);

            diskQuotaManager   = Substitute.For <IDiskQuotaManager>();
            containerDiskQuota = Substitute.For <IContainerDiskQuota>();
            diskQuotaManager.CreateDiskQuotaControl(null, "").ReturnsForAnyArgs(containerDiskQuota);

            var directoryFactory = Substitute.For <IContainerDirectoryFactory>();

            containerDirectory = Substitute.For <IContainerDirectory>();
            directoryFactory.Create(FileSystem, ContainerBasePath, Id).Returns(containerDirectory);

            var containerFactory = new TestContainerFactory();

            Service = new ContainerService(
                HandleHelper,
                UserManager,
                FileSystem,
                ContainerPropertiesService,
                TcpPortManager,
                ProcessRunner,
                ContainerHostService,
                diskQuotaManager,
                directoryFactory,
                containerFactory,
                ContainerBasePath
                );
        }
            public void CreatesContainerSpecificHost()
            {
                var expectedCredentials = new NetworkCredential();

                UserManager.CreateUser("").ReturnsForAnyArgs(expectedCredentials);
                var spec = new ContainerSpec
                {
                    Handle = "handle",
                };

                Service.CreateContainer(spec);

                ContainerHostService.Received(1).StartContainerHost(Arg.Any <string>(), Arg.Any <IContainerDirectory>(), Arg.Any <JobObject>(), expectedCredentials);
            }