diff options
author | Heikki Kallasjoki <fis@zem.fi> | 2018-12-12 18:51:03 +0000 |
---|---|---|
committer | Heikki Kallasjoki <fis+github@zem.fi> | 2018-12-12 19:10:51 +0000 |
commit | e6bac22eb94b25f3918d15d10029cfe8ea70eb23 (patch) | |
tree | 6d0ca06935588a5c09765b4b0bd1a69ebda82396 /test | |
parent | 2f1cce70ee79d0f23a1b2f36cd530a7a9fd8c457 (diff) | |
download | nano-exporter-e6bac22eb94b25f3918d15d10029cfe8ea70eb23.tar.gz nano-exporter-e6bac22eb94b25f3918d15d10029cfe8ea70eb23.tar.xz nano-exporter-e6bac22eb94b25f3918d15d10029cfe8ea70eb23.zip |
Add basic test for the textfile collector.
Diffstat (limited to 'test')
-rw-r--r-- | test/Makefile | 2 | ||||
-rw-r--r-- | test/mock_scrape.c | 37 | ||||
-rw-r--r-- | test/textfile_test.c | 58 |
3 files changed, 91 insertions, 6 deletions
diff --git a/test/Makefile b/test/Makefile index df38c75..2e18d39 100644 --- a/test/Makefile +++ b/test/Makefile @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -COLLECTOR_TESTS := cpu diskstats filesystem hwmon meminfo network stat +COLLECTOR_TESTS := cpu diskstats filesystem hwmon meminfo network stat textfile COLLECTOR_TEST_PROGS := $(foreach c,$(COLLECTOR_TESTS),$(c)_test) COLLECTOR_TEST_OBJS := $(foreach p,$(COLLECTOR_TEST_PROGS),$(p).o) diff --git a/test/mock_scrape.c b/test/mock_scrape.c index 01a681e..9f2ec1c 100644 --- a/test/mock_scrape.c +++ b/test/mock_scrape.c @@ -38,7 +38,7 @@ struct scrape_req { unsigned metrics_written; unsigned metrics_tested; - const char *raws[MAX_RAWS]; + char *raws[MAX_RAWS]; unsigned raws_written; unsigned raws_tested; }; @@ -64,7 +64,10 @@ void scrape_write_raw(scrape_req *req, const void *buf, size_t len) { if (req->metrics_written >= MAX_RAWS) test_fail(req->env, "exceeded MAX_RAWS: %u raw blocks already written", req->raws_written); - test_fail(req->env, "TODO: implement scrape_write_raw"); + char *raw = must_malloc(len + 1); + memcpy(raw, buf, len); + raw[len] = '\0'; + req->raws[req->raws_written++] = raw; } scrape_req *mock_scrape_start(test_env *env) { @@ -80,6 +83,8 @@ void mock_scrape_free(scrape_req *req) { free(req->metrics[i].metric); free_labels(req->metrics[i].labels); } + for (unsigned i = 0; i < req->raws_written; i++) + free(req->raws[i]); free(req); } @@ -141,7 +146,18 @@ void mock_scrape_expect(scrape_req *req, const char *metric, const struct label } void mock_scrape_expect_raw(scrape_req *req, const char *str) { - test_fail(req->env, "TODO: implement"); + if (req->raws_tested >= req->raws_written) { + dump_metrics(req); + test_fail(req->env, "got no more raw blocks, expected [[[%s]]]", str); + } + char *got = req->raws[req->raws_tested]; + + if (strcmp(got, str) != 0) { + dump_metrics(req); + test_fail(req->env, "got raw block [[[%s]]], expected [[[%s]]]", got, str); + } + + req->raws_tested++; } void mock_scrape_expect_no_more(scrape_req *req) { @@ -150,7 +166,8 @@ void mock_scrape_expect_no_more(scrape_req *req) { test_fail(req->env, "got metric %s, expected no more metrics", req->metrics[req->metrics_tested].metric); } if (req->raws_tested < req->raws_written) { - test_fail(req->env, "TODO: raw stuff"); + dump_metrics(req); + test_fail(req->env, "got raw block [[[%s]]], expected no more raw blocks", req->raws[req->raws_tested]); } } @@ -171,7 +188,17 @@ static void dump_metrics(scrape_req *req) { } } - // TODO: dump raw + if (req->raws_tested > 0) { + printf("expected raw blocks:\n"); + for (unsigned i = 0; i < req->raws_tested; i++) + printf("[[[%s]]]\n", req->raws[i]); + } + + if (req->raws_written > req->raws_tested) { + printf("unexpected raw blocks:\n"); + for (unsigned i = req->raws_tested; i < req->raws_written; i++) + printf("[[[%s]]]\n", req->raws[i]); + } } static void dump_metric(const char *metric, const struct label *labels, double value) { diff --git a/test/textfile_test.c b/test/textfile_test.c new file mode 100644 index 0000000..b2ee2ca --- /dev/null +++ b/test/textfile_test.c @@ -0,0 +1,58 @@ +/* + * Copyright 2018 Google LLC + * + * Licensed 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 + * + * https://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. + */ + +#include "harness.h" +#include "mock_scrape.h" +#include "../collector.h" + +extern const struct collector textfile_collector; + +TEST(textfile_metrics) { + test_write_file( + env, + "textfile/metrics.prom", + "test_metric{label=\"value\"} 1234\n"); + scrape_req *req = mock_scrape_start(env); + + void *ctx = textfile_collector.init(1, (char *[]){ "dir=textfile", 0 }); + textfile_collector.collect(req, ctx); + + mock_scrape_expect_raw(req, "test_metric{label=\"value\"} 1234\n"); + mock_scrape_expect_no_more(req); + mock_scrape_free(req); +} + +TEST(appends_missing_newline) { + test_write_file( + env, + "textfile/metrics.prom", + "metric{newline=\"yes\"} 1234\n" + "metric{newline=\"no\"} 4321"); + scrape_req *req = mock_scrape_start(env); + + void *ctx = textfile_collector.init(1, (char *[]){ "dir=textfile", 0 }); + textfile_collector.collect(req, ctx); + + mock_scrape_expect_raw(req, "metric{newline=\"yes\"} 1234\nmetric{newline=\"no\"} 4321\n"); + mock_scrape_expect_no_more(req); + mock_scrape_free(req); +} + +TEST_SUITE { + TEST_SUITE_START; + RUN_TEST(textfile_metrics); + TEST_SUITE_END; +} |