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

feat: File ownership methods added #56

Merged
merged 2 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: File ownership methods added
  • Loading branch information
lholota committed Aug 2, 2021
commit dfd28cbd73c4254099453a01181e3be09c8a4bb3
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"));
}
}