Skip to content

Commit

Permalink
Merge 55876a5 into eafacc4
Browse files Browse the repository at this point in the history
  • Loading branch information
smoliji committed Dec 17, 2020
2 parents eafacc4 + 55876a5 commit 511b01c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -179,6 +180,18 @@ func readCluster(projectID string) (cluster *gcloud.Cluster) {
return
}

type byLength []string

func (s byLength) Len() int {
return len(s)
}
func (s byLength) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s byLength) Less(i, j int) bool {
return len(s[i]) < len(s[j])
}

func filterStrings(options []string, filter string) []string {
if len(filter) == 0 {
return options
Expand All @@ -193,6 +206,7 @@ func filterStrings(options []string, filter string) []string {
results = append(results, option)
}
}
sort.Sort(byLength(results))
return results
}

Expand Down
31 changes: 31 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,34 @@ func ExampleNoPods() {
// K8S Namespace: namespace-1
// Could not find any K8S Pods in namespace namespace-1
}

func TestAutoselectionFixSuffixes(t *testing.T) {
resetFlags()
unmockAll := mockAll(
[]string{"project-1-suffixed", "project-1"},
[]*kubectl.Pod{
{Name: "pod-1-suffixed", ContainerPorts: []int{1}, Containers: []string{"container-1"}},
{Name: "pod-1", ContainerPorts: []int{1}, Containers: []string{"container-1"}},
},
[]*gcloud.Cluster{
{Name: "cluster-1-suffixed", Location: "location-1"},
{Name: "cluster-1", Location: "location-1"},
},
"POD",
[]string{"namespace-1-suffixed", "namespace-1"},
)
defer unmockAll()
os.Args = []string{"goproxie", "-project=project-1", "-pod=pod-1", "-cluster=cluster-1", "-namespace=namespace-1", "-local_port=1234"}
unmockPortForward := mockKubectlPortForward()
main()
calledWith := unmockPortForward()
if calledWith.localPort != 1234 {
t.Errorf("Expected port-forward to be called with localPort=%v, but was called with %v", 1234, calledWith.localPort)
}
if calledWith.podName != "pod-1" {
t.Errorf("Expected port-forward to be called with podName=%v, but was called with %v", 1, calledWith.podName)
}
if calledWith.namespace != "namespace-1" {
t.Errorf("Expected port-forward to be called with namespace=%v, but was called with %v", 1, calledWith.namespace)
}
}

0 comments on commit 511b01c

Please sign in to comment.