Skip to content

Commit

Permalink
Merge pull request kubernetes#50554 from zhangxiaoyu-zidif/refactor-f…
Browse files Browse the repository at this point in the history
…actory-test

Automatic merge from submit-queue (batch tested with PRs 50257, 50247, 50665, 50554, 51077)

Refactor kubectl factory test case with sets.String

**What this PR does / why we need it**:
change to make got and want use sets.String instead, since that is both safe and more clearly shows the intent.

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
  • Loading branch information
Kubernetes Submit Queue authored Aug 23, 2017
2 parents 178a5ff + e83e799 commit 617dc79
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
1 change: 1 addition & 0 deletions pkg/kubectl/cmd/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ go_test(
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/diff:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/validation/field:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/version:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/watch:go_default_library",
Expand Down
33 changes: 12 additions & 21 deletions pkg/kubectl/cmd/util/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/apiserver/pkg/util/flag"
manualfake "k8s.io/client-go/rest/fake"
Expand Down Expand Up @@ -88,22 +89,16 @@ func TestPortsForObject(t *testing.T) {
},
}

expected := []string{"101"}
got, err := f.PortsForObject(pod)
expected := sets.NewString("101")
ports, err := f.PortsForObject(pod)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(expected) != len(got) {
t.Fatalf("Ports size mismatch! Expected %d, got %d", len(expected), len(got))
}

sort.Strings(expected)
sort.Strings(got)
got := sets.NewString(ports...)

for i, port := range got {
if port != expected[i] {
t.Fatalf("Port mismatch! Expected %s, got %s", expected[i], port)
}
if !expected.Equal(got) {
t.Fatalf("Ports mismatch! Expected %v, got %v", expected, got)
}
}

Expand All @@ -130,22 +125,18 @@ func TestProtocolsForObject(t *testing.T) {
},
}

expected := "101/TCP,102/UDP"
expected := sets.NewString("101/TCP", "102/UDP")
protocolsMap, err := f.ProtocolsForObject(pod)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
got := kubectl.MakeProtocols(protocolsMap)
expectedSlice := strings.Split(expected, ",")
gotSlice := strings.Split(got, ",")

sort.Strings(expectedSlice)
sort.Strings(gotSlice)
protocolsString := kubectl.MakeProtocols(protocolsMap)
protocolsStrings := strings.Split(protocolsString, ",")
got := sets.NewString(protocolsStrings...)

for i, protocol := range gotSlice {
if protocol != expectedSlice[i] {
t.Fatalf("Protocols mismatch! Expected %s, got %s", expectedSlice[i], protocol)
}
if !expected.Equal(got) {
t.Fatalf("Protocols mismatch! Expected %v, got %v", expected, got)
}
}

Expand Down

0 comments on commit 617dc79

Please sign in to comment.