summaryrefslogtreecommitdiffstats
path: root/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileSyncTest.java
blob: 44868e1746468989d17c7391ce5fde1d24f5613f (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
// 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.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class FileSyncTest {
    private final TestTaskContext taskContext = new TestTaskContext();
    private final FileSystem fileSystem = TestFileSystem.create();

    private final Path path = fileSystem.getPath("/dir/file.txt");
    private final UnixPath unixPath = new UnixPath(path);
    private final FileSync fileSync = new FileSync(path);

    private String content = "content";
    private String owner = "owner"; // default is user
    private String group = "group1"; // default is group
    private String permissions = "rw-r-xr--";

    @Test
    public void trivial() {
        assertConvergence("Creating file /dir/file.txt",
                "Changing owner of /dir/file.txt from user to owner",
                "Changing group of /dir/file.txt from group to group1",
                "Changing permissions of /dir/file.txt from rw-r--r-- to rw-r-xr--");

        content = "new-content";
        assertConvergence("Patching file /dir/file.txt");

        owner = "new-owner";
        assertConvergence("Changing owner of /dir/file.txt from owner to " +
                        owner);

        group = "new-group1";
        assertConvergence("Changing group of /dir/file.txt from group1 to new-group1");

        permissions = "rwxr--rwx";
        assertConvergence("Changing permissions of /dir/file.txt from rw-r-xr-- to " +
                permissions);
    }

    private void assertConvergence(String... systemModificationMessages) {
        PartialFileData fileData = PartialFileData.builder()
                .withContent(content)
                .withOwner(owner)
                .withGroup(group)
                .withPermissions(permissions)
                .create();
        taskContext.clearSystemModificationLog();
        assertTrue(fileSync.convergeTo(taskContext, fileData));

        assertTrue(Files.isRegularFile(path));
        fileData.getContent().ifPresent(content -> assertEquals(content, unixPath.readUtf8File()));
        fileData.getOwner().ifPresent(owner -> assertEquals(owner, unixPath.getOwner()));
        fileData.getGroup().ifPresent(group -> assertEquals(group, unixPath.getGroup()));
        fileData.getPermissions().ifPresent(permissions -> assertEquals(permissions, unixPath.getPermissions()));

        List<String> actualMods = taskContext.getSystemModificationLog();
        List<String> expectedMods = Arrays.asList(systemModificationMessages);
        assertEquals(expectedMods, actualMods);

        UnixPath unixPath = new UnixPath(path);
        Instant lastModifiedTime = unixPath.getLastModifiedTime();
        taskContext.clearSystemModificationLog();
        assertFalse(fileSync.convergeTo(taskContext, fileData));
        assertEquals(lastModifiedTime, unixPath.getLastModifiedTime());

        actualMods = taskContext.getSystemModificationLog();
        assertEquals(new ArrayList<>(), actualMods);
    }
}