aboutsummaryrefslogtreecommitdiffstats
path: root/node-maintainer/src/test/java/com/yahoo/vespa/hosted/node/verification/spec/retrievers/DiskRetrieverTest.java
blob: 1a9e035ab7bf3fa7964eba83206be8482c81c9ed (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.yahoo.vespa.hosted.node.verification.spec.retrievers;

import com.yahoo.vespa.hosted.node.verification.commons.parser.ParseResult;
import com.yahoo.vespa.hosted.node.verification.mock.MockCommandExecutor;
import com.yahoo.vespa.hosted.node.verification.spec.retrievers.HardwareInfo.DiskType;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * Created by olaa on 06/07/2017.
 */
public class DiskRetrieverTest {

    private MockCommandExecutor commandExecutor;
    private HardwareInfo hardwareInfo;
    private DiskRetriever diskRetriever;
    private static String CAT_RESOURCE_PATH = "cat src/test/java/com/yahoo/vespa/hosted/node/verification/spec/resources/";
    private static final double DELTA = 0.1;

    @Before
    public void setup() {
        hardwareInfo = new HardwareInfo();
        commandExecutor = new MockCommandExecutor();
        diskRetriever = new DiskRetriever(hardwareInfo, commandExecutor);
    }

    @Test
    public void updateInfo_should_store_diskType_and_diskSize_in_hardware_info() {
        commandExecutor.addCommand(CAT_RESOURCE_PATH + "DiskTypeFastDisk");
        commandExecutor.addCommand(CAT_RESOURCE_PATH + "filesize");
        diskRetriever.updateInfo();
        assertEquals(DiskType.FAST, hardwareInfo.getDiskType());
        double expectedSize = 1759.84;
        assertEquals(expectedSize, hardwareInfo.getMinDiskAvailableGb(), DELTA);
    }

    @Test
    public void updateDiskType__should_store_diskType_in_hardwareInfo() throws IOException {
        commandExecutor.addCommand(CAT_RESOURCE_PATH + "DiskTypeFastDisk");
        diskRetriever.updateDiskType();
        assertEquals(DiskType.FAST, hardwareInfo.getDiskType());
    }

    @Test
    public void updateDiskSize__should_store_diskSize_in_hardwareInfo() throws IOException {
        commandExecutor.addCommand(CAT_RESOURCE_PATH + "filesize");
        diskRetriever.updateDiskSize();
        double expectedSize = 1759.84;
        assertEquals(expectedSize, hardwareInfo.getMinDiskAvailableGb(), DELTA);
    }

    @Test
    public void parseDiskType_should_find_fast_disk() throws Exception {
        diskRetriever = new DiskRetriever(hardwareInfo, commandExecutor);
        ArrayList<String> mockOutput = commandExecutor.outputFromString("Name  Rota \nsda 0");
        ParseResult parseResult = diskRetriever.parseDiskType(mockOutput);
        ParseResult expectedParseResult = new ParseResult("sda", "0");
        assertEquals(expectedParseResult, parseResult);
    }

    @Test
    public void parseDiskType_should_not_find_fast_disk() throws Exception {
        ArrayList<String> mockOutput = commandExecutor.outputFromString("Name  Rota \nsda 1");
        ParseResult parseResult = diskRetriever.parseDiskType(mockOutput);
        ParseResult expectedParseResult = new ParseResult("sda", "1");
        assertEquals(expectedParseResult, parseResult);
    }

    @Test
    public void parseDiskType_with_invalid_outputstream_does_not_contain_searchword_should_throw_exception() throws Exception {
        ArrayList<String> mockOutput = commandExecutor.outputFromString("Name  Rota");
        try {
            ParseResult parseResult = diskRetriever.parseDiskType(mockOutput);
            fail("Should have thrown IOException when outputstream doesn't contain search word");
        } catch (IOException e) {
            String expectedExceptionMessage = "Parsing for disk type failed";
            assertEquals(expectedExceptionMessage, e.getMessage());
        }

    }

    @Test
    public void parseDiskSize_should_find_size_from_file_and_insert_into_parseResult() throws Exception {
        String filepath = "src/test/java/com/yahoo/vespa/hosted/node/verification/spec/resources/filesize";
        ArrayList<String> mockOutput = MockCommandExecutor.readFromFile(filepath);
        ArrayList<ParseResult> parseResults = diskRetriever.parseDiskSize(mockOutput);
        ParseResult expectedParseResult1 = new ParseResult("Size", "799.65");
        assertEquals(expectedParseResult1, parseResults.get(0));
        ParseResult expectedParseResult2 = new ParseResult("Size", "960.19");
        assertEquals(expectedParseResult2, parseResults.get(1));
    }

    @Test
    public void setDiskType_invalid_ParseResult_should_set_fastDisk_to_invalid() {
        ParseResult parseResult = new ParseResult("Invalid", "Invalid");
        diskRetriever.setDiskType(parseResult);
        HardwareInfo.DiskType expectedDiskType = HardwareInfo.DiskType.UNKNOWN;
        assertEquals(expectedDiskType, hardwareInfo.getDiskType());
    }

}