test_config.cpp 3.8 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

G
groot 已提交
18
#include <gtest/gtest.h>
S
starlord 已提交
19
#include <gtest/gtest-death-test.h>
G
groot 已提交
20

Y
yudong.cai 已提交
21
#include "config/YamlConfigMgr.h"
S
starlord 已提交
22 23
#include "utils/CommonUtil.h"
#include "utils/ValidationUtil.h"
S
starlord 已提交
24
#include "server/Config.h"
S
starlord 已提交
25
#include "server/utils.h"
G
groot 已提交
26

G
groot 已提交
27 28
namespace {

S
starlord 已提交
29
static constexpr uint64_t KB = 1024;
S
starlord 已提交
30 31
static constexpr uint64_t MB = KB * 1024;
static constexpr uint64_t GB = MB * 1024;
S
starlord 已提交
32

S
starlord 已提交
33
} // namespace
G
groot 已提交
34

S
starlord 已提交
35
TEST_F(ConfigTest, CONFIG_TEST) {
Y
yudong.cai 已提交
36
    milvus::server::ConfigMgr *config_mgr = milvus::server::YamlConfigMgr::GetInstance();
G
groot 已提交
37

Y
yudong.cai 已提交
38 39
    milvus::Status s = config_mgr->LoadConfigFile("");
    ASSERT_FALSE(s.ok());
G
groot 已提交
40

S
starlord 已提交
41 42
    std::string config_path(CONFIG_PATH);
    s = config_mgr->LoadConfigFile(config_path+ INVALID_CONFIG_FILE);
Y
yudong.cai 已提交
43
    ASSERT_FALSE(s.ok());
G
groot 已提交
44

S
starlord 已提交
45
    s = config_mgr->LoadConfigFile(config_path + VALID_CONFIG_FILE);
Y
yudong.cai 已提交
46
    ASSERT_TRUE(s.ok());
G
groot 已提交
47

G
groot 已提交
48 49
    config_mgr->Print();

S
starlord 已提交
50 51 52 53 54 55
    milvus::server::ConfigNode &root_config = config_mgr->GetRootNode();
    milvus::server::ConfigNode &server_config = root_config.GetChild("server_config");
    milvus::server::ConfigNode &db_config = root_config.GetChild("db_config");
    milvus::server::ConfigNode &metric_config = root_config.GetChild("metric_config");
    milvus::server::ConfigNode &cache_config = root_config.GetChild("cache_config");
    milvus::server::ConfigNode invalid_config = root_config.GetChild("invalid_config");
S
starlord 已提交
56 57 58
    auto valus = invalid_config.GetSequence("not_exist");
    float ff = invalid_config.GetFloatValue("not_exist", 3.0);
    ASSERT_EQ(ff, 3.0);
G
groot 已提交
59 60 61 62

    std::string address = server_config.GetValue("address");
    ASSERT_TRUE(!address.empty());
    int64_t port = server_config.GetInt64Value("port");
S
starlord 已提交
63
    ASSERT_NE(port, 0);
G
groot 已提交
64 65 66 67 68

    server_config.SetValue("test", "2.5");
    double test = server_config.GetDoubleValue("test");
    ASSERT_EQ(test, 2.5);

S
starlord 已提交
69
    milvus::server::ConfigNode fake;
G
groot 已提交
70 71
    server_config.AddChild("fake", fake);
    fake = server_config.GetChild("fake");
S
starlord 已提交
72
    milvus::server::ConfigNodeArr arr;
G
groot 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    server_config.GetChildren(arr);
    ASSERT_EQ(arr.size(), 1UL);

    server_config.ClearChildren();
    auto children = server_config.GetChildren();
    ASSERT_TRUE(children.empty());

    server_config.ClearConfig();
    auto configs = server_config.GetConfig();
    ASSERT_TRUE(configs.empty());

    server_config.AddSequenceItem("seq", "aaa");
    server_config.AddSequenceItem("seq", "bbb");
    auto seq = server_config.GetSequence("seq");
    ASSERT_EQ(seq.size(), 2UL);

S
starlord 已提交
89
    milvus::server::ConfigNode combine;
G
groot 已提交
90 91 92 93 94 95
    combine.Combine(server_config);

    combine.PrintAll();
    std::string all = combine.DumpString();
    ASSERT_TRUE(!all.empty());

G
groot 已提交
96 97 98 99 100
    server_config.ClearSequences();
    auto seqs = server_config.GetSequences();
    ASSERT_TRUE(seqs.empty());
}

S
starlord 已提交
101
TEST_F(ConfigTest, SERVER_CONFIG_TEST) {
S
starlord 已提交
102
    std::string config_path(CONFIG_PATH);
S
starlord 已提交
103
    milvus::server::Config &config = milvus::server::Config::GetInstance();
S
starlord 已提交
104
    milvus::Status s = config.LoadConfigFile(config_path + VALID_CONFIG_FILE);
Y
yudong.cai 已提交
105
    ASSERT_TRUE(s.ok());
G
groot 已提交
106

Y
yudong.cai 已提交
107 108
    s = config.ValidateConfig();
    ASSERT_TRUE(s.ok());
S
starlord 已提交
109

Y
yudong.cai 已提交
110
    config.PrintAll();
Y
yudong.cai 已提交
111 112 113

    s = config.ResetDefaultConfig();
    ASSERT_TRUE(s.ok());
S
starlord 已提交
114
}