mirror of
https://github.com/Dvorinka/Containr.git
synced 2026-10-28 15:51:26 +00:00
Phase 0 cleanup verified end-to-end:
- Delete vendored APwhy runtime (cmd/apwhy, internal/apwhy) and the
unwired copy (internal/api/server.go + static bundle + cloned
internal/{storage,analytics,auth,rbac}) — ~7.5k lines of dead code.
- Keep Containr's native internal/gateway; move APwhy management
routes under /api/v1/gateway/* so they stop colliding with the
native /services paths. Fix handlers against the real schema:
UUID ids via RETURNING, integer enabled flags, plan validation,
service validate endpoint, unique-violation mapping.
- Port agents.go off GORM onto sqlc (sqlc/queries/agents.sql);
drop gorm dependencies; fix sqlc schema drift (services.environment_id).
- Replace the SELECT 1 goose baseline with the real consolidated
schema (52 tables) minus APwhy's parallel auth/RBAC objects;
idempotent via IF NOT EXISTS / OR REPLACE / guarded DO blocks so it
replays cleanly over legacy installs. Add a drop migration for the
dead objects and delete the legacy filename-tracked migrations/
loader, directory, and Dockerfile copy. Verified via cmd/migrate:
fresh install 53 tables, legacy upgrade clean to 20260917000000.
- Canvas: add invisible React Flow handles to ServiceNode so inferred
auto-connections stop emitting warning #008.
- Remove reactflow dep (superseded by @xyflow/react), test harness
cmds, and legacy 0001_init.sql backups.
Authored By: TDvorak <[email protected]>
114 lines
2.5 KiB
Go
114 lines
2.5 KiB
Go
package logger
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestLogger(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
logger := New(DebugLevel, &buf)
|
|
|
|
logger.Info("test message")
|
|
|
|
var entry Entry
|
|
err := json.Unmarshal(buf.Bytes(), &entry)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "INFO", entry.Level)
|
|
assert.Equal(t, "test message", entry.Message)
|
|
assert.NotEmpty(t, entry.Timestamp)
|
|
}
|
|
|
|
func TestLoggerWithFields(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
logger := New(InfoLevel, &buf)
|
|
|
|
logger.WithFields(map[string]interface{}{
|
|
"user_id": "123",
|
|
"action": "create",
|
|
}).Info("user action")
|
|
|
|
var entry Entry
|
|
err := json.Unmarshal(buf.Bytes(), &entry)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "123", entry.Fields["user_id"])
|
|
assert.Equal(t, "create", entry.Fields["action"])
|
|
}
|
|
|
|
func TestLoggerWithError(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
logger := New(InfoLevel, &buf)
|
|
|
|
testErr := errors.New("test error")
|
|
logger.WithError(testErr).Error("operation failed")
|
|
|
|
var entry Entry
|
|
err := json.Unmarshal(buf.Bytes(), &entry)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "ERROR", entry.Level)
|
|
assert.Equal(t, "test error", entry.Fields["error"])
|
|
}
|
|
|
|
func TestLoggerWithContext(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
logger := New(InfoLevel, &buf)
|
|
|
|
ctx := context.WithValue(context.Background(), "request_id", "req-123")
|
|
ctx = context.WithValue(ctx, "user_id", "user-456")
|
|
|
|
logger.WithContext(ctx).Info("request processed")
|
|
|
|
var entry Entry
|
|
err := json.Unmarshal(buf.Bytes(), &entry)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "req-123", entry.Fields["request_id"])
|
|
assert.Equal(t, "user-456", entry.Fields["user_id"])
|
|
}
|
|
|
|
func TestLogLevels(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
logLevel Level
|
|
logFunc func(*Logger)
|
|
shouldLog bool
|
|
}{
|
|
{
|
|
name: "debug logs at debug level",
|
|
logLevel: DebugLevel,
|
|
logFunc: func(l *Logger) { l.Debug("test") },
|
|
shouldLog: true,
|
|
},
|
|
{
|
|
name: "debug doesn't log at info level",
|
|
logLevel: InfoLevel,
|
|
logFunc: func(l *Logger) { l.Debug("test") },
|
|
shouldLog: false,
|
|
},
|
|
{
|
|
name: "error logs at info level",
|
|
logLevel: InfoLevel,
|
|
logFunc: func(l *Logger) { l.Error("test") },
|
|
shouldLog: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
logger := New(tt.logLevel, &buf)
|
|
tt.logFunc(logger)
|
|
|
|
if tt.shouldLog {
|
|
assert.NotEmpty(t, buf.String())
|
|
} else {
|
|
assert.Empty(t, buf.String())
|
|
}
|
|
})
|
|
}
|
|
}
|