Add V(1)/V(2) verbose logging to the GCP provider
One V(1) line per GCP API call (insert/get/delete/aggregatedList) with outcome and operation name, V(2) request/per-instance detail, and raw googleapi status+reasons logged before classify collapses them. Curated fields only — cloud-init user-data never reaches logs (test-enforced). Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -3,10 +3,13 @@ package gcp
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"cloud.google.com/go/compute/apiv1/computepb"
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/go-logr/logr/funcr"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"gitea.home.hrajfrisbee.cz/kacerr/egress-proxies-operator/internal/provider"
|
||||
@@ -269,3 +272,172 @@ func TestParseProviderID_roundTrip(t *testing.T) {
|
||||
t.Errorf("round trip = %s/%s (%v), want europe-west1-b/proxy-abc", zone, name, err)
|
||||
}
|
||||
}
|
||||
|
||||
// captureContext returns a ctx carrying a funcr logger that records every
|
||||
// emitted line, capped at the given verbosity — the test stand-in for
|
||||
// --zap-log-level=<verbosity>.
|
||||
func captureContext(verbosity int) (context.Context, *[]string) {
|
||||
lines := &[]string{}
|
||||
log := funcr.New(func(prefix, args string) {
|
||||
*lines = append(*lines, prefix+" "+args)
|
||||
}, funcr.Options{Verbosity: verbosity})
|
||||
return logr.NewContext(context.Background(), log), lines
|
||||
}
|
||||
|
||||
func runningInstance() *computepb.Instance {
|
||||
return &computepb.Instance{
|
||||
Name: proto.String("proxy-abc123def456ghij"),
|
||||
Status: proto.String("RUNNING"),
|
||||
Zone: proto.String("https://www.googleapis.com/compute/v1/projects/my-project/zones/europe-west1-b"),
|
||||
CreationTimestamp: proto.String("2026-08-09T10:00:00+02:00"),
|
||||
Labels: map[string]string{
|
||||
provider.LabelManaged: provider.LabelManagedYes,
|
||||
provider.LabelUID: "uid-1",
|
||||
},
|
||||
NetworkInterfaces: []*computepb.NetworkInterface{{
|
||||
AccessConfigs: []*computepb.AccessConfig{{NatIP: proto.String("34.1.2.3")}},
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
func runAllOps(t *testing.T, ctx context.Context, req provider.CreateRequest) {
|
||||
t.Helper()
|
||||
inst := runningInstance()
|
||||
p := newTestProvider(&fakeAPI{getInst: inst, listInsts: []*computepb.Instance{inst}})
|
||||
if _, err := p.Create(ctx, req); err != nil {
|
||||
t.Fatalf("Create: %v", err)
|
||||
}
|
||||
if _, err := p.Get(ctx, "zones/europe-west1-b/instances/proxy-abc123def456ghij"); err != nil {
|
||||
t.Fatalf("Get: %v", err)
|
||||
}
|
||||
if err := p.Delete(ctx, "zones/europe-west1-b/instances/proxy-abc123def456ghij"); err != nil {
|
||||
t.Fatalf("Delete: %v", err)
|
||||
}
|
||||
if _, err := p.ListByTag(ctx); err != nil {
|
||||
t.Fatalf("ListByTag: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogging_verbosityTiers(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
verbosity int
|
||||
wantLines []string
|
||||
absentLines []string
|
||||
}{
|
||||
{
|
||||
name: "v0 stays silent",
|
||||
verbosity: 0,
|
||||
absentLines: []string{
|
||||
"GCP instance insert submitted",
|
||||
"GCP instance fetched",
|
||||
"GCP instance delete submitted",
|
||||
"GCP instances listed",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "v1 logs one line per API call",
|
||||
verbosity: 1,
|
||||
wantLines: []string{
|
||||
`"msg"="GCP instance insert submitted"`,
|
||||
`"opName"="op-insert"`,
|
||||
`"msg"="GCP instance fetched"`,
|
||||
`"status"="RUNNING"`,
|
||||
`"msg"="GCP instance delete submitted"`,
|
||||
`"opName"="op-delete"`,
|
||||
`"msg"="GCP instances listed"`,
|
||||
`"provider"="gcp-eu"`,
|
||||
},
|
||||
absentLines: []string{
|
||||
"GCP insert request built",
|
||||
"GCP listed instance",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "v2 adds request and per-instance detail",
|
||||
verbosity: 2,
|
||||
wantLines: []string{
|
||||
`"msg"="GCP insert request built"`,
|
||||
`"cloudInitBytes"=`,
|
||||
`"msg"="GCP listed instance"`,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx, lines := captureContext(tc.verbosity)
|
||||
req := testCreateRequest()
|
||||
const sentinel = "SENTINEL-cloud-init-must-never-be-logged"
|
||||
req.CloudInit = sentinel
|
||||
|
||||
runAllOps(t, ctx, req)
|
||||
|
||||
joined := strings.Join(*lines, "\n")
|
||||
if tc.verbosity == 0 && len(*lines) != 0 {
|
||||
t.Errorf("verbosity 0 logged %d lines:\n%s", len(*lines), joined)
|
||||
}
|
||||
for _, want := range tc.wantLines {
|
||||
if !strings.Contains(joined, want) {
|
||||
t.Errorf("output missing %q:\n%s", want, joined)
|
||||
}
|
||||
}
|
||||
for _, absent := range tc.absentLines {
|
||||
if strings.Contains(joined, absent) {
|
||||
t.Errorf("output unexpectedly contains %q:\n%s", absent, joined)
|
||||
}
|
||||
}
|
||||
if strings.Contains(joined, sentinel) {
|
||||
t.Errorf("cloud-init content leaked into logs:\n%s", joined)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogging_apiErrorKeepsHTTPDetail(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx, lines := captureContext(1)
|
||||
p := newTestProvider(&fakeAPI{insertErr: gerr(403, "quotaExceeded")})
|
||||
|
||||
if _, err := p.Create(ctx, testCreateRequest()); err == nil {
|
||||
t.Fatal("Create: want error")
|
||||
}
|
||||
|
||||
joined := strings.Join(*lines, "\n")
|
||||
for _, want := range []string{
|
||||
`"msg"="GCP API call failed"`,
|
||||
`"httpStatus"=403`,
|
||||
`"quotaExceeded"`,
|
||||
`"op"="create"`,
|
||||
} {
|
||||
if !strings.Contains(joined, want) {
|
||||
t.Errorf("output missing %q:\n%s", want, joined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogging_treatedAsSuccessPathsAreExplicit(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx, lines := captureContext(1)
|
||||
p := newTestProvider(&fakeAPI{insertErr: gerr(409), deleteErr: gerr(404)})
|
||||
|
||||
if _, err := p.Create(ctx, testCreateRequest()); err != nil {
|
||||
t.Fatalf("Create with 409: %v", err)
|
||||
}
|
||||
if err := p.Delete(ctx, "zones/z/instances/gone"); err != nil {
|
||||
t.Fatalf("Delete with 404: %v", err)
|
||||
}
|
||||
|
||||
joined := strings.Join(*lines, "\n")
|
||||
for _, want := range []string{
|
||||
`"msg"="GCP instance already exists, insert treated as success"`,
|
||||
`"msg"="GCP instance already gone, delete treated as success"`,
|
||||
} {
|
||||
if !strings.Contains(joined, want) {
|
||||
t.Errorf("output missing %q:\n%s", want, joined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user