aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerTest.java
blob: 9b274861e0c9ce083f585f1bea01d9bd1acb3202 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.standalone;

import com.yahoo.vdslib.distribution.Distribution;
import junit.framework.TestCase;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

public abstract class ClusterControllerTest extends TestCase {
    private final Map<String, String> overriddenProperties = new TreeMap<>();
    private File tempDirectory;

    protected void setProperty(String p, String val) {
        if (!overriddenProperties.containsKey(p)) {
            overriddenProperties.put(p, System.getProperty(p));
        }
        System.setProperty(p, val);
    }

    protected File createTemporaryDirectory() throws IOException {
        File f = File.createTempFile("clustercontroller", "configtest");
        if (f.exists()) f.delete();
        f.mkdirs();
        return f;
    }

    @Override
    public void setUp() throws Exception {
        tempDirectory = createTemporaryDirectory();
    }

    @Override
    public void tearDown() {
        for (Map.Entry<String, String> e : overriddenProperties.entrySet()) {
            if (e.getValue() == null) {
                System.clearProperty(e.getKey());
            } else {
                System.setProperty(e.getKey(), e.getValue());
            }
        }
        overriddenProperties.clear();
        if (tempDirectory != null) {
            for (File f : tempDirectory.listFiles()) {
                f.delete();
            }
            tempDirectory.delete();
            tempDirectory = null;
        }
    }

    protected void writeConfig(String config, String value) throws IOException {
        File f = new File(tempDirectory, config + ".cfg");
        FileWriter fw = new FileWriter(f);
        fw.write(value);
        fw.close();
    }

    protected void setFleetControllerConfigProperty() {
        setProperty("config.id", "dir:" + tempDirectory.toString());
    }

    protected void addFleetControllerConfig(int stateGatherCount, int fleetcontrollers) throws IOException {
        writeConfig("fleetcontroller", "cluster_name \"storage\"\n" +
                "index 0\n" +
                "state_gather_count " + stateGatherCount + "\n" +
                "fleet_controller_count " + fleetcontrollers + "\n" +
                "zookeeper_server \"\"");
    }
    protected void setSlobrokConfigProperty() {
        setProperty("slobrok.config.id", "dir:" + tempDirectory.toString());
    }
    protected void addSlobrokConfig() throws IOException {
        writeConfig("slobroks", "cluster_name \"storage\"\n" +
                "index 0\n" +
                "zookeeper_server \"\"");
    }
    protected void addDistributionConfig() throws IOException {
        writeConfig("stor-distribution", Distribution.getDefaultDistributionConfig(2, 10));
    }
    protected void addZookeepersConfig() throws IOException {
        writeConfig("zookeepers", "zookeeperserverlist \"\"");
    }

    protected void setupConfig() throws Exception {
        setFleetControllerConfigProperty();
        setSlobrokConfigProperty();
        addFleetControllerConfig(2, 1);
        addSlobrokConfig();
        addDistributionConfig();
        addZookeepersConfig();
    }
}