Skip to content

Commit 831d005

Browse files
committed
Fix gcp unit tests
This PR fixes the gcp unit tests and delays the initialization of the cloud resource manager client.
1 parent bc09e46 commit 831d005

File tree

3 files changed

+91
-26
lines changed

3 files changed

+91
-26
lines changed

‎authority/provisioner/gcp.go‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,7 @@ func (p *GCP) Init(config Config) (err error) {
251251
return
252252
}
253253

254-
if p.projectValidator, err = gcp.NewOrganizationValidator(context.Background(), p.ProjectIDs, p.OrganizationID); err != nil {
255-
return
256-
}
257-
254+
p.projectValidator = gcp.NewOrganizationValidator(p.ProjectIDs, p.OrganizationID)
258255
return
259256
}
260257

‎authority/provisioner/gcp/projectvalidator.go‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"context"
55
"net/http"
66

7-
"github.com/smallstep/certificates/errs"
8-
97
"google.golang.org/api/cloudresourcemanager/v1"
8+
9+
"github.com/smallstep/certificates/errs"
1010
)
1111

1212
type ProjectValidator struct {
@@ -29,31 +29,31 @@ func (p *ProjectValidator) ValidateProject(_ context.Context, projectID string)
2929

3030
type OrganizationValidator struct {
3131
*ProjectValidator
32-
33-
OrganizationID string
34-
projectsService *cloudresourcemanager.ProjectsService
32+
OrganizationID string
3533
}
3634

37-
func NewOrganizationValidator(ctx context.Context, projectIDs []string, organizationID string) (*OrganizationValidator, error) {
38-
crm, err := cloudresourcemanager.NewService(ctx)
39-
40-
if err != nil {
41-
return nil, err
42-
}
43-
35+
func NewOrganizationValidator(projectIDs []string, organizationID string) *OrganizationValidator {
4436
return &OrganizationValidator{
4537
&ProjectValidator{projectIDs},
4638
organizationID,
47-
crm.Projects,
48-
}, nil
39+
}
4940
}
5041

5142
func (p *OrganizationValidator) ValidateProject(ctx context.Context, projectID string) error {
5243
if err := p.ProjectValidator.ValidateProject(ctx, projectID); err != nil {
5344
return err
5445
}
5546

56-
ancestry, err := p.projectsService.
47+
if p.OrganizationID == "" {
48+
return nil
49+
}
50+
51+
crm, err := cloudresourcemanager.NewService(ctx)
52+
if err != nil {
53+
return err
54+
}
55+
56+
ancestry, err := crm.Projects.
5757
GetAncestry(projectID, &cloudresourcemanager.GetAncestryRequest{}).
5858
Context(ctx).
5959
Do()

‎authority/provisioner/gcp/projectvalidator_test.go‎

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,84 @@ package gcp
33
import (
44
"context"
55
"testing"
6-
)
76

8-
func TestProjectValidator(t *testing.T) {
9-
validator := &ProjectValidator{ProjectIDs: []string{"allowed-1", "allowed-2"}}
7+
"github.com/stretchr/testify/assert"
8+
"google.golang.org/api/cloudresourcemanager/v1"
9+
)
1010

11-
if err := validator.ValidateProject(context.Background(), "not-allowed"); err == nil {
12-
t.Errorf("ProjectValidator.ValidateProject() = nil, want err")
11+
func TestProjectValidator_ValidateProject(t *testing.T) {
12+
ctx := context.Background()
13+
type fields struct {
14+
ProjectIDs []string
15+
}
16+
type args struct {
17+
in0 context.Context
18+
projectID string
1319
}
20+
tests := []struct {
21+
name string
22+
fields fields
23+
args args
24+
assertion assert.ErrorAssertionFunc
25+
}{
26+
{"allowed-1", fields{[]string{"allowed-1", "allowed-2"}}, args{ctx, "allowed-1"}, assert.NoError},
27+
{"allowed-2", fields{[]string{"allowed-1", "allowed-2"}}, args{ctx, "allowed-2"}, assert.NoError},
28+
{"empty", fields{nil}, args{ctx, "allowed-1"}, assert.NoError},
29+
{"not allowed", fields{[]string{"allowed-1", "allowed-2"}}, args{ctx, "not-allowed"}, assert.Error},
30+
}
31+
for _, tt := range tests {
32+
t.Run(tt.name, func(t *testing.T) {
33+
p := &ProjectValidator{
34+
ProjectIDs: tt.fields.ProjectIDs,
35+
}
36+
tt.assertion(t, p.ValidateProject(tt.args.in0, tt.args.projectID))
37+
})
38+
}
39+
}
1440

15-
if err := validator.ValidateProject(context.Background(), "allowed-2"); err != nil {
16-
t.Errorf("ProjectValidator.ValidateProject() = %v, want nil", err)
41+
func TestNewOrganizationValidator(t *testing.T) {
42+
got := NewOrganizationValidator([]string{"project-1", "project-2"}, "organization")
43+
assert.Equal(t, &OrganizationValidator{
44+
ProjectValidator: &ProjectValidator{ProjectIDs: []string{"project-1", "project-2"}},
45+
OrganizationID: "organization",
46+
}, got)
47+
}
48+
49+
func TestOrganizationValidator_ValidateProject(t *testing.T) {
50+
ctx := context.Background()
51+
_, err := cloudresourcemanager.NewService(ctx)
52+
skip := (err != nil)
53+
54+
type fields struct {
55+
ProjectValidator *ProjectValidator
56+
OrganizationID string
57+
}
58+
type args struct {
59+
ctx context.Context
60+
projectID string
61+
}
62+
tests := []struct {
63+
name string
64+
skip bool
65+
fields fields
66+
args args
67+
assertion assert.ErrorAssertionFunc
68+
}{
69+
{"ok projects", false, fields{&ProjectValidator{ProjectIDs: []string{"allowed"}}, ""}, args{ctx, "allowed"}, assert.NoError},
70+
{"fail projects", false, fields{&ProjectValidator{ProjectIDs: []string{"allowed"}}, "organization"}, args{ctx, "not-allowed"}, assert.Error},
71+
{"fail organization", skip, fields{&ProjectValidator{ProjectIDs: []string{"allowed"}}, "fake-organization"}, args{ctx, "allowed"}, assert.Error},
72+
}
73+
for _, tt := range tests {
74+
t.Run(tt.name, func(t *testing.T) {
75+
p := &OrganizationValidator{
76+
ProjectValidator: tt.fields.ProjectValidator,
77+
OrganizationID: tt.fields.OrganizationID,
78+
}
79+
if tt.skip {
80+
t.SkipNow()
81+
return
82+
}
83+
tt.assertion(t, p.ValidateProject(tt.args.ctx, tt.args.projectID))
84+
})
1785
}
1886
}

0 commit comments

Comments
 (0)