From b96ab68e8572a6ecc025e7637f8fe34439516395 Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Fri, 4 Jul 2025 18:46:33 +0000 Subject: [PATCH] test(lib/policy/config): ensure valkey stores can be loaded Signed-off-by: Xe Iaso --- internal/unbreakdocker.go | 22 ++++++++++++++++++++++ lib/policy/config/store_test.go | 24 ++++++++++++++++++++++++ lib/store/valkey/valkey_test.go | 20 +++++--------------- 3 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 internal/unbreakdocker.go diff --git a/internal/unbreakdocker.go b/internal/unbreakdocker.go new file mode 100644 index 00000000..8bf29fd3 --- /dev/null +++ b/internal/unbreakdocker.go @@ -0,0 +1,22 @@ +package internal + +import ( + "os" + "os/exec" +) + +func UnbreakDocker() { + // XXX(Xe): This is bad code. Do not do this. + // + // I have to do this because I'm running from inside the context of a dev + // container. This dev container runs in a different docker network than + // the valkey test container runs in. In order to let my dev container + // connect to the test container, they need to share a network in common. + // The easiest network to use for this is the default "bridge" network. + // + // This is a horrifying monstrosity, but the part that scares me the most + // is the fact that it works. + if hostname, err := os.Hostname(); err == nil { + exec.Command("docker", "network", "connect", "bridge", hostname).Run() + } +} diff --git a/lib/policy/config/store_test.go b/lib/policy/config/store_test.go index 66de31d2..1c1556e7 100644 --- a/lib/policy/config/store_test.go +++ b/lib/policy/config/store_test.go @@ -7,6 +7,7 @@ import ( "github.com/TecharoHQ/anubis/lib/policy/config" "github.com/TecharoHQ/anubis/lib/store/bbolt" + "github.com/TecharoHQ/anubis/lib/store/valkey" ) func TestStoreValid(t *testing.T) { @@ -33,6 +34,29 @@ func TestStoreValid(t *testing.T) { Parameters: json.RawMessage(`{"path": "/tmp/foo", "bucket": "bar"}`), }, }, + { + name: "valkey backend", + input: config.Store{ + Backend: "valkey", + Parameters: json.RawMessage(`{"url": "redis://valkey:6379/0"}`), + }, + }, + { + name: "valkey backend no URL", + input: config.Store{ + Backend: "valkey", + Parameters: json.RawMessage(`{}`), + }, + err: valkey.ErrNoURL, + }, + { + name: "valkey backend bad URL", + input: config.Store{ + Backend: "valkey", + Parameters: json.RawMessage(`{"url": "http://anubis.techaro.lol"}`), + }, + err: valkey.ErrBadURL, + }, { name: "bbolt backend no path", input: config.Store{ diff --git a/lib/store/valkey/valkey_test.go b/lib/store/valkey/valkey_test.go index cbe0b475..eb1b8811 100644 --- a/lib/store/valkey/valkey_test.go +++ b/lib/store/valkey/valkey_test.go @@ -4,14 +4,18 @@ import ( "encoding/json" "fmt" "os" - "os/exec" "testing" + "github.com/TecharoHQ/anubis/internal" "github.com/TecharoHQ/anubis/lib/store/storetest" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" ) +func init() { + internal.UnbreakDocker() +} + func TestImpl(t *testing.T) { if os.Getenv("DONT_USE_NETWORK") != "" { t.Skip("test requires network egress") @@ -38,20 +42,6 @@ func TestImpl(t *testing.T) { t.Fatal(err) } - // XXX(Xe): This is bad code. Do not do this. - // - // I have to do this because I'm running from inside the context of a dev - // container. This dev container runs in a different docker network than - // the valkey test container runs in. In order to let my dev container - // connect to the test container, they need to share a network in common. - // The easiest network to use for this is the default "bridge" network. - // - // This is a horrifying monstrosity, but the part that scares me the most - // is the fact that it works. - if hostname, err := os.Hostname(); err == nil { - exec.Command("docker", "network", "connect", "bridge", hostname).Run() - } - data, err := json.Marshal(Config{ URL: fmt.Sprintf("redis://%s:6379/0", containerIP), })