diff options
author | Håkon Hallingstad <hakon@oath.com> | 2018-01-30 00:16:20 +0100 |
---|---|---|
committer | Håkon Hallingstad <hakon@oath.com> | 2018-01-30 00:16:20 +0100 |
commit | dd6de3c18ed9f5a6d89f843b8a0835c8e8480a9d (patch) | |
tree | ba1010d94236d797fc2b27f8072674eedce9c705 /node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin | |
parent | 0e6ec009fcce1796b4d9ad0ad7accaa403221930 (diff) |
Implement directory resource
Diffstat (limited to 'node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin')
2 files changed, 105 insertions, 0 deletions
diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/MakeDirectoryTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/MakeDirectoryTest.java new file mode 100644 index 00000000000..05662de3b95 --- /dev/null +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/MakeDirectoryTest.java @@ -0,0 +1,91 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +package com.yahoo.vespa.hosted.node.admin.task.util.file; + +import com.yahoo.vespa.test.file.TestFileSystem; +import org.junit.Test; + +import java.io.UncheckedIOException; +import java.nio.file.FileSystem; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.util.Arrays; +import java.util.Collections; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * @author hakonhall + */ +public class MakeDirectoryTest { + private final FileSystem fileSystem = TestFileSystem.create(); + private final TestTaskContext context = new TestTaskContext(); + + private String path = "/parent/dir"; + private String permissions = "rwxr----x"; + private String owner = "test-owner"; + private String group = "test-group"; + + @Test + public void newDirectory() { + verifySystemModifications( + "Creating directory " + path, + "Changing owner of /parent/dir from user to test-owner", + "Changing group of /parent/dir from group to test-group"); + + owner = "new-owner"; + verifySystemModifications("Changing owner of /parent/dir from test-owner to new-owner"); + + group = "new-group"; + verifySystemModifications("Changing group of /parent/dir from test-group to new-group"); + + permissions = "--x---r--"; + verifySystemModifications("Changing permissions of /parent/dir from rwxr----x to --x---r--"); + } + + private void verifySystemModifications(String... modifications) { + context.clearSystemModificationLog(); + MakeDirectory makeDirectory = new MakeDirectory(fileSystem.getPath(path)) + .createParents() + .withPermissions(permissions) + .withOwner(owner) + .withGroup(group); + assertTrue(makeDirectory.converge(context)); + + assertEquals(Arrays.asList(modifications), context.getSystemModificationLog()); + + context.clearSystemModificationLog(); + assertFalse(makeDirectory.converge(context)); + assertEquals(Collections.emptyList(), context.getSystemModificationLog()); + } + + @Test + public void exceptionIfMissingParent() { + String path = "/parent/dir"; + MakeDirectory makeDirectory = new MakeDirectory(fileSystem.getPath(path)); + + try { + makeDirectory.converge(context); + } catch (UncheckedIOException e) { + if (e.getCause() instanceof NoSuchFileException) { + return; + } + throw e; + } + fail(); + } + + @Test + public void okIfParentExists() { + String path = "/dir"; + MakeDirectory makeDirectory = new MakeDirectory(fileSystem.getPath(path)); + assertTrue(makeDirectory.converge(context)); + assertTrue(Files.isDirectory(fileSystem.getPath(path))); + + MakeDirectory makeDirectory2 = new MakeDirectory(fileSystem.getPath(path)); + assertFalse(makeDirectory2.converge(context)); + } +}
\ No newline at end of file diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java index bd29f239e1d..6f1991ec3d4 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java @@ -13,6 +13,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +/** + * @author hakonhall + */ public class UnixPathTest { final FileSystem fileSystem = TestFileSystem.create(); @@ -63,4 +66,15 @@ public class UnixPathTest { unixPath.setGroup("group"); assertEquals("group", unixPath.getGroup()); } + + @Test + public void createDirectoryWithPermissions() { + FileSystem fs = TestFileSystem.create(); + Path path = fs.getPath("dir"); + UnixPath unixPath = new UnixPath(path); + String permissions = "rwxr-xr--"; + unixPath.createDirectory(permissions); + assertTrue(Files.isDirectory(path)); + assertEquals(permissions, unixPath.getPermissions()); + } } |