Esempio n. 1
0
        private void describe_consumer_can_destroy_a_container()
        {
            Helpers.ContainerizerProcess process = null;
            HttpClient client = null;
            string     handle = null;

            before = () => process = Helpers.CreateContainerizerProcess();
            after  = () => process.Dispose();

            context["given that I am a consumer of the api and a container exists"] = () =>
            {
                before = () =>
                {
                    client = process.GetClient();

                    handle = Helpers.CreateContainer(client);
                };

                context["when I send two destroy request"] = () =>
                {
                    it["responds with 200 and then 404"] = () =>
                    {
                        var response = client.DeleteAsync("/api/containers/" + handle).GetAwaiter().GetResult();
                        response.StatusCode.should_be(HttpStatusCode.OK);

                        response = client.DeleteAsync("/api/containers/" + handle).GetAwaiter().GetResult();
                        response.StatusCode.should_be(HttpStatusCode.NotFound);
                    };
                };
            };
        }
        private void describe_()
        {
            context["Given that I'm a consumer of the containerizer API"] = () =>
            {
                HttpClient client = null;
                Helpers.ContainerizerProcess process = null;

                before = () =>
                {
                    process = Helpers.CreateContainerizerProcess();
                    client  = process.GetClient();
                };

                after = () => process.Dispose();

                context["And there exists a container with a given id"] = () =>
                {
                    string handle = null;

                    before = () => handle = Helpers.CreateContainer(client);
                    after  = () => Helpers.DestroyContainer(client, handle);

                    it["allows the consumer to interact with the property endpoints correctly"] = () =>
                    {
                        var properties = new Dictionary <string, string>
                        {
                            { "mysecret", "dontread" },
                            { "hello", "goodbye" },
                        };

                        PutsProperties(client, handle, properties);
                        GetsAllProperties(client, handle, properties);
                        GetsInfo(client, process, handle, properties);
                        GetsEachProperty(client, handle, properties);
                        DeletesProperty(client, handle, properties);
                    };
                };

                context["And there exists multiple containers"] = () =>
                {
                    string handle1 = null, handle2 = null;

                    before = () => { handle1 = Helpers.CreateContainer(client); handle2 = Helpers.CreateContainer(client); };
                    after  = () => { Helpers.DestroyContainer(client, handle1); Helpers.DestroyContainer(client, handle2); };

                    it["bulkinfo returns info for the given handles"] = () =>
                    {
                        var handles = JsonConvert.SerializeObject(new string[] { handle1, handle2 });
                        var result  = client.PostAsync("/api/bulkcontainerinfo", new StringContent(handles, Encoding.UTF8, "application/json")).Result;
                        result.IsSuccessStatusCode.should_be_true();
                        var response = result.Content.ReadAsJson();

                        response.Count.should_be(2);
                        response[handle1]["Info"]["ExternalIP"].ToString().should_be(process.ExternalIP);
                        response[handle2]["Info"]["ExternalIP"].ToString().should_be(process.ExternalIP);
                    };
                };
            };
        }
        private static void GetsInfo(HttpClient client, Helpers.ContainerizerProcess process, string handle, Dictionary <string, string> properties)
        {
            var infoPath     = "/api/containers/" + handle + "/info";
            var infoResponse = client.GetAsync(infoPath).Result.Content.ReadAsJson() as JObject;

            infoResponse["ExternalIP"].ToString().should_be(process.ExternalIP);
            infoResponse["Properties"].ToObject <Dictionary <string, string> >().should_be(properties);
        }
        private void describe_()
        {
            context["Given that I'm a consumer of the containerizer API"] = () =>
            {
                HttpClient client = null;
                Helpers.ContainerizerProcess process = null;

                before = () =>
                {
                    process = Helpers.CreateContainerizerProcess();
                    client  = process.GetClient();
                };

                after = () => process.Dispose();


                context["And there exists a container with a given id"] = () =>
                {
                    string containerId = null;
                    before = () =>
                    {
                        containerId = Helpers.CreateContainer(client);
                    };

                    after = () =>
                    {
                        Helpers.DestroyContainer(client, containerId);
                    };

                    context["When I POST a request to /api/containers/:id/net/in with a host port of 0 in the body"] = () =>
                    {
                        int resultingHostPort = 0;

                        before = () =>
                        {
                            var response = client.PostAsJsonAsync("/api/containers/" + containerId + "/net/in", new { hostPort = 0, containerPort = 8080 }).GetAwaiter().GetResult();
                            var json     = response.Content.ReadAsJson();
                            resultingHostPort = json["hostPort"].Value <int>();
                        };

                        it["returns an assigned host port"] = () =>
                        {
                            resultingHostPort.should_not_be(0);
                        };

                        it["returns the assigned host ports on an INFO call and a LIE for the container port (see comment)"] = () =>
                        {
                            var response    = client.GetAsync("/api/containers/" + containerId + "/info").GetAwaiter().GetResult();
                            var json        = response.Content.ReadAsJson();
                            var portMapping = json["MappedPorts"][0];
                            portMapping["HostPort"].Value <int>().should_be(resultingHostPort);
                            portMapping["ContainerPort"].Value <int>().should_be(8080);
                        };
                    };
                };
            };
        }
        private void describe_consumer_can_destroy_a_container()
        {
            Helpers.ContainerizerProcess process = null;
            HttpClient client = null;
            string     handle = null;
            string     containerRootDirectory = null;

            before = () =>
            {
                containerRootDirectory = Factories.ContainerServiceFactory.GetContainerDefaultRoot();
                process = Helpers.CreateContainerizerProcess(containerRootDirectory);
            };

            after = () => process.Dispose();

            context["given that I am a consumer of the api and a container exists"] = () =>
            {
                before = () =>
                {
                    client = process.GetClient();

                    handle = Helpers.CreateContainer(client);
                };

                context["when I send two destroy request"] = () =>
                {
                    it["responds with 200 and then 404"] = () =>
                    {
                        var response = client.DeleteAsync("/api/containers/" + handle).GetAwaiter().GetResult();
                        response.StatusCode.should_be(HttpStatusCode.OK);

                        response = client.DeleteAsync("/api/containers/" + handle).GetAwaiter().GetResult();
                        response.StatusCode.should_be(HttpStatusCode.NotFound);
                    };
                };

                it["deletes the container's resources"] = () =>
                {
                    var response = client.DeleteAsync("/api/containers/" + handle).GetAwaiter().GetResult();
                    response.StatusCode.should_be(HttpStatusCode.OK);

                    Directory.Exists(Helpers.GetContainerPath(containerRootDirectory, handle)).should_be_false();
                };
            };
        }
Esempio n. 6
0
        private void describe_ping()
        {
            Helpers.ContainerizerProcess process = null;

            before = () => process = Helpers.CreateContainerizerProcess();
            after  = () => process.Dispose();

            context["given that containerizer is running"] = () =>
            {
                describe["ping"] = () =>
                {
                    it["succeeds"] = () =>
                    {
                        HttpResponseMessage getTask = process.GetClient().GetAsync("/api/Ping").GetAwaiter().GetResult();
                        getTask.IsSuccessStatusCode.should_be_true();
                    };
                };
            };
        }
Esempio n. 7
0
        private void describe_()
        {
            context["Given that I'm a consumer of the containerizer API"] = () =>
            {
                HttpClient client = null;
                Helpers.ContainerizerProcess process = null;

                before = () =>
                {
                    process = Helpers.CreateContainerizerProcess();
                    client  = process.GetClient();
                };

                after = () => process.Dispose();

                context["And there exists a container with a given id"] = () =>
                {
                    string handle = null;

                    before = () => handle = Helpers.CreateContainer(client);
                    after  = () => Helpers.DestroyContainer(client, handle);

                    it["allows the consumer to get and set limits"] = () =>
                    {
                        var limit_in_bytes = 32UL * 1024 * 1024;
                        var url            = "/api/containers/" + handle + "/memory_limit";
                        var result         = client.PostAsJsonAsync(url, new { limit_in_bytes = limit_in_bytes }).Result;
                        result.IsSuccessStatusCode.should_be_true();
                        result = client.GetAsync(url).Result;
                        result.IsSuccessStatusCode.should_be_true();
                        var limits = JsonConvert.DeserializeObject <MemoryLimits>(result.Content.ReadAsString());
                        limits.LimitInBytes.should_be(limit_in_bytes);
                    };
                };
            };
        }
 private void before_each()
 {
     process = Helpers.CreateContainerizerProcess();
 }
 private void before_each()
 {
     process = Helpers.CreateContainerizerProcess();
 }
Esempio n. 10
0
        private void describe_consumer_can_run_a_process()
        {
            ClientWebSocket client     = null;
            HttpClient      httpClient = null;
            string          handle     = null;

            Helpers.ContainerizerProcess process = null;

            before = () =>
            {
                process    = Helpers.CreateContainerizerProcess();
                httpClient = process.GetClient();
                handle     = Helpers.CreateContainerWithGUID(httpClient);
            };
            after = () =>
            {
                Helpers.DestroyContainer(httpClient, handle);
                process.Dispose();
            };

            context["given that I am a consumer of the api"] = () =>
            {
                string containerPath = null;
                var    hostPort      = 0;

                context["I have a file to run in my container"] = () =>
                {
                    before = () =>
                    {
                        var response = httpClient.PutAsync("/api/containers/" + handle + "/properties/executor:env", new StringContent("[{\"name\":\"INSTANCE_GUID\",\"value\":\"ExcitingGuid\"}]")).Result;
                        response.IsSuccessStatusCode.should_be_true();

                        containerPath = Helpers.GetContainerPath(handle);
                        File.WriteAllBytes(containerPath + "/myfile.bat",
                                           new UTF8Encoding(true).GetBytes(
                                               "@echo off\r\n@echo Hi Fred\r\n@echo Jane is good\r\n@echo Jill is better\r\nset PORT\r\nset INSTANCE_GUID\r\n"));
                        File.WriteAllBytes(containerPath + "/loop.bat",
                                           new UTF8Encoding(true).GetBytes(
                                               "@echo off\r\n:loop\r\ndate /t\r\ngoto loop\r\n"));

                        response =
                            httpClient.PostAsJsonAsync("/api/containers/" + handle + "/net/in", new { containerPort = 8080, hostPort = 0 })
                            .GetAwaiter()
                            .GetResult();
                        var json = response.Content.ReadAsJson();
                        hostPort = json["hostPort"].Value <int>();

                        client = ConnectToWebsocket(client, handle, process);
                    };

                    describe["when I send a start request"] = () =>
                    {
                        List <String> messages = null;

                        before = () =>
                        {
                            var encoder = new UTF8Encoding();
                            var buffer  =
                                encoder.GetBytes(
                                    "{\"type\":\"run\", \"pspec\":{\"Path\":\"myfile.bat\", Env:[\"PORT=8080\"]}}");
                            client.SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Text, true,
                                             CancellationToken.None).GetAwaiter().GetResult();

                            messages = new List <string>();
                            while (client.State == WebSocketState.Open)
                            {
                                ReceiveWebsocketMessageAndAppend(client, messages);
                            }
                            ;
                        };

                        it["should run a process, return stdout and close the socket"] = () =>
                        {
                            messages.should_contain("{\"type\":\"stdout\",\"data\":\"Hi Fred\\r\\n\"}");
                            messages.should_contain("{\"type\":\"stdout\",\"data\":\"PORT=" + hostPort + "\\r\\n\"}");
                            messages.should_contain("{\"type\":\"stdout\",\"data\":\"INSTANCE_GUID=ExcitingGuid\\r\\n\"}");
                            messages.should_contain("{\"type\":\"close\",\"data\":\"0\"}");
                        };
                    };
                };
            };
        }
Esempio n. 11
0
 private static ClientWebSocket ConnectToWebsocket(ClientWebSocket client, string handle, Helpers.ContainerizerProcess process)
 {
     client = new ClientWebSocket();
     try
     {
         client.ConnectAsync(
             new Uri("ws://localhost:" + process.Port + "/api/containers/" + handle + "/run"),
             CancellationToken.None).GetAwaiter().GetResult();
     }
     catch (WebSocketException ex)
     {
         throw new Exception("Make sure to enable websockets following instructions in the README.",
                             ex);
     }
     return(client);
 }
 private void before_each()
 {
     process   = Helpers.CreateContainerizerProcess();
     tarStream = new MemoryStream(Resources.fixture1);
 }
 private void before_each()
 {
     process = Helpers.CreateContainerizerProcess();
     tarStream = new MemoryStream(Resources.fixture1);
 }