-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make several CLI commands testable #86
Changes from all commits
9977e5f
837ac6a
ed97b07
c38f78f
0dca1a4
1b6b280
da97dc6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
pb "github.com/runconduit/conduit/controller/gen/public" | ||
) | ||
|
||
func TestGetPods(t *testing.T) { | ||
t.Run("Returns names of existing pods if everything went ok", func(t *testing.T) { | ||
mockClient := &mockApiClient{} | ||
|
||
pods := []*pb.Pod{ | ||
{Name: "pod-a"}, | ||
{Name: "pod-b"}, | ||
{Name: "pod-c"}, | ||
} | ||
|
||
expectedPodNames := []string{ | ||
"pod-a", | ||
"pod-b", | ||
"pod-c", | ||
} | ||
response := &pb.ListPodsResponse{ | ||
Pods: pods, | ||
} | ||
|
||
mockClient.listPodsResponseToReturn = response | ||
actualPodNames, err := getPods(mockClient) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
|
||
for i, actualName := range actualPodNames { | ||
expectedName := expectedPodNames[i] | ||
if expectedName != actualName { | ||
t.Fatalf("Expected %dth element on %v to be [%s], but was [%s]", i, actualPodNames, expectedName, actualName) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("Returns empty list if no [ods found", func(t *testing.T) { | ||
mockClient := &mockApiClient{} | ||
|
||
mockClient.listPodsResponseToReturn = &pb.ListPodsResponse{ | ||
Pods: []*pb.Pod{}, | ||
} | ||
|
||
actualPodNames, err := getPods(mockClient) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
|
||
if len(actualPodNames) != 0 { | ||
t.Fatalf("Expecting no pod names, got %v", actualPodNames) | ||
} | ||
}) | ||
|
||
t.Run("Returns error if cant find pods in API", func(t *testing.T) { | ||
mockClient := &mockApiClient{} | ||
mockClient.errorToReturn = errors.New("expected") | ||
|
||
_, err := getPods(mockClient) | ||
if err == nil { | ||
t.Fatalf("Expecting error, got noting") | ||
} | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"os" | ||
|
||
"github.com/runconduit/conduit/cli/k8s" | ||
"github.com/runconduit/conduit/cli/shell" | ||
"github.com/runconduit/conduit/controller/api/public" | ||
pb "github.com/runconduit/conduit/controller/gen/public" | ||
"github.com/spf13/cobra" | ||
|
@@ -34,12 +33,7 @@ func addControlPlaneNetworkingArgs(cmd *cobra.Command) { | |
cmd.PersistentFlags().StringVar(&apiAddr, "api-addr", "", "Override kubeconfig and communicate directly with the control plane at host:port (mostly for testing)") | ||
} | ||
|
||
func newApiClient() (pb.ApiClient, error) { | ||
kubeApi, err := k8s.MakeK8sAPi(shell.MakeUnixShell(), kubeconfigPath, apiAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
func newApiClient(kubeApi k8s.KubernetesApi) (pb.ApiClient, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. curious: was this change to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was trying to be consistent with the way we create other objects. With this PR we have every command instantiating the objects it needs and wiring them together. Arguably this function should not be in the root command but rather follow the pattern established elsewhere and be something like |
||
url, err := kubeApi.UrlFor(controlPlaneNamespace, "/services/http:api:http/proxy/") | ||
if err != nil { | ||
return nil, err | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[ods