summaryrefslogtreecommitdiff
path: root/test/test_exporter_local.cpp
blob: 5915caf487a704017060a44435ce1a894ec5f3b4 (plain)
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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <gtest/gtest.h>
#include "fieldstat.h"
#include "fieldstat_exporter.h"
#include "cjson/cJSON.h"

/*******************************************************************************
 * case: output counter 
*******************************************************************************/

static struct fieldstat *create_fieldstat_MetricTypeCounter()
{
    const char *metric_name[] = {
        "version", "threads", "tables", "plug_cached", "plug_acc", "group", 
        "not_grp", "compile", "garbage_num", "outer_mid", "scan_bytes",
        "scan_times", "update_err", "scan_error", "z_stream","nt_grp_hit", 
        "cmd_commit", "cmd_in_q", "line_cmd/s"};
   
    struct fieldstat *instance = fieldstat_new();
    EXPECT_NE(nullptr, instance);

    int cube_id = fieldstat_register_cube(instance, NULL, 0, 
                                          SAMPLING_MODE_COMPREHENSIVE, 3);
    EXPECT_EQ(0, cube_id);

    for(unsigned int i= 0; i < sizeof(metric_name)/sizeof(metric_name[0]); i++)
    {
        int counter_id = fieldstat_register_counter(instance, cube_id, 
                                                    metric_name[i],
                                                    COUNTER_MERGE_BY_SUM);
        int cell_id = fieldstat_cube_add(instance, cube_id, NULL, 0, 1);
        EXPECT_EQ(0, cell_id);    
        if(cell_id >= 0) 
        {
            fieldstat_counter_incrby(instance, cube_id, counter_id, cell_id, i);
        }
    }
    EXPECT_NE(nullptr, instance);

    return instance;
}




TEST(ExporterLocal, MetricTypeCounter)

{
    int ret = 0;
    struct fieldstat_exporter *exporter = NULL;
    struct fieldstat *instance = NULL;
    // input start
    const char *exporter_name = "firewall";

    exporter = fieldstat_exporter_new(exporter_name, NULL, 0);
    EXPECT_NE(nullptr, exporter);

    ret = fieldstat_exporter_enable_prometheus_exporter(exporter);
    EXPECT_EQ(0, ret);

    ret = fieldstat_exporter_set_local_exporter(exporter, "/tmp/fs4_exporter");
    EXPECT_EQ(0, ret);

    fieldstat_exporter_start(exporter);

    instance = create_fieldstat_MetricTypeCounter();

    ret = fieldstat_exporter_merge_fieldstat(exporter, &instance, 1);
    EXPECT_EQ(0, ret);

    ret = fieldstat_exporter_local_export(exporter);
    EXPECT_EQ(0, ret);

    fieldstat_exporter_free(exporter);
    fieldstat_free(instance);
}



int main(int argc, char *argv[]) 
{
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}