Files
probo/pkg/bootstrap/write_test.go
Émile Ré c043a7f1f5 Extract config structs into pkg/probodconfig
probod-bootstrap only needs the config struct definitions for
YAML marshaling but transitively pulled in ~40 heavy runtime
dependencies via pkg/probod. Move all config types and their
methods to a new pkg/probodconfig package and re-export them
from pkg/probod via type aliases for backward compatibility.

Signed-off-by: Émile Ré <emile@getprobo.com>
2026-04-29 12:48:43 +04:00

159 lines
4.7 KiB
Go

// Copyright (c) 2026 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package bootstrap
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.probo.inc/probo/pkg/probodconfig"
"sigs.k8s.io/yaml"
)
func TestWriteConfig(t *testing.T) {
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, "probod.yml")
cfg := &probodconfig.FullConfig{
Unit: probodconfig.UnitConfig{
Metrics: probodconfig.MetricsConfig{Addr: "localhost:9090"},
},
Probod: probodconfig.Config{
BaseURL: "http://localhost:8080",
EncryptionKey: "test-key",
},
}
err := WriteConfig(cfg, configPath)
require.NoError(t, err)
data, err := os.ReadFile(configPath)
require.NoError(t, err)
var loaded probodconfig.FullConfig
err = yaml.Unmarshal(data, &loaded)
require.NoError(t, err)
assert.Equal(t, cfg.Unit.Metrics.Addr, loaded.Unit.Metrics.Addr)
assert.Equal(t, cfg.Probod.BaseURL, loaded.Probod.BaseURL)
assert.Equal(t, cfg.Probod.EncryptionKey, loaded.Probod.EncryptionKey)
}
func TestWriteConfig_CreatesDirectory(t *testing.T) {
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, "nested", "dir", "probod.yml")
cfg := &probodconfig.FullConfig{
Probod: probodconfig.Config{BaseURL: "http://localhost:8080"},
}
err := WriteConfig(cfg, configPath)
require.NoError(t, err)
_, err = os.Stat(configPath)
require.NoError(t, err)
}
func TestWriteConfig_FilePermissions(t *testing.T) {
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, "probod.yml")
cfg := &probodconfig.FullConfig{}
err := WriteConfig(cfg, configPath)
require.NoError(t, err)
info, err := os.Stat(configPath)
require.NoError(t, err)
assert.Equal(t, os.FileMode(0600), info.Mode().Perm())
}
func TestWriteConfig_CompleteConfig(t *testing.T) {
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, "probod.yml")
cfg := &probodconfig.FullConfig{
Unit: probodconfig.UnitConfig{
Metrics: probodconfig.MetricsConfig{Addr: "localhost:8081"},
Tracing: probodconfig.TracingConfig{
Addr: "localhost:4317",
MaxBatchSize: 512,
BatchTimeout: 5,
ExportTimeout: 30,
MaxQueueSize: 2048,
},
},
Probod: probodconfig.Config{
BaseURL: "http://localhost:8080",
EncryptionKey: "test-key",
ChromeDPAddr: "localhost:9222",
Api: probodconfig.APIConfig{
Addr: ":8080",
Cors: probodconfig.CorsConfig{
AllowedOrigins: []string{"http://localhost:8080"},
},
ExtraHeaderFields: map[string]string{},
},
Pg: probodconfig.PgConfig{
Addr: "localhost:5432",
Username: "postgres",
Password: "postgres",
Database: "probod",
PoolSize: 100,
MinPoolSize: 10,
MaxConnIdleTimeSeconds: 1800,
MaxConnLifetimeSeconds: 3600,
},
Connectors: []probodconfig.ConnectorConfig{
{
Provider: "slack",
Protocol: "oauth2",
RawConfig: probodconfig.ConnectorConfigOAuth2{
ClientID: "client-id",
ClientSecret: "client-secret",
},
RawSettings: map[string]any{
"signing-secret": "secret",
},
},
},
},
}
err := WriteConfig(cfg, configPath)
require.NoError(t, err)
data, err := os.ReadFile(configPath)
require.NoError(t, err)
var loaded probodconfig.FullConfig
err = yaml.Unmarshal(data, &loaded)
require.NoError(t, err)
assert.Equal(t, cfg.Unit.Metrics.Addr, loaded.Unit.Metrics.Addr)
assert.Equal(t, cfg.Unit.Tracing.MaxBatchSize, loaded.Unit.Tracing.MaxBatchSize)
assert.Equal(t, cfg.Probod.Api.Cors.AllowedOrigins, loaded.Probod.Api.Cors.AllowedOrigins)
assert.Equal(t, cfg.Probod.Pg.PoolSize, loaded.Probod.Pg.PoolSize)
assert.Equal(t, cfg.Probod.Pg.MinPoolSize, loaded.Probod.Pg.MinPoolSize)
assert.Equal(t, cfg.Probod.Pg.MaxConnIdleTimeSeconds, loaded.Probod.Pg.MaxConnIdleTimeSeconds)
assert.Equal(t, cfg.Probod.Pg.MaxConnLifetimeSeconds, loaded.Probod.Pg.MaxConnLifetimeSeconds)
require.Len(t, loaded.Probod.Connectors, 1)
assert.Equal(t, "SLACK", loaded.Probod.Connectors[0].Provider)
}