Skip to content

Commit

Permalink
Merge pull request kubernetes#59490 from agau4779/gce-unittest
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue (batch tested with PRs 58437, 59490, 55684). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

[GCE] Unit test ExternalLoadBalancer

**What this PR does / why we need it**:

- Unit tests for `pkg/cloudprovider/providers/gce/gce_loadbalancer_external.go`
- Tests creating, updating, and deleting an external LoadBalancer

**Future Improvements**
In order to further test `gce_loadbalancer_external.go`, we should add tests for the following cases:

- Network Tiers - when the current/desired network tier doesn't match, existing resources with the wrong tier should be torn down
- Expect an error when the TargetPool does not exist
- Expect an error when the LoadBalancer Firewall does not exist
- Case when TargetPool needs to be recreated 
- Case when IP needs to be released (calls gce.DeleteRegionAddress)
```release-note
NONE
```
  • Loading branch information
Kubernetes Submit Queue authored Feb 9, 2018
2 parents c179b8a + 5e38d78 commit 0b6a0a6
Show file tree
Hide file tree
Showing 8 changed files with 706 additions and 336 deletions.
2 changes: 2 additions & 0 deletions pkg/cloudprovider/providers/gce/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ go_test(
importpath = "k8s.io/kubernetes/pkg/cloudprovider/providers/gce",
deps = [
"//pkg/cloudprovider:go_default_library",
"//pkg/cloudprovider/providers/gce/cloud:go_default_library",
"//pkg/cloudprovider/providers/gce/cloud/mock:go_default_library",
"//pkg/kubelet/apis:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library",
"//vendor/github.com/stretchr/testify/require:go_default_library",
Expand Down
1 change: 1 addition & 0 deletions pkg/cloudprovider/providers/gce/cloud/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ filegroup(
"//pkg/cloudprovider/providers/gce/cloud/filter:all-srcs",
"//pkg/cloudprovider/providers/gce/cloud/gen:all-srcs",
"//pkg/cloudprovider/providers/gce/cloud/meta:all-srcs",
"//pkg/cloudprovider/providers/gce/cloud/mock:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
Expand Down
636 changes: 318 additions & 318 deletions pkg/cloudprovider/providers/gce/cloud/gen.go

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions pkg/cloudprovider/providers/gce/cloud/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,27 +352,27 @@ type {{.MockWrapType}} struct {
// execution flow of the mock. Return (false, nil, nil) to continue with
// normal mock behavior/ after the hook function executes.
{{- if .GenerateGet}}
GetHook func(m *{{.MockWrapType}}, ctx context.Context, key *meta.Key) (bool, *{{.FQObjectType}}, error)
GetHook func(ctx context.Context, key *meta.Key, m *{{.MockWrapType}}) (bool, *{{.FQObjectType}}, error)
{{- end -}}
{{- if .GenerateList}}
{{- if .KeyIsGlobal}}
ListHook func(m *{{.MockWrapType}}, ctx context.Context, fl *filter.F) (bool, []*{{.FQObjectType}}, error)
ListHook func(ctx context.Context, fl *filter.F, m *{{.MockWrapType}}) (bool, []*{{.FQObjectType}}, error)
{{- end -}}
{{- if .KeyIsRegional}}
ListHook func(m *{{.MockWrapType}}, ctx context.Context, region string, fl *filter.F) (bool, []*{{.FQObjectType}}, error)
ListHook func(ctx context.Context, region string, fl *filter.F, m *{{.MockWrapType}}) (bool, []*{{.FQObjectType}}, error)
{{- end -}}
{{- if .KeyIsZonal}}
ListHook func(m *{{.MockWrapType}}, ctx context.Context, zone string, fl *filter.F) (bool, []*{{.FQObjectType}}, error)
ListHook func(ctx context.Context, zone string, fl *filter.F, m *{{.MockWrapType}}) (bool, []*{{.FQObjectType}}, error)
{{- end}}
{{- end -}}
{{- if .GenerateInsert}}
InsertHook func(m *{{.MockWrapType}}, ctx context.Context, key *meta.Key, obj *{{.FQObjectType}}) (bool, error)
InsertHook func(ctx context.Context, key *meta.Key, obj *{{.FQObjectType}}, m *{{.MockWrapType}}) (bool, error)
{{- end -}}
{{- if .GenerateDelete}}
DeleteHook func(m *{{.MockWrapType}}, ctx context.Context, key *meta.Key) (bool, error)
DeleteHook func(ctx context.Context, key *meta.Key, m *{{.MockWrapType}}) (bool, error)
{{- end -}}
{{- if .AggregatedList}}
AggregatedListHook func(m *{{.MockWrapType}}, ctx context.Context, fl *filter.F) (bool, map[string][]*{{.FQObjectType}}, error)
AggregatedListHook func(ctx context.Context, fl *filter.F, m *{{.MockWrapType}}) (bool, map[string][]*{{.FQObjectType}}, error)
{{- end}}
{{- with .Methods -}}
Expand All @@ -390,7 +390,7 @@ type {{.MockWrapType}} struct {
// Get returns the object from the mock.
func (m *{{.MockWrapType}}) Get(ctx context.Context, key *meta.Key) (*{{.FQObjectType}}, error) {
if m.GetHook != nil {
if intercept, obj, err := m.GetHook(m, ctx, key); intercept {
if intercept, obj, err := m.GetHook(ctx, key, m); intercept {
glog.V(5).Infof("{{.MockWrapType}}.Get(%v, %s) = %+v, %v", ctx, key, obj ,err)
return obj, err
}
Expand Down Expand Up @@ -436,15 +436,15 @@ func (m *{{.MockWrapType}}) List(ctx context.Context, zone string, fl *filter.F)
{{- end}}
if m.ListHook != nil {
{{if .KeyIsGlobal -}}
if intercept, objs, err := m.ListHook(m, ctx, fl); intercept {
if intercept, objs, err := m.ListHook(ctx, fl, m); intercept {
glog.V(5).Infof("{{.MockWrapType}}.List(%v, %v) = [%v items], %v", ctx, fl, len(objs), err)
{{- end -}}
{{- if .KeyIsRegional -}}
if intercept, objs, err := m.ListHook(m, ctx, region, fl); intercept {
if intercept, objs, err := m.ListHook(ctx, region, fl, m); intercept {
glog.V(5).Infof("{{.MockWrapType}}.List(%v, %q, %v) = [%v items], %v", ctx, region, fl, len(objs), err)
{{- end -}}
{{- if .KeyIsZonal -}}
if intercept, objs, err := m.ListHook(m, ctx, zone, fl); intercept {
if intercept, objs, err := m.ListHook(ctx, zone, fl, m); intercept {
glog.V(5).Infof("{{.MockWrapType}}.List(%v, %q, %v) = [%v items], %v", ctx, zone, fl, len(objs), err)
{{- end}}
return objs, err
Expand Down Expand Up @@ -508,7 +508,7 @@ func (m *{{.MockWrapType}}) List(ctx context.Context, zone string, fl *filter.F)
// Insert is a mock for inserting/creating a new object.
func (m *{{.MockWrapType}}) Insert(ctx context.Context, key *meta.Key, obj *{{.FQObjectType}}) error {
if m.InsertHook != nil {
if intercept, err := m.InsertHook(m, ctx, key, obj); intercept {
if intercept, err := m.InsertHook(ctx, key, obj, m); intercept {
glog.V(5).Infof("{{.MockWrapType}}.Insert(%v, %v, %+v) = %v", ctx, key, obj, err)
return err
}
Expand Down Expand Up @@ -548,7 +548,7 @@ func (m *{{.MockWrapType}}) Insert(ctx context.Context, key *meta.Key, obj *{{.F
// Delete is a mock for deleting the object.
func (m *{{.MockWrapType}}) Delete(ctx context.Context, key *meta.Key) error {
if m.DeleteHook != nil {
if intercept, err := m.DeleteHook(m, ctx, key); intercept {
if intercept, err := m.DeleteHook(ctx, key, m); intercept {
glog.V(5).Infof("{{.MockWrapType}}.Delete(%v, %v) = %v", ctx, key, err)
return err
}
Expand Down Expand Up @@ -583,7 +583,7 @@ func (m *{{.MockWrapType}}) Delete(ctx context.Context, key *meta.Key) error {
// AggregatedList is a mock for AggregatedList.
func (m *{{.MockWrapType}}) AggregatedList(ctx context.Context, fl *filter.F) (map[string][]*{{.FQObjectType}}, error) {
if m.AggregatedListHook != nil {
if intercept, objs, err := m.AggregatedListHook(m, ctx, fl); intercept {
if intercept, objs, err := m.AggregatedListHook(ctx, fl, m); intercept {
glog.V(5).Infof("{{.MockWrapType}}.AggregatedList(%v, %v) = [%v items], %v", ctx, fl, len(objs), err)
return objs, err
}
Expand Down Expand Up @@ -632,17 +632,17 @@ func (m *{{.MockWrapType}}) Obj(o *{{.FQObjectType}}) *Mock{{.Service}}Obj {
func (m *{{.MockWrapType}}) {{.FcnArgs}} {
{{- if .IsOperation }}
if m.{{.MockHookName}} != nil {
return m.{{.MockHookName}}(m, ctx, key {{.CallArgs}})
return m.{{.MockHookName}}(ctx, key {{.CallArgs}}, m)
}
return nil
{{- else if .IsGet}}
if m.{{.MockHookName}} != nil {
return m.{{.MockHookName}}(m, ctx, key {{.CallArgs}})
return m.{{.MockHookName}}(ctx, key {{.CallArgs}}, m)
}
return nil, fmt.Errorf("{{.MockHookName}} must be set")
{{- else if .IsPaged}}
if m.{{.MockHookName}} != nil {
return m.{{.MockHookName}}(m, ctx, key {{.CallArgs}}, fl)
return m.{{.MockHookName}}(ctx, key {{.CallArgs}}, fl, m)
}
return nil, nil
{{- end}}
Expand Down
3 changes: 2 additions & 1 deletion pkg/cloudprovider/providers/gce/cloud/meta/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,15 @@ func (m *Method) MockHookName() string {
// MockHook is the definition of the hook function.
func (m *Method) MockHook() string {
args := m.args(m.argsSkip(), false, []string{
fmt.Sprintf("*%s", m.MockWrapType()),
"context.Context",
"*meta.Key",
})
if m.kind == MethodPaged {
args = append(args, "*filter.F")
}

args = append(args, fmt.Sprintf("*%s", m.MockWrapType()))

switch m.kind {
case MethodOperation:
return fmt.Sprintf("%v func(%v) error", m.MockHookName(), strings.Join(args, ", "))
Expand Down
28 changes: 28 additions & 0 deletions pkg/cloudprovider/providers/gce/cloud/mock/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["mock.go"],
importpath = "k8s.io/kubernetes/pkg/cloudprovider/providers/gce/cloud/mock",
visibility = ["//visibility:public"],
deps = [
"//pkg/cloudprovider/providers/gce/cloud:go_default_library",
"//pkg/cloudprovider/providers/gce/cloud/meta:go_default_library",
"//vendor/google.golang.org/api/compute/v1:go_default_library",
"//vendor/google.golang.org/api/googleapi:go_default_library",
],
)

filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)

filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
76 changes: 76 additions & 0 deletions pkg/cloudprovider/providers/gce/cloud/mock/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2017 The Kubernetes Authors.
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
http://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.
*/

// Package mock encapsulates mocks for testing GCE provider functionality.
// These methods are used to override the mock objects' methods in order to
// intercept the standard processing and to add custom logic for test purposes.
//
// // Example usage:
// cloud := cloud.NewMockGCE()
// cloud.MockTargetPools.AddInstanceHook = mock.AddInstanceHook
package mock

import (
"context"
"fmt"
"net/http"

ga "google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
cloud "k8s.io/kubernetes/pkg/cloudprovider/providers/gce/cloud"
"k8s.io/kubernetes/pkg/cloudprovider/providers/gce/cloud/meta"
)

// AddInstanceHook mocks adding a Instance to MockTargetPools
func AddInstanceHook(ctx context.Context, key *meta.Key, req *ga.TargetPoolsAddInstanceRequest, m *cloud.MockTargetPools) error {
pool, err := m.Get(ctx, key)
if err != nil {
return &googleapi.Error{
Code: http.StatusNotFound,
Message: fmt.Sprintf("Key: %s was not found in TargetPools", key.String()),
}
}

for _, instance := range req.Instances {
pool.Instances = append(pool.Instances, instance.Instance)
}

return nil
}

// RemoveInstanceHook mocks removing a Instance from MockTargetPools
func RemoveInstanceHook(ctx context.Context, key *meta.Key, req *ga.TargetPoolsRemoveInstanceRequest, m *cloud.MockTargetPools) error {
pool, err := m.Get(ctx, key)
if err != nil {
return &googleapi.Error{
Code: http.StatusNotFound,
Message: fmt.Sprintf("Key: %s was not found in TargetPools", key.String()),
}
}

for _, instanceToRemove := range req.Instances {
for i, instance := range pool.Instances {
if instanceToRemove.Instance == instance {
// Delete instance from pool.Instances without preserving order
pool.Instances[i] = pool.Instances[len(pool.Instances)-1]
pool.Instances = pool.Instances[:len(pool.Instances)-1]
break
}
}
}

return nil
}
Loading

0 comments on commit 0b6a0a6

Please sign in to comment.