Add implementations, implement S3SwapStore (with only tars), FSRepoStore, Tar and File utils, add tests

This commit is contained in:
Winston Li
2016-08-23 17:51:56 +01:00
committed by Michael Mazour
parent 1850689a63
commit 8c0937511e
32 changed files with 848 additions and 34 deletions

View File

@@ -10,6 +10,8 @@ import uk.ac.ic.wlgitbridge.bridge.snapshot.SnapshotAPI;
import uk.ac.ic.wlgitbridge.bridge.swap.SwapJob;
import uk.ac.ic.wlgitbridge.bridge.swap.SwapStore;
import java.time.Duration;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -35,6 +37,7 @@ public class BridgeTest {
dbStore = mock(DBStore.class);
swapStore = mock(SwapStore.class);
snapshotAPI = mock(SnapshotAPI.class);
resourceCache = mock(ResourceCache.class);
swapJob = mock(SwapJob.class);
bridge = new Bridge(
lock,
@@ -49,9 +52,9 @@ public class BridgeTest {
@Test
public void shutdownStopsSwapJob() {
bridge.startSwapJob(1000);
bridge.startSwapJob(Duration.ofSeconds(1));
bridge.doShutdown();
verify(swapJob).start(1000);
verify(swapJob).start(Duration.ofSeconds(1));
verify(swapJob).stop();
}

View File

@@ -0,0 +1,75 @@
package uk.ac.ic.wlgitbridge.bridge.repo;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import uk.ac.ic.wlgitbridge.util.Files;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
/**
* Created by winston on 23/08/2016.
*/
public class FSRepoStoreTest {
private FSRepoStore repoStore;
private File original;
@Before
public void setup() throws IOException {
TemporaryFolder tmpFolder = new TemporaryFolder();
tmpFolder.create();
File tmp = tmpFolder.newFolder("repostore");
Path rootdir = Paths.get(
"src/test/resources/uk/ac/ic/wlgitbridge/"
+ "bridge/repo/FSRepoStoreTest/rootdir"
);
FileUtils.copyDirectory(rootdir.toFile(), tmp);
Files.renameAll(tmp, "DOTgit", ".git");
original = tmpFolder.newFolder("original");
FileUtils.copyDirectory(tmp, original);
repoStore = new FSRepoStore(tmp.getAbsolutePath());
}
@Test
public void testPurgeNonexistentProjects() {
File toDelete = new File(repoStore.getRootDirectory(), "idontexist");
File wlgb = new File(repoStore.getRootDirectory(), ".wlgb");
Assert.assertTrue(toDelete.exists());
Assert.assertTrue(wlgb.exists());
repoStore.purgeNonexistentProjects(Arrays.asList("proj1", "proj2"));
Assert.assertFalse(toDelete.exists());
Assert.assertTrue(wlgb.exists());
}
@Test
public void testTotalSize() {
assertEquals(31860, repoStore.totalSize());
}
@Test
public void zipAndUnzipShouldBeTheSame() throws IOException {
long beforeSize = repoStore.totalSize();
InputStream zipped = repoStore.bzip2Project("proj1");
repoStore.remove("proj1");
Assert.assertTrue(beforeSize > repoStore.totalSize());
repoStore.unbzip2Project("proj1", zipped);
Assert.assertEquals(beforeSize, repoStore.totalSize());
Assert.assertTrue(
Files.contentsAreEqual(
original,
repoStore.getRootDirectory()
)
);
}
}

View File

@@ -0,0 +1,41 @@
package uk.ac.ic.wlgitbridge.bridge.swap;
import org.junit.Before;
/**
* Created by winston on 21/08/2016.
*/
public class S3SwapStoreTest {
private static final String accessKey = null;
private static final String secret = null;
private static final String bucketName = "com.overleaf.testbucket";
private S3SwapStore s3;
@Before
public void setup() {
if (accessKey == null || secret == null) {
s3 = null;
return;
}
s3 = new S3SwapStore(accessKey, secret, bucketName);
}
// @Ignore
// @Test
// public void testUploadDownloadDelete() throws Exception {
// assumeNotNull(s3);
// String projName = "abc123";
// byte[] contents = "hello".getBytes();
// s3.upload(
// projName,
// new ByteArrayInputStream(contents),
// contents.length
// );
// InputStream down = s3.openDownloadStream(projName);
// s3.remove(projName);
// assertArrayEquals(contents, IOUtils.toByteArray(down));
// }
}

View File

@@ -0,0 +1,57 @@
package uk.ac.ic.wlgitbridge.bridge.swap;
import org.junit.Before;
import org.junit.Test;
import uk.ac.ic.wlgitbridge.bridge.db.DBStore;
import uk.ac.ic.wlgitbridge.bridge.lock.ProjectLock;
import uk.ac.ic.wlgitbridge.bridge.repo.RepoStore;
import java.time.Duration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
/**
* Created by winston on 20/08/2016.
*/
public class SwapJobImplTest {
private SwapJobImpl swapJob;
private ProjectLock lock;
private RepoStore repoStore;
private DBStore dbStore;
private SwapStore swapStore;
@Before
public void setup() {
lock = mock(ProjectLock.class);
repoStore = mock(RepoStore.class);
dbStore = mock(DBStore.class);
swapStore = mock(SwapStore.class);
swapJob = new SwapJobImpl(
lock,
repoStore,
dbStore,
swapStore
);
}
@Test
public void startingTimerAlwaysCausesASwap() {
assertEquals(0, swapJob.swaps.get());
swapJob.start(Duration.ofHours(1));
while (swapJob.swaps.get() <= 0);
assertTrue(swapJob.swaps.get() > 0);
}
@Test
public void swapsHappenEveryInterval() {
assertEquals(0, swapJob.swaps.get());
swapJob.start(Duration.ofMillis(1));
while (swapJob.swaps.get() <= 1);
assertTrue(swapJob.swaps.get() > 1);
}
}

View File

@@ -0,0 +1,18 @@
package uk.ac.ic.wlgitbridge.util;
import org.junit.Assert;
import org.junit.Test;
/**
* Created by winston on 23/08/2016.
*/
public class ProjectTest {
@Test
public void testValidProjectNames() {
Assert.assertFalse(Project.isValidProjectName(null));
Assert.assertFalse(Project.isValidProjectName(""));
Assert.assertFalse(Project.isValidProjectName(".wlgb"));
}
}

View File

@@ -0,0 +1,45 @@
package uk.ac.ic.wlgitbridge.util;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.junit.Assert.assertTrue;
/**
* Created by winston on 23/08/2016.
*/
public class TarTest {
private File testDir;
@Before
public void setup() throws IOException {
TemporaryFolder tmpFolder = new TemporaryFolder();
tmpFolder.create();
testDir = tmpFolder.newFolder("testdir");
Path resdir = Paths.get(
"src/test/resources/uk/ac/ic/wlgitbridge/util/TarTest/testdir"
);
FileUtils.copyDirectory(resdir.toFile(), testDir);
}
@Test
public void tarAndUntarProducesTheSameResult() throws IOException {
InputStream tar = Tar.tar(testDir);
TemporaryFolder tmpF = new TemporaryFolder();
tmpF.create();
File parentDir = tmpF.newFolder();
Tar.untar(tar, parentDir);
File untarred = new File(parentDir, "testdir");
assertTrue(Files.contentsAreEqual(testDir, untarred));
}
}

View File

@@ -0,0 +1,19 @@
package uk.ac.ic.wlgitbridge.util;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Created by winston on 23/08/2016.
*/
public class TimerTest {
@Test
public void testMakeTimerTask() {
int[] iPtr = new int[] { 3 };
Timer.makeTimerTask(() -> iPtr[0] = 5).run();
assertEquals(5, iPtr[0]);
}
}