Skip to content

Commit

Permalink
Delete long running tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkocher committed Aug 15, 2023
1 parent 3f1a39a commit 1fd9967
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 405 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
*/

import ch.cyberduck.core.Profile;
import ch.cyberduck.core.ProtocolFactory;
import ch.cyberduck.core.exception.AccessDeniedException;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.s3.S3Protocol;
import ch.cyberduck.core.serializer.impl.dd.ProfilePlistReader;
import ch.cyberduck.test.TestcontainerTest;

import org.apache.logging.log4j.LogManager;
Expand All @@ -25,19 +29,30 @@
import org.junit.ClassRule;
import org.junit.experimental.categories.Category;
import org.testcontainers.containers.DockerComposeContainer;
import org.testcontainers.containers.wait.strategy.Wait;

import static ch.cyberduck.core.sts.STSTestSetup.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

@Category(TestcontainerTest.class)
public abstract class AbstractAssumeRoleWithWebIdentityTest {
protected static final Logger log = LogManager.getLogger(AbstractAssumeRoleWithWebIdentityTest.class);

protected static final int MILLIS = 1000;

// lag to wait after token expiry
protected static final int LAG = 10 * MILLIS;

protected static int OAUTH_TTL_SECS = 30;
protected static final long MILLIS = 1000;
protected static int OAUTH_TTL_SECS = 5;

protected static Profile profile = null;

Expand All @@ -48,4 +63,76 @@ public abstract class AbstractAssumeRoleWithWebIdentityTest {
public void setup() throws BackgroundException {
profile = readProfile();
}

public static DockerComposeContainer prepareDockerComposeContainer(final String keyCloakRealmTempFile) {
log.info("Preparing docker compose container...");
return new DockerComposeContainer<>(
new File(AbstractAssumeRoleWithWebIdentityTest.class.getResource("/testcontainer/docker-compose.yml").getFile()))
.withEnv("KEYCLOAK_REALM_JSON", keyCloakRealmTempFile)
.withPull(false)
.withLocalCompose(true)
.withOptions("--compatibility")
.withExposedService("keycloak_1", 8080, Wait.forListeningPort())
.withExposedService("minio_1", 9000, Wait.forListeningPort());
}

public static String getKeyCloakFile(Map<String, String> replacements) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement je = new Gson().fromJson(new InputStreamReader(AbstractAssumeRoleWithWebIdentityTest.class.getResourceAsStream("/testcontainer/keycloak/keycloak-realm.json")), JsonElement.class);
JsonObject jo = je.getAsJsonObject();

for(Map.Entry<String, String> replacement : replacements.entrySet()) {
updateJsonValues(jo, replacement.getKey(), replacement.getValue());
}

String content = gson.toJson(jo);
try {
final Path tempFile = Files.createTempFile(null, null);
Files.write(tempFile, content.getBytes(StandardCharsets.UTF_8));
return tempFile.toAbsolutePath().toString();

}
catch(IOException e) {
throw new RuntimeException(e);
}
}

private static void updateJsonValues(JsonObject jsonObj, String key, String newVal) {
for(Map.Entry<String, JsonElement> entry : jsonObj.entrySet()) {
JsonElement element = entry.getValue();
if(element.isJsonArray()) {
updateJsonValues(element.getAsJsonArray(), key, newVal);
}
else if(element.isJsonObject()) {
updateJsonValues(element.getAsJsonObject(), key, newVal);
}
else if(entry.getKey().equals(key)) {
jsonObj.remove(key);
jsonObj.addProperty(key, newVal);
break;
}
}
}

private static void updateJsonValues(JsonArray asJsonArray, String key, String newVal) {
for(int index = 0; index < asJsonArray.size(); index++) {
JsonElement element = asJsonArray.get(index);
if(element.isJsonArray()) {
updateJsonValues(element.getAsJsonArray(), key, newVal);
}
else if(element.isJsonObject()) {
updateJsonValues(element.getAsJsonObject(), key, newVal);
}
}
}

public static String getKeyCloakFile() {
return getKeyCloakFile(Collections.emptyMap());
}

public static Profile readProfile() throws AccessDeniedException {
final ProtocolFactory factory = new ProtocolFactory(new HashSet<>(Collections.singleton(new S3Protocol())));
return new ProfilePlistReader(factory).read(
AbstractAssumeRoleWithWebIdentityTest.class.getResourceAsStream("/S3 (OIDC).cyberduckprofile"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,19 @@ public void testTokenRefresh() throws BackgroundException, InterruptedException
session.open(new DisabledProxyFinder().find(new HostUrlProvider().get(host)), new DisabledHostKeyCallback(), new DisabledLoginCallback(), new DisabledCancelCallback());
session.login(new DisabledProxyFinder().find(new HostUrlProvider().get(host)), new DisabledLoginCallback(), new DisabledCancelCallback());

final OAuthTokens oauth = host.getCredentials().getOauth();
final Credentials credentials = host.getCredentials();
final OAuthTokens oauth = credentials.getOauth();
assertTrue(oauth.validate());
final STSTokens tokens = host.getCredentials().getTokens();
final STSTokens tokens = credentials.getTokens();
assertTrue(tokens.validate());
host.getCredentials().reset();

Path container = new Path("cyberduckbucket", EnumSet.of(Path.Type.directory, Path.Type.volume));
assertTrue(new S3FindFeature(session, new S3AccessControlListFeature(session)).find(container));

Thread.sleep(MILLIS * OAUTH_TTL_SECS + LAG);
Thread.sleep(MILLIS * OAUTH_TTL_SECS);
assertTrue(credentials.getOauth().isExpired());
assertTrue(credentials.getTokens().isExpired());

assertTrue(new S3FindFeature(session, new S3AccessControlListFeature(session)).find(container));

session.close();
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 1fd9967

Please sign in to comment.