aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/MakeDirectoryTest.java
blob: b5a4d7475f3adbfe3d675d948521fe50896c164a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright Yahoo. 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.hosted.node.admin.component.TestTaskContext;
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.List;

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 final 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(List.of(modifications), context.getSystemModificationLog());

        context.clearSystemModificationLog();
        assertFalse(makeDirectory.converge(context));
        assertEquals(List.of(), 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));
    }
}