diff options
| author | fumingwei <[email protected]> | 2023-03-23 12:56:03 +0800 |
|---|---|---|
| committer | fumingwei <[email protected]> | 2023-03-23 13:19:34 +0800 |
| commit | 08a8907cdb1f7537d95ee144f947a743e2ae697d (patch) | |
| tree | bafe8ec995c59b0e4604986dbf97ce97f1078950 | |
| parent | 1b3d40eaa3db4867d71b0908f6121f1f21b8b448 (diff) | |
feature:1.新增执行测试用例执行流程2.新增cppcheck执行流程
| -rw-r--r-- | .gitlab-ci.yml | 14 | ||||
| -rwxr-xr-x | CMakeLists.txt | 31 | ||||
| -rw-r--r-- | ci/travis.sh | 9 | ||||
| -rw-r--r-- | src/fieldstat.cpp | 2 | ||||
| -rw-r--r-- | src/file_output.cpp | 6 | ||||
| -rw-r--r-- | src/line_protocol_output.cpp | 2 | ||||
| -rw-r--r-- | src/prometheus_output.cpp | 10 | ||||
| -rw-r--r-- | test/bin/telegraf_unit_test.conf | 25 | ||||
| -rw-r--r-- | test/src/gtest_dynamic_fieldstat.cpp | 26 | ||||
| -rw-r--r-- | test/src/gtest_fieldstat.cpp | 1 |
10 files changed, 103 insertions, 23 deletions
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9c24dec..512fc30 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -27,7 +27,7 @@ stages: - yum install -y libmnl-devel - yum install -y libnfnetlink-devel - ./ci/travis.sh - - cd build + - cd build tags: - share @@ -47,6 +47,7 @@ branch_build_debug_for_centos7: extends: .build_by_travis_for_centos7 variables: BUILD_TYPE: Debug + UNIT_TEST: 1 except: - /^develop.*$/i - /^master.*$/i @@ -56,6 +57,7 @@ branch_build_release_for_centos7: stage: build variables: BUILD_TYPE: RelWithDebInfo + UNIT_TEST: 1 extends: .build_by_travis_for_centos7 except: - /^develop.*$/i @@ -67,6 +69,7 @@ develop_build_debug_for_centos7: extends: .build_by_travis_for_centos7 variables: BUILD_TYPE: Debug + UNIT_TEST: 1 PACKAGE: 1 UPLOAD_RPM: 1 ASAN_OPTION: ADDRESS @@ -86,6 +89,7 @@ develop_build_release_for_centos7: extends: .build_by_travis_for_centos7 variables: BUILD_TYPE: RelWithDebInfo + UNIT_TEST: 1 PACKAGE: 1 UPLOAD_RPM: 1 TESTING_VERSION_BUILD: 1 @@ -103,6 +107,7 @@ release_build_debug_for_centos7: stage: build variables: BUILD_TYPE: Debug + UNIT_TEST: 1 PACKAGE: 1 UPLOAD_RPM: 1 PULP3_REPO_NAME: framework-stable-x86_64.el7 @@ -119,6 +124,7 @@ release_build_release_for_centos7: stage: build variables: BUILD_TYPE: RelWithDebInfo + UNIT_TEST: 1 PACKAGE: 1 UPLOAD_RPM: 1 UPLOAD_SYMBOL_FILES: 1 @@ -138,6 +144,7 @@ branch_build_debug_for_centos8: extends: .build_by_travis_for_centos8 variables: BUILD_TYPE: Debug + UNIT_TEST: 1 except: - /^develop.*$/i - /^master.*$/i @@ -147,6 +154,7 @@ branch_build_release_for_centos8: stage: build variables: BUILD_TYPE: RelWithDebInfo + UNIT_TEST: 1 extends: .build_by_travis_for_centos8 except: - /^develop.*$/i @@ -158,6 +166,7 @@ develop_build_debug_for_centos8: extends: .build_by_travis_for_centos8 variables: BUILD_TYPE: Debug + UNIT_TEST: 1 PACKAGE: 1 UPLOAD_RPM: 1 ASAN_OPTION: ADDRESS @@ -177,6 +186,7 @@ develop_build_release_for_centos8: extends: .build_by_travis_for_centos8 variables: BUILD_TYPE: RelWithDebInfo + UNIT_TEST: 1 PACKAGE: 1 UPLOAD_RPM: 1 TESTING_VERSION_BUILD: 1 @@ -194,6 +204,7 @@ release_build_debug_for_centos8: stage: build variables: BUILD_TYPE: Debug + UNIT_TEST: 1 PACKAGE: 1 UPLOAD_RPM: 1 PULP3_REPO_NAME: framework-stable-x86_64.el8 @@ -210,6 +221,7 @@ release_build_release_for_centos8: stage: build variables: BUILD_TYPE: RelWithDebInfo + UNIT_TEST: 1 PACKAGE: 1 UPLOAD_RPM: 1 UPLOAD_SYMBOL_FILES: 1 diff --git a/CMakeLists.txt b/CMakeLists.txt index 78df60d..8caa3cf 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,37 @@ if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) set(CMAKE_INSTALL_PREFIX "/opt/MESA" CACHE PATH "default install path" FORCE) endif() +find_program(CMAKE_CXX_CPPCHECK NAMES cppcheck) +if (CMAKE_CXX_CPPCHECK) + list( + APPEND CMAKE_CXX_CPPCHECK + "--enable=all" + "--error-exitcode=1" + "--suppress=unreachableCode" + "--suppress=unusedFunction" + "--suppress=missingInclude" + "--suppress=uselessAssignmentPtrArg" + "--suppress=unmatchedSuppression" + "--suppress=internalAstError" + "--suppress=unmatchedSuppression" + "--suppress=unreadVariable" + "--suppress=memleakOnRealloc" + "--suppress=redundantAssignment" + "--suppress=constParameter" + "--suppress=unsignedLessThanZero" + "--suppress=variableScope" + "--suppress=cstyleCast" + "--suppress=objectIndex" + "--suppress=shadowVariable" + "--suppress=nullPointerRedundantCheck" + "--suppress=ctunullpointer" + "--suppress=shadowFunction" + ) + set(CMAKE_C_CPPCHECK ${CMAKE_CXX_CPPCHECK}) +else() + message(FATAL_ERROR "Could not find the program cppcheck.") +endif() + #for ASAN set(ASAN_OPTION "OFF" CACHE STRING " set asan type chosen by the user, using OFF as default") set_property(CACHE ASAN_OPTION PROPERTY STRINGS OFF ADDRESS THREAD) diff --git a/ci/travis.sh b/ci/travis.sh index 3cfc243..c543ff4 100644 --- a/ci/travis.sh +++ b/ci/travis.sh @@ -54,11 +54,18 @@ cmake3 -DCMAKE_CXX_FLAGS=$CXX_FLAGS \ make + +if [ -n "${UNIT_TEST}" ]; then + yum install -y https://dl.influxdata.com/telegraf/releases/telegraf-1.25.1-1.x86_64.rpm + /usr/bin/telegraf -config ../test/bin/telegraf_unit_test.conf --quiet & + make test + ctest --verbose +fi + if [ -n "${PACKAGE}" ]; then make package fi - if [ -n "${UPLOAD_RPM}" ]; then cp ~/rpm_upload_tools.py ./ python3 rpm_upload_tools.py ${PULP3_REPO_NAME} ${PULP3_DIST_NAME} *.rpm diff --git a/src/fieldstat.cpp b/src/fieldstat.cpp index 3a8747c..cdb7de9 100644 --- a/src/fieldstat.cpp +++ b/src/fieldstat.cpp @@ -293,7 +293,7 @@ int send_udp(int sd, unsigned int dest_ip, unsigned short dest_port, const char return -1; } } - already_sended_len=+this_sended_len; + already_sended_len += this_sended_len; } return 0; diff --git a/src/file_output.cpp b/src/file_output.cpp index 40d28a1..7289f28 100644 --- a/src/file_output.cpp +++ b/src/file_output.cpp @@ -512,8 +512,10 @@ int fieldstat_output_file(struct fieldstat_instance *instance,long long interval } fseek(instance->local_output_fp, 0, SEEK_SET); - fwrite(print_buf,append_pos - print_buf,1,instance->local_output_fp); - + if(print_buf) + { + fwrite(print_buf, append_pos - print_buf, 1,instance->local_output_fp); + } fflush(instance->local_output_fp); if(print_buf) diff --git a/src/line_protocol_output.cpp b/src/line_protocol_output.cpp index f1b2c16..9a9bfa9 100644 --- a/src/line_protocol_output.cpp +++ b/src/line_protocol_output.cpp @@ -119,7 +119,7 @@ static void output_line_protocol_table(struct fieldstat_instance *instance,int t line_value_is_not_zero = 1; } field_pos += snprintf(field_pos, - sizeof(field_set_buff) - (field_set_buff - field_set_buff), + sizeof(field_set_buff) - (field_pos - field_set_buff), "%s=%lld,", metric->table->column_name[metric->table_column_id], value diff --git a/src/prometheus_output.cpp b/src/prometheus_output.cpp index 8b31ae3..96016c1 100644 --- a/src/prometheus_output.cpp +++ b/src/prometheus_output.cpp @@ -188,12 +188,12 @@ static int prometheus_output_histogram_and_summary(struct metric *metric, char * if(metric->field_type == FILED_TYPE_HISTOGRAM) { - output_format = (char *)"%s_bucket{%s,le=\"%0.2f\"} %llu\n"; + output_format = (char *)"%s_bucket{%s,le=\"%0.2f\"} %lld\n"; } if(metric->field_type == FIELD_TYPE_SUMMARY) { - output_format = (char *)"%s{%s,quantile=\"%0.2f\"} %llu\n"; + output_format = (char *)"%s{%s,quantile=\"%0.2f\"} %lld\n"; } hdr_init(h->lowest_trackable_value, h->highest_trackable_value, h->significant_figures, &(h_tmp)); @@ -224,7 +224,7 @@ static int prometheus_output_histogram_and_summary(struct metric *metric, char * } used_len+=snprintf(payload+used_len, payload_len-used_len, - "%s_count{%s} %llu\n", + "%s_count{%s} %lld\n", metric_name, metric_tags, (long long)h_out->total_count @@ -237,7 +237,7 @@ static int prometheus_output_histogram_and_summary(struct metric *metric, char * } used_len+=snprintf(payload+used_len, payload_len-used_len, - "%s_sum{%s} %llu\n", + "%s_sum{%s} %lld\n", metric_name, metric_tags, sum @@ -329,7 +329,7 @@ static int prometheus_get_instance_metric_playload(struct fieldstat_instance *in value = get_metric_unit_val(metric, FS_CALC_CURRENT, 1); append_offset += snprintf(new_payload + append_offset, new_payload_size - append_offset, - "%s{%s} %llu\n", + "%s{%s} %lld\n", metric_name, metric_tags, value diff --git a/test/bin/telegraf_unit_test.conf b/test/bin/telegraf_unit_test.conf new file mode 100644 index 0000000..d239263 --- /dev/null +++ b/test/bin/telegraf_unit_test.conf @@ -0,0 +1,25 @@ +[agent] + interval = "5s" + round_interval = true + metric_batch_size = 1000 + metric_buffer_limit = 10000 + collection_jitter = "0s" + flush_interval = "1s" + flush_jitter = "0s" + precision = "" + debug = false + quiet = false + logfile = "" + hostname = "" + omit_hostname = false + + +[[outputs.file]] + files = ["/tmp/metrics.out"] + rotation_max_size = "0MB" + data_format = "json" + json_timestamp_units = "1s" + +[[inputs.socket_listener]] + service_address = "udp://:8600" + data_format = "influx" diff --git a/test/src/gtest_dynamic_fieldstat.cpp b/test/src/gtest_dynamic_fieldstat.cpp index 7c3aa09..994df26 100644 --- a/test/src/gtest_dynamic_fieldstat.cpp +++ b/test/src/gtest_dynamic_fieldstat.cpp @@ -614,14 +614,15 @@ void parse_telegraf_cjson_output(const char *compare) cJSON_DeleteItemFromObject(cjson_tags, "host"); cjson_metric_str = cJSON_PrintUnformatted(cjson_metric); - ret = strcmp(cjson_metric_str, compare); - EXPECT_EQ(0, ret); - cJSON_Delete(cjson_metric); + EXPECT_NE(nullptr, cjson_metric_str); if(cjson_metric_str) { + ret = strcmp(cjson_metric_str, compare); + EXPECT_EQ(0, ret); free(cjson_metric_str); cjson_metric_str = NULL; } + cJSON_Delete(cjson_metric); return; } @@ -1054,14 +1055,16 @@ void parse_telegraf_cjson_output_not_equal(const char *compare) EXPECT_NE(nullptr, cjson_tags); cJSON_DeleteItemFromObject(cjson_tags, "host"); cjson_metric_str = cJSON_PrintUnformatted(cjson_metric); - ret = strcmp(cjson_metric_str, compare); - EXPECT_NE(0, ret); - cJSON_Delete(cjson_metric); + EXPECT_NE(nullptr, cjson_metric_str); + if(cjson_metric_str) { + ret = strcmp(cjson_metric_str, compare); + EXPECT_NE(0, ret); free(cjson_metric_str); cjson_metric_str = NULL; } + cJSON_Delete(cjson_metric); return; } @@ -1187,13 +1190,14 @@ TEST(FeildStatDynamicAPI, FieldStatDynamicOutputLineProtocolMultiMetric) memset(compare, 0, sizeof(compare)); snprintf(compare, sizeof(compare), "{\"fields\":{\"Active_sessions_%d\":12},\"name\":\"Active_sessions_%d\",\"tags\":{\"app_name\":\"firewall\"}}", n_line, n_line); cjson_metric_str = cJSON_PrintUnformatted(cjson_metric); - EXPECT_STREQ(compare, cjson_metric_str); - cJSON_Delete(cjson_metric); + EXPECT_NE(nullptr, cjson_metric_str); if(cjson_metric_str) { + EXPECT_STREQ(compare, cjson_metric_str); free(cjson_metric_str); cjson_metric_str = NULL; } + cJSON_Delete(cjson_metric); n_line++; } fclose(fp); @@ -1263,13 +1267,14 @@ TEST(FeildStatDynamicAPI, FieldStatDynamicOutputLineProtocolMultiTableRow) snprintf(compare, sizeof(compare), "{\"fields\":{\"bytes\":30,\"packages\":30},\"name\":\"security_rule_hits_%d\",\"tags\":{\"app_name\":\"firewall\",\"table_name\":\"shaping\"}}", n_line); cjson_metric_str = cJSON_PrintUnformatted(cjson_metric); - EXPECT_STREQ(compare, cjson_metric_str); - cJSON_Delete(cjson_metric); + EXPECT_NE(nullptr, cjson_metric_str); if(cjson_metric_str) { + EXPECT_STREQ(compare, cjson_metric_str); free(cjson_metric_str); cjson_metric_str = NULL; } + cJSON_Delete(cjson_metric); n_line++; } fclose(fp); @@ -2574,5 +2579,4 @@ int main(int argc, char *argv[]) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); - return 0; }
\ No newline at end of file diff --git a/test/src/gtest_fieldstat.cpp b/test/src/gtest_fieldstat.cpp index 2434539..cf99443 100644 --- a/test/src/gtest_fieldstat.cpp +++ b/test/src/gtest_fieldstat.cpp @@ -226,5 +226,4 @@ int main(int argc, char *argv[]) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); - return 0; }
\ No newline at end of file |
