/*
 * Copyright 1&1 Internet AG, https://github.com/1and1/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.oneandone.stool.docker;

import net.oneandone.inline.ArgumentException;
import net.oneandone.sushi.fs.World;
import net.oneandone.sushi.fs.file.FileNode;
import net.oneandone.sushi.fs.http.StatusException;
import net.oneandone.sushi.util.Strings;
import org.junit.Test;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Collections;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class DaemonIT {
    private static final World WORLD = World.createMinimal();

    private Daemon create() throws IOException {
        return Daemon.create("target/wire.log");
    }

    //-- images

    @Test(expected = ArgumentException.class)
    public void rejectBuildWithUppercaseTag() throws IOException {
        try (Daemon docker = create()) {
            docker.imageBuild("tagWithUpperCase", Collections.emptyMap(), Collections.emptyMap(), dockerfile("FROM debian:stretch-slim\nCMD ls -la /\n"), false, null);
        }
    }

    @Test
    public void runFailure() throws IOException {
        String image = "stooltest";

        try (Daemon docker = create()) {
            docker.imageBuildWithOutput(image, dockerfile("FROM debian:stretch-slim\nRUN /bin/nosuchcmd\nCMD [\"echo\", \"hi\", \"/\"]\n"));
            fail();
        } catch (BuildError e) {
            // ok
            assertNotNull("", e.error);
            assertEquals(127, e.details.get("code").getAsInt());
            assertNotNull("", e.output);
        }
    }

    @Test
    public void invalidDockerfile() throws IOException {
        try (Daemon docker = create()) {
            docker.imageBuild("sometag", Collections.emptyMap(), Collections.emptyMap(), dockerfile("FROM debian:stretch-slim\nls -la /dev/fuse\n"), false, null);
            fail();
        } catch (StatusException e) {
            assertEquals(400, e.getStatusLine().code);
        }
    }

    @Test
    public void copy() throws IOException {
        String image = "stooltest";

        try (Daemon docker = create()) {
            docker.imageBuildWithOutput(image, WORLD.guessProjectHome(getClass()).join("src/test/docker"));
            docker.imageRemove(image, false);
        }
    }

    @Test
    public void copyFailure() throws IOException {
        String image = "stooltest";

        try (Daemon docker = create()) {
            docker.imageBuildWithOutput(image, dockerfile("FROM debian:stretch-slim\ncopy nosuchfile /nosuchfile\nCMD [\"echo\", \"hi\", \"/\"]\n"));
            fail();
        } catch (BuildError e) {
            // ok
            assertTrue(e.error.contains("COPY failed"));
            assertNotNull("", e.output);
        }
    }

    @Test
    public void imageLabels() throws IOException {
        Map<String, String> labels = Strings.toMap("a", "b", "1", "234");
        StringWriter output;
        String image;
        ImageInfo info;

        try (Daemon docker = create()) {
            output = new StringWriter();
            image = docker.imageBuild("labeltest", Collections.emptyMap(), labels, dockerfile("FROM debian:stretch-slim\nCMD [\"echo\", \"hi\", \"/\"]\n"),
                    false, output);
            info = docker.imageList().get(image);
            assertEquals(labels, info.labels);
        }
    }

    public static FileNode dockerfile(String dockerfile, FileNode ... extras) throws IOException {
        FileNode dir;

        dir = WORLD.getTemp().createTempDirectory();
        dir.join("Dockerfile").writeString(dockerfile);
        for (FileNode extra : extras) {
            extra.copyFile(dir.join(extra.getName()));
        }
        return dir;
    }
}

@@ -2244,409 +2244,8 @@
  %7D%0A%0A
-    @Test%0A    public void invalidDockerfile() throws IOException %7B%0A        try (Daemon docker = create()) %7B%0A            docker.imageBuild(%22sometag%22, Collections.emptyMap(), Collections.emptyMap(), dockerfile(%22FROM debian:stretch-slim%5Cnls -la /dev/fuse%5Cn%22), false, null);%0A            fail();%0A        %7D catch (StatusException e) %7B%0A            assertEquals(400, e.getStatusLine().code);%0A        %7D%0A    %7D%0A%0A
     
@@ -2286,32 +2286,32 @@
 s IOException %7B%0A
+
         String i
@@ -2767,20 +2767,20 @@
 h-slim%5Cn
-copy
+COPY
  nosuchf
@@ -2926,16 +2926,25 @@
 ertTrue(
+e.error, 
 e.error.
@@ -2957,19 +2957,18 @@
 ns(%22
-COPY fa
+nosuchf
 ile
-d
 %22));

test fixes for docker 1.26