Skip to content

Commit

Permalink
feat(transport): Log DirectPath misconfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanli-ml committed Nov 3, 2023
1 parent e0fff1a commit 2daa2ab
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions transport/grpc/dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
package grpc

import (
"bytes"
"context"
"log"
"strings"
"testing"

"cloud.google.com/go/compute/metadata"
"golang.org/x/oauth2/google"
"google.golang.org/api/internal"
)

func TestCheckDirectPathEndPoint(t *testing.T) {
Expand Down Expand Up @@ -47,3 +55,73 @@ func TestCheckDirectPathEndPoint(t *testing.T) {
})
}
}

func TestLogDirectPathMisconfigAttrempDirectPathNotSet(t *testing.T) {
o := &internal.DialSettings{}
o.EnableDirectPathXds = true

endpoint := "abc.googleapis.com"

creds, err := internal.Creds(context.Context(context.Background()), o)
if err != nil {
t.Fatalf("failed to create creds")
}

buf := bytes.Buffer{}
log.SetOutput(&buf)

logDirectPathMisconfig(endpoint, creds.TokenSource, o)

wantedLog := "WARNING: DirectPath is misconfigured. Please set the EnableDirectPath option along with the EnableDirectPathXds option."
if !strings.Contains(buf.String(), wantedLog) {
t.Fatalf("got: %v, want: %v", buf.String(), wantedLog)
}

}

func TestLogDirectPathMisconfigWrongCredential(t *testing.T) {
o := &internal.DialSettings{}
o.EnableDirectPath = true
o.EnableDirectPathXds = true

endpoint := "abc.googleapis.com"

creds := &google.Credentials{}

buf := bytes.Buffer{}
log.SetOutput(&buf)

logDirectPathMisconfig(endpoint, creds.TokenSource, o)

wantedLog := "WARNING: DirectPath is misconfigured. Please make sure the token source is fetched from GCE metadata server and the default service account is used."
if !strings.Contains(buf.String(), wantedLog) {
t.Fatalf("got: %v, want: %v", buf.String(), wantedLog)
}

}

func TestLogDirectPathMisconfigNotOnGCE(t *testing.T) {
o := &internal.DialSettings{}
o.EnableDirectPath = true
o.EnableDirectPathXds = true

endpoint := "abc.googleapis.com"

creds, err := internal.Creds(context.Context(context.Background()), o)
if err != nil {
t.Fatalf("failed to create creds")
}

buf := bytes.Buffer{}
log.SetOutput(&buf)

logDirectPathMisconfig(endpoint, creds.TokenSource, o)

if !metadata.OnGCE() {
wantedLog := "WARNING: DirectPath is misconfigured. DirectPath is only available in a GCE environment.."
if !strings.Contains(buf.String(), wantedLog) {
t.Fatalf("got: %v, want: %v", buf.String(), wantedLog)
}
}

}

0 comments on commit 2daa2ab

Please sign in to comment.