package it.com.atlassian.stash.installer.docker;

import com.google.common.collect.ImmutableMap;
import org.junit.Test;

import java.util.Map;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;

public class DockerInstaller3XUpgradeTest extends BaseDockerInstallerTest {
    private static final Map<String, String> BITBUCKET_VAR_FILE = ImmutableMap.<String, String>builder()
            .put("app.install.service$Boolean", "true")
            .put("portChoice", "custom")
            .put("httpPort", "7991")
            .put("serverPort", "8007")
            .put("app.bitbucketHome", BITBUCKET_HOME)
            .put("app.defaultInstallDir", BITBUCKET_INSTALL_DIR)
            .put("installation.type$Integer", "1")
            .build();
    private static final String STASH_INIT_SCRIPT = "/etc/init.d/atlstash";
    private static final String STASH_INSTALL_DIR = "/tmp/stash-install";
    private static final String STASH_INSTALLER_BINARY = "./stash-installer-x64.bin";
    private static final Map<String, String> STASH_VAR_FILE = ImmutableMap.<String, String>builder()
            .put("app.install.service$Boolean", "true")
            .put("portChoice", "custom")
            .put("httpPort", "7991")
            .put("serverPort", "8007")
            .put("app.stashHome", BITBUCKET_HOME)
            .put("app.defaultInstallDir", STASH_INSTALL_DIR)
            .build();

    @Test
    public void testUpgradeFromStash3X() {
        sh("apt-get install -y git");

        // Install and check Stash
        runInstaller(STASH_INSTALLER_BINARY, STASH_VAR_FILE);

        waitForFirstRun();

        String stopOutput = sh(STASH_INIT_SCRIPT + " stop");
        assertThat(stopOutput, containsString("Stopped Atlassian Stash"));

        assertThat("Expected atlstash user to be created", sh("id atlstash"), containsString("uid="));

        String exportStashHome = sh("grep 'export STASH_HOME=' " + STASH_INSTALL_DIR + "/bin/setenv.sh");
        assertThat("Expected script to contain export for STASH_HOME", exportStashHome, containsString(BITBUCKET_HOME));

        // Install and check Bitbucket Server
        runInstaller(BITBUCKET_INSTALLER_BINARY, BITBUCKET_VAR_FILE);

        assertThat("Expected atlbitbucket user to be created", sh("id atlbitbucket"), containsString("uid="));

        assertThat("Expected atlstash user has been deleted", sh("id atlstash"), containsString("no such user"));

        String exportBitbucketHome = sh("grep 'export BITBUCKET_HOME=' " + BITBUCKET_INSTALL_DIR + "/bin/set-bitbucket-home.sh");
        assertThat("Expected script to contain export for BITBUCKET_HOME", exportBitbucketHome, containsString(BITBUCKET_HOME));

        String initdFiles = sh("ls /etc/init.d");
        assertThat("atlstash script has been removed from init.d", initdFiles, not(containsString("atlstash")));
        assertThat("atlbitbucket script has been added to init.d", initdFiles, containsString("atlbitbucket"));

        waitForFirstRun();

        stopOutput = sh(BITBUCKET_INIT_SCRIPT + " stop");
        assertThat(stopOutput, containsString("Stopped Atlassian Bitbucket"));
    }
}
