aboutsummaryrefslogtreecommitdiffstats
path: root/vdstestlib/src/tests/dirconfig/dirconfigtest.cpp
blob: 1346f9c4ca3a2f42a9393ea1973eb3ea32989b41 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vdstestlib/config/dirconfig.h>
#include <fstream>
#include <iostream>

using namespace vdstestlib;

namespace vespalib {

class Test : public vespalib::TestApp
{
public:
    void testNormalUsage();
    int Main() override;
};

int
Test::Main()
{
    TEST_INIT("dirconfig_test");
    srandom(1);
    testNormalUsage();
    TEST_DONE();
}

#define ASSERT_FILE_CONTENT(file, content) \
{ \
    std::ifstream in(file); \
    EXPECT_TRUE(bool(in)); \
    std::ostringstream ost; \
    std::string line; \
    while (getline(in, line, '\n')) { \
        ost << line << '\n'; \
    } \
    EXPECT_EQUAL(content, ost.str()); \
}

void Test::testNormalUsage() {
    DirConfig config1;
    DirConfig config2;
    EXPECT_EQUAL(strncmp("dir:dirconfig.tmp.", config1.getConfigId().c_str(), 18), 0);
    EXPECT_EQUAL(26u, config1.getConfigId().size());
    EXPECT_EQUAL('/', config1.getConfigId()[24]);
    EXPECT_EQUAL('0', config1.getConfigId()[25]);
    EXPECT_EQUAL(strncmp("dir:dirconfig.tmp.", config2.getConfigId().c_str(), 18), 0);
    EXPECT_EQUAL(26u, config2.getConfigId().size());
    EXPECT_EQUAL('/', config2.getConfigId()[24]);
    EXPECT_EQUAL('1', config2.getConfigId()[25]);

    try{
        config1.getConfig("testconfig");
        TEST_FATAL("Not supposed to get here");
    } catch (vespalib::Exception& e) {
        EXPECT_EQUAL("No config named testconfig", e.getMessage());
    }
    DirConfig::Config& file1(config1.addConfig("testconfig"));
    try{
        config1.addConfig("testconfig");
        TEST_FATAL("Not supposed to get here");
    } catch (vespalib::Exception& e) {
        EXPECT_EQUAL("There is already a config named testconfig",
                   e.getMessage());
    }
    EXPECT_EQUAL(&file1, &config1.getConfig("testconfig"));
    file1.set("intval", "5");
    file1.set("intval", "7");
    file1.set("stringval", "\"foo\"");
    file1.set("tmpval", "4");
    file1.remove("tmpval");
        // Trigger publish
    config1.getConfigId();

    ASSERT_FILE_CONTENT(config1.getDir() + "/testconfig.cfg",
                        "intval 7\n"
                        "stringval \"foo\"\n");

    DirConfig::Config& file2(config2.addConfig("testconfig"));
    file2.set("longval", "6");
    file2.clear();
    file2.set("intval", "4");

    DirConfig::Config& file3(config1.addConfig("config2"));
    file3.set("intval", "3");
    file3.set("myarray[2]");
    file3.set("myarray[0].foo", "4");
    file3.set("myarray[1].foo", "2");

    config1.publish();
    config2.publish();

    ASSERT_FILE_CONTENT(config2.getDir() + "/testconfig.cfg",
                        "intval 4\n");
    ASSERT_FILE_CONTENT(config1.getDir() + "/config2.cfg",
                        "intval 3\n"
                        "myarray[2]\n"
                        "myarray[0].foo 4\n"
                        "myarray[1].foo 2\n");

}

} // vespalib

TEST_APPHOOK(vespalib::Test)