Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix integration tests when uid/gid already exists in docker image for maven typescript generator #2711

Merged
merged 1 commit into from
Oct 6, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ protected void installTypeScriptCompiler() throws IOException, InterruptedExcept

// avoid root permissions in generated files
if (SystemInfo.isLinux()) {

command.add("groupadd -g " + getGid() + " user && useradd -u" + getUid() + " -g user user && (chown --silent -R user.user /usr/src/app || true) && cd /usr/src/app/ && npm install && (chown --silent -R user.user /usr/src/app || true)");
command.add(wrapLinuxCommand("npm install"));
} else {
command.add("npm install");
}

// setup typescript compiler
ProcessBuilder processBuilder = new ProcessBuilder().command(command).directory(rootPath.toFile()).redirectErrorStream(true).inheritIO();
Process process = processBuilder.start();
Expand All @@ -224,6 +224,21 @@ protected void installTypeScriptCompiler() throws IOException, InterruptedExcept

}


/**
* Wrap the given command into a command with chown. Also add group/user that match host environment if not exists
* @param command the command to wrap
* @return an updated command with chown applied on it
*/
protected String wrapLinuxCommand(String command) throws IOException, InterruptedException {

String setGroup = "export GROUP_NAME=`(getent group " + getGid() + " || (groupadd -g " + getGid() + " user && echo user:x:" + getGid() +")) | cut -d: -f1`";
String setUser = "export USER_NAME=`(getent passwd " + getUid() + " || (useradd -u " + getUid() + " -g ${GROUP_NAME} user && echo user:x:" + getGid() +")) | cut -d: -f1`";
String chownCommand= "chown --silent -R ${USER_NAME}.${GROUP_NAME} /usr/src/app || true";
return setGroup + " && " + setUser + " && " + chownCommand + " && " + command + " && " + chownCommand;
}


/**
* Starts tests by compiling first generated DTO from maven plugin
* @throws IOException if unable to start process
Expand Down Expand Up @@ -253,7 +268,7 @@ public void compileDTOAndLaunchTests() throws IOException, InterruptedException

// avoid root permissions in generated files
if (SystemInfo.isLinux()) {
command.add("groupadd -g " + getGid() + " user && useradd -u" + getUid() + " -g user user && (chown --silent -R user.user /usr/src/app || true) && npm test && (chown --silent -R user.user /usr/src/app || true)");
command.add(wrapLinuxCommand("npm test"));
} else {
command.add("npm test");
}
Expand Down