aboutsummaryrefslogtreecommitdiff
path: root/internal/queue/gcpqueue/queue_test.go
blob: 6644c25ca1d5615f5f5b3e823a668739ee2d00ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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 (
	"testing"

	taskspb "cloud.google.com/go/cloudtasks/apiv2/cloudtaskspb"
	"github.com/google/go-cmp/cmp"
	"golang.org/x/pkgsite/internal/config"
	"golang.org/x/pkgsite/internal/queue"
	"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)
	}

}