예제 #1
0
        public ContainerTests()
        {
            ConstrainedProcessRunner = Substitute.For<IProcessRunner>();
            ContainerEnvironment = new Dictionary<string, string>() { { "Handle", "handle" } };

            Directory = Substitute.For<IContainerDirectory>();

            JobObject = Substitute.For<JobObject>();
            JobObject.GetCpuStatistics().Returns(new CpuStatistics
            {
                TotalKernelTime = TimeSpan.Zero,
                TotalUserTime = TimeSpan.Zero,
            });
            JobObject.GetProcessIds().Returns(new int[0]);

            ProcessHelper = Substitute.For<ProcessHelper>();
            ProcessRunner = Substitute.For<IProcessRunner>();
            ContainerPropertiesService = Substitute.For<IContainerPropertyService>();

            TcpPortManager = Substitute.For<ILocalTcpPortManager>();

            User = Substitute.For<IContainerUser>();
            _containerUsername = string.Concat("container-username-", Guid.NewGuid().ToString());
            User.UserName.Returns(_containerUsername);

            DiskQuotaControl = Substitute.For<DiskQuotaControl>();

            DependencyHelper = Substitute.For<ContainerHostDependencyHelper>();

            Container = new Container(
                string.Concat("id-", Guid.NewGuid()),
                string.Concat("handle-", Guid.NewGuid()),
                User,
                Directory,
                ContainerPropertiesService,
                TcpPortManager,
                JobObject,
                DiskQuotaControl,
                ProcessRunner,
                ConstrainedProcessRunner,
                ProcessHelper,
                ContainerEnvironment,
                DependencyHelper);
        }
예제 #2
0
        IContainer RestoreContainerFromPath(string containerPath)
        {
            var id = Path.GetFileName(containerPath);

            var user = ContainerUser.Restore(userManager, id);
            var directory = ContainerDirectory.Restore(fileSystem, containerPath);

            var jobObjectName = id;
            var jobObject = new JobObject(jobObjectName);

            var environment = new Dictionary<string, string>();
            var processHelper = new ProcessHelper();

            var diskQuotaControl = new DiskQuotaControl();
            diskQuotaControl.UserNameResolution = UserNameResolutionConstants.dqResolveNone;
            diskQuotaControl.Initialize(directory.Volume, true);

            var dependencyHelper = new ContainerHostDependencyHelper();

            var container = new Container(
                id,
                id, // TODO: Recover the handle from container metadata
                user,
                directory,
                containerPropertiesService,
                tcpPortManager,
                jobObject,
                diskQuotaControl,
                processRunner,
                processRunner,
                processHelper,
                environment,
                dependencyHelper);

            return container;
        }
예제 #3
0
        public IContainer CreateContainer(ContainerSpec containerSpec)
        {
            Guard.NotNull(containerSpec, "containerSpec");

            UndoStack undoStack = new UndoStack();
            IContainer container;

            try
            {
                var handle = containerSpec.Handle;
                if (String.IsNullOrEmpty(handle))
                    handle = handleHelper.GenerateHandle();

                var id = handleHelper.GenerateId(handle);

                var user = ContainerUser.Create(userManager, id);
                undoStack.Push(() => user.Delete());

                var directory = directoryFactory.Create(fileSystem, containerBasePath, id);
                directory.CreateSubdirectories(user);
                undoStack.Push(directory.Destroy);

                directory.CreateBindMounts(containerSpec.BindMounts, user);

                var jobObject = new JobObject(id);
                undoStack.Push(() => jobObject.Dispose());

                var containerHostClient = containerHostService.StartContainerHost(id, directory, jobObject, user.GetCredential());
                undoStack.Push(() => containerHostClient.Shutdown());

                var constrainedProcessRunner = new ConstrainedProcessRunner(containerHostClient);
                undoStack.Push(() => constrainedProcessRunner.Dispose());

                var processHelper = new ProcessHelper();
                var dependencyHelper = new ContainerHostDependencyHelper();

                var diskQuotaControl = diskQuotaManager.CreateDiskQuotaControl(directory);

                container = new Container(
                    id,
                    handle,
                    user,
                    directory,
                    containerPropertiesService,
                    tcpPortManager,
                    jobObject,
                    diskQuotaControl,
                    processRunner,
                    constrainedProcessRunner,
                    processHelper,
                    containerSpec.Environment,
                    dependencyHelper);

                containerPropertiesService.SetProperties(container, containerSpec.Properties);
                lock (containers)
                {
                    containers.Add(container);
                }
            }
            catch (Exception e)
            {
                try
                {
                    undoStack.UndoAll();
                    throw;
                }
                catch (AggregateException undoException)
                {
                    throw new AggregateException(new[] { e, undoException });
                }
            }

            return container;
        }