forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubernetes#59490 from agau4779/gce-unittest
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
Showing
8 changed files
with
706 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.