Пример #1
0
        private void describe_()
        {
            Mock <IContainerInfoService> mockContainerInfoService = null;
            MetricsController            metricsController        = null;
            string      containerHandle = null;
            const ulong privateBytes    = 28;
            ContainerMetricsApiModel containerMetrics = null;

            before = () =>
            {
                mockContainerInfoService = new Mock <IContainerInfoService>();
                metricsController        = new MetricsController(mockContainerInfoService.Object)
                {
                    Configuration = new HttpConfiguration(),
                    Request       = new HttpRequestMessage()
                };
                containerHandle  = Guid.NewGuid().ToString();
                containerMetrics = new ContainerMetricsApiModel
                {
                    MemoryStat = new ContainerMemoryStatApiModel
                    {
                        TotalBytesUsed = privateBytes
                    }
                };

                mockContainerInfoService.Setup(x => x.GetMetricsByHandle(containerHandle))
                .Returns(() => containerMetrics);
            };

            describe[Controller.Show] = () =>
            {
                IHttpActionResult result = null;

                act = () => result = metricsController.Show(containerHandle);

                it["returns a successful status code"] = () =>
                {
                    result.VerifiesSuccessfulStatusCode();
                };

                it["returns the container metrics as a json"] = () =>
                {
                    var message = result.should_cast_to <JsonResult <ContainerMetricsApiModel> >();
                    message.Content.should_be(containerMetrics);
                };

                context["when the container does not exist"] = () =>
                {
                    before = () => containerMetrics = null;

                    it["returns a 404"] = () =>
                    {
                        var message = result.should_cast_to <ResponseMessageResult>();
                        message.Response.StatusCode.should_be(HttpStatusCode.NotFound);
                    };
                };
            };
        }
Пример #2
0
        private void describe_()
        {
            describe[Controller.Index] = () =>
            {
                Mock <IContainerInfoService> mockContainerService = null;
                string            handle     = "container-handle";
                InfoController    controller = null;
                IHttpActionResult result     = null;

                before = () =>
                {
                    mockContainerService = new Mock <IContainerInfoService>();
                    controller           = new InfoController(mockContainerService.Object);
                };

                act = () => result = controller.GetInfo(handle);


                context["when the container exists"] = () =>
                {
                    ContainerInfo info = null;

                    before = () =>
                    {
                        info = new ContainerInfo();
                        mockContainerService.Setup(x => x.GetInfoByHandle(handle))
                        .Returns(info);
                    };

                    it["returns info about the container"] = () =>
                    {
                        var jsonResult = result.should_cast_to <JsonResult <ContainerInfo> >();
                        jsonResult.Content.should_be(info);
                    };
                };

                context["when the container does not exist"] = () =>
                {
                    before = () =>
                    {
                        mockContainerService.Setup(x => x.GetInfoByHandle(handle))
                        .Returns((ContainerInfo)null);
                    };

                    it["returns not found"] = () =>
                    {
                        result.should_cast_to <NotFoundResult>();
                    };
                };
            };
        }
Пример #3
0
        private void describe_()
        {
            Mock <IContainerService> mockContainerService = null;
            Mock <IContainer>        mockContainer        = null;
            PropertiesController     propertiesController = null;
            string containerHandle = null;
            string key             = null;
            string value           = null;

            before = () =>
            {
                mockContainerService = new Mock <IContainerService>();
                mockContainer        = new Mock <IContainer>();
                propertiesController = new PropertiesController(mockContainerService.Object)
                {
                    Configuration = new HttpConfiguration(),
                    Request       = new HttpRequestMessage()
                };
                containerHandle = Guid.NewGuid().ToString();
                key             = "some:key";
                value           = "value";

                mockContainerService.Setup(x => x.GetContainerByHandle(containerHandle))
                .Returns(() =>
                {
                    return(mockContainer != null ? mockContainer.Object : null);
                });
            };

            /*
             * describe[Controller.Index] = () =>
             * {
             *  Dictionary<string,string> result = null;
             *  Dictionary<string,string> properties = null;
             *
             *  before = () =>
             *  {
             *      properties = new Dictionary<string,string> {
             *          { "wardrobe", "a lion" },
             *          { "a hippo", "the number 25" }
             *      };
             *      mockPropertyService.Setup(x => x.GetProperties(mockContainer.Object))
             *          .Returns(() => properties);
             *
             *     result = propertiesController.Index(containerHandle);
             *  };
             *
             *  it["returns the correct property value"] = () =>
             *  {
             *      result.should_be(properties);
             *  };
             * };
             * */

            describe[Controller.Show] = () =>
            {
                IHttpActionResult result        = null;
                string            propertyValue = null;

                before = () =>
                {
                    propertyValue = "a lion, a hippo, the number 25";
                    mockContainer.Setup(x => x.GetProperty(key)).Returns(() => propertyValue);
                };

                act = () => result = propertiesController.Show(containerHandle, key);

                it["returns a successful status code"] = () =>
                {
                    result.VerifiesSuccessfulStatusCode();
                };

                it["returns the correct property value"] = () =>
                {
                    var actualValue = result.should_cast_to <JsonResult <string> >();
                    actualValue.Content.should_be(propertyValue);
                };

                context["when the property does not exist"] = () =>
                {
                    before = () => propertyValue = null;
                    it["returns a 404"] = () =>
                    {
                        var message = result.should_cast_to <ResponseMessageResult>();
                        message.Response.StatusCode.should_be(HttpStatusCode.NotFound);
                    };
                };

                context["when the container does not exist"] = () =>
                {
                    before = () => mockContainer = null;
                    it["returns a 404"] = () =>
                    {
                        var message = result.should_cast_to <ResponseMessageResult>();
                        message.Response.StatusCode.should_be(HttpStatusCode.NotFound);
                    };
                };
            };

            describe[Controller.Update] = () =>
            {
                IHttpActionResult result = null;
                before = () =>
                {
                    propertiesController.Request.Content = new StringContent(value);
                    result = propertiesController.Update(containerHandle, key).Result;
                };

                it["calls the propertyService set method"] = () =>
                {
                    mockContainer.Verify(x => x.SetProperty(key, value));
                };

                it["returns a successful status code"] = () =>
                {
                    result.VerifiesSuccessfulStatusCode();
                };
            };

            describe[Controller.Destroy] = () =>
            {
                IHttpActionResult result = null;
                before = () =>
                {
                    result = propertiesController.Destroy(containerHandle, key).Result;
                };

                it["calls the propertyService destroy method"] = () =>
                {
                    mockContainer.Verify(x => x.RemoveProperty(key));
                };

                it["returns a successful status code"] = () =>
                {
                    result.VerifiesSuccessfulStatusCode();
                };
            };
        }
        private void describe_()
        {
            ContainerProcessController containerProcessController = null;
            Mock <IContainerService>   mockContainerService       = null;
            int pid = 9876;

            before = () =>
            {
                mockContainerService       = new Mock <IContainerService>();
                containerProcessController = new ContainerProcessController(mockContainerService.Object)
                {
                    Configuration = new HttpConfiguration(),
                    Request       = new HttpRequestMessage()
                };
            };


            describe["Stop"] = () =>
            {
                IHttpActionResult result = null;
                act = () => result = containerProcessController.Stop("handle", pid);

                context["container exists"] = () =>
                {
                    Mock <IContainer> mockContainer = null;
                    before = () =>
                    {
                        mockContainer = mockContainerWithHandle("handle");
                        mockContainerService.Setup(x => x.GetContainerByHandle("handle"))
                        .Returns(mockContainer.Object);
                    };

                    context["process exists"] = () =>
                    {
                        Mock <IContainerProcess> mockProcess = null;
                        before = () =>
                        {
                            mockProcess = new Mock <IContainerProcess>();
                            mockContainer.Setup(x => x.FindProcessById(pid)).Returns(mockProcess.Object);
                        };

                        it["returns OK"]            = () => result.should_cast_to <OkResult>();
                        it["calls Kill on process"] = () => mockProcess.Verify(x => x.Kill());
                    };

                    context["process does not exist"] = () =>
                    {
                        before = () => mockContainer.Setup(x => x.FindProcessById(pid)).Returns(null as IContainerProcess);

                        it["returns NotFound"] = () => result.should_cast_to <NotFoundResult>();
                    };
                };

                context["container does not exist"] = () =>
                {
                    before = () => mockContainerService.Setup(x => x.GetContainerByHandle("handle")).Returns(null as IContainer);

                    it["returns NotFound"] = () => result.should_cast_to <NotFoundResult>();
                };
            };
        }
        private void describe_()
        {
            Mock <IContainerService> mockContainerService = null;
            GraceTimeController      GraceController      = null;
            string handle = null;

            Mock <IContainer> mockContainer = null;

            before = () =>
            {
                mockContainerService = new Mock <IContainerService>();
                GraceController      = new GraceTimeController(mockContainerService.Object);

                handle = Guid.NewGuid().ToString();

                mockContainer = new Mock <IContainer>();
                mockContainerService.Setup(x => x.GetContainerByHandle(handle)).Returns(mockContainer.Object);
            };

            describe["#SetGraceTime"] = () =>
            {
                const long        graceTimeInNanoSeconds = 1000 * 1000 * 10;
                GraceTime         grace  = null;
                IHttpActionResult result = null;

                before = () =>
                {
                    grace = new GraceTime {
                        GraceTimeInNanoSeconds = graceTimeInNanoSeconds
                    };
                };
                act = () =>
                {
                    result = GraceController.SetGraceTime(handle, grace);
                };

                it["sets the grace time on the container"] = () =>
                {
                    mockContainer.Verify(x => x.SetProperty("GraceTime", graceTimeInNanoSeconds.ToString()));
                };

                context["when grace time is null"] = () =>
                {
                    before = () =>
                    {
                        grace = new GraceTime {
                        };
                    };

                    it["sets the grace time on the container to null"] = () =>
                    {
                        mockContainer.Verify(x => x.SetProperty("GraceTime", null));
                    };
                };

                context["when the container does not exist"] = () =>
                {
                    before = () =>
                    {
                        mockContainerService.Setup(x => x.GetContainerByHandle(It.IsAny <string>())).Returns(null as IContainer);
                    };

                    it["Returns not found"] = () =>
                    {
                        result.should_cast_to <NotFoundResult>();
                    };
                };
            };

            describe["#GetGraceTime"] = () =>
            {
                it["returns the current grace time of the container"] = () =>
                {
                    mockContainer.Setup(x => x.GetProperty("GraceTime")).Returns("1234");

                    var result     = GraceController.GetGraceTime(handle);
                    var jsonResult = result.should_cast_to <JsonResult <GraceTime> >();
                    jsonResult.Content.GraceTimeInNanoSeconds.should_be(1234);
                };

                context["when grace time is null"] = () =>
                {
                    before = () =>
                    {
                        mockContainer.Setup(x => x.GetProperty("GraceTime")).Returns <System.Func <string> >(null);
                    };

                    it["sets the grace time on the container to null"] = () =>
                    {
                        var result     = GraceController.GetGraceTime(handle);
                        var jsonResult = result.should_cast_to <JsonResult <GraceTime> >();
                        jsonResult.Content.GraceTimeInNanoSeconds.should_be(null);
                    };
                };

                context["when the container does not exist"] = () =>
                {
                    before = () =>
                    {
                        mockContainerService.Setup(x => x.GetContainerByHandle(It.IsAny <string>())).Returns(null as IContainer);
                    };

                    it["Returns not found"] = () =>
                    {
                        var result = GraceController.GetGraceTime(handle);
                        result.should_cast_to <NotFoundResult>();
                    };
                };
            };
        }
Пример #6
0
        private void describe_()
        {
            Mock <IContainerService> mockContainerService = null;
            NetController            netController        = null;

            before = () =>
            {
                mockContainerService = new Mock <IContainerService>();

                netController = new NetController(mockContainerService.Object);
            };

            describe["#NetIn"] = () =>
            {
                string            containerId            = null;
                var               requestedHostPort      = 0;
                var               requestedContainerPort = 5678;
                IHttpActionResult result        = null;
                Mock <IContainer> mockContainer = null;

                before = () =>
                {
                    containerId       = Guid.NewGuid().ToString();
                    requestedHostPort = 0;
                    mockContainer     = new Mock <IContainer>();

                    mockContainerService.Setup(x => x.GetContainerByHandle(containerId)).Returns(mockContainer.Object);
                };

                act = () =>
                {
                    result = netController.NetIn(containerId,
                                                 new NetInRequest {
                        ContainerPort = requestedContainerPort, HostPort = requestedHostPort
                    });
                };

                it["reserves the Port in the container"] = () =>
                {
                    mockContainer.Verify(x => x.ReservePort(requestedHostPort));
                };

                context["when the container does not exist"] = () =>
                {
                    before = () =>
                    {
                        mockContainerService.Setup(x => x.GetContainerByHandle(It.IsAny <string>()))
                        .Returns(null as IContainer);
                    };

                    it["Returns not found"] = () =>
                    {
                        result.should_cast_to <NotFoundResult>();
                    };
                };

                context["reserving the Port in the container succeeds and returns a Port"] = () =>
                {
                    const int returnedPort = 8765;
                    before = () =>
                    {
                        mockContainer.Setup(x => x.ReservePort(requestedHostPort)).Returns(returnedPort);
                    };

                    it["calls reservePort on the container"] = () =>
                    {
                        mockContainer.Verify(x => x.ReservePort(requestedHostPort));
                    };

                    context["container reservePort succeeds and returns a Port"] = () =>
                    {
                        before = () =>
                        {
                            mockContainer.Setup(x => x.ReservePort(requestedHostPort)).Returns(returnedPort);
                        };

                        it["returns the Port that the net in service returns"] = () =>
                        {
                            var jsonResult = result.should_cast_to <JsonResult <NetInResponse> >();
                            jsonResult.Content.HostPort.should_be(8765);
                        };


                        it["sets the containerport property key lookup"] = () =>
                        {
                            mockContainer.Verify(
                                x => x.SetProperty("ContainerPort:" + requestedContainerPort.ToString(), returnedPort.ToString()));
                        };
                    };

                    context["reserving the Port in the container fails and throws an exception"] = () =>
                    {
                        before = () =>
                        {
                            mockContainer.Setup(x => x.ReservePort(requestedHostPort)).Throws(new Exception("BOOM"));
                        };

                        it["returns an error"] = () =>
                        {
                            var errorResult = result.should_cast_to <ExceptionResult>();
                            errorResult.Exception.Message.should_be("BOOM");
                        };
                    };
                };
            };

            describe["#NetOut"] = () =>
            {
                string                  containerId      = null;
                IHttpActionResult       result           = null;
                Mock <IContainer>       mockContainer    = null;
                Mock <FirewallRuleSpec> firewallRuleSpec = null;

                before = () =>
                {
                    containerId      = Guid.NewGuid().ToString();
                    mockContainer    = new Mock <IContainer>();
                    firewallRuleSpec = new Mock <FirewallRuleSpec>();
                    mockContainerService.Setup(x => x.GetContainerByHandle(containerId)).Returns(mockContainer.Object);
                };

                act = () =>
                {
                    result = netController.NetOut(containerId, firewallRuleSpec.Object);
                };

                it["reserves the Port in the container"] = () =>
                {
                    mockContainer.Verify(x => x.CreateOutboundFirewallRule(firewallRuleSpec.Object));
                };
            };
        }
Пример #7
0
        private void describe_()
        {
            ContainersController     containersController = null;
            Mock <IContainerService> mockContainerService = null;
            Mock <ILogger>           mockLogger           = null;
            MockOptions mockOptions = null;

            before = () =>
            {
                mockContainerService = new Mock <IContainerService>();
                mockLogger           = new Mock <ILogger>();
                mockOptions          = new MockOptions();

                containersController = new ContainersController(mockContainerService.Object, mockLogger.Object, mockOptions)
                {
                    Configuration = new HttpConfiguration(),
                    Request       = new HttpRequestMessage()
                };
            };


            describe[Controller.Index] = () =>
            {
                IReadOnlyList <string> result         = null;
                Mock <IContainer>      mockContainer1 = null;

                before = () =>
                {
                    mockContainer1 = mockContainerWithHandle("handle1");
                    var mockContainer2 = mockContainerWithHandle("handle2");
                    var mockContainer3 = mockContainerWithHandle("handle3");

                    mockContainerService.Setup(x => x.GetContainers())
                    .Returns(new List <IContainer>
                    {
                        mockContainer1.Object,
                        mockContainer2.Object,
                        mockContainer3.Object
                    });

                    mockContainer1.Setup(x => x.GetProperties()).Returns(new Dictionary <string, string> {
                        { "a", "b" }
                    });
                    mockContainer2.Setup(x => x.GetProperties()).Returns(new Dictionary <string, string> {
                        { "a", "b" }, { "c", "d" }, { "e", "f" }
                    });
                    mockContainer3.Setup(x => x.GetProperties()).Returns(new Dictionary <string, string> {
                        { "e", "f" }
                    });
                };

                it["when filter is provided returns a filtered list of container id's as strings"] = () =>
                {
                    result = containersController.Index("{\"a\":\"b\", \"e\":\"f\"}");

                    result.should_not_be_null();
                    result.Count.should_be(1);
                    result.should_contain("handle2");
                };

                it["without filter returns a list of all containers id's as strings"] = () =>
                {
                    result = containersController.Index();

                    result.should_not_be_null();
                    result.Count.should_be(3);
                    result.should_contain("handle1");
                    result.should_contain("handle2");
                    result.should_contain("handle3");
                };

                it["filters out destroyed containers when a query is passed"] = () =>
                {
                    mockContainer1.Setup(x => x.GetProperties())
                    .Returns(() => { throw new InvalidOperationException(); });

                    result = containersController.Index("{}");
                    result.should_not_contain("handle1");
                    result.should_contain("handle2");
                    result.should_contain("handle3");
                };

                it["includes partially destroyed containers when a query is passed"] = () =>
                {
                    mockContainer1.Setup(x => x.GetProperties())
                    .Returns(() => { throw new DirectoryNotFoundException(); });

                    result = containersController.Index("{}");
                    result.should_contain("handle1");
                    result.should_contain("handle2");
                    result.should_contain("handle3");
                };
            };

            describe["#Create"] = () =>
            {
                Mock <IContainer>     mockContainer = null;
                ContainerSpecApiModel containerSpec = null;
                before = () =>
                {
                    mockContainer = mockContainerWithHandle("thisHandle");
                    containerSpec = new ContainerSpecApiModel
                    {
                        Limits = new Limits
                        {
                            MemoryLimits = new MemoryLimits {
                                LimitInBytes = 500
                            },
                            CpuLimits = new CpuLimits {
                                Weight = 9999
                            },
                            DiskLimits = new DiskLimits {
                                ByteHard = 999
                            }
                        },
                        GraceTime = 1234
                    };
                };

                context["on success"] = () =>
                {
                    before = () =>
                    {
                        mockContainerService.Setup(x => x.CreateContainer(It.IsAny <ContainerSpec>()))
                        .Returns(mockContainer.Object);
                    };

                    it["returns a handle"] = () =>
                    {
                        var result = containersController.Create(containerSpec);
                        result.Handle.should_be("thisHandle");
                    };

                    it["sets ActiveProcessLimit on the Container"] = () =>
                    {
                        containersController.Create(containerSpec);
                        mockContainer.Verify(x => x.SetActiveProcessLimit(15));
                    };

                    it["sets PriorityClass on the Container"] = () =>
                    {
                        containersController.Create(containerSpec);
                        mockContainer.Verify(x => x.SetPriorityClass(ProcessPriorityClass.BelowNormal));
                    };

                    it["sets limits on the container"] = () =>
                    {
                        containersController.Create(containerSpec);
                        mockContainer.Verify(x => x.LimitMemory(500));
                        mockContainer.Verify(x => x.LimitDisk(999));
                    };

                    it["sets GraceTime on the Container"] = () =>
                    {
                        containersController.Create(containerSpec);
                        mockContainer.Verify(x => x.SetProperty("GraceTime", "1234"));
                    };

                    it["doesn't set limits when the values are null"] = () =>
                    {
                        containerSpec.Limits.MemoryLimits.LimitInBytes = null;
                        containerSpec.Limits.CpuLimits.Weight          = null;
                        containerSpec.Limits.DiskLimits.ByteHard       = null;

                        containersController.Create(containerSpec);

                        mockContainer.Verify(x => x.LimitMemory(It.IsAny <ulong>()), Times.Never());
                        mockContainer.Verify(x => x.LimitDisk(It.IsAny <ulong>()), Times.Never());
                    };

                    it["hard-codes the CPU limits to 5"] = () =>
                    {
                        containersController.Create(containerSpec);
                        mockContainer.Verify(x => x.LimitCpu(5));
                    };
                };

                context["on failure"] = () =>
                {
                    before = () => mockContainerService.Setup(x => x.CreateContainer(It.IsAny <ContainerSpec>())).Throws(new Exception());

                    it["throws HttpResponseException"] = () =>
                    {
                        expect <HttpResponseException>(() => containersController.Create(containerSpec));
                    };
                };
            };

            describe["#NetIn"] = () =>
            {
                string containerHandle              = null;
                string containerPath                = null;
                ContainerSpecApiModel specModel     = null;
                CreateResponse        result        = null;
                Mock <IContainer>     mockContainer = null;
                string key   = null;
                string value = null;

                context["when the container is created successfully"] = () =>
                {
                    before = () =>
                    {
                        containerHandle = Guid.NewGuid().ToString();
                        containerPath   = Path.Combine(@"C:\containerizer", containerHandle);

                        mockContainer = new Mock <IContainer>();
                        mockContainer.Setup(x => x.Handle).Returns(containerHandle);

                        mockContainerService.Setup(x => x.CreateContainer(It.IsAny <ContainerSpec>()))
                        .Returns(mockContainer.Object);

                        key   = "hiwillyou";
                        value = "bemyfriend";

                        specModel = new ContainerSpecApiModel
                        {
                            Handle     = containerHandle,
                            Properties = new Dictionary <string, string>
                            {
                                { key, value }
                            }
                        };
                    };

                    act = () => result = containersController.Create(specModel);

                    it["returns the passed in container's id"] = () =>
                    {
                        result.Handle.should_be(containerHandle);
                    };

                    it["sets properties"] = () =>
                    {
                        mockContainerService.Verify(
                            x => x.CreateContainer(
                                It.Is <ContainerSpec>(createSpec => createSpec.Properties[key] == value)));
                    };

                    context["when properties are not passed to the endpoint"] = () =>
                    {
                        before = () =>
                        {
                            specModel = new ContainerSpecApiModel
                            {
                                Handle     = containerHandle,
                                Properties = null,
                            };
                        };

                        it["returns the passed in container's id"] = () =>
                        {
                            result.Handle.should_be(containerHandle);
                        };
                    };
                };

                context["when creating a container fails"] = () =>
                {
                    Exception ex = null;

                    act = () =>
                    {
                        try
                        {
                            containersController.Create(new ContainerSpecApiModel()
                            {
                                Handle = "foo"
                            });
                        }
                        catch (Exception e)
                        {
                            ex = e;
                        }
                    };

                    context["because the user already exists"] = () =>
                    {
                        before = () =>
                        {
                            mockContainerService.Setup(x => x.CreateContainer(It.IsAny <ContainerSpec>()))
                            .Throws(new PrincipalExistsException());
                        };

                        it["throw HttpResponseException with handle already exists message"] = () =>
                        {
                            var httpResponse = ex.should_cast_to <HttpResponseException>();
                            httpResponse.Response.StatusCode.should_be(HttpStatusCode.Conflict);
                            httpResponse.Response.Content.ReadAsJsonString().should_be("handle already exists: foo");
                        };
                    };
                };
            };

            string handle = "MySecondContainer";

            describe["#Stop"] = () =>
            {
                IHttpActionResult result = null;

                act = () => result = containersController.Stop(handle);

                context["a handle which exists"] = () =>
                {
                    Mock <IContainer> mockContainer = null;
                    before = () =>
                    {
                        mockContainer = new Mock <IContainer>();
                        mockContainerService.Setup(x => x.GetContainerByHandle(handle)).Returns(mockContainer.Object);
                    };

                    it["returns 200"] = () =>
                    {
                        result.should_cast_to <System.Web.Http.Results.OkResult>();
                    };

                    it["calls stop on the container"] = () =>
                    {
                        mockContainer.Verify(x => x.Stop(true));
                    };
                };

                context["a handle which does not exist"] = () =>
                {
                    before = () =>
                    {
                        mockContainerService.Setup(x => x.GetContainerByHandle(handle)).Returns(null as IContainer);
                    };

                    it["returns 404"] = () =>
                    {
                        result.should_cast_to <System.Web.Http.Results.NotFoundResult>();
                    };
                };
            };

            describe["#Destroy"] = () =>
            {
                IHttpActionResult result = null;

                act = () => result = containersController.Destroy(handle);

                context["a handle which exists"] = () =>
                {
                    Mock <IContainer> mockContainer = null;
                    before = () =>
                    {
                        mockContainer = new Mock <IContainer>();
                        mockContainerService.Setup(x => x.GetContainerByHandleIncludingDestroyed(handle)).Returns(mockContainer.Object);
                    };

                    it["returns 200"] = () =>
                    {
                        result.should_cast_to <System.Web.Http.Results.OkResult>();
                    };

                    it["calls delete on the containerPathService"] = () =>
                    {
                        mockContainerService.Verify(x => x.DestroyContainer(handle));
                    };

                    var setupDestroyExceptions = new Action <int>((int errorCount) =>
                    {
                        var callsCount = 0;
                        mockContainerService.Setup(x => x.DestroyContainer(handle)).Callback(() =>
                        {
                            if (callsCount++ < errorCount)
                            {
                                throw new IOException("file is in use");
                            }
                        });
                    });
                };

                context["a handle which does not exist"] = () =>
                {
                    before = () =>
                    {
                        mockContainerService.Setup(x => x.GetContainerByHandleIncludingDestroyed(handle)).Returns(null as IContainer);
                    };

                    it["returns 404"] = () =>
                    {
                        result.should_cast_to <System.Web.Http.Results.NotFoundResult>();
                    };
                };
            };
        }
        private void describe_()
        {
            Mock <IContainerService> mockContainerService = null;
            LimitsController         LimitsController     = null;
            string handle = null;

            Mock <IContainer> mockContainer = null;

            before = () =>
            {
                mockContainerService = new Mock <IContainerService>();
                LimitsController     = new LimitsController(mockContainerService.Object);

                handle = Guid.NewGuid().ToString();

                mockContainer = new Mock <IContainer>();
                mockContainerService.Setup(x => x.GetContainerByHandle(handle)).Returns(mockContainer.Object);
            };

            describe["#LimitMemory"] = () =>
            {
                const ulong       limitInBytes = 876;
                MemoryLimits      limits       = null;
                IHttpActionResult result       = null;

                before = () =>
                {
                    limits = new MemoryLimits {
                        LimitInBytes = limitInBytes
                    };
                };
                act = () =>
                {
                    result = LimitsController.LimitMemory(handle, limits);
                };

                it["sets limits on the container"] = () =>
                {
                    mockContainer.Verify(x => x.LimitMemory(limitInBytes));
                };

                context["when the container does not exist"] = () =>
                {
                    before = () =>
                    {
                        mockContainerService.Setup(x => x.GetContainerByHandle(It.IsAny <string>())).Returns(null as IContainer);
                    };

                    it["Returns not found"] = () =>
                    {
                        result.should_cast_to <NotFoundResult>();
                    };
                };
            };

            describe["#CurrentMemoryLimit"] = () =>
            {
                it["returns the current limit on the container"] = () =>
                {
                    mockContainer.Setup(x => x.CurrentMemoryLimit()).Returns(3072);
                    var result     = LimitsController.CurrentMemoryLimit(handle);
                    var jsonResult = result.should_cast_to <JsonResult <MemoryLimits> >();
                    jsonResult.Content.LimitInBytes.should_be(3072);
                };

                context["when the container does not exist"] = () =>
                {
                    before = () =>
                    {
                        mockContainerService.Setup(x => x.GetContainerByHandle(It.IsAny <string>())).Returns(null as IContainer);
                    };

                    it["Returns not found"] = () =>
                    {
                        var result = LimitsController.CurrentMemoryLimit(handle);
                        result.should_cast_to <NotFoundResult>();
                    };
                };
            };

            describe["#LimitCpu"] = () =>
            {
                IHttpActionResult result = null;
                const int         weight = 5;
                act = () =>
                {
                    var limits = new CpuLimits {
                        Weight = weight
                    };
                    result = LimitsController.LimitCpu(handle, limits);
                };

                it["sets limits on the container"] = () =>
                {
                    mockContainer.Verify(x => x.LimitCpu(weight));
                };

                context["when the container does not exist"] = () =>
                {
                    before = () =>
                    {
                        mockContainerService.Setup(x => x.GetContainerByHandle(It.IsAny <string>())).Returns(null as IContainer);
                    };

                    it["Returns not found"] = () =>
                    {
                        result.should_cast_to <NotFoundResult>();
                    };
                };
            };

            describe["#CurrentCpuLimit"] = () =>
            {
                it["returns the current limit on the container"] = () =>
                {
                    mockContainer.Setup(x => x.CurrentCpuLimit()).Returns(6);
                    var result     = LimitsController.CurrentCpuLimit(handle);
                    var jsonResult = result.should_cast_to <JsonResult <CpuLimits> >();
                    jsonResult.Content.Weight.should_be(6);
                };

                context["when the container does not exist"] = () =>
                {
                    before = () =>
                    {
                        mockContainerService.Setup(x => x.GetContainerByHandle(It.IsAny <string>())).Returns(null as IContainer);
                    };

                    it["Returns not found"] = () =>
                    {
                        var result = LimitsController.CurrentCpuLimit(handle);
                        result.should_cast_to <NotFoundResult>();
                    };
                };
            };

            describe["#LimitDisk"] = () =>
            {
                IHttpActionResult result = null;
                const ulong       bytes  = 5;
                act = () =>
                {
                    var limits = new DiskLimits {
                        ByteHard = bytes
                    };
                    result = LimitsController.LimitDisk(handle, limits);
                };

                it["sets limits on the container"] = () =>
                {
                    mockContainer.Verify(x => x.LimitDisk(bytes));
                };

                context["when the container does not exist"] = () =>
                {
                    before = () =>
                    {
                        mockContainerService.Setup(x => x.GetContainerByHandle(It.IsAny <string>())).Returns(null as IContainer);
                    };

                    it["Returns not found"] = () =>
                    {
                        result.should_cast_to <NotFoundResult>();
                    };
                };
            };

            describe["#CurrentDiskLimit"] = () =>
            {
                it["returns the current limit on the container"] = () =>
                {
                    mockContainer.Setup(x => x.CurrentDiskLimit()).Returns(6);
                    var result     = LimitsController.CurrentDiskLimit(handle);
                    var jsonResult = result.should_cast_to <JsonResult <DiskLimits> >();
                    jsonResult.Content.ByteHard.should_be(6);
                };

                context["when the container does not exist"] = () =>
                {
                    before = () =>
                    {
                        mockContainerService.Setup(x => x.GetContainerByHandle(It.IsAny <string>()))
                        .Returns(null as IContainer);
                    };

                    it["Returns not found"] = () =>
                    {
                        var result = LimitsController.CurrentDiskLimit(handle);
                        result.should_cast_to <NotFoundResult>();
                    };
                };
            };
        }