Skip to content

Commit

Permalink
Log error when parsing GCP disk with non-JSON description
Browse files Browse the repository at this point in the history
Fixes #41

Signed-off-by: Leandro López (inkel) <[email protected]>
  • Loading branch information
inkel committed Jan 3, 2023
1 parent f86b9a9 commit 662e3b0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion gcp/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ func (p *Provider) ListUnusedDisks(ctx context.Context) (unused.Disks, error) {

m, err := diskMetadata(d)
if err != nil {
return fmt.Errorf("reading disk metadata: %w", err)
p.logger.Log("cannot parse disk metadata", logfmt.Labels{
"project": p.project,
"disk": d.Name,
"err": err,
})
}
disks = append(disks, &Disk{d, p, m})
}
Expand Down
12 changes: 11 additions & 1 deletion gcp/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gcp_test

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -132,7 +133,10 @@ func TestProviderListUnusedDisks(t *testing.T) {
t.Fatalf("unexpected error creating GCP compute service: %v", err)
}

p, err := gcp.NewProvider(svc, "my-project", nil)
var buf bytes.Buffer
l := logfmt.NewLogger(&buf)

p, err := gcp.NewProvider(l, svc, "my-project", nil)
if err != nil {
t.Fatal("unexpected error creating provider:", err)
}
Expand All @@ -145,5 +149,11 @@ func TestProviderListUnusedDisks(t *testing.T) {
if len(disks) != 1 {
t.Fatalf("expecting 1 unused disk, got %d", len(disks))
}

// check that we logged about it
m, _ := regexp.MatchString(`msg="cannot parse disk metadata".+disk="disk-2"`, buf.String())
if !m {
t.Fatal("expecting a log line to be emitted")
}
})
}

0 comments on commit 662e3b0

Please sign in to comment.