|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package push_subscription_separate_pub_sub |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "testing" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud" |
| 23 | + "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +func TestPushSubscriptionSeparatePubSub(t *testing.T) { |
| 28 | + bpt := tft.NewTFBlueprintTest(t) |
| 29 | + |
| 30 | + bpt.DefineVerify(func(assert *assert.Assertions) { |
| 31 | + bpt.DefaultVerify(assert) |
| 32 | + |
| 33 | + projectId := bpt.GetStringOutput("project_id") |
| 34 | + |
| 35 | + op := gcloud.Runf(t, "pubsub topics describe cft-tf-pub-topic --project=%s", projectId) |
| 36 | + assert.Equal(fmt.Sprintf("projects/%s/topics/cft-tf-pub-topic", projectId), op.Get("name").String(), "has expected name") |
| 37 | + assert.Equal("bar_value", op.Get("labels.bar_label").String(), "has expected labels") |
| 38 | + assert.Equal("foo_value", op.Get("labels.foo_label").String(), "has expected labels") |
| 39 | + |
| 40 | + op = gcloud.Runf(t, "pubsub subscriptions describe cr-service --project=%s", projectId) |
| 41 | + assert.Equal(fmt.Sprintf("projects/%s/subscriptions/cr-service", projectId), op.Get("name").String(), "has expected name") |
| 42 | + assert.Equal(fmt.Sprintf("projects/%s/topics/cft-tf-pub-topic", projectId), op.Get("topic").String(), "has expected topic") |
| 43 | + // Publish a test message |
| 44 | + message := "Hello Runner!" |
| 45 | + publishCmd := fmt.Sprintf("pubsub topics publish cft-tf-pub-topic --message='%s' --project=%s", message, projectId) |
| 46 | + gcloud.Runf(t, publishCmd) |
| 47 | + |
| 48 | + // Wait for a short time to allow the message to be processed. Adjust if necessary. |
| 49 | + time.Sleep(30 * time.Second) |
| 50 | + |
| 51 | + // Check Cloud Run logs for the message |
| 52 | + logCmd := fmt.Sprintf("logging read \"resource.type=cloud_run_revision AND resource.labels.service_name=run-service AND textPayload:\\\"%s\\\"\" --project=%s --limit=1", message, projectId) |
| 53 | + logOp := gcloud.Runf(t, logCmd) |
| 54 | + |
| 55 | + // Assert that the log entry is found |
| 56 | + assert.Contains(logOp.String(), message, "Cloud Run logs should contain the published message") |
| 57 | + }) |
| 58 | + |
| 59 | + bpt.Test() |
| 60 | +} |
0 commit comments