Skip to content

Commit

Permalink
test: adding tests that run more error conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
tpryan committed Mar 14, 2023
1 parent f7e60ac commit 47dd292
Show file tree
Hide file tree
Showing 12 changed files with 487 additions and 9 deletions.
36 changes: 36 additions & 0 deletions gcloud/cloudbuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package gcloud

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"google.golang.org/api/cloudbuild/v1"
)

Expand Down Expand Up @@ -64,3 +66,37 @@ func TestTriggerCreate(t *testing.T) {
})
}
}

func TestCloudBuildBadProject(t *testing.T) {
t.Parallel()
bad := "notavalidprojectnameanditshouldfaildasdas"
tests := map[string]struct {
servicefunc func() error
err error
}{
"CloudBuildTriggerCreate": {
servicefunc: func() error {
c := NewClient(context.Background(), "testing")
_, err := c.CloudBuildTriggerCreate(bad, cloudbuild.BuildTrigger{})
return err
},
err: fmt.Errorf("error activating service for polling"),
},
"CloudBuildTriggerDelete": {
servicefunc: func() error {
c := NewClient(context.Background(), "testing")
return c.CloudBuildTriggerDelete(bad, "")
},
err: fmt.Errorf("error activating service for polling"),
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
tc := tc
t.Parallel()
err := tc.servicefunc()
assert.ErrorContains(t, err, tc.err.Error())
})
}
}
58 changes: 58 additions & 0 deletions gcloud/clouddomains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package gcloud

import (
"context"
"fmt"
"os"
"path/filepath"
Expand All @@ -23,11 +24,13 @@ import (

"github.com/go-test/deep"
"github.com/kylelemons/godebug/diff"
"github.com/stretchr/testify/assert"
domainspb "google.golang.org/genproto/googleapis/cloud/domains/v1beta1"
"google.golang.org/genproto/googleapis/type/postaladdress"
)

func TestContactDataYAML(t *testing.T) {
t.Parallel()
tests := map[string]struct {
file string
contact ContactData
Expand Down Expand Up @@ -77,6 +80,7 @@ func TestContactDataYAML(t *testing.T) {
}

func TestContactDataReadFrom(t *testing.T) {
t.Parallel()
tests := map[string]struct {
file string
want ContactData
Expand Down Expand Up @@ -121,6 +125,7 @@ func TestContactDataReadFrom(t *testing.T) {
}

func TestContactDataWriteTo(t *testing.T) {
t.Parallel()
tests := map[string]struct {
contact ContactData
}{
Expand Down Expand Up @@ -257,6 +262,7 @@ func TestDomainIsVerified(t *testing.T) {
}

func TestDomainContact(t *testing.T) {
t.Parallel()
contact := &domainspb.ContactSettings_Contact{
PostalAddress: &postaladdress.PostalAddress{
RegionCode: "US",
Expand Down Expand Up @@ -314,3 +320,55 @@ func TestDomainContact(t *testing.T) {
})
}
}

func TestDomainsBadProject(t *testing.T) {
t.Parallel()
bad := "notavalidprojectnameanditshouldfaildasdas"
tests := map[string]struct {
servicefunc func() error
err error
}{
"DomainsSearch": {
servicefunc: func() error {
c := NewClient(context.Background(), "testing")
_, err := c.DomainsSearch(bad, "")
return err
},
err: fmt.Errorf("error activating service for polling"),
},
"DomainIsAvailable": {
servicefunc: func() error {
c := NewClient(context.Background(), "testing")
_, err := c.DomainIsAvailable(bad, "")
return err
},
err: fmt.Errorf("error activating service for polling"),
},

"DomainIsVerified": {
servicefunc: func() error {
c := NewClient(context.Background(), "testing")
_, err := c.DomainIsVerified(bad, "")
return err
},
err: fmt.Errorf("error activating service for polling"),
},

"DomainRegister": {
servicefunc: func() error {
c := NewClient(context.Background(), "testing")
return c.DomainRegister(bad, nil, ContactData{})
},
err: fmt.Errorf("error activating service for polling"),
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
tc := tc
t.Parallel()
err := tc.servicefunc()
assert.ErrorContains(t, err, tc.err.Error())
})
}
}
60 changes: 60 additions & 0 deletions gcloud/cloudfunctions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package gcloud

import (
"context"
"fmt"
"path/filepath"
"reflect"
Expand All @@ -23,6 +24,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"google.golang.org/api/cloudfunctions/v1"
)

Expand Down Expand Up @@ -153,3 +155,61 @@ func TestCloudFunctionCreate(t *testing.T) {
})
}
}

func TestCloudFunctionsBadProject(t *testing.T) {
t.Parallel()
bad := "notavalidprojectnameanditshouldfaildasdas"
tests := map[string]struct {
servicefunc func() error
err error
}{
"FunctionRegionList": {
servicefunc: func() error {
c := NewClient(context.Background(), "testing")
_, err := c.FunctionRegionList(bad)
return err
},
err: fmt.Errorf("error activating service for polling"),
},
"FunctionDeploy": {
servicefunc: func() error {
c := NewClient(context.Background(), "testing")
return c.FunctionDeploy(bad, "", cloudfunctions.CloudFunction{})
},
err: fmt.Errorf("error activating service for polling"),
},
"FunctionDelete": {
servicefunc: func() error {
c := NewClient(context.Background(), "testing")
return c.FunctionDelete(bad, "", "")
},
err: fmt.Errorf("error activating service for polling"),
},
"FunctionGet": {
servicefunc: func() error {
c := NewClient(context.Background(), "testing")
_, err := c.FunctionGet(bad, "", "")
return err
},
err: fmt.Errorf("error activating service for polling"),
},

"FunctionGenerateSignedURL": {
servicefunc: func() error {
c := NewClient(context.Background(), "testing")
_, err := c.FunctionGenerateSignedURL(bad, "")
return err
},
err: fmt.Errorf("error activating service for polling"),
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
tc := tc
t.Parallel()
err := tc.servicefunc()
assert.ErrorContains(t, err, tc.err.Error())
})
}
}
31 changes: 31 additions & 0 deletions gcloud/cloudrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
package gcloud

import (
"context"
"fmt"
"path/filepath"
"reflect"
"sort"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetRunRegions(t *testing.T) {
Expand Down Expand Up @@ -53,3 +57,30 @@ func TestGetRunRegions(t *testing.T) {
})
}
}

func TestCloudRunBadProject(t *testing.T) {
t.Parallel()
bad := "notavalidprojectnameanditshouldfaildasdas"
tests := map[string]struct {
servicefunc func() error
err error
}{
"RunRegionList": {
servicefunc: func() error {
c := NewClient(context.Background(), "testing")
_, err := c.RunRegionList(bad)
return err
},
err: fmt.Errorf("error activating service for polling"),
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
tc := tc
t.Parallel()
err := tc.servicefunc()
assert.ErrorContains(t, err, tc.err.Error())
})
}
}
65 changes: 65 additions & 0 deletions gcloud/computeengine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package gcloud

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
Expand All @@ -27,6 +28,7 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"google.golang.org/api/compute/v1"
)

Expand Down Expand Up @@ -92,6 +94,7 @@ func TestZones(t *testing.T) {
}

func TestFormatMBToGB(t *testing.T) {
t.Parallel()
tests := map[string]struct {
input int64
want string
Expand Down Expand Up @@ -519,6 +522,7 @@ func TestImages(t *testing.T) {
}

func TestGetLatestImage(t *testing.T) {
t.Parallel()
c := NewClient(ctx, defaultUserAgent)
f := filepath.Join(testFilesDir, "gcloudout/images.json")
dat, err := os.ReadFile(f)
Expand Down Expand Up @@ -558,3 +562,64 @@ func TestGetLatestImage(t *testing.T) {
})
}
}

func TestComputeBadProject(t *testing.T) {
t.Parallel()
bad := "notavalidprojectnameanditshouldfaildasdas"
tests := map[string]struct {
servicefunc func() error
err error
}{
"ComputeRegionList": {
servicefunc: func() error {
c := NewClient(context.Background(), "testing")
_, err := c.ComputeRegionList(bad)
return err
},
err: fmt.Errorf("error activating service for polling"),
},
"ZoneList": {
servicefunc: func() error {
c := NewClient(context.Background(), "testing")
_, err := c.ZoneList(bad, "")
return err
},
err: fmt.Errorf("error activating service for polling"),
},

"MachineTypeList": {
servicefunc: func() error {
c := NewClient(context.Background(), "testing")
_, err := c.MachineTypeList(bad, "")
return err
},
err: fmt.Errorf("error activating service for polling"),
},
"ImageList": {
servicefunc: func() error {
c := NewClient(context.Background(), "testing")
_, err := c.ImageList(bad, "")
return err
},
err: fmt.Errorf("error activating service for polling"),
},

"ImageLatestGet": {
servicefunc: func() error {
c := NewClient(context.Background(), "testing")
_, err := c.ImageList(bad, "")
return err
},
err: fmt.Errorf("error activating service for polling"),
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
tc := tc
t.Parallel()
err := tc.servicefunc()
assert.ErrorContains(t, err, tc.err.Error())
})
}
}
Loading

0 comments on commit 47dd292

Please sign in to comment.