Skip to content

Commit

Permalink
add tests for bsp-agent/config module
Browse files Browse the repository at this point in the history
Signed-off-by: Pranay Valson <[email protected]>
  • Loading branch information
noslav committed Jan 16, 2025
1 parent d8dddb6 commit d99020e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ run:
modules-download-mode: readonly
skip-files:
- "internal/config/utils_test.go"
- "internal/config/env_config_test.go"
skip-dirs:
- internal/metrics
- bin/
Expand Down
68 changes: 68 additions & 0 deletions internal/config/env_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package config

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestLoadEnvConfig(t *testing.T) {
t.Run("successful config loading with defaults", func(t *testing.T) {
// Clear any existing env vars that might interfere
os.Clearenv()

cfg, err := loadEnvConfig()
assert.NoError(t, err)
assert.NotNil(t, cfg)

// Check default values
assert.Equal(t, "", cfg.RedisConfig.Password)
assert.Equal(t, "http://127.0.0.1:3000", cfg.IpfsConfig.IpfsPinnerServer)
})

t.Run("successful config loading with custom values", func(t *testing.T) {
// Clear any existing env vars
os.Clearenv()

// Set test environment variables
envVars := map[string]string{
"REDIS_PWD": "testpassword",
"MB_RPC_URL": "https://test.rpc.url",
"MB_PRIVATE_KEY": "testkey123",
"MB_KEYSTORE_PATH": "/test/path",
"MB_KEYSTORE_PWD": "keystorepass",
"IFPS_PINNER_SERVER": "https://custom.ipfs.server",
}

for k, v := range envVars {
os.Setenv(k, v)
}

cfg, err := loadEnvConfig()
assert.NoError(t, err)
assert.NotNil(t, cfg)

// Verify all values were loaded correctly
assert.Equal(t, "testpassword", cfg.RedisConfig.Password)
assert.Equal(t, "https://test.rpc.url", cfg.EthConfig.RPCURL)
assert.Equal(t, "testkey123", cfg.EthConfig.PrivateKey)
assert.Equal(t, "/test/path", cfg.EthConfig.KeystorePath)
assert.Equal(t, "keystorepass", cfg.EthConfig.KeyStorePwd)
assert.Equal(t, "https://custom.ipfs.server", cfg.IpfsConfig.IpfsPinnerServer)
})

t.Run("invalid environment variable type", func(t *testing.T) {
// Clear any existing env vars
os.Clearenv()

// Set an invalid environment variable (if we had any integer fields)
// This test is included as an example, but with the current config
// structure it's hard to trigger a real parsing error since all fields
// are strings

cfg, err := loadEnvConfig()
assert.NoError(t, err) // Should still succeed as we have no non-string fields
assert.NotNil(t, cfg)
})
}

0 comments on commit d99020e

Please sign in to comment.