Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(compute): add integration tests for compute #4421

Merged
merged 23 commits into from Jul 16, 2021
Merged
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
54f9228
tests: add go integartion tests
georgiyekkert Jul 12, 2021
cc2e3e9
Merge branch 'googleapis:master' into master
georgiyekkert Jul 12, 2021
5a30666
tests: review fixes + formatting
georgiyekkert Jul 12, 2021
478eefd
tests: review fixes + formatting
georgiyekkert Jul 12, 2021
18c64fa
Merge branch 'master' of https://github.com/georgiyekkert/google-clou…
georgiyekkert Jul 12, 2021
c906601
Merge branch 'master' into master
georgiyekkert Jul 12, 2021
bf08f74
tests: review fixes
georgiyekkert Jul 12, 2021
7530380
Merge branch 'master' of https://github.com/georgiyekkert/google-clou…
georgiyekkert Jul 12, 2021
4ad0aea
Merge branch 'googleapis:master' into master
georgiyekkert Jul 13, 2021
059c905
tests: review fixes
georgiyekkert Jul 13, 2021
775152b
Merge branch 'master' into master
georgiyekkert Jul 13, 2021
66845b8
Merge branch 'master' into master
georgiyekkert Jul 14, 2021
4b71c2c
tests: review fixes
georgiyekkert Jul 14, 2021
596c03e
Merge branch 'master' of https://github.com/georgiyekkert/google-clou…
georgiyekkert Jul 14, 2021
3c52d7a
tests: review fixes
georgiyekkert Jul 14, 2021
2a400b6
tests: review fixes
georgiyekkert Jul 14, 2021
2244554
Merge branch 'master' into master
georgiyekkert Jul 14, 2021
2286baf
Merge branch 'googleapis:master' into master
georgiyekkert Jul 15, 2021
3333a2c
tests: formatting
georgiyekkert Jul 15, 2021
235c3d4
Merge branch 'master' of https://github.com/georgiyekkert/google-clou…
georgiyekkert Jul 15, 2021
9874753
Merge branch 'master' into master
georgiyekkert Jul 15, 2021
4850139
tests: goimports
georgiyekkert Jul 15, 2021
428e119
tests: remove yoda condition
georgiyekkert Jul 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
290 changes: 290 additions & 0 deletions compute/apiv1/smoke_test.go
@@ -0,0 +1,290 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// To run these tests, set GCLOUD_TESTS_GOLANG_PROJECT_ID env var to your GCP projectID

package compute

import (
"cloud.google.com/go/internal/testutil"
"context"
"fmt"
"github.com/stretchr/testify/assert"
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
computepb "google.golang.org/genproto/googleapis/cloud/compute/v1"
"google.golang.org/protobuf/proto"
"math/rand"
"testing"
"time"
)

var projectId = testutil.ProjID()
var defaultZone = "us-central1-a"

func TestCreateGetListInstance(t *testing.T){
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
rand.Seed(time.Now().UTC().UnixNano())
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
name := fmt.Sprintf("gotest%d", rand.Int())
description := "тест"
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
machineType := fmt.Sprintf(
"https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/n1-standard-1",
projectId, defaultZone)
ctx := context.Background()
c, err := NewInstancesRESTClient(ctx)
if err != nil {
t.Fatal(err)
}
zonesClient, err := NewZoneOperationsRESTClient(ctx)
if err != nil {
t.Fatal(err)
}
configName := "default"
accessConfig := computepb.AccessConfig{
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
Name: &configName,
}
configs := []*computepb.AccessConfig {
&accessConfig,
}
networkInterface := computepb.NetworkInterface{
AccessConfigs: configs,
}
interfaces := []*computepb.NetworkInterface{
&networkInterface,
}
sourceImage := "projects/debian-cloud/global/images/family/debian-10"
initializeParams := &computepb.AttachedDiskInitializeParams{
SourceImage: &sourceImage,
}
diskType := computepb.AttachedDisk_PERSISTENT
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
disk := computepb.AttachedDisk{
AutoDelete: proto.Bool(true),
Boot: proto.Bool(true),
Type: &diskType,
InitializeParams: initializeParams,
}
disks := []*computepb.AttachedDisk {
&disk,
}
instance := &computepb.Instance{
Name: &name,
Description: &description,
MachineType: &machineType,
Disks: disks,
NetworkInterfaces: interfaces,
}

createRequest := &computepb.InsertInstanceRequest{
Project: projectId,
Zone: defaultZone,
InstanceResource: instance,
codyoss marked this conversation as resolved.
Show resolved Hide resolved
}

insert, err := c.Insert(ctx, createRequest)
if err != nil {
fmt.Println(err)
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
t.Error(err)
codyoss marked this conversation as resolved.
Show resolved Hide resolved
}

waitZonalRequest := &computepb.WaitZoneOperationRequest{
Project: projectId,
Zone: defaultZone,
Operation: insert.GetName(),
}
_, err = zonesClient.Wait(ctx, waitZonalRequest)
if err != nil {
return
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
}
fmt.Printf("Inserted instance named %s\n", name)
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
defer ForceDeleteInstance(name, ctx, c)

getRequest := &computepb.GetInstanceRequest{
Project: projectId,
Zone: defaultZone,
Instance: name,
}
get, err := c.Get(ctx, getRequest)
if err != nil {
fmt.Println(err)
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
t.Error(err)
}
assert.Equal(t, name, get.GetName())
assert.Equal(t, "тест", get.GetDescription())

listRequest := &computepb.ListInstancesRequest{
Project: projectId,
Zone: defaultZone,
}

list, err := c.List(ctx, listRequest)
if err != nil {
fmt.Println(err)
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
t.Error(err)
}
items := list.GetItems()
found := false
for _, element := range items {
if element.GetName() == name {
found = true
}
}
if found == false{
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
t.Error("Couldn't find the instance in list response")
}

deleteInstanceRequest := &computepb.DeleteInstanceRequest{
codyoss marked this conversation as resolved.
Show resolved Hide resolved
Project: projectId,
Zone: defaultZone,
Instance: name,
}
_, err = c.Delete(ctx, deleteInstanceRequest)
if err != nil {
fmt.Println(err)
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
t.Error(err)
}
}

func ForceDeleteInstance(name string, ctx context.Context, client *InstancesClient){
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
deleteInstanceRequest := &computepb.DeleteInstanceRequest{
Project: projectId,
Zone: defaultZone,
Instance: name,
}
_, err := client.Delete(ctx, deleteInstanceRequest)
if err != nil {}
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved

}

func TestCreateGetRemoveSecurityPolicies(t *testing.T){
rand.Seed(time.Now().UTC().UnixNano())
name := fmt.Sprintf("gotest%d", rand.Int())
ctx := context.Background()
c, err := NewSecurityPoliciesRESTClient(ctx)
if err != nil {
t.Fatal(err)
}
globalCLient, err := NewGlobalOperationsRESTClient(ctx)
if err != nil {
t.Fatal(err)
}
defaultDescription := "default rule"
description := "test rule"
defaultPriority := int32(2147483647)
priority := int32(0)
action := "allow"

srcIpRanges := []string{
"*",
}
config := &computepb.SecurityPolicyRuleMatcherConfig{
SrcIpRanges: srcIpRanges,
}
versionExpr := computepb.SecurityPolicyRuleMatcher_SRC_IPS_V1
matcher := &computepb.SecurityPolicyRuleMatcher{
Config: config,
VersionedExpr: &versionExpr,
}
securityPolicyRule := &computepb.SecurityPolicyRule{
Action: &action,
Priority: &priority,
Description: &description,
Match: matcher,
}
securityPolicyRuleDefault := &computepb.SecurityPolicyRule{
Action: &action,
Priority: &defaultPriority,
Description: &defaultDescription,
Match: matcher,
}

rules := []*computepb.SecurityPolicyRule{
securityPolicyRule,
securityPolicyRuleDefault,
}

securityPolicy := &computepb.SecurityPolicy{
Name: &name,
Rules: rules,
}

insertRequest := &computepb.InsertSecurityPolicyRequest{
Project: projectId,
SecurityPolicyResource: securityPolicy,
}
insert, err := c.Insert(ctx, insertRequest)
if err != nil {
fmt.Println(err)
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
t.Error(err)
}

waitGlobalRequest := &computepb.WaitGlobalOperationRequest{
Project: projectId,
Operation: insert.GetName(),
}
_, err = globalCLient.Wait(ctx, waitGlobalRequest)
if err != nil {
fmt.Println(err)
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
t.Error(err)
}
fmt.Printf("Inserted security policy named %s\n", name)
defer ForceDeleteSecurityPolicy(name, ctx, c)

removeRuleRequest := &computepb.RemoveRuleSecurityPolicyRequest{
Priority: &priority,
Project: projectId,
SecurityPolicy: name,
}

rule, err := c.RemoveRule(ctx, removeRuleRequest)
if err != nil {
fmt.Println(err)
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
t.Error(err)
}
waitGlobalRequestRemove := &computepb.WaitGlobalOperationRequest{
Project: projectId,
Operation: rule.GetName(),
}
_, err = globalCLient.Wait(ctx, waitGlobalRequestRemove)
if err != nil {
fmt.Println(err)
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
t.Error(err)
}

getRequest := &computepb.GetSecurityPolicyRequest{
Project: projectId,
SecurityPolicy: name,
}
get, err := c.Get(ctx, getRequest)
if err != nil {
fmt.Println(err)
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
t.Error(err)
}
assert.Equal(t, 1, len(get.GetRules()))

deleteRequest := &computepb.DeleteSecurityPolicyRequest{
Project: projectId,
SecurityPolicy: name,
}
_, err = c.Delete(ctx, deleteRequest)
if err != nil {
fmt.Println(err)
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
t.Error(err)
}
}

func ForceDeleteSecurityPolicy(name string, ctx context.Context, client *SecurityPoliciesClient){
deleteRequest := &computepb.DeleteSecurityPolicyRequest{
Project: projectId,
SecurityPolicy: name,
}
_, err := client.Delete(ctx, deleteRequest)
if err != nil {}
georgiyekkert marked this conversation as resolved.
Show resolved Hide resolved
}