From 96b17712a822c1eded85e478adfa1244b1990d8e Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Tue, 18 Jul 2023 15:09:40 -0400 Subject: internal/queue: split out gcp queue into different package internal/frontend depends on internal/queue. Move the parts with gcp dependencies into its own package so internal/frontend no longer has those gcp queue dependencies. For #61399 Change-Id: I0083c31da1b367f135e53686e4c994d7c4d6420f Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/510815 TryBot-Result: Gopher Robot kokoro-CI: kokoro Run-TryBot: Michael Matloob Reviewed-by: Jamal Carvalho --- internal/queue/gcpqueue/queue_test.go | 81 +++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 internal/queue/gcpqueue/queue_test.go (limited to 'internal/queue/gcpqueue/queue_test.go') diff --git a/internal/queue/gcpqueue/queue_test.go b/internal/queue/gcpqueue/queue_test.go new file mode 100644 index 00000000..cbe92e78 --- /dev/null +++ b/internal/queue/gcpqueue/queue_test.go @@ -0,0 +1,81 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package gcpqueue + +import ( + "golang.org/x/pkgsite/internal/queue" + "testing" + + "github.com/google/go-cmp/cmp" + "golang.org/x/pkgsite/internal/config" + taskspb "google.golang.org/genproto/googleapis/cloud/tasks/v2" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/types/known/durationpb" +) + +func TestNewTaskID(t *testing.T) { + for _, test := range []struct { + modulePath, version string + want string + }{ + {"m-1", "v2", "acc5-m-1_vv2"}, + {"my_module", "v1.2.3", "0cb9-my__module_vv1_o2_o3"}, + {"µπΩ/github.com", "v2.3.4-ß", "a49c-_00b5_03c0_03a9_-github_ocom_vv2_o3_o4-_00df"}, + } { + got := newTaskID(test.modulePath, test.version) + if got != test.want { + t.Errorf("%s@%s: got %s, want %s", test.modulePath, test.version, got, test.want) + } + } +} + +func TestNewTaskRequest(t *testing.T) { + cfg := config.Config{ + ProjectID: "Project", + LocationID: "us-central1", + QueueURL: "http://1.2.3.4:8000", + ServiceAccount: "sa", + QueueAudience: "qa", + } + want := &taskspb.CreateTaskRequest{ + Parent: "projects/Project/locations/us-central1/queues/queueID", + Task: &taskspb.Task{ + DispatchDeadline: durationpb.New(maxCloudTasksTimeout), + MessageType: &taskspb.Task_HttpRequest{ + HttpRequest: &taskspb.HttpRequest{ + HttpMethod: taskspb.HttpMethod_POST, + Url: "http://1.2.3.4:8000/fetch/mod/@v/v1.2.3", + AuthorizationHeader: &taskspb.HttpRequest_OidcToken{ + OidcToken: &taskspb.OidcToken{ + ServiceAccountEmail: "sa", + Audience: "qa", + }, + }, + }, + }, + }, + } + gcp, err := newGCP(&cfg, nil, "queueID") + if err != nil { + t.Fatal(err) + } + opts := &queue.Options{ + Suffix: "suf", + } + got := gcp.newTaskRequest("mod", "v1.2.3", opts) + want.Task.Name = got.Task.Name + if diff := cmp.Diff(want, got, cmp.Comparer(proto.Equal)); diff != "" { + t.Errorf("mismatch (-want, +got):\n%s", diff) + } + + want.Task.MessageType.(*taskspb.Task_HttpRequest).HttpRequest.Url += "?proxyfetch=off" + opts.DisableProxyFetch = true + got = gcp.newTaskRequest("mod", "v1.2.3", opts) + want.Task.Name = got.Task.Name + if diff := cmp.Diff(want, got, cmp.Comparer(proto.Equal)); diff != "" { + t.Errorf("mismatch (-want, +got):\n%s", diff) + } + +} -- cgit v1.3