cloud.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package util
  14. import (
  15. "time"
  16. "github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"
  17. "golang.org/x/oauth2"
  18. "k8s.io/legacy-cloud-providers/gce"
  19. )
  20. const (
  21. // TestProjectID is the project id used for creating NewMockGCECloud
  22. TestProjectID = "test-project"
  23. // TestNetworkProjectID is the network project id for creating NewMockGCECloud
  24. TestNetworkProjectID = "net-test-project"
  25. // TestRegion is the region for creating NewMockGCECloud
  26. TestRegion = "test-region"
  27. // TestZone is the zone for creating NewMockGCECloud
  28. TestZone = "test-zone"
  29. // TestNetworkName is the network name for creating NewMockGCECloud
  30. TestNetworkName = "test-network"
  31. // TestSubnetworkName is the sub network name for creating NewMockGCECloud
  32. TestSubnetworkName = "test-sub-network"
  33. // TestSecondaryRangeName is the secondary range name for creating NewMockGCECloud
  34. TestSecondaryRangeName = "test-secondary-range"
  35. )
  36. type mockTokenSource struct{}
  37. func (*mockTokenSource) Token() (*oauth2.Token, error) {
  38. return &oauth2.Token{
  39. AccessToken: "access",
  40. TokenType: "Bearer",
  41. RefreshToken: "refresh",
  42. Expiry: time.Now().Add(1 * time.Hour),
  43. }, nil
  44. }
  45. // NewMockGCECloud returns a handle to a Cloud instance that is
  46. // served by a mock http server
  47. func NewMockGCECloud(cloud cloud.Cloud) (*gce.Cloud, error) {
  48. config := &gce.CloudConfig{
  49. ProjectID: TestProjectID,
  50. NetworkProjectID: TestNetworkProjectID,
  51. Region: TestRegion,
  52. Zone: TestZone,
  53. ManagedZones: []string{TestZone},
  54. NetworkName: TestNetworkName,
  55. SubnetworkName: TestSubnetworkName,
  56. SecondaryRangeName: TestSecondaryRangeName,
  57. NodeTags: []string{},
  58. UseMetadataServer: false,
  59. TokenSource: &mockTokenSource{},
  60. }
  61. return gce.CreateGCECloudWithCloud(config, cloud)
  62. }