Skip to content

Commit 64fff7a

Browse files
chore(compute): add integration tests for compute (#4421)
1 parent 60160af commit 64fff7a

1 file changed

Lines changed: 256 additions & 0 deletions

File tree

compute/apiv1/smoke_test.go

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
// Copyright 2021 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+
// https://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+
// To run these tests, set GCLOUD_TESTS_GOLANG_PROJECT_ID env var to your GCP projectID
16+
17+
package compute
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"testing"
23+
24+
"cloud.google.com/go/internal/testutil"
25+
"cloud.google.com/go/internal/uid"
26+
computepb "google.golang.org/genproto/googleapis/cloud/compute/v1"
27+
"google.golang.org/protobuf/proto"
28+
)
29+
30+
var projectId = testutil.ProjID()
31+
var defaultZone = "us-central1-a"
32+
33+
func TestCreateGetListInstance(t *testing.T) {
34+
if testing.Short() {
35+
t.Skip("skipping smoke test in short mode")
36+
}
37+
space := uid.NewSpace("gogapic", nil)
38+
name := space.New()
39+
ctx := context.Background()
40+
c, err := NewInstancesRESTClient(ctx)
41+
if err != nil {
42+
t.Fatal(err)
43+
}
44+
zonesClient, err := NewZoneOperationsRESTClient(ctx)
45+
if err != nil {
46+
t.Fatal(err)
47+
}
48+
createRequest := &computepb.InsertInstanceRequest{
49+
Project: projectId,
50+
Zone: defaultZone,
51+
InstanceResource: &computepb.Instance{
52+
Name: &name,
53+
Description: proto.String("тест"),
54+
MachineType: proto.String(fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/n1-standard-1", projectId, defaultZone)),
55+
Disks: []*computepb.AttachedDisk{
56+
{
57+
AutoDelete: proto.Bool(true),
58+
Boot: proto.Bool(true),
59+
Type: computepb.AttachedDisk_PERSISTENT.Enum(),
60+
InitializeParams: &computepb.AttachedDiskInitializeParams{
61+
SourceImage: proto.String("projects/debian-cloud/global/images/family/debian-10"),
62+
},
63+
},
64+
},
65+
NetworkInterfaces: []*computepb.NetworkInterface{
66+
{
67+
AccessConfigs: []*computepb.AccessConfig{
68+
{
69+
Name: proto.String("default"),
70+
},
71+
},
72+
},
73+
},
74+
},
75+
}
76+
77+
insert, err := c.Insert(ctx, createRequest)
78+
if err != nil {
79+
t.Fatal(err)
80+
}
81+
82+
waitZonalRequest := &computepb.WaitZoneOperationRequest{
83+
Project: projectId,
84+
Zone: defaultZone,
85+
Operation: insert.GetName(),
86+
}
87+
_, err = zonesClient.Wait(ctx, waitZonalRequest)
88+
if err != nil {
89+
t.Error(err)
90+
}
91+
defer ForceDeleteInstance(ctx, name, c)
92+
93+
getRequest := &computepb.GetInstanceRequest{
94+
Project: projectId,
95+
Zone: defaultZone,
96+
Instance: name,
97+
}
98+
get, err := c.Get(ctx, getRequest)
99+
if err != nil {
100+
t.Error(err)
101+
}
102+
if get.GetName() != name {
103+
t.Fatal(fmt.Sprintf("expected instance name: %s, got: %s", name, get.GetName()))
104+
}
105+
if get.GetDescription() != "тест" {
106+
t.Fatal(fmt.Sprintf("expected instance description: %s, got: %s", "тест", get.GetDescription()))
107+
}
108+
listRequest := &computepb.ListInstancesRequest{
109+
Project: projectId,
110+
Zone: defaultZone,
111+
}
112+
113+
list, err := c.List(ctx, listRequest)
114+
if err != nil {
115+
t.Error(err)
116+
}
117+
items := list.GetItems()
118+
found := false
119+
for _, element := range items {
120+
if element.GetName() == name {
121+
found = true
122+
}
123+
}
124+
if !found {
125+
t.Error("Couldn't find the instance in list response")
126+
}
127+
128+
deleteInstanceRequest := &computepb.DeleteInstanceRequest{
129+
Project: projectId,
130+
Zone: defaultZone,
131+
Instance: name,
132+
}
133+
_, err = c.Delete(ctx, deleteInstanceRequest)
134+
if err != nil {
135+
t.Error(err)
136+
}
137+
}
138+
139+
func ForceDeleteInstance(ctx context.Context, name string, client *InstancesClient) {
140+
deleteInstanceRequest := &computepb.DeleteInstanceRequest{
141+
Project: projectId,
142+
Zone: defaultZone,
143+
Instance: name,
144+
}
145+
client.Delete(ctx, deleteInstanceRequest)
146+
}
147+
148+
func TestCreateGetRemoveSecurityPolicies(t *testing.T) {
149+
if testing.Short() {
150+
t.Skip("skipping smoke test in short mode")
151+
}
152+
space := uid.NewSpace("gogapic", nil)
153+
name := space.New()
154+
ctx := context.Background()
155+
c, err := NewSecurityPoliciesRESTClient(ctx)
156+
if err != nil {
157+
t.Fatal(err)
158+
}
159+
globalCLient, err := NewGlobalOperationsRESTClient(ctx)
160+
if err != nil {
161+
t.Fatal(err)
162+
}
163+
action := "allow"
164+
matcher := &computepb.SecurityPolicyRuleMatcher{
165+
Config: &computepb.SecurityPolicyRuleMatcherConfig{
166+
SrcIpRanges: []string{
167+
"*",
168+
},
169+
},
170+
VersionedExpr: computepb.SecurityPolicyRuleMatcher_SRC_IPS_V1.Enum(),
171+
}
172+
securityPolicyRule := &computepb.SecurityPolicyRule{
173+
Action: &action,
174+
Priority: proto.Int32(0),
175+
Description: proto.String("test rule"),
176+
Match: matcher,
177+
}
178+
securityPolicyRuleDefault := &computepb.SecurityPolicyRule{
179+
Action: &action,
180+
Priority: proto.Int32(2147483647),
181+
Description: proto.String("default rule"),
182+
Match: matcher,
183+
}
184+
insertRequest := &computepb.InsertSecurityPolicyRequest{
185+
Project: projectId,
186+
SecurityPolicyResource: &computepb.SecurityPolicy{
187+
Name: &name,
188+
Rules: []*computepb.SecurityPolicyRule{
189+
securityPolicyRule,
190+
securityPolicyRuleDefault,
191+
},
192+
},
193+
}
194+
insert, err := c.Insert(ctx, insertRequest)
195+
if err != nil {
196+
t.Fatal(err)
197+
}
198+
199+
waitGlobalRequest := &computepb.WaitGlobalOperationRequest{
200+
Project: projectId,
201+
Operation: insert.GetName(),
202+
}
203+
_, err = globalCLient.Wait(ctx, waitGlobalRequest)
204+
if err != nil {
205+
t.Error(err)
206+
}
207+
defer ForceDeleteSecurityPolicy(ctx, name, c)
208+
209+
removeRuleRequest := &computepb.RemoveRuleSecurityPolicyRequest{
210+
Priority: proto.Int32(0),
211+
Project: projectId,
212+
SecurityPolicy: name,
213+
}
214+
215+
rule, err := c.RemoveRule(ctx, removeRuleRequest)
216+
if err != nil {
217+
t.Error(err)
218+
}
219+
waitGlobalRequestRemove := &computepb.WaitGlobalOperationRequest{
220+
Project: projectId,
221+
Operation: rule.GetName(),
222+
}
223+
_, err = globalCLient.Wait(ctx, waitGlobalRequestRemove)
224+
if err != nil {
225+
t.Error(err)
226+
}
227+
228+
getRequest := &computepb.GetSecurityPolicyRequest{
229+
Project: projectId,
230+
SecurityPolicy: name,
231+
}
232+
get, err := c.Get(ctx, getRequest)
233+
if err != nil {
234+
t.Error(err)
235+
}
236+
if len(get.GetRules()) != 1 {
237+
t.Fatal(fmt.Sprintf("expected count for rules: %d, got: %d", 1, len(get.GetRules())))
238+
}
239+
240+
deleteRequest := &computepb.DeleteSecurityPolicyRequest{
241+
Project: projectId,
242+
SecurityPolicy: name,
243+
}
244+
_, err = c.Delete(ctx, deleteRequest)
245+
if err != nil {
246+
t.Error(err)
247+
}
248+
}
249+
250+
func ForceDeleteSecurityPolicy(ctx context.Context, name string, client *SecurityPoliciesClient) {
251+
deleteRequest := &computepb.DeleteSecurityPolicyRequest{
252+
Project: projectId,
253+
SecurityPolicy: name,
254+
}
255+
client.Delete(ctx, deleteRequest)
256+
}

0 commit comments

Comments
 (0)