Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ var testCmd = &cobra.Command{
return err
}

if cfg.APIKey == "" {
return fmt.Errorf("DD_API_KEY is required (set via environment variable or run 'pup auth login')")
}
if cfg.AppKey == "" {
return fmt.Errorf("DD_APP_KEY is required (set via environment variable or run 'pup auth login')")
}

fmt.Println("Configuration is valid:")
fmt.Printf(" Site: %s\n", cfg.Site)
fmt.Printf(" API Key: %s...%s\n", cfg.APIKey[:8], cfg.APIKey[len(cfg.APIKey)-4:])
Expand Down
67 changes: 67 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"errors"
"strings"
"testing"

"github.com/DataDog/pup/pkg/config"
)

func TestRootCmd_SilenceUsage(t *testing.T) {
Expand Down Expand Up @@ -185,6 +187,71 @@ func TestFormatAPIError(t *testing.T) {
}
}

func TestTestCmd_MissingKeys(t *testing.T) {
tests := []struct {
name string
apiKey string
appKey string
wantErr string
}{
{
name: "missing API key",
apiKey: "",
appKey: "test-app-key-value",
wantErr: "DD_API_KEY is required",
},
{
name: "missing App key",
apiKey: "test-api-key-value",
appKey: "",
wantErr: "DD_APP_KEY is required",
},
{
name: "both keys missing",
apiKey: "",
appKey: "",
wantErr: "DD_API_KEY is required",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
origCfg := cfg
defer func() { cfg = origCfg }()

cfg = &config.Config{
APIKey: tt.apiKey,
AppKey: tt.appKey,
Site: "datadoghq.com",
}

err := testCmd.RunE(testCmd, []string{})
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("error = %q, want containing %q", err.Error(), tt.wantErr)
}
})
}
}

func TestTestCmd_ValidKeys(t *testing.T) {
origCfg := cfg
defer func() { cfg = origCfg }()

cfg = &config.Config{
APIKey: "abcdefgh12345678",
AppKey: "ijklmnop87654321",
Site: "datadoghq.com",
}

err := testCmd.RunE(testCmd, []string{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}

func TestFormatAPIError_AllStatusCodes(t *testing.T) {
// Test that all documented status codes get special handling
statusTests := []struct {
Expand Down