summaryrefslogtreecommitdiffstats
path: root/messagebus/src/tests/configagent/configagent.cpp
blob: e8cab1e679a1255cfa393bc6902d8da23ec89253 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/messagebus/configagent.h>
#include <vespa/messagebus/iconfighandler.h>
#include <vespa/messagebus/routing/routingspec.h>
#include <vespa/messagebus/config-messagebus.h>
#include <vespa/config/print/fileconfigreader.hpp>
#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/vespalib/testkit/test_path.h>

using namespace mbus;
using namespace messagebus;
using namespace config;

class ConfigAgentTest : public testing::Test, public IConfigHandler {
protected:
    RoutingSpec _spec;
    ConfigAgentTest();
    ~ConfigAgentTest() override;
    bool checkHalf();
    bool checkFull();
    void checkTables(uint32_t numTables, bool& success);
    bool setupRouting(RoutingSpec spec) override;
};

ConfigAgentTest::ConfigAgentTest()
    : testing::Test(),
      IConfigHandler(),
      _spec()
{
}

ConfigAgentTest::~ConfigAgentTest() = default;

bool
ConfigAgentTest::setupRouting(RoutingSpec spec)
{
    _spec = std::move(spec);
    return true;
}

void
ConfigAgentTest::checkTables(uint32_t numTables, bool& success)
{
    ASSERT_EQ(numTables, _spec.getNumTables());
    if (numTables > 0) {
        ASSERT_EQ("foo", _spec.getTable(0).getProtocol());
        ASSERT_EQ(2u, _spec.getTable(0).getNumHops());
        ASSERT_EQ("foo-h1", _spec.getTable(0).getHop(0).getName());
        ASSERT_EQ("foo-h1-sel", _spec.getTable(0).getHop(0).getSelector());
        ASSERT_EQ(2u, _spec.getTable(0).getHop(0).getNumRecipients());
        ASSERT_EQ("foo-h1-r1", _spec.getTable(0).getHop(0).getRecipient(0));
        ASSERT_EQ("foo-h1-r2", _spec.getTable(0).getHop(0).getRecipient(1));
        ASSERT_EQ(true, _spec.getTable(0).getHop(0).getIgnoreResult());
        ASSERT_EQ("foo-h2", _spec.getTable(0).getHop(1).getName());
        ASSERT_EQ("foo-h2-sel", _spec.getTable(0).getHop(1).getSelector());
        ASSERT_EQ(2u, _spec.getTable(0).getHop(1).getNumRecipients());
        ASSERT_EQ("foo-h2-r1", _spec.getTable(0).getHop(1).getRecipient(0));
        ASSERT_EQ("foo-h2-r2", _spec.getTable(0).getHop(1).getRecipient(1));
        ASSERT_EQ(2u, _spec.getTable(0).getNumRoutes());
        ASSERT_EQ("foo-r1", _spec.getTable(0).getRoute(0).getName());
        ASSERT_EQ(2u, _spec.getTable(0).getRoute(0).getNumHops());
        ASSERT_EQ("foo-h1", _spec.getTable(0).getRoute(0).getHop(0));
        ASSERT_EQ("foo-h2", _spec.getTable(0).getRoute(0).getHop(1));
        ASSERT_EQ("foo-r2", _spec.getTable(0).getRoute(1).getName());
        ASSERT_EQ(2u, _spec.getTable(0).getRoute(1).getNumHops());
        ASSERT_EQ("foo-h2", _spec.getTable(0).getRoute(1).getHop(0));
        ASSERT_EQ("foo-h1", _spec.getTable(0).getRoute(1).getHop(1));
    }
    if (numTables > 1) {
        ASSERT_EQ("bar", _spec.getTable(1).getProtocol());
        ASSERT_EQ(2u, _spec.getTable(1).getNumHops());
        ASSERT_EQ("bar-h1", _spec.getTable(1).getHop(0).getName());
        ASSERT_EQ("bar-h1-sel", _spec.getTable(1).getHop(0).getSelector());
        ASSERT_EQ(2u, _spec.getTable(1).getHop(0).getNumRecipients());
        ASSERT_EQ("bar-h1-r1", _spec.getTable(1).getHop(0).getRecipient(0));
        ASSERT_EQ("bar-h1-r2", _spec.getTable(1).getHop(0).getRecipient(1));
        ASSERT_EQ("bar-h2", _spec.getTable(1).getHop(1).getName());
        ASSERT_EQ("bar-h2-sel", _spec.getTable(1).getHop(1).getSelector());
        ASSERT_EQ(2u, _spec.getTable(1).getHop(1).getNumRecipients());
        ASSERT_EQ("bar-h2-r1", _spec.getTable(1).getHop(1).getRecipient(0));
        ASSERT_EQ("bar-h2-r2", _spec.getTable(1).getHop(1).getRecipient(1));
        ASSERT_EQ(2u, _spec.getTable(1).getNumRoutes());
        ASSERT_EQ("bar-r1", _spec.getTable(1).getRoute(0).getName());
        ASSERT_EQ(2u, _spec.getTable(1).getRoute(0).getNumHops());
        ASSERT_EQ("bar-h1", _spec.getTable(1).getRoute(0).getHop(0));
        ASSERT_EQ("bar-h2", _spec.getTable(1).getRoute(0).getHop(1));
        ASSERT_EQ("bar-r2", _spec.getTable(1).getRoute(1).getName());
        ASSERT_EQ(2u, _spec.getTable(1).getRoute(1).getNumHops());
        ASSERT_EQ("bar-h2", _spec.getTable(1).getRoute(1).getHop(0));
        ASSERT_EQ("bar-h1", _spec.getTable(1).getRoute(1).getHop(1));
    }
    success = true;
}

bool
ConfigAgentTest::checkHalf()
{
    bool success = false;
    return _spec.getNumTables() == 1 && (checkTables(1, success), success);
}

bool
ConfigAgentTest::checkFull()
{
    bool success = false;
    return _spec.getNumTables() == 2 && (checkTables(2, success), success);
}

TEST_F(ConfigAgentTest, test_config_agent)
{
    EXPECT_TRUE(!checkHalf());
    EXPECT_TRUE(!checkFull());
    ConfigAgent agent(*this);
    EXPECT_TRUE(!checkHalf());
    EXPECT_TRUE(!checkFull());
    agent.configure(FileConfigReader<MessagebusConfig>(TEST_PATH("full.cfg")).read());
    EXPECT_TRUE(!checkHalf());
    EXPECT_TRUE(checkFull());
    agent.configure(FileConfigReader<MessagebusConfig>(TEST_PATH("half.cfg")).read());
    EXPECT_TRUE(checkHalf());
    EXPECT_TRUE(!checkFull());
    agent.configure(FileConfigReader<MessagebusConfig>(TEST_PATH("full.cfg")).read());
    EXPECT_TRUE(checkFull());
    EXPECT_TRUE(!checkHalf());
}

GTEST_MAIN_RUN_ALL_TESTS()