Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ jobs:
- name: Run tests
run: |
curl -sSfL https://git.io/GeoLite2-City.mmdb -o /tmp/GeoLite2-City.mmdb && \
export KEEP_GEOIP_DB=true && \
go test -v ./...
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ jobs:
- name: Build binaries
run: |
curl -sSfL https://git.io/GeoLite2-City.mmdb -o /tmp/GeoLite2-City.mmdb && \
export KEEP_GEOIP_DB=true && \
make all
- name: Create SHA256 checksums
run: make checksum
Expand Down
46 changes: 10 additions & 36 deletions internal/geoip/geoip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ Licensed under the MIT License, see LICENSE file in the project root for details
package geoip

import (
"io"
"net/http"
"net/netip"
"os"
"testing"
Expand All @@ -15,40 +13,15 @@ import (
"github.com/stretchr/testify/assert"
)

const geoDatabasePath = "/tmp/GeoLite2-City.mmdb"
const geoDatabaseUrl = "https://git.io/GeoLite2-City.mmdb"

func setupGeoDatabase() {
if _, err := os.Stat(geoDatabasePath); os.IsNotExist(err) {
resp, err := http.Get(geoDatabaseUrl)
if err != nil {
panic(err)
}
defer func() {
_ = resp.Body.Close()
}()

out, err := os.Create(geoDatabasePath)
if err != nil {
panic(err)
}
defer func() {
_ = out.Close()
}()

_, err = io.Copy(out, resp.Body)
if err != nil {
panic(err)
}
}
}
var geoDatabasePath string

func newReturnsError_InvalidDatabase(t *testing.T) {
_, err := NewGeoIP("../../README.md")
assert.EqualError(t, err, "error opening database: invalid MaxMind DB file")
}

func newReturnsInstance_ValidDatabase(t *testing.T) {
t.Logf("Using GeoIP database at %s", geoDatabasePath)
geoIP, err := NewGeoIP(geoDatabasePath)
assert.NoError(t, err)
assert.NotNil(t, geoIP)
Expand Down Expand Up @@ -86,16 +59,17 @@ func locationReturnsLocation_ResolvedIP(t *testing.T) {
}

func TestGeoIP(t *testing.T) {
setupGeoDatabase()
var ok bool
geoDatabasePath, ok = os.LookupEnv("CONNTRACKD_GEOIP_DATABASE")
if !ok || geoDatabasePath == "" {
geoDatabasePath = "/tmp/GeoLite2-City.mmdb"
}
if _, err := os.Stat(geoDatabasePath); os.IsNotExist(err) {
t.Skip("GeoIP database not found, skipping GeoIP tests")
}

t.Run("New returns error for invalid database", newReturnsError_InvalidDatabase)
t.Run("New returns instance for valid database", newReturnsInstance_ValidDatabase)
t.Run("Location returns nil for unresolved IPs", locationReturnsNil_UnresolvedIP)
t.Run("Location returns location for resolved IPs", locationReturnsLocation_ResolvedIP)

skip, ok := os.LookupEnv("KEEP_GEOIP_DB")
if ok && skip == "1" || skip == "true" {
return
}
_ = os.Remove(geoDatabasePath)
}
45 changes: 9 additions & 36 deletions internal/record/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ package record
import (
"bytes"
"encoding/json"
"io"
"log/slog"
"maps"
"net/http"
"net/netip"
"os"
"slices"
Expand All @@ -22,8 +20,7 @@ import (
"github.com/tschaefer/conntrackd/internal/geoip"
)

const geoDatabasePath = "/tmp/GeoLite2-City.mmdb"
const geoDatabaseUrl = "https://git.io/GeoLite2-City.mmdb"
var geoDatabasePath string

var log bytes.Buffer

Expand All @@ -40,31 +37,6 @@ func setupLogger() *slog.Logger {
return slog.New(slog.NewJSONHandler(&log, loggerOptions))
}

func setupGeoDatabase() {
if _, err := os.Stat(geoDatabasePath); os.IsNotExist(err) {
resp, err := http.Get(geoDatabaseUrl)
if err != nil {
panic(err)
}
defer func() {
_ = resp.Body.Close()
}()

out, err := os.Create(geoDatabasePath)
if err != nil {
panic(err)
}
defer func() {
_ = out.Close()
}()

_, err = io.Copy(out, resp.Body)
if err != nil {
panic(err)
}
}
}

func recordLogsBasicData(t *testing.T) {
logger := setupLogger()

Expand Down Expand Up @@ -132,14 +104,15 @@ func recordLogsWithGeoIPData(t *testing.T) {
}

func TestRecord(t *testing.T) {
setupGeoDatabase()
var ok bool
geoDatabasePath, ok = os.LookupEnv("CONNTRACKD_GEOIP_DATABASE")
if !ok || geoDatabasePath == "" {
geoDatabasePath = "/tmp/GeoLite2-City.mmdb"
}
if _, err := os.Stat(geoDatabasePath); os.IsNotExist(err) {
t.Skip("GeoIP database not found, skipping GeoIP tests")
}

t.Run("Record logs basic data", recordLogsBasicData)
t.Run("Record logs with GeoIP data", recordLogsWithGeoIPData)

skip, ok := os.LookupEnv("KEEP_GEOIP_DB")
if ok && skip == "1" || skip == "true" {
return
}
_ = os.Remove(geoDatabasePath)
}