Skip to content

Commit d09cae6

Browse files
committed
add the ability to write/run platform-specific tests, as well as platform-agnostic tests
1 parent 18375c5 commit d09cae6

File tree

7 files changed

+265
-188
lines changed

7 files changed

+265
-188
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tempodb_tests
2-
tempodb_tests.txt
1+
*tempodb_tests
2+
*tempodb_tests.txt
33
lib/*.a
44
lib/test/*.a
55
objs/*

Makefile

+13-11
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
SILENCE = @
55

66
ifdef PLATFORM
7-
COMPONENT_NAME = "$(PLATFORM)_"
7+
COMPONENT_NAME = $(PLATFORM)_tempodb
88
SRC_FILES = src/tempodb/platform/$(PLATFORM).c
99
TEST_SRC_FILES = tests/TempoDb/platform/$(PLATFORM)_test.c
1010
MOCKS_SRC_DIRS = mocks/$(PLATFORM)
11+
else
12+
COMPONENT_NAME = tempodb
13+
TEST_SRC_FILES = tests/TempoDb/tempodb_test.c
14+
MOCKS_SRC_DIRS = mocks
1115
endif
1216

13-
#---- Outputs ----#
14-
COMPONENT_NAME = tempodb
15-
1617
#--- Inputs ----#
1718
CPPUTEST_HOME = CppUTest
1819
CPP_PLATFORM = Gcc
@@ -23,8 +24,6 @@ SRC_DIRS += \
2324

2425
TEST_SRC_DIRS += \
2526
.\
26-
mocks\
27-
tests/TempoDb\
2827
tests
2928

3029
INCLUDE_DIRS =\
@@ -33,15 +32,12 @@ INCLUDE_DIRS =\
3332
mocks\
3433
include/tempodb
3534

36-
MOCKS_SRC_DIRS += \
37-
mocks
38-
3935
CPPUTEST_WARNINGFLAGS = -Wall -Wswitch-default -Werror
4036
#CPPUTEST_CFLAGS = -std=c89
4137
CPPUTEST_CFLAGS += -Wall -Wstrict-prototypes -pedantic
4238
LD_LIBRARIES = -lpthread
4339

44-
STUFF_TO_CLEAN += objs/*.o objs/platform/*.o objs/src/tempodb/* objs/tests/* objs/mocks/* objs/tests/tempodb/* lib/libtempodb*.a
40+
STUFF_TO_CLEAN += objs/*.o objs/platform/*.o objs/src/tempodb/* objs/tests/* objs/mocks/* objs/tests/tempodb/* lib/libtempodb*.a *tempodb_tests
4541

4642
CC = cc
4743

@@ -69,6 +65,12 @@ vtest_posix:
6965
$(SILENCE)PLATFORM="posix" make vtest_platform
7066
$(SILENCE)echo "<<< Finished Posix Tests"
7167

72-
vtest: vtest_posix
68+
vtest_common:
69+
$(SILENCE)echo ">>> Running TempoDB Tests"
70+
$(SILENCE)make vtest_platform
71+
$(SILENCE)echo "<<< Finished TempoDB Tests"
72+
73+
74+
vtest: vtest_common vtest_posix
7375

7476
include $(CPPUTEST_HOME)/build/MakefileWorker.mk

mocks/platform_mock.c

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "platform_mock.h"
2+
3+
static size_t response_buffer_size = 1024;
4+
char *response_buffer;
5+
6+
void test_init(void) {
7+
last_request = malloc(1);
8+
response_buffer = malloc(response_buffer_size + 1);
9+
memset(response_buffer, 0, response_buffer_size + 1);
10+
set_test_response("200 OK");
11+
}
12+
13+
void test_cleanup(void) {
14+
free(last_request);
15+
free(response_buffer);
16+
}
17+
18+
tempodb_platform_config * tempodb_platform_create(void) {
19+
return malloc(1);
20+
}
21+
22+
int tempodb_platform_destroy(tempodb_platform_config *config) {
23+
free(config);
24+
return 0;
25+
}
26+
27+
int tempodb_platform_send(tempodb_platform_config *config, const char *command) {
28+
free(last_request);
29+
last_request = malloc(strlen(command) + 1);
30+
strncpy(last_request, command, strlen(command));
31+
return 0;
32+
}
33+
34+
int tempodb_platform_read_response(tempodb_platform_config *config, char *buffer, const int buffer_size) {
35+
int response_size = response_buffer_size;
36+
if (buffer_size < response_buffer_size) {
37+
response_size = buffer_size;
38+
}
39+
strncpy(buffer, response_buffer, response_size);
40+
return 0;
41+
}
42+
43+
void set_test_response(const char *buffer) {
44+
strncpy(response_buffer, buffer, response_buffer_size);
45+
}

mocks/platform_mock.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef TEMPODB_PLATFORM_MOCK_H
2+
#define TEMPODB_PLATFORM_MOCK_H
3+
4+
#include <string.h>
5+
#include "tempodb.h"
6+
7+
char *last_request;
8+
9+
void test_init(void);
10+
void test_cleanup(void);
11+
12+
void set_test_response(const char *buffer);
13+
14+
#endif

mocks/posix/posix_mock.c

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ static size_t response_buffer_bytes_sent;
1010
char *response_buffer;
1111
char *response_buffer_remaining;
1212

13-
1413
static int last_request_size = 1024;
1514

1615
int socket(int domain, int type, int protocol) {

tests/TempoDb/platform/posix_test.cpp

-174
Original file line numberDiff line numberDiff line change
@@ -18,99 +18,6 @@ TEST_GROUP(tempodb) {
1818
}
1919
};
2020

21-
TEST(tempodb, BulkIncrement_Request)
22-
{
23-
tempodb_config *config = tempodb_create("my_access_key", "my_secret_key");
24-
char *response_buffer = (char *)malloc(255);
25-
26-
tempodb_bulk_update *updates = (tempodb_bulk_update *)malloc(sizeof(tempodb_bulk_update) * 2);
27-
tempodb_bulk_update update_by_id = { "series_1_id", TEMPODB_ID, 1.1};
28-
updates[0] = update_by_id;
29-
tempodb_bulk_update update_by_key = { "series_2_key", TEMPODB_KEY, 2};
30-
updates[1] = update_by_key;
31-
32-
tempodb_bulk_increment(config, updates, 2, response_buffer, 255);
33-
STRCMP_CONTAINS("POST /v1/increment", last_request);
34-
STRCMP_CONTAINS("{\"data\":[{\"id\":\"series_1_id\",\"v\":1.100000},{\"key\":\"series_2_key\",\"v\":2.000000},]}", last_request);
35-
free(response_buffer);
36-
free(updates);
37-
tempodb_destroy(config);
38-
}
39-
40-
TEST(tempodb, BulkWrite_Request)
41-
{
42-
tempodb_config *config = tempodb_create("my_access_key", "my_secret_key");
43-
char *response_buffer = (char *)malloc(255);
44-
45-
tempodb_bulk_update *updates = (tempodb_bulk_update *)malloc(sizeof(tempodb_bulk_update) * 2);
46-
tempodb_bulk_update update_by_id = { "series_1_id", TEMPODB_ID, 1.1};
47-
updates[0] = update_by_id;
48-
tempodb_bulk_update update_by_key = { "series_2_key", TEMPODB_KEY, 2};
49-
updates[1] = update_by_key;
50-
51-
tempodb_bulk_write(config, updates, 2, response_buffer, 255);
52-
STRCMP_CONTAINS("POST /v1/data", last_request);
53-
STRCMP_CONTAINS("{\"data\":[{\"id\":\"series_1_id\",\"v\":1.100000},{\"key\":\"series_2_key\",\"v\":2.000000},]}", last_request);
54-
free(response_buffer);
55-
free(updates);
56-
tempodb_destroy(config);
57-
}
58-
59-
TEST(tempodb, IncrementByKey_Request)
60-
{
61-
tempodb_config *config = tempodb_create("my_access_key", "my_secret_key");
62-
char *response_buffer = (char *)malloc(255);
63-
tempodb_increment_by_key(config, "series_key", 10, response_buffer, 255);
64-
STRCMP_CONTAINS("POST /v1/series/key/series_key/increment", last_request);
65-
STRCMP_CONTAINS("[{\"v\":10.000000}]", last_request);
66-
free(response_buffer);
67-
tempodb_destroy(config);
68-
}
69-
70-
TEST(tempodb, IncrementById_Request)
71-
{
72-
tempodb_config *config = tempodb_create("my_access_key", "my_secret_key");
73-
char *response_buffer = (char *)malloc(255);
74-
tempodb_increment_by_id(config, "series_id", 10, response_buffer, 255);
75-
STRCMP_CONTAINS("POST /v1/series/id/series_id/increment", last_request);
76-
STRCMP_CONTAINS("[{\"v\":10.000000}]", last_request);
77-
free(response_buffer);
78-
tempodb_destroy(config);
79-
}
80-
81-
TEST(tempodb, WriteByKey_Request)
82-
{
83-
tempodb_config *config = tempodb_create("my_access_key", "my_secret_key");
84-
char *response_buffer = (char *)malloc(255);
85-
tempodb_write_by_key(config, "series_key", 10, response_buffer, 255);
86-
STRCMP_CONTAINS("POST /v1/series/key/series_key/data", last_request);
87-
STRCMP_CONTAINS("[{\"v\":10.000000}]", last_request);
88-
free(response_buffer);
89-
tempodb_destroy(config);
90-
}
91-
92-
TEST(tempodb, WriteById_Request)
93-
{
94-
tempodb_config *config = tempodb_create("my_access_key", "my_secret_key");
95-
char *response_buffer = (char *)malloc(255);
96-
tempodb_write_by_id(config, "series_id", 10, response_buffer, 255);
97-
STRCMP_CONTAINS("POST /v1/series/id/series_id/data", last_request);
98-
STRCMP_CONTAINS("[{\"v\":10.000000}]", last_request);
99-
free(response_buffer);
100-
tempodb_destroy(config);
101-
}
102-
103-
TEST(tempodb, Write_Response)
104-
{
105-
tempodb_config *config = tempodb_create("my_access_key", "my_secret_key");
106-
char *response_buffer = (char *)malloc(255);
107-
set_test_response("200 OK");
108-
tempodb_write_by_id(config, "series_id", 10, response_buffer, 255);
109-
STRCMP_CONTAINS("200 OK", response_buffer);
110-
free(response_buffer);
111-
tempodb_destroy(config);
112-
}
113-
11421
TEST(tempodb, Write_Response_Overrun)
11522
{
11623
tempodb_config *config = tempodb_create("my_access_key", "my_secret_key");
@@ -122,84 +29,3 @@ TEST(tempodb, Write_Response_Overrun)
12229
free(response_buffer);
12330
tempodb_destroy(config);
12431
}
125-
126-
TEST(tempodb, BuildQuery_IncludesHTTPVerbPathAndVersion)
127-
{
128-
tempodb_config *config = tempodb_create("my_access_key", "my_secret_key");
129-
char *buffer = (char *)malloc(255);
130-
tempodb_build_query(config, buffer, 255, "GET", "/a/path", "");
131-
STRCMP_CONTAINS("GET /a/path HTTP/1.0", buffer);
132-
free(buffer);
133-
tempodb_destroy(config);
134-
}
135-
136-
TEST(tempodb, BuildQuery_DoesNotOverrun)
137-
{
138-
tempodb_config *config = tempodb_create("my_access_key", "my_secret_key");
139-
char *buffer = (char *)malloc(255);
140-
memset(buffer, 1, 255);
141-
tempodb_build_query(config, buffer, 10, "GET", "/a/long/path", "");
142-
STRCMP_EQUAL("GET /a/lo", buffer);
143-
free(buffer);
144-
tempodb_destroy(config);
145-
}
146-
147-
TEST(tempodb, BuildQuery_IncludesUserAgent)
148-
{
149-
tempodb_config *config = tempodb_create("my_access_key", "my_secret_key");
150-
char *buffer = (char *)malloc(255);
151-
tempodb_build_query(config, buffer, 255, "GET", "/a/path", "");
152-
STRCMP_CONTAINS("\r\nUser-Agent: tempodb-embedded-c/1.0.0\r\n", buffer);
153-
free(buffer);
154-
tempodb_destroy(config);
155-
}
156-
157-
TEST(tempodb, BuildQuery_IncludesHost)
158-
{
159-
tempodb_config *config = tempodb_create("my_access_key", "my_secret_key");
160-
char *buffer = (char *)malloc(255);
161-
tempodb_build_query(config, buffer, 255, "GET", "/a/path", "");
162-
STRCMP_CONTAINS("\r\nHost: api.tempo-db.com\r\n", buffer);
163-
free(buffer);
164-
tempodb_destroy(config);
165-
}
166-
167-
TEST(tempodb, BuildQuery_IncludesPayload)
168-
{
169-
tempodb_config *config = tempodb_create("my_access_key", "my_secret_key");
170-
char *buffer = (char *)malloc(255);
171-
tempodb_build_query(config, buffer, 255, "GET", "/a/path", "[{\"a\":1,\"b\":2}]");
172-
STRCMP_CONTAINS("\r\n\r\n[{\"a\":1,\"b\":2}]", buffer);
173-
free(buffer);
174-
tempodb_destroy(config);
175-
}
176-
177-
TEST(tempodb, BuildQuery_IncludesCredentials)
178-
{
179-
tempodb_config *config = tempodb_create("my_access_key", "my_secret_key");
180-
char *buffer = (char *)malloc(255);
181-
tempodb_build_query(config, buffer, 255, "GET", "/a/path", "");
182-
STRCMP_CONTAINS("\r\nAuthorization: Basic bXlfYWNjZXNzX2tleTpteV9zZWNyZXRfa2V5\r\n", buffer);
183-
free(buffer);
184-
tempodb_destroy(config);
185-
}
186-
187-
TEST(tempodb, BuildQuery_IncludesContentType)
188-
{
189-
tempodb_config *config = tempodb_create("my_access_key", "my_secret_key");
190-
char *buffer = (char *)malloc(255);
191-
tempodb_build_query(config, buffer, 255, "GET", "/a/path", "");
192-
STRCMP_CONTAINS("\r\nContent-Type: application/json\r\n", buffer);
193-
free(buffer);
194-
tempodb_destroy(config);
195-
}
196-
197-
TEST(tempodb, BuildQuery_IncludesContentLength)
198-
{
199-
tempodb_config *config = tempodb_create("my_access_key", "my_secret_key");
200-
char *buffer = (char *)malloc(255);
201-
tempodb_build_query(config, buffer, 255, "POST", "/a/path", "123456");
202-
STRCMP_CONTAINS("\r\nContent-Length: 6\r\n", buffer);
203-
free(buffer);
204-
tempodb_destroy(config);
205-
}

0 commit comments

Comments
 (0)