-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/gomote, internal/gomote/protos: add the upload file endpoint
This change adds the upload file endpoint which will be used by the gomote clients to upload files to GCS before they are retrieved by a gomote instance. The endpoint generates a signed URL and associated fields which must be used in the upload. For golang/go#47521 Updates golang/go#48742 Change-Id: Id85a55b41b8211b3aae8c2e30245a0b71ecfa238 Reviewed-on: https://go-review.googlesource.com/c/build/+/397595 Trust: Carlos Amedee <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
- Loading branch information
1 parent
a7bbee6
commit fc95939
Showing
7 changed files
with
327 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,13 @@ package gomote | |
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"testing" | ||
"time" | ||
|
||
"cloud.google.com/go/storage" | ||
"github.com/google/go-cmp/cmp" | ||
"golang.org/x/build/internal/access" | ||
"golang.org/x/build/internal/coordinator/remote" | ||
|
@@ -27,13 +29,17 @@ import ( | |
"google.golang.org/protobuf/testing/protocmp" | ||
) | ||
|
||
const testBucketName = "unit-testing-bucket" | ||
|
||
func fakeGomoteServer(t *testing.T, ctx context.Context) protos.GomoteServiceServer { | ||
signer, err := ssh.ParsePrivateKey([]byte(devCertCAPrivate)) | ||
if err != nil { | ||
t.Fatalf("unable to parse raw certificate authority private key into signer=%s", err) | ||
} | ||
return &Server{ | ||
bucket: &fakeBucketHandler{bucketName: testBucketName}, | ||
buildlets: remote.NewSessionPool(ctx), | ||
gceBucketName: testBucketName, | ||
scheduler: schedule.NewFake(), | ||
sshCertificateAuthority: signer, | ||
} | ||
|
@@ -675,6 +681,48 @@ func TestSignSSHKeyError(t *testing.T) { | |
} | ||
} | ||
|
||
func TestUploadFile(t *testing.T) { | ||
ctx := access.FakeContextWithOutgoingIAPAuth(context.Background(), fakeIAP()) | ||
client := setupGomoteTest(t, context.Background()) | ||
_ = mustCreateInstance(t, client, fakeIAP()) | ||
if _, err := client.UploadFile(ctx, &protos.UploadFileRequest{}); err != nil { | ||
t.Fatalf("client.UploadFile(ctx, req) = response, %s; want no error", err) | ||
} | ||
} | ||
|
||
func TestUploadFileError(t *testing.T) { | ||
// This test will create a gomote instance and attempt to call UploadFile. | ||
// If overrideID is set to true, the test will use a different gomoteID than the | ||
// the one created for the test. | ||
testCases := []struct { | ||
desc string | ||
ctx context.Context | ||
overrideID bool | ||
filename string | ||
wantCode codes.Code | ||
}{ | ||
{ | ||
desc: "unauthenticated request", | ||
ctx: context.Background(), | ||
wantCode: codes.Unauthenticated, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
client := setupGomoteTest(t, context.Background()) | ||
_ = mustCreateInstance(t, client, fakeIAP()) | ||
req := &protos.UploadFileRequest{} | ||
got, err := client.UploadFile(tc.ctx, req) | ||
if err != nil && status.Code(err) != tc.wantCode { | ||
t.Fatalf("unexpected error: %s; want %s", err, tc.wantCode) | ||
} | ||
if err == nil { | ||
t.Fatalf("client.UploadFile(ctx, %v) = %v, nil; want error", req, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestWriteTGZFromURL(t *testing.T) { | ||
ctx := access.FakeContextWithOutgoingIAPAuth(context.Background(), fakeIAP()) | ||
client := setupGomoteTest(t, context.Background()) | ||
|
@@ -871,3 +919,17 @@ OfjWFhdu6e4JYiVfN7ZYAAAAE3Rlc3R1c2VyQGdvbGFuZy5vcmcBAg== | |
// devCertCAPublic is a public SSH CA certificate to be used for development. | ||
devCertCAPublic = `ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJV3YUncNv+hXneJEO3VEuxxOfjWFhdu6e4JYiVfN7ZY [email protected]` | ||
) | ||
|
||
type fakeBucketHandler struct{ bucketName string } | ||
|
||
func (fbc *fakeBucketHandler) GenerateSignedPostPolicyV4(object string, opts *storage.PostPolicyV4Options) (*storage.PostPolicyV4, error) { | ||
if object == "" || opts == nil { | ||
return nil, errors.New("invalid arguments") | ||
} | ||
return &storage.PostPolicyV4{ | ||
URL: fmt.Sprintf("https://localhost/%s/%s", fbc.bucketName, object), | ||
Fields: map[string]string{ | ||
"x-permission-to-post": "granted", | ||
}, | ||
}, nil | ||
} |
Oops, something went wrong.