Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
feat: File ownership methods added (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
lholota committed Aug 2, 2021
1 parent c81a07e commit 411dfef
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.images.builder.ImageFromDockerfile;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
Expand Down Expand Up @@ -66,6 +67,30 @@ public Integer getProcessGid(String processName) throws IOException, Interrupted
return Integer.parseInt(output);
}

public Integer getFileOwnerUid(String filePath) throws IOException, InterruptedException {
ExecResult result = executeShellCommand("stat -c '%u' " + filePath);

if(result.getExitCode() != 0) {
throw new FileNotFoundException(filePath);
}

String output = result.getStdout().trim();

return Integer.parseInt(output);
}

public Integer getFileOwningGid(String filePath) throws IOException, InterruptedException {
ExecResult result = executeShellCommand("stat -c '%g' " + filePath);

if(result.getExitCode() != 0) {
throw new FileNotFoundException(filePath);
}

String output = result.getStdout().trim();

return Integer.parseInt(output);
}

public ExecResult executeShellCommand(String command) throws IOException, InterruptedException {
return this.execInContainer(
getShellExecutable(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.homecentr.testcontainers.containers;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.output.Slf4jLogConsumer;

import java.io.FileNotFoundException;
import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

public class GenericContainerEx_getFileOwnerUidShould {
private static final Logger logger = LoggerFactory.getLogger(GenericContainerEx_getProcessGidShould.class);

private GenericContainerEx _container;

@Before
public void before() throws IOException, InterruptedException {
_container = new GenericContainerEx<>("centos").withCommand("bash", "-c", "sleep 100s");
_container.start();
_container.followOutput(new Slf4jLogConsumer(logger));

_container.executeShellCommand("useradd -u 1100 test");
}

@After
public void after() {
_container.close();
}

@Test
public void returnFileOwnerUid() throws IOException, InterruptedException {
_container.executeShellCommand("echo Hello > /tmp/out.txt");
_container.executeShellCommand("chown 1000 /tmp/out.txt");

Integer ownerUid = _container.getFileOwnerUid("/tmp/out.txt");

assertEquals(1000, (long)ownerUid);
}

@Test
public void throwFileNotFoundException_GivenNonExistingPath() {
assertThrows(FileNotFoundException.class, () -> _container.getFileOwnerUid("/tmp/out.txt"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.homecentr.testcontainers.containers;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.output.Slf4jLogConsumer;

import java.io.FileNotFoundException;
import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

public class GenericContainerEx_getFileOwningGidShould {
private static final Logger logger = LoggerFactory.getLogger(GenericContainerEx_getProcessGidShould.class);

private GenericContainerEx _container;

@Before
public void before() throws IOException, InterruptedException {
_container = new GenericContainerEx<>("centos").withCommand("bash", "-c", "sleep 100s");
_container.start();
_container.followOutput(new Slf4jLogConsumer(logger));

_container.executeShellCommand("groupadd -g 1100 test");
}

@After
public void after() {
_container.close();
}

@Test
public void returnFileOwnerUid() throws IOException, InterruptedException {
_container.executeShellCommand("echo Hello > /tmp/out.txt");
_container.executeShellCommand("chgrp 1000 /tmp/out.txt");

Integer ownerUid = _container.getFileOwningGid("/tmp/out.txt");

assertEquals(1000, (long)ownerUid);
}

@Test
public void throwFileNotFoundException_GivenNonExistingPath() {
assertThrows(FileNotFoundException.class, () -> _container.getFileOwningGid("/tmp/out.txt"));
}
}

0 comments on commit 411dfef

Please sign in to comment.