Skip to content

Commit c5759ab

Browse files
authored
Merge pull request kubernetes#98787 from smarterclayton/pass_zones
test/e2e: Allow zones to be passed to e2e cloud provider
2 parents f5fb1c9 + 9830cc9 commit c5759ab

File tree

5 files changed

+176
-2
lines changed

5 files changed

+176
-2
lines changed

staging/src/k8s.io/component-base/cli/flag/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ go_test(
1515
"map_string_bool_test.go",
1616
"map_string_string_test.go",
1717
"namedcertkey_flag_test.go",
18+
"string_slice_flag_test.go",
1819
],
1920
embed = [":go_default_library"],
2021
deps = ["//vendor/github.com/spf13/pflag:go_default_library"],
@@ -36,6 +37,7 @@ go_library(
3637
"omitempty.go",
3738
"sectioned.go",
3839
"string_flag.go",
40+
"string_slice_flag.go",
3941
"tristate.go",
4042
],
4143
importmap = "k8s.io/kubernetes/vendor/k8s.io/component-base/cli/flag",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package flag
18+
19+
import (
20+
goflag "flag"
21+
"strings"
22+
23+
"github.com/spf13/pflag"
24+
)
25+
26+
// StringSlice implements goflag.Value and plfag.Value,
27+
// and allows set to be invoked repeatedly to accumulate values.
28+
type StringSlice struct {
29+
value *[]string
30+
changed bool
31+
}
32+
33+
func NewStringSlice(s *[]string) *StringSlice {
34+
return &StringSlice{value: s}
35+
}
36+
37+
var _ goflag.Value = &StringSlice{}
38+
var _ pflag.Value = &StringSlice{}
39+
40+
func (s *StringSlice) String() string {
41+
return strings.Join(*s.value, " ")
42+
}
43+
44+
func (s *StringSlice) Set(val string) error {
45+
if s.value == nil || !s.changed {
46+
*s.value = make([]string, 0)
47+
}
48+
*s.value = append(*s.value, val)
49+
s.changed = true
50+
return nil
51+
}
52+
53+
func (StringSlice) Type() string {
54+
return "sliceString"
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package flag
18+
19+
import (
20+
"fmt"
21+
"reflect"
22+
"strings"
23+
"testing"
24+
25+
"github.com/spf13/pflag"
26+
)
27+
28+
func TestStringSlice(t *testing.T) {
29+
tests := []struct {
30+
args []string
31+
def []string
32+
expected []string
33+
parseError string
34+
changed bool
35+
}{
36+
{
37+
args: []string{},
38+
expected: nil,
39+
},
40+
{
41+
args: []string{"a"},
42+
expected: []string{"a"},
43+
changed: true,
44+
},
45+
{
46+
args: []string{"a", "b"},
47+
expected: []string{"a", "b"},
48+
changed: true,
49+
},
50+
{
51+
def: []string{"a"},
52+
args: []string{"a", "b"},
53+
expected: []string{"a", "b"},
54+
changed: true,
55+
},
56+
{
57+
def: []string{"a", "b"},
58+
args: []string{"a", "b"},
59+
expected: []string{"a", "b"},
60+
changed: true,
61+
},
62+
}
63+
for i, test := range tests {
64+
fs := pflag.NewFlagSet("testStringSlice", pflag.ContinueOnError)
65+
var s []string
66+
s = append(s, test.def...)
67+
68+
v := NewStringSlice(&s)
69+
fs.Var(v, "slice", "usage")
70+
71+
args := []string{}
72+
for _, a := range test.args {
73+
args = append(args, fmt.Sprintf("--slice=%s", a))
74+
}
75+
76+
err := fs.Parse(args)
77+
if test.parseError != "" {
78+
if err == nil {
79+
t.Errorf("%d: expected error %q, got nil", i, test.parseError)
80+
} else if !strings.Contains(err.Error(), test.parseError) {
81+
t.Errorf("%d: expected error %q, got %q", i, test.parseError, err)
82+
}
83+
} else if err != nil {
84+
t.Errorf("%d: expected nil error, got %v", i, err)
85+
}
86+
if !reflect.DeepEqual(s, test.expected) {
87+
t.Errorf("%d: expected %+v, got %+v", i, test.expected, s)
88+
}
89+
if v.changed != test.changed {
90+
t.Errorf("%d: expected %t got %t", i, test.changed, v.changed)
91+
}
92+
}
93+
}

test/e2e/framework/providers/gce/gce.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package gce
1919
import (
2020
"context"
2121
"fmt"
22+
"math/rand"
2223
"net/http"
2324
"os/exec"
2425
"regexp"
@@ -47,6 +48,21 @@ func factory() (framework.ProviderInterface, error) {
4748
framework.Logf("Fetching cloud provider for %q\r", framework.TestContext.Provider)
4849
zone := framework.TestContext.CloudConfig.Zone
4950
region := framework.TestContext.CloudConfig.Region
51+
allowedZones := framework.TestContext.CloudConfig.Zones
52+
53+
// ensure users don't specify a zone outside of the requested zones
54+
if len(zone) > 0 && len(allowedZones) > 0 {
55+
var found bool
56+
for _, allowedZone := range allowedZones {
57+
if zone == allowedZone {
58+
found = true
59+
break
60+
}
61+
}
62+
if !found {
63+
return nil, fmt.Errorf("the provided zone %q must be included in the list of allowed zones %v", zone, allowedZones)
64+
}
65+
}
5066

5167
var err error
5268
if region == "" {
@@ -59,6 +75,9 @@ func factory() (framework.ProviderInterface, error) {
5975
if !framework.TestContext.CloudConfig.MultiZone {
6076
managedZones = []string{zone}
6177
}
78+
if len(allowedZones) > 0 {
79+
managedZones = allowedZones
80+
}
6281

6382
gceCloud, err := gcecloud.CreateGCECloud(&gcecloud.CloudConfig{
6483
APIEndpoint: framework.TestContext.CloudConfig.APIEndpoint,
@@ -79,7 +98,10 @@ func factory() (framework.ProviderInterface, error) {
7998
return nil, fmt.Errorf("Error building GCE/GKE provider: %v", err)
8099
}
81100

82-
// Arbitrarily pick one of the zones we have nodes in
101+
// Arbitrarily pick one of the zones we have nodes in, looking at prepopulated zones first.
102+
if framework.TestContext.CloudConfig.Zone == "" && len(managedZones) > 0 {
103+
framework.TestContext.CloudConfig.Zone = managedZones[rand.Intn(len(managedZones))]
104+
}
83105
if framework.TestContext.CloudConfig.Zone == "" && framework.TestContext.CloudConfig.MultiZone {
84106
zones, err := gceCloud.GetAllZonesFromCloudProvider()
85107
if err != nil {

test/e2e/framework/test_context.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ type NodeTestContextType struct {
229229
type CloudConfig struct {
230230
APIEndpoint string
231231
ProjectID string
232-
Zone string // for multizone tests, arbitrarily chosen zone
232+
Zone string // for multizone tests, arbitrarily chosen zone
233+
Zones []string // for multizone tests, use this set of zones instead of querying the cloud provider. Must include Zone.
233234
Region string
234235
MultiZone bool
235236
MultiMaster bool
@@ -339,6 +340,7 @@ func RegisterClusterFlags(flags *flag.FlagSet) {
339340
flags.StringVar(&cloudConfig.APIEndpoint, "gce-api-endpoint", "", "The GCE APIEndpoint being used, if applicable")
340341
flags.StringVar(&cloudConfig.ProjectID, "gce-project", "", "The GCE project being used, if applicable")
341342
flags.StringVar(&cloudConfig.Zone, "gce-zone", "", "GCE zone being used, if applicable")
343+
flags.Var(cliflag.NewStringSlice(&cloudConfig.Zones), "gce-zones", "The set of zones to use in a multi-zone test instead of querying the cloud provider.")
342344
flags.StringVar(&cloudConfig.Region, "gce-region", "", "GCE region being used, if applicable")
343345
flags.BoolVar(&cloudConfig.MultiZone, "gce-multizone", false, "If true, start GCE cloud provider with multizone support.")
344346
flags.BoolVar(&cloudConfig.MultiMaster, "gce-multimaster", false, "If true, the underlying GCE/GKE cluster is assumed to be multi-master.")

0 commit comments

Comments
 (0)