Skip to content

Commit

Permalink
Merge branch 'master' into release-prep-1.5.5
Browse files Browse the repository at this point in the history
  • Loading branch information
vkmc committed Aug 22, 2024
2 parents 31825e5 + 88dbdfb commit 7d40677
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4.1.3
- name: Run clang-format-lint
uses: DoozyX/clang-format-lint-action@v0.11
with:
Expand All @@ -34,6 +34,6 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4.1.3
- name: Check build of Dockerfile is successful
run: docker build -t sg-bridge:check-build -f build/Dockerfile .
20 changes: 20 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Unit tests suite
env:
TEST_IMAGE: quay.io/centos/centos:stream9
PROJECT_ROOT: /src/github.com/infrawatch/sg-bridge
OPSTOOLS_REPO: https://git.centos.org/rpms/centos-release-opstools/raw/c9s-sig-opstools/f/SOURCES/CentOS-OpsTools.repo

on: [push, pull_request]

jobs:
unit-tests:
name: Unit tests
runs-on: ubuntu-20.04
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Run sg-core unit test suite
run: |
docker run --name=testsuite -uroot --network host -e OPSTOOLS_REPO \
--volume ${{ github.workspace }}:$PROJECT_ROOT:z --workdir $PROJECT_ROOT \
$TEST_IMAGE bash $PROJECT_ROOT/ci/run_tests.sh
41 changes: 31 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@

MAJOR?=0
MINOR?=1

VERSION=$(MAJOR).$(MINOR)

BIN := bridge
TEST_EXEC = tests

SRCS = $(wildcard *.c)
# Specify the source files, excluding the test and the main source file
MAIN_SRC = bridge.c
SRCS = $(filter-out tests.c $(MAIN_SRC), $(wildcard *.c))
TEST_SRC = tests.c

OBJDIR := obj
TEST_OBJDIR := test_obj

DEPDIR := $(OBJDIR)/.deps

# object files, auto generated from source files
OBJS := $(patsubst %,$(OBJDIR)/%.o,$(basename $(SRCS)))
TEST_OBJS := $(patsubst %,$(TEST_OBJDIR)/%.o,$(basename $(SRCS)))
TEST_OBJS += $(TEST_OBJDIR)/$(basename $(TEST_SRC)).o

# dependency files, auto generated from source files
DEPS := $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))

# compilers (at least gcc and clang) don't create the subdirectories automatically
$(shell mkdir -p $(dir $(OBJS)) >/dev/null)
$(shell mkdir -p $(dir $(TEST_OBJS)) >/dev/null)
$(shell mkdir -p $(dir $(DEPS)) >/dev/null)

CC=gcc
Expand Down Expand Up @@ -48,7 +56,7 @@ debug: all

.PHONY: clean
clean:
rm -fr $(OBJDIR) $(DEPDIR)
rm -fr $(OBJDIR) $(TEST_OBJDIR) $(DEPDIR)

.PHONY: clean-image
clean-image: version-check
Expand All @@ -61,33 +69,46 @@ image: version-check
@buildah bud -t ${HUB_NAMESPACE}/${BRIDGE_IMAGE_NAME}:latest -f build/Dockerfile .
@echo 'Done.'

$(BIN): $(OBJS)
$(BIN): $(OBJS) $(OBJDIR)/$(basename $(MAIN_SRC)).o
$(CC) -o $@ $^ $(LDFLAGS) $(CFLAGS) $(LDLIBS)

$(OBJDIR)/%.o: %.c
$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d
$(OBJDIR)/%.o: %.c %.h
$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d %.h
$(PRECOMPILE)
$(COMPILE.c) $<
$(POSTCOMPILE)

$(OBJDIR)/%.o : %.c $(DEPDIR)/%.d | $(DEPDIR)
$(OBJDIR)/%.o : %.c $(DEPDIR)/%.d %.h | $(DEPDIR)
$(COMPILE.c) $(OUTPUT_OPTION) $<

.PRECIOUS: $(DEPDIR)/%.d
$(DEPDIR)/%.d: ;

# Build unit test executable
$(TEST_EXEC): $(TEST_OBJS)
$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

# Compile unit test file
$(TEST_OBJDIR)/%.o: %.c
@mkdir -p $(TEST_OBJDIR)
$(CC) $(CFLAGS) -c -o $@ $<

# Run unit tests
test: $(TEST_EXEC)
@$(TEST_EXEC)

#################################
# Utilities
#################################

.PHONY: version-check
version-check:
@echo "+ $@"
ifdef VERSION
ifdef VERSION
@echo "VERSION is ${VERSION}"
else
else
@echo "VERSION is not set!"
@false;
endif
endif

-include $(DEPS)
50 changes: 8 additions & 42 deletions bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <proton/session.h>
#include <proton/transport.h>
#include <pthread.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -136,44 +135,6 @@ static void usage(char *program) {
}
}

static int match_regex(char *regmatch, char *matches[], int n_matches,
const char *to_match) {
/* "M" contains the matches found. */
regmatch_t m[n_matches];
regex_t regex;

if (regcomp(&regex, regmatch, REG_EXTENDED)) {
fprintf(stderr, "Could not compile regex: %s\n", regmatch);

return -1;
}

int nomatch = regexec(&regex, to_match, n_matches, m, 0);
if (nomatch == REG_NOMATCH) {
return 0;
}

int match_count = 0;
for (int i = 0; i < n_matches; i++) {
if (m[i].rm_so == -1) {
continue;
}
match_count++;

int match_len = m[i].rm_eo - m[i].rm_so;

matches[i] = malloc(match_len + 1); // make room for '\0'

int k = 0;
for (int j = m[i].rm_so; j < m[i].rm_eo; j++) {
matches[i][k++] = to_match[j];
}
matches[i][k] = '\0';
}

return match_count;
}

int main(int argc, char **argv) {
app_data_t app = {0};
char cid_buf[100];
Expand Down Expand Up @@ -275,16 +236,21 @@ int main(int argc, char **argv) {

match_regex(AMQP_URL_REGEX, matches, 10, app.amqp_con.url);
if (matches[3] != NULL) {
app.amqp_con.user = strdup(matches[2]);
app.amqp_con.user = strdup(matches[3]);
}
if (matches[5] != NULL) {
app.amqp_con.password = strdup(matches[4]);
app.amqp_con.password = strdup(matches[5]);
}
if (matches[6] == NULL || matches[9] == NULL) {
fprintf(stderr, "Invalid AMQP URL: %s", app.amqp_con.url);
exit(1);
}
app.amqp_con.host = strdup(matches[6]);
if (strchr(matches[6], '[') != NULL && strchr(matches[6], ']') != NULL) {
app.amqp_con.host = strndup(matches[6] + 1, strlen(matches[6]) - 2);
} else {
app.amqp_con.host = strdup(matches[6]);
}

app.amqp_con.address = strdup(matches[9]);
if (matches[8] != NULL) {
app.amqp_con.port = strdup(matches[8]);
Expand Down
4 changes: 3 additions & 1 deletion bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#define DEFAULT_AMQP_BLOCK "false"

#define AMQP_URL_REGEX \
"^(amqps*)://(([a-z]+)(:([a-z]+))*@)*([a-zA-Z_0-9.-]+)(:([0-9]+))*(.+)$"
"^(amqps*)://" \
"(([a-z]+)(:([a-z]+))*@)*([a-zA-Z_0-9.-]+|\\[[:a-fA-F0-9]+\\])(:([0-9]+))" \
"?(/[^[:space:]]*)?$"

typedef struct {
char *user;
Expand Down
4 changes: 2 additions & 2 deletions build/repos/opstools.repo
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ enabled=0

[centos-opstools]
name=CentOS-OpsTools - collectd
mirrorlist=http://mirrorlist.centos.org/?arch=$basearch&release=$releasever-stream&repo=opstools-collectd-5
#baseurl=http://mirror.centos.org/$contentdir/$releasever-stream/opstools/$basearch/collectd-5/
#mirrorlist=http://mirrorlist.centos.org/?arch=$basearch&release=$releasever-stream&repo=opstools-collectd-5
baseurl=http://vault.centos.org/$releasever-stream/opstools/$basearch/collectd-5/
gpgcheck=0
enabled=1
skip_if_unavailable=1
Expand Down
11 changes: 11 additions & 0 deletions ci/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/env bash
# purpose: runt unit test suite

set -ex

# enable required repo(s)
dnf install -y centos-release-opstools tree
dnf install make gcc qpid-proton-c-devel annobin-annocheck gcc-plugin-annobin rpm-build -y

make tests
./tests
18 changes: 18 additions & 0 deletions minunit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* taken from https://jera.com/techinfo/jtns/jtn002
*/

/* file: minunit.h */
#define mu_assert(message, test) \
do { \
if (!(test)) \
return message; \
} while (0)
#define mu_run_test(test) \
do { \
char *message = test(); \
tests_run++; \
if (message) \
return message; \
} while (0)
extern int tests_run;
Loading

0 comments on commit 7d40677

Please sign in to comment.