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

import static org.junit.jupiter.api.Assertions.*;

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 int ownerId = 123; // default is 1
    private int groupId = 456; // default is 2
    private String permissions = "rw-r-xr--";

    @Test
    void trivial() {
        assertConvergence("Creating file /dir/file.txt with permissions rw-r-xr--",
                "Changing user ID of /dir/file.txt from 1 to 123",
                "Changing group ID of /dir/file.txt from 2 to 456");

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

        ownerId = 124;
        assertConvergence("Changing user ID of /dir/file.txt from 123 to 124");

        groupId = 457;
        assertConvergence("Changing group ID of /dir/file.txt from 456 to 457");

        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)
                .withOwnerId(ownerId)
                .withGroupId(groupId)
                .withPermissions(permissions)
                .create();
        taskContext.clearSystemModificationLog();
        assertTrue(fileSync.convergeTo(taskContext, fileData));

        assertTrue(Files.isRegularFile(path));
        fileData.getContent().ifPresent(content -> assertArrayEquals(content, unixPath.readBytes()));
        fileData.getOwnerId().ifPresent(owner -> assertEquals((int) owner, unixPath.getOwnerId()));
        fileData.getGroupId().ifPresent(group -> assertEquals((int) group, unixPath.getGroupId()));
        fileData.getPermissions().ifPresent(permissions -> assertEquals(permissions, unixPath.getPermissions()));

        List<String> actualMods = taskContext.getSystemModificationLog();
        List<String> expectedMods = List.of(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);
    }
}