From 8c0cd2f7fd62f0a58089b58a0d2fe0c44f68d149 Mon Sep 17 00:00:00 2001 From: Denis Machard <5562930+dmachard@users.noreply.github.com> Date: Fri, 17 Nov 2023 08:01:28 +0100 Subject: [PATCH] GHactions count tests and number of lines (#466) * GHactions count tests * add badges * add tests --- .github/workflows/testing-go.yml | 26 +++++++++++++++++++++- Makefile | 3 ++- README.md | 2 ++ dnsutils/message.go | 2 +- docs/collectors/collector_powerdns.md | 1 - docs/development.md | 2 +- docs/loggers/logger_loki.md | 2 +- docs/loggers/logger_redis.md | 2 +- docs/loggers/logger_stdout.md | 4 ++-- processors/dns_test.go | 31 +++++++++++++++++++++++++++ 10 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 processors/dns_test.go diff --git a/.github/workflows/testing-go.yml b/.github/workflows/testing-go.yml index ef03183d..93523927 100644 --- a/.github/workflows/testing-go.yml +++ b/.github/workflows/testing-go.yml @@ -116,4 +116,28 @@ jobs: cd prometheus-$PROM_VERSION.linux-amd64/ curl -u admin:changeme http://127.0.0.1:8081/metrics | ./promtool check metrics env: - PROM_VERSION: "2.47.0" \ No newline at end of file + PROM_VERSION: "2.47.0" + + count: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: "1.21" + + - id: count_tests + run: | + data=$(sudo go test -v ./collectors ./processors ./dnsutils ./netlib ./loggers ./transformers 2>&1 | grep -c RUN) + echo "Count of Tests: $data" + echo "data=$data" >> $GITHUB_OUTPUT + + - id: count_lines + run: | + data=$(find . -name "*.go" -exec grep -v "^$" {} \; -exec grep -v "//" {} \; | wc -l) + echo "Count of Lines: $data" + echo "data=$data" >> $GITHUB_OUTPUT \ No newline at end of file diff --git a/Makefile b/Makefile index 57e783dc..bed0cfd0 100644 --- a/Makefile +++ b/Makefile @@ -50,12 +50,13 @@ version: build lint: $(GOPATH)/bin/golangci-lint run --config=.golangci.yml ./... -test: +tests: @go test ./dnsutils/ -race -cover -v @go test ./netlib/ -race -cover -v @go test -timeout 30s ./transformers/ -race -cover -v @go test -timeout 30s ./collectors/ -race -cover -v @go test -timeout 90s ./loggers/ -race -cover -v + @go test -timeout 30s ./processors/ -race -cover -v clean: @go clean diff --git a/README.md b/README.md index 00798894..b4be44bd 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ [![Go Report Card](https://goreportcard.com/badge/github.com/dmachard/go-dns-collector)](https://goreportcard.com/report/dmachard/go-dns-collector) ![Go version](https://img.shields.io/badge/go%20version-min%201.20-blue) +![Go tests](https://img.shields.io/badge/go%20tests-333-green) +![Go lines](https://img.shields.io/badge/go%20lines-47871-red) ![Go Tests](https://github.com/dmachard/go-dns-collector/actions/workflows/testing-go.yml/badge.svg) ![Github Actions](https://github.com/dmachard/go-dns-collector/actions/workflows/testing-dnstap.yml/badge.svg) diff --git a/dnsutils/message.go b/dnsutils/message.go index b295c1d4..be0e8600 100644 --- a/dnsutils/message.go +++ b/dnsutils/message.go @@ -835,7 +835,7 @@ func GetFakeDnsMessage() DnsMessage { func GetFakeDnsMessageWithPayload() DnsMessage { // fake dns query payload dnsmsg := new(dns.Msg) - dnsmsg.SetQuestion("dnscollector.dev.", dns.TypeA) + dnsmsg.SetQuestion("dnscollector.dev.", dns.TypeAAAA) dnsquestion, _ := dnsmsg.Pack() dm := GetFakeDnsMessage() diff --git a/docs/collectors/collector_powerdns.md b/docs/collectors/collector_powerdns.md index 0f8ba083..84153336 100644 --- a/docs/collectors/collector_powerdns.md +++ b/docs/collectors/collector_powerdns.md @@ -29,7 +29,6 @@ powerdns: sock-rcvbuf: 0 reset-conn: true chan-buffer-size: 65535 - add-dns-payload: false ``` The DNS-collector has a full [Protobuf Logging](https://dnsdist.org/reference/protobuf.html) support for PowerDNS's products. diff --git a/docs/development.md b/docs/development.md index b163bc9f..832e4bf4 100644 --- a/docs/development.md +++ b/docs/development.md @@ -39,7 +39,7 @@ make lint Execute testunits before to commit ```bash -sudo make test +sudo make tests ``` Execute a test for one specific testcase in a package diff --git a/docs/loggers/logger_loki.md b/docs/loggers/logger_loki.md index b15b5e4c..e92d6385 100644 --- a/docs/loggers/logger_loki.md +++ b/docs/loggers/logger_loki.md @@ -10,7 +10,7 @@ Options: - `flush-interval`: (integer) flush batch every X seconds - `batch-size`: (integer) batch size for log entries in bytes - `retry-interval`: (integer) interval in second between before to retry to send batch -- `text-format`: (string) output text format, please refer to the default text format to see all available directives, use this parameter if you want a specific format +- `text-format`: (string) output text format, please refer to the default text format to see all available [directives](../configuration.md#custom-text-format), use this parameter if you want a specific format - `proxy-url`: (string) Proxy URL - `tls-insecure`: (boolean) insecure tls, skip certificate and hostname verify - `tls-min-version`: (string) min tls version diff --git a/docs/loggers/logger_redis.md b/docs/loggers/logger_redis.md index 145e9630..dce80156 100644 --- a/docs/loggers/logger_redis.md +++ b/docs/loggers/logger_redis.md @@ -11,7 +11,7 @@ Redis Pub logger Options: * `transport`: (string) network transport to use: `tcp`|`unix`|`tcp+tls` -* `remote-ip`: (string) remote address +* `remote-address`: (string) remote IP or host address * `remote-port`: (integer) remote tcp port * `sock-path` **DEPRECATED, replaced by remote-address**: (string) unix socket path * `connect-timeout`: (integer) connect timeout in second diff --git a/docs/loggers/logger_stdout.md b/docs/loggers/logger_stdout.md index 3e0f9166..0b952158 100644 --- a/docs/loggers/logger_stdout.md +++ b/docs/loggers/logger_stdout.md @@ -8,8 +8,8 @@ Print to your standard output, all DNS logs received Options: -* `mode`: (string) output format: text, json, flat-json or pcap -* `text-format`: (string) output text format, please refer to the default text format to see all available directives, use this parameter if you want a specific format +* `mode`: (string) output format: `text`, `json`, `flat-json` or `pcap` +* `text-format`: (string) output text format, please refer to the default text format to see all available [directives](../configuration.md#custom-text-format), use this parameter if you want a specific format * `chan-buffer-size`: (integer) channel buffer size used on incoming dns message, number of messages before to drop it. Default values: diff --git a/processors/dns_test.go b/processors/dns_test.go new file mode 100644 index 00000000..a4a72d1b --- /dev/null +++ b/processors/dns_test.go @@ -0,0 +1,31 @@ +package processors + +import ( + "bytes" + "testing" + + "github.com/dmachard/go-dnscollector/dnsutils" + "github.com/dmachard/go-logger" +) + +func Test_DnsProcessor(t *testing.T) { + logger := logger.New(true) + var o bytes.Buffer + logger.SetOutput(&o) + + // init and run the dns processor + consumer := NewDnsProcessor(dnsutils.GetFakeConfig(), logger, "test", 512) + chan_to := make(chan dnsutils.DnsMessage, 512) + go consumer.Run([]chan dnsutils.DnsMessage{chan_to}, []string{"test"}) + + dm := dnsutils.GetFakeDnsMessageWithPayload() + consumer.GetChannel() <- dm + + // read dns message from dnstap consumer + dmOut := <-chan_to + + if dmOut.DNS.Qname != "dnscollector.dev" { + t.Errorf("invalid qname in dns message: %s", dm.DNS.Qname) + } + +}