Skip to content

Commit

Permalink
fix(tasks): docker config are not overwrite properly
Browse files Browse the repository at this point in the history
  • Loading branch information
tchiotludo committed Oct 27, 2023
1 parent c7f2bad commit 4780858
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package io.kestra.plugin.scripts.shell;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import io.kestra.core.models.executions.LogEntry;
import io.kestra.core.queues.QueueFactoryInterface;
import io.kestra.core.queues.QueueInterface;
import io.kestra.core.runners.RunContext;
import io.kestra.core.runners.RunContextFactory;
import io.kestra.core.utils.Rethrow;
import io.kestra.core.utils.TestsUtils;
import io.kestra.plugin.scripts.exec.scripts.models.DockerOptions;
import io.kestra.plugin.scripts.exec.scripts.models.RunnerType;
import io.kestra.plugin.scripts.exec.scripts.models.ScriptOutput;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -91,4 +95,41 @@ void massLog(RunnerType runner, DockerOptions dockerOptions) throws Exception {
assertThat(run.getStdOutLineCount(), is(2000));
assertThat(run.getStdErrLineCount(), is(0));
}

@SuppressWarnings("unchecked")
@Test
void overwrite() throws Exception {
Function<String, Script> function = (String username) -> Script.builder()
.id("unit-test")
.type(Script.class.getName())
.docker(DockerOptions.builder()
.image("ubuntu")
.credentials(DockerOptions.Credentials.builder()
.registry("own.registry")
.username(username)
.password("doe")
.build()
)
.build()
)
.script("""
echo '::{"outputs":{"config":'$(cat config.json)'}}::'
""")
.build();


Script bash = function.apply("john");
RunContext runContext = TestsUtils.mockRunContext(runContextFactory, bash, ImmutableMap.of());

ScriptOutput run = bash.run(runContext);
assertThat(run.getExitCode(), is(0));
assertThat(((Map<String, Map<String, Map<String, Object>>>)run.getVars().get("config")).get("auths").get("own.registry").get("username"), is("john"));
assertThat(run.getExitCode(), is(0));

bash = function.apply("jane");
run = bash.run(runContext);
assertThat(run.getExitCode(), is(0));
assertThat(((Map<String, Map<String, Map<String, Object>>>)run.getVars().get("config")).get("auths").get("own.registry").get("username"), is("jane"));
assertThat(run.getExitCode(), is(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import io.kestra.core.utils.MapUtils;
import io.kestra.plugin.scripts.exec.scripts.models.DockerOptions;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
Expand Down Expand Up @@ -92,16 +94,21 @@ public static Path createConfig(RunContext runContext, @Nullable Object config,
finalConfig = MapUtils.merge(finalConfig, Map.of("auths", Map.of(registry, auths)));
}

Path docker = runContext.tempDir(true);
Path file = Files.createFile(docker.resolve("config.json"));
File docker = runContext.tempDir(true).resolve("config.json").toFile();

if (docker.exists()) {
//noinspection ResultOfMethodCallIgnored
docker.delete();
} else {
Files.createFile(docker.toPath());
}

Files.write(
file,
runContext.render(JacksonMapper.ofJson().writeValueAsString(finalConfig)).getBytes(),
StandardOpenOption.TRUNCATE_EXISTING
docker.toPath(),
runContext.render(JacksonMapper.ofJson().writeValueAsString(finalConfig)).getBytes()
);

return file.getParent();
return docker.toPath().getParent();
}

public static String registryUrlFromImage(String image) {
Expand Down

0 comments on commit 4780858

Please sign in to comment.