-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make several CLI commands testable (#86)
* Add func to rsolve kubectl-like names to canonical names Signed-off-by: Phil Calcado <phil@buoyant.io> * Refactor API instantiation Signed-off-by: Phil Calcado <phil@buoyant.io> * Make version command testable Signed-off-by: Phil Calcado <phil@buoyant.io> * Make get command testable Signed-off-by: Phil Calcado <phil@buoyant.io> * Add tests for api utils Signed-off-by: Phil Calcado <phil@buoyant.io> * Make stat command testable Signed-off-by: Phil Calcado <phil@buoyant.io> * Make tap command testablë Signed-off-by: Phil Calcado <phil@buoyant.io>
- Loading branch information
Showing
19 changed files
with
1,051 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.