aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/MakeDirectoryTest.java
blob: 11675bbe46f2fd81f196598d8df6501c8b0498e9 (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
// Copyright Vespa.ai. 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.jupiter.api.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.jupiter.api.Assertions.*;

/**
 * @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 int ownerId = 123;
    private int groupId = 456;

    @Test
    void newDirectory() {
        verifySystemModifications(
                "Creating directory " + path,
                "Changing user ID of /parent/dir from 1 to 123",
                "Changing group ID of /parent/dir from 2 to 456");

        ownerId = 124;
        verifySystemModifications("Changing user ID of /parent/dir from 123 to 124");

        groupId = 457;
        verifySystemModifications("Changing group ID of /parent/dir from 456 to 457");

        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)
                .withOwnerId(ownerId)
                .withGroupId(groupId);
        assertTrue(makeDirectory.converge(context));

        assertEquals(List.of(modifications), context.getSystemModificationLog());

        context.clearSystemModificationLog();
        assertFalse(makeDirectory.converge(context));
        assertEquals(List.of(), context.getSystemModificationLog());
    }

    @Test
    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
    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));
    }
}