util_test.cpp 11.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>
G
groot 已提交
19
#include <thread>
20
#include "utils/easylogging++.h"
H
Heisenberg 已提交
21 22 23
#include <sys/types.h>
#include <sys/stat.h>
#include <boost/filesystem.hpp>
G
groot 已提交
24

G
groot 已提交
25 26
#include "utils/CommonUtil.h"
#include "utils/Error.h"
G
groot 已提交
27
#include "utils/StringHelpFunctions.h"
G
groot 已提交
28 29
#include "utils/TimeRecorder.h"
#include "utils/BlockingQueue.h"
G
groot 已提交
30
#include "utils/LogUtil.h"
G
groot 已提交
31 32
#include "utils/ValidationUtil.h"
#include "db/engine/ExecutionEngine.h"
G
groot 已提交
33

J
jinhai 已提交
34
using namespace zilliz::milvus;
G
groot 已提交
35

G
groot 已提交
36
namespace {
G
groot 已提交
37

J
jinhai 已提交
38
static const char* LOG_FILE_PATH = "./milvus/conf/log_config.conf";
G
groot 已提交
39

G
groot 已提交
40 41
}

G
groot 已提交
42 43
TEST(UtilTest, EXCEPTION_TEST) {
    std::string err_msg = "failed";
G
groot 已提交
44 45
    server::ServerException ex(SERVER_UNEXPECTED_ERROR, err_msg);
    ASSERT_EQ(ex.error_code(), SERVER_UNEXPECTED_ERROR);
G
groot 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59
    std::string msg = ex.what();
    ASSERT_EQ(msg, err_msg);
}

TEST(UtilTest, COMMON_TEST) {
    unsigned long total_mem = 0, free_mem = 0;
    server::CommonUtil::GetSystemMemInfo(total_mem, free_mem);
    ASSERT_GT(total_mem, 0);
    ASSERT_GT(free_mem, 0);

    unsigned int thread_cnt = 0;
    server::CommonUtil::GetSystemAvailableThreads(thread_cnt);
    ASSERT_GT(thread_cnt, 0);

G
groot 已提交
60 61 62
    std::string path1 = "/tmp/milvus_test/";
    std::string path2 = path1 + "common_test_12345/";
    std::string path3 = path2 + "abcdef";
G
groot 已提交
63 64
    ErrorCode err = server::CommonUtil::CreateDirectory(path3);
    ASSERT_EQ(err, SERVER_SUCCESS);
G
groot 已提交
65 66
    //test again
    err = server::CommonUtil::CreateDirectory(path3);
G
groot 已提交
67
    ASSERT_EQ(err, SERVER_SUCCESS);
G
groot 已提交
68

G
groot 已提交
69
    ASSERT_TRUE(server::CommonUtil::IsDirectoryExist(path3));
G
groot 已提交
70

G
groot 已提交
71
    err = server::CommonUtil::DeleteDirectory(path1);
G
groot 已提交
72
    ASSERT_EQ(err, SERVER_SUCCESS);
G
groot 已提交
73
    //test again
G
groot 已提交
74
    err = server::CommonUtil::DeleteDirectory(path1);
G
groot 已提交
75
    ASSERT_EQ(err, SERVER_SUCCESS);
G
groot 已提交
76

G
groot 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    ASSERT_FALSE(server::CommonUtil::IsDirectoryExist(path1));
    ASSERT_FALSE(server::CommonUtil::IsFileExist(path1));

    std::string exe_path = server::CommonUtil::GetExePath();
    ASSERT_FALSE(exe_path.empty());

    time_t tt;
    time( &tt );
    tm time_struct;
    memset(&time_struct, 0, sizeof(tm));
    server::CommonUtil::ConvertTime(tt, time_struct);
    ASSERT_GT(time_struct.tm_year, 0);
    ASSERT_GT(time_struct.tm_mon, 0);
    ASSERT_GT(time_struct.tm_mday, 0);
    server::CommonUtil::ConvertTime(time_struct, tt);
    ASSERT_GT(tt, 0);

    bool res = server::CommonUtil::TimeStrToTime("2019-03-23", tt, time_struct);
    ASSERT_EQ(time_struct.tm_year, 119);
    ASSERT_EQ(time_struct.tm_mon, 2);
    ASSERT_EQ(time_struct.tm_mday, 23);
    ASSERT_GT(tt, 0);
    ASSERT_TRUE(res);
G
groot 已提交
100 101 102 103 104 105 106
}

TEST(UtilTest, STRINGFUNCTIONS_TEST) {
    std::string str = " test zilliz";
    server::StringHelpFunctions::TrimStringBlank(str);
    ASSERT_EQ(str, "test zilliz");

G
groot 已提交
107 108 109 110
    str = "\"test zilliz\"";
    server::StringHelpFunctions::TrimStringQuote(str, "\"");
    ASSERT_EQ(str, "test zilliz");

G
groot 已提交
111 112
    str = "a,b,c";
    std::vector<std::string> result;
G
groot 已提交
113 114
    ErrorCode err = server::StringHelpFunctions::SplitStringByDelimeter(str , ",", result);
    ASSERT_EQ(err, SERVER_SUCCESS);
G
groot 已提交
115 116 117 118
    ASSERT_EQ(result.size(), 3UL);

    result.clear();
    err = server::StringHelpFunctions::SplitStringByQuote(str , ",", "\"", result);
G
groot 已提交
119
    ASSERT_EQ(err, SERVER_SUCCESS);
G
groot 已提交
120 121 122 123
    ASSERT_EQ(result.size(), 3UL);

    result.clear();
    err = server::StringHelpFunctions::SplitStringByQuote(str , ",", "", result);
G
groot 已提交
124
    ASSERT_EQ(err, SERVER_SUCCESS);
G
groot 已提交
125 126 127 128
    ASSERT_EQ(result.size(), 3UL);

    str = "55,\"aa,gg,yy\",b";
    result.clear();
G
groot 已提交
129
    err = server::StringHelpFunctions::SplitStringByQuote(str , ",", "\"", result);
G
groot 已提交
130
    ASSERT_EQ(err, SERVER_SUCCESS);
G
groot 已提交
131
    ASSERT_EQ(result.size(), 3UL);
G
groot 已提交
132 133


G
groot 已提交
134 135 136 137 138 139 140 141 142 143 144
}

TEST(UtilTest, BLOCKINGQUEUE_TEST) {
    server::BlockingQueue<std::string> bq;

    static const size_t count = 10;
    bq.SetCapacity(count);

    for(size_t i = 1; i <= count; i++) {
        std::string id = "No." + std::to_string(i);
        bq.Put(id);
G
groot 已提交
145 146
    }

G
groot 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160
    ASSERT_EQ(bq.Size(), count);
    ASSERT_FALSE(bq.Empty());

    std::string str = bq.Front();
    ASSERT_EQ(str, "No.1");

    str = bq.Back();
    ASSERT_EQ(str, "No." + std::to_string(count));

    for(size_t i = 1; i <= count; i++) {
        std::string id = "No." + std::to_string(i);
        str = bq.Take();
        ASSERT_EQ(id, str);
    }
G
groot 已提交
161 162

    ASSERT_EQ(bq.Size(), 0);
G
groot 已提交
163 164
}

G
groot 已提交
165 166 167 168
TEST(UtilTest, LOG_TEST) {
    int32_t res = server::InitLog(LOG_FILE_PATH);
    ASSERT_EQ(res, 0);

G
groot 已提交
169 170 171
    EXPECT_FALSE(el::Loggers::hasFlag(el::LoggingFlag::NewLineForContainer));
    EXPECT_FALSE(el::Loggers::hasFlag(el::LoggingFlag::LogDetailedCrashReason));

G
groot 已提交
172 173 174
    std::string fname = server::GetFileName(LOG_FILE_PATH);
    ASSERT_EQ(fname, "log_config.conf");
}
G
groot 已提交
175

G
groot 已提交
176 177 178 179 180
TEST(UtilTest, TIMERECORDER_TEST) {
    for(int64_t log_level = 0; log_level <= 6; log_level++) {
        if(log_level == 5) {
            continue; //skip fatal
        }
G
groot 已提交
181
        TimeRecorder rc("time", log_level);
G
groot 已提交
182 183 184 185 186
        rc.RecordSection("end");
    }
}

TEST(ValidationUtilTest, VALIDATE_TABLENAME_TEST) {
G
groot 已提交
187
    std::string table_name = "Normal123_";
G
groot 已提交
188 189
    ErrorCode res = server::ValidationUtil::ValidateTableName(table_name);
    ASSERT_EQ(res, SERVER_SUCCESS);
G
groot 已提交
190 191 192

    table_name = "12sds";
    res = server::ValidationUtil::ValidateTableName(table_name);
G
groot 已提交
193
    ASSERT_EQ(res, SERVER_INVALID_TABLE_NAME);
G
groot 已提交
194 195 196

    table_name = "";
    res = server::ValidationUtil::ValidateTableName(table_name);
G
groot 已提交
197
    ASSERT_EQ(res, SERVER_INVALID_TABLE_NAME);
G
groot 已提交
198 199 200

    table_name = "_asdasd";
    res = server::ValidationUtil::ValidateTableName(table_name);
G
groot 已提交
201
    ASSERT_EQ(res, SERVER_SUCCESS);
G
groot 已提交
202 203 204

    table_name = "!@#!@";
    res = server::ValidationUtil::ValidateTableName(table_name);
G
groot 已提交
205
    ASSERT_EQ(res, SERVER_INVALID_TABLE_NAME);
G
groot 已提交
206

G
groot 已提交
207
    table_name = "_!@#!@";
G
groot 已提交
208
    res = server::ValidationUtil::ValidateTableName(table_name);
G
groot 已提交
209
    ASSERT_EQ(res, SERVER_INVALID_TABLE_NAME);
G
groot 已提交
210

G
groot 已提交
211 212
    table_name = "中文";
    res = server::ValidationUtil::ValidateTableName(table_name);
G
groot 已提交
213
    ASSERT_EQ(res, SERVER_INVALID_TABLE_NAME);
G
groot 已提交
214

G
groot 已提交
215
    table_name = std::string(10000, 'a');
G
groot 已提交
216
    res = server::ValidationUtil::ValidateTableName(table_name);
G
groot 已提交
217
    ASSERT_EQ(res, SERVER_INVALID_TABLE_NAME);
G
groot 已提交
218 219
}

G
groot 已提交
220
TEST(ValidationUtilTest, VALIDATE_DIMENSION_TEST) {
G
groot 已提交
221 222 223 224 225
    ASSERT_EQ(server::ValidationUtil::ValidateTableDimension(-1), SERVER_INVALID_VECTOR_DIMENSION);
    ASSERT_EQ(server::ValidationUtil::ValidateTableDimension(0), SERVER_INVALID_VECTOR_DIMENSION);
    ASSERT_EQ(server::ValidationUtil::ValidateTableDimension(16385), SERVER_INVALID_VECTOR_DIMENSION);
    ASSERT_EQ(server::ValidationUtil::ValidateTableDimension(16384), SERVER_SUCCESS);
    ASSERT_EQ(server::ValidationUtil::ValidateTableDimension(1), SERVER_SUCCESS);
G
groot 已提交
226 227
}

G
groot 已提交
228
TEST(ValidationUtilTest, VALIDATE_INDEX_TEST) {
G
groot 已提交
229
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexType((int)engine::EngineType::INVALID), SERVER_INVALID_INDEX_TYPE);
G
groot 已提交
230
    for(int i = 1; i <= (int)engine::EngineType::MAX_VALUE; i++) {
G
groot 已提交
231
        ASSERT_EQ(server::ValidationUtil::ValidateTableIndexType(i), SERVER_SUCCESS);
G
groot 已提交
232
    }
G
groot 已提交
233
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexType((int)engine::EngineType::MAX_VALUE + 1), SERVER_INVALID_INDEX_TYPE);
G
groot 已提交
234

G
groot 已提交
235 236
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexNlist(0), SERVER_INVALID_INDEX_NLIST);
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexNlist(100), SERVER_SUCCESS);
G
groot 已提交
237

G
groot 已提交
238 239
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexFileSize(0), SERVER_INVALID_INDEX_FILE_SIZE);
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexFileSize(100), SERVER_SUCCESS);
G
groot 已提交
240

G
groot 已提交
241 242 243
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexMetricType(0), SERVER_INVALID_INDEX_METRIC_TYPE);
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexMetricType(1), SERVER_SUCCESS);
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexMetricType(2), SERVER_SUCCESS);
G
groot 已提交
244 245
}

G
groot 已提交
246
TEST(ValidationUtilTest, VALIDATE_TOPK_TEST) {
247
    engine::meta::TableSchema schema;
G
groot 已提交
248 249 250
    ASSERT_EQ(server::ValidationUtil::ValidateSearchTopk(10, schema), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::ValidateSearchTopk(65536, schema), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::ValidateSearchTopk(0, schema), SERVER_SUCCESS);
251 252
}

G
groot 已提交
253
TEST(ValidationUtilTest, VALIDATE_NPROBE_TEST) {
254 255
    engine::meta::TableSchema schema;
    schema.nlist_ = 100;
G
groot 已提交
256 257 258
    ASSERT_EQ(server::ValidationUtil::ValidateSearchNprobe(10, schema), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::ValidateSearchNprobe(0, schema), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::ValidateSearchNprobe(101, schema), SERVER_SUCCESS);
259 260
}

G
groot 已提交
261
TEST(ValidationUtilTest, VALIDATE_GPU_TEST) {
G
groot 已提交
262 263
    ASSERT_EQ(server::ValidationUtil::ValidateGpuIndex(0), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::ValidateGpuIndex(100), SERVER_SUCCESS);
G
groot 已提交
264 265

    size_t memory = 0;
G
groot 已提交
266 267
    ASSERT_EQ(server::ValidationUtil::GetGpuMemory(0, memory), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::GetGpuMemory(100, memory), SERVER_SUCCESS);
G
groot 已提交
268 269
}

G
groot 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
TEST(ValidationUtilTest, VALIDATE_IPADDRESS_TEST) {
    ASSERT_EQ(server::ValidationUtil::ValidateIpAddress("127.0.0.1"), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::ValidateIpAddress("not ip"), SERVER_SUCCESS);
}

TEST(ValidationUtilTest, VALIDATE_NUMBER_TEST) {
    ASSERT_EQ(server::ValidationUtil::ValidateStringIsNumber("1234"), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::ValidateStringIsNumber("not number"), SERVER_SUCCESS);
}

TEST(ValidationUtilTest, VALIDATE_BOOL_TEST) {
    std::string str = "true";
    ASSERT_EQ(server::ValidationUtil::ValidateStringIsBool(str), SERVER_SUCCESS);
    str = "not bool";
    ASSERT_NE(server::ValidationUtil::ValidateStringIsBool(str), SERVER_SUCCESS);
}

TEST(ValidationUtilTest, VALIDATE_DOUBLE_TEST) {
    double ret = 0.0;
    ASSERT_EQ(server::ValidationUtil::ValidateStringIsDouble("2.5", ret), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::ValidateStringIsDouble("not double", ret), SERVER_SUCCESS);
}

TEST(ValidationUtilTest, VALIDATE_DBURI_TEST) {
    ASSERT_EQ(server::ValidationUtil::ValidateDbURI("sqlite://:@:/"), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::ValidateDbURI("xxx://:@:/"), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::ValidateDbURI("not uri"), SERVER_SUCCESS);
    ASSERT_EQ(server::ValidationUtil::ValidateDbURI("mysql://root:123456@127.0.0.1:3303/milvus"), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::ValidateDbURI("mysql://root:123456@127.0.0.1:port/milvus"), SERVER_SUCCESS);
G
groot 已提交
299
}
H
Heisenberg 已提交
300 301 302 303 304

TEST(UtilTest, ROLLOUTHANDLER_TEST){
    std::string dir1 = "/tmp/milvus_test";
    std::string dir2 = "/tmp/milvus_test/log_test";
    std::string filename[6] = {"log_global.log", "log_debug.log", "log_warning.log", "log_trace.log", "log_error.log", "log_fatal.log"};
305
    el::Level list[6] = {el::Level::Global, el::Level::Debug, el::Level::Warning, el::Level::Trace, el::Level::Error, el::Level::Fatal};
H
Heisenberg 已提交
306 307 308 309 310 311 312 313 314 315

    mkdir(dir1.c_str(), S_IRWXU);
    mkdir(dir2.c_str(), S_IRWXU);
    for (int i = 0; i < 6; ++i) {
        std::string tmp = dir2 + "/" + filename[i];

        std::ofstream file;
        file.open(tmp.c_str());
        file << "zilliz" << std::endl;

316
        server::RolloutHandler(tmp.c_str(), 0, list[i]);
H
Heisenberg 已提交
317 318 319 320 321 322 323 324 325 326 327

        tmp.append(".1");
        std::ifstream file2;
        file2.open(tmp);

        std::string tmp2;
        file2 >> tmp2;
        ASSERT_EQ(tmp2, "zilliz");
    }
    boost::filesystem::remove_all(dir2);
}