chore: Misc cleanup and test improvements

This commit is contained in:
abhisek 2025-08-15 20:26:55 +05:30
parent 4b80c4a624
commit db6832e782
No known key found for this signature in database
GPG Key ID: CB92A4990C02A88F
4 changed files with 159 additions and 10 deletions

View File

@ -27,6 +27,9 @@ type McpClientToolBuilderConfig struct {
SQLQueryToolEnabled bool
SQLQueryToolDBPath string
PackageRegistryToolEnabled bool
// Enable debug mode for the MCP client.
Debug bool
}
type mcpClientToolBuilder struct {
@ -107,15 +110,18 @@ func (b *mcpClientToolBuilder) buildStdioClient() (*client.Client, error) {
return nil, fmt.Errorf("failed to get running binary path: %w", err)
}
// TODO: We should not log by default. This is only for debugging purposes.
vetMcpServerLogFile := filepath.Join(os.TempDir(), "vet-mcp-server.log")
// vet-mcp server defaults to stdio transport. See cmd/server/mcp.go
vetMcpServerCommandArgs := []string{"server", "mcp", "-l", vetMcpServerLogFile}
vetMcpServerCommandArgs := []string{"server", "mcp"}
if b.config.Debug {
vetMcpServerLogFile := filepath.Join(os.TempDir(), "vet-mcp-server.log")
vetMcpServerCommandArgs = append(vetMcpServerCommandArgs, "-l", vetMcpServerLogFile)
}
if b.config.SQLQueryToolEnabled {
vetMcpServerCommandArgs = append(vetMcpServerCommandArgs, "--sql-query-tool")
vetMcpServerCommandArgs = append(vetMcpServerCommandArgs, "--sql-query-tool-db-path", b.config.SQLQueryToolDBPath)
vetMcpServerCommandArgs = append(vetMcpServerCommandArgs, "--sql-query-tool-db-path",
b.config.SQLQueryToolDBPath)
}
if b.config.PackageRegistryToolEnabled {
@ -126,9 +132,12 @@ func (b *mcpClientToolBuilder) buildStdioClient() (*client.Client, error) {
vetMcpServerCommandArgs = append(vetMcpServerCommandArgs, "--skip-default-tools")
}
cli, err := client.NewStdioMCPClient(binaryPath, []string{
"APP_LOG_LEVEL=debug",
}, vetMcpServerCommandArgs...)
environmentVariables := []string{}
if b.config.Debug {
environmentVariables = append(environmentVariables, "APP_LOG_LEVEL=debug")
}
cli, err := client.NewStdioMCPClient(binaryPath, environmentVariables, vetMcpServerCommandArgs...)
if err != nil {
return nil, fmt.Errorf("failed to create stdio client: %w", err)
}

View File

@ -22,6 +22,9 @@ const (
communityModeEnvKey = "VET_COMMUNITY_MODE"
controlTowerTenantEnvKey = "VET_CONTROL_TOWER_TENANT_ID"
defaultSafeDepApiKeyEnvKey = "SAFEDEP_API_KEY"
defaultSafeDepTenantIdEnvKey = "SAFEDEP_TENANT_ID"
defaultApiUrl = "https://api.safedep.io/insights/v1"
defaultCommunityApiUrl = "https://api.safedep.io/insights-community/v1"
@ -234,8 +237,11 @@ func CommunityServicesApiUrl() string {
}
func TenantDomain() string {
tenantFromEnv := os.Getenv(controlTowerTenantEnvKey)
if tenantFromEnv != "" {
if tenantFromEnv, ok := os.LookupEnv(controlTowerTenantEnvKey); ok && tenantFromEnv != "" {
return tenantFromEnv
}
if tenantFromEnv, ok := os.LookupEnv(defaultSafeDepTenantIdEnvKey); ok && tenantFromEnv != "" {
return tenantFromEnv
}
@ -271,6 +277,10 @@ func ApiKey() string {
return key
}
if key, ok := os.LookupEnv(defaultSafeDepApiKeyEnvKey); ok {
return key
}
if globalConfig != nil {
return globalConfig.ApiKey
}

View File

@ -61,3 +61,128 @@ func TestInsightsApiV2Url(t *testing.T) {
assert.Equal(t, "https://test.safedep.io", InsightsApiV2Url())
})
}
func TestTenantDomain(t *testing.T) {
t.Run("should return the env variable if set", func(t *testing.T) {
t.Setenv("VET_CONTROL_TOWER_TENANT_ID", "test-tenant")
assert.Equal(t, "test-tenant", TenantDomain())
})
t.Run("should return env value when alternate env variable is set", func(t *testing.T) {
t.Setenv("SAFEDEP_TENANT_ID", "test-tenant")
assert.Equal(t, "test-tenant", TenantDomain())
})
t.Run("should fallback to default from global config", func(t *testing.T) {
oldVal := globalConfig.TenantDomain
t.Cleanup(func() {
globalConfig.TenantDomain = oldVal
})
globalConfig.TenantDomain = "test-tenant-from-config"
assert.Equal(t, "test-tenant-from-config", TenantDomain())
})
}
func TestApiKey(t *testing.T) {
t.Run("should return the env variable if set", func(t *testing.T) {
t.Setenv("VET_API_KEY", "test-api-key")
assert.Equal(t, "test-api-key", ApiKey())
})
t.Run("should return the env value when alternate env variable is set", func(t *testing.T) {
t.Setenv("VET_INSIGHTS_API_KEY", "test-api-key-alt")
assert.Equal(t, "test-api-key-alt", ApiKey())
})
t.Run("should return other alternate env variable if set", func(t *testing.T) {
t.Setenv("SAFEDEP_API_KEY", "test-api-key-safe")
assert.Equal(t, "test-api-key-safe", ApiKey())
})
t.Run("should fallback to default from global config", func(t *testing.T) {
oldVal := globalConfig.ApiKey
t.Cleanup(func() {
globalConfig.ApiKey = oldVal
})
globalConfig.ApiKey = "test-api-key-from-config"
assert.Equal(t, "test-api-key-from-config", ApiKey())
})
}
func TestApiUrl(t *testing.T) {
t.Run("should return the env variable if set", func(t *testing.T) {
t.Setenv("VET_INSIGHTS_API_URL", "https://test-api.safedep.io")
assert.Equal(t, "https://test-api.safedep.io", ApiUrl())
})
t.Run("should return community API URL when in community mode", func(t *testing.T) {
t.Setenv("VET_COMMUNITY_MODE", "true")
assert.Equal(t, defaultCommunityApiUrl, ApiUrl())
})
t.Run("should fallback to default from global config", func(t *testing.T) {
oldVal := globalConfig.ApiUrl
t.Cleanup(func() {
globalConfig.ApiUrl = oldVal
})
globalConfig.ApiUrl = "https://test-api-from-config.safedep.io"
assert.Equal(t, "https://test-api-from-config.safedep.io", ApiUrl())
})
t.Run("should return default API URL when no config is set", func(t *testing.T) {
// Clear global config temporarily
oldConfig := globalConfig
globalConfig = nil
t.Cleanup(func() {
globalConfig = oldConfig
})
assert.Equal(t, defaultApiUrl, ApiUrl())
})
}
func TestCommunityMode(t *testing.T) {
t.Run("should return true when env variable is set to true", func(t *testing.T) {
t.Setenv("VET_COMMUNITY_MODE", "true")
assert.True(t, CommunityMode())
})
t.Run("should return true when env variable is set to 1", func(t *testing.T) {
t.Setenv("VET_COMMUNITY_MODE", "1")
assert.True(t, CommunityMode())
})
t.Run("should return false when env variable is set to false", func(t *testing.T) {
t.Setenv("VET_COMMUNITY_MODE", "false")
assert.False(t, CommunityMode())
})
t.Run("should return false when env variable is set to invalid value", func(t *testing.T) {
t.Setenv("VET_COMMUNITY_MODE", "invalid")
assert.False(t, CommunityMode())
})
t.Run("should fallback to default from global config", func(t *testing.T) {
oldVal := globalConfig.Community
t.Cleanup(func() {
globalConfig.Community = oldVal
})
globalConfig.Community = true
assert.True(t, CommunityMode())
})
t.Run("should return false when no config is set", func(t *testing.T) {
// Clear global config temporarily
oldConfig := globalConfig
globalConfig = nil
t.Cleanup(func() {
globalConfig = oldConfig
})
assert.False(t, CommunityMode())
})
}

View File

@ -22,6 +22,11 @@ func getLogLevelFromEnv() logrus.Level {
envLogLevel = strings.ToLower(os.Getenv("VET_LOG_LEVEL"))
}
// Fallback to safedep defaults
if envLogLevel == "" {
envLogLevel = strings.ToLower(os.Getenv("APP_LOG_LEVEL"))
}
switch envLogLevel {
case "debug":
return logrus.DebugLevel