Skip to content

Commit

Permalink
Merge pull request #3 from lindig/CP-18272
Browse files Browse the repository at this point in the history
CP-18272 - add integration tests
  • Loading branch information
euanh authored Aug 9, 2016
2 parents 35d116b + 85747c7 commit 37f1566
Show file tree
Hide file tree
Showing 23 changed files with 3,030 additions and 115 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "parson"]
path = parson
url = https://github.com/kgabis/parson.git
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ matrix:
os: linux

script:
- make parson
- make
- make test

31 changes: 16 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# vim: set ts=8 sw=8 noet:
#
# To run the integration tests you need the ocaml-rrd-transport-devel
# package:
#
# yum install -y ocaml-rrd-transport-devel
#

CC = gcc
Expand Down Expand Up @@ -28,9 +32,18 @@ clean:
test: rrdtest rrdclient
./rrdtest
seq 1 10 | ./rrdclient rrdclient.rrd
seq 1 10 \
| while read i; do echo $$i; sleep 1; done \
| ./rrdclient rrdclient.rrd

.PHONY: test-integration
test-integration:
seq 1 10 | while read i; do \
echo $$i | ./rrdclient rrdclient.rrd ;\
rrdreader file --once rrdclient.rrd v2 ;\
done
seq 1 20 | while read i; do \
echo $$i | ./rrdclient rrdclient.rrd ;\
sleep 4 ;\
done & rrdreader file rrdclient.rrd v2 \
|| echo "a final exception Rrd_protocol.No_update is OK"

.PHONY: valgrind
valgrind: rrdtest
Expand All @@ -45,12 +58,6 @@ indent: librrd.h librrd.c rrdtest.c
depend: librrd.c rrdtest.c
$(CC) -MM $^

.PHONY: parson
parson:
# git submodule add https://github.com/kgabis/parson.git
git submodule init
git submodule update

%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<

Expand Down Expand Up @@ -91,9 +98,3 @@ parson/parson.o: parson/parson.h
rrdtest.o: parson/parson.h librrd.h
librrd.o: parson/parson.h librrd.h

# OCaml test utility
# You need: yum install -y ocaml-rrd-transport-devel

rrdreader:
cd ocaml; $(OCB) -pkg rrd-transport -tag thread rrdreader.native

17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

[![Build Status](https://travis-ci.org/lindig/rrd-client-lib-1.svg?branch=master)](https://travis-ci.org/lindig/rrd-client-lib-1)
[![Build Status](https://travis-ci.org/xapi-project/rrd-client-lib.svg?branch=master)](https://travis-ci.org/xapi-project/rrd-client-lib)

# rrd-client-lib - A Library to Provide RRD Data

Expand All @@ -19,7 +19,6 @@ The `Makefile` builds the library and a simple test. The implementation
relies on a small JSON library that is included as a Git submodule. This
needs to be initialised:

make parson
make

make test
Expand All @@ -28,9 +27,7 @@ needs to be initialised:
## Parson

The JSON library [Parson](https://github.com/kgabis/parson.git) is
included as a Git submodule. A submodule points to a specific commit in
an external repository and does not track its master branch as this
advances. Instead, it needs to be updated explicitly.
included as a copy of the source code.

## Documentation - Overview

Expand All @@ -45,7 +42,7 @@ library:
int rrd_close(RRD_PLUGIN * plugin);
int rrd_add_src(RRD_PLUGIN * plugin, RRD_SOURCE * source);
int rrd_del_src(RRD_PLUGIN * plugin, RRD_SOURCE * source);
int rrd_sample(RRD_PLUGIN * plugin);
int rrd_sample(RRD_PLUGIN * plugin, time_t (*t)(time_t*));

A plugin reports streams of data to the RRD service. Each such
stream is represented as an `RRD_SOURCE` value. An `RRD_PLUGIN`
Expand Down Expand Up @@ -86,7 +83,7 @@ All strings are in UTF8 encoding. The library implements the following
policy to manage memory: it does not free any memory that is hasn't
itself allocated. This means, if the client passes dynamically allocated
data into the library, it is the client's responsibility to de-allocate
it.
it.

## Open, Sample, Close

Expand All @@ -105,12 +102,14 @@ considered private to the library.
<<function declarations>>=
RRD_PLUGIN *rrd_open(char *name, rrd_domain_t domain, char *path);
int rrd_close(RRD_PLUGIN * plugin);
int rrd_sample(RRD_PLUGIN * plugin);
int rrd_sample(RRD_PLUGIN * plugin, time_t (*t)(time_t*));


The name of the plugin is descriptive, as whether it reports data
for a single machine (`RRD_LOCAL_DOMAIN`) or multiple
(`RRD_INTER_DOMAIN`).
(`RRD_INTER_DOMAIN`). The second parameter of `rrd_sample` is typically
NULL. If it isn't, it us used to obtain a timestamp instead of using
time(3).

## Data Sources

Expand Down
22 changes: 16 additions & 6 deletions librrd.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <zlib.h>
#include <time.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -144,17 +143,27 @@ json_for_source(RRD_SOURCE * source)
}
json_object_set_string(src, "value_type", value_type);

#define RRD_TRANSPORT_1_0_0
#ifdef RRD_TRANSPORT_1_1_0
#define GAUGE "gauge"
#define ABSOLUTE "absolute"
#define DERIVE "derive"
#else
#define GAUGE "absolute"
#define ABSOLUTE "rate"
#define DERIVE "absolute_to_rate"
#endif

char *scale = NULL;
switch (source->scale) {
case RRD_GAUGE:
scale = "gauge";
scale = GAUGE;
break;
case RRD_ABSOLUTE:
scale = "absolute";
scale = ABSOLUTE;
break;
case RRD_DERIVE:
scale = "derive";
scale = DERIVE;
break;
default:
abort();
Expand Down Expand Up @@ -390,7 +399,7 @@ write_exact(int fd, const void *data, size_t size)
* first.
*/
int
rrd_sample(RRD_PLUGIN * plugin)
rrd_sample(RRD_PLUGIN * plugin, time_t (*t)(time_t*))
{
assert(plugin);
JSON_Value *json;
Expand Down Expand Up @@ -430,7 +439,8 @@ rrd_sample(RRD_PLUGIN * plugin)
/*
* update timestamp, calculate crc
*/
header->rrd_timestamp = htonll((uint64_t) time(NULL));

header->rrd_timestamp = htonll((uint64_t) (t ? t(NULL) : time(NULL)));
uint32_t crc = crc32(0L, Z_NULL, 0);
crc = crc32(crc,
(unsigned char *) &header->rrd_timestamp,
Expand Down
10 changes: 7 additions & 3 deletions librrd.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@


#include <stdint.h>
#include <time.h>
#include "parson/parson.h"

#define RRD_MAX_SOURCES 128
Expand Down Expand Up @@ -124,7 +125,10 @@ int rrd_del_src(RRD_PLUGIN * plugin, RRD_SOURCE * source);

/*
* calling rrd_sample(plugin) triggers that all data sources are sampled
* and the results are reported to the RRD daemon. This function needs to
* be called every 5 seconds by the client,
* and the results are reported to the RRD daemon. This function needs
* to be called every 5 seconds by the client. The second parameter is
* typically NULL. If it isn't, it will be used instead of time(3) to
* obtain the time stamp that is written to the RRD file. This can be
* used to create RRD files that don't depend on the current time.
*/
int rrd_sample(RRD_PLUGIN * plugin);
int rrd_sample(RRD_PLUGIN * plugin, time_t (*t)(time_t*));
73 changes: 0 additions & 73 deletions ocaml/rrdreader.ml

This file was deleted.

1 change: 0 additions & 1 deletion parson
Submodule parson deleted from a1c356
1 change: 1 addition & 0 deletions parson
20 changes: 20 additions & 0 deletions parson-a1c356e/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CC = gcc
CFLAGS = -O0 -g -Wall -Wextra -std=c89 -pedantic-errors

CPPC = g++
CPPFLAGS = -O0 -g -Wall -Wextra

all: test testcpp

.PHONY: test testcpp
test: tests.c parson.c
$(CC) $(CFLAGS) -o $@ tests.c parson.c
./$@

testcpp: tests.c parson.c
$(CPPC) $(CPPFLAGS) -o $@ tests.c parson.c
./$@

clean:
rm -f test *.o

Loading

0 comments on commit 37f1566

Please sign in to comment.