mirror of https://github.com/linkerd/linkerd2.git
100 lines
2.9 KiB
Go
100 lines
2.9 KiB
Go
package util
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/runconduit/conduit/pkg/k8s"
|
|
)
|
|
|
|
func TestBuildStatSummaryRequest(t *testing.T) {
|
|
t.Run("Maps Kubernetes friendly names to canonical names", func(t *testing.T) {
|
|
expectations := map[string]string{
|
|
"deployments": k8s.KubernetesDeployments,
|
|
"deployment": k8s.KubernetesDeployments,
|
|
"deploy": k8s.KubernetesDeployments,
|
|
"pods": k8s.KubernetesPods,
|
|
"pod": k8s.KubernetesPods,
|
|
"po": k8s.KubernetesPods,
|
|
}
|
|
|
|
for friendly, canonical := range expectations {
|
|
statSummaryRequest, err := BuildStatSummaryRequest(
|
|
StatSummaryRequestParams{
|
|
ResourceType: friendly,
|
|
},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error from BuildStatSummaryRequest [%s => %s]: %s", friendly, canonical, err)
|
|
}
|
|
if statSummaryRequest.Selector.Resource.Type != canonical {
|
|
t.Fatalf("Unexpected resource type from BuildStatSummaryRequest [%s => %s]: %s", friendly, canonical, statSummaryRequest.Selector.Resource.Type)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("Parses valid time windows", func(t *testing.T) {
|
|
expectations := []string{
|
|
"1m",
|
|
"60s",
|
|
"1m",
|
|
}
|
|
|
|
for _, timeWindow := range expectations {
|
|
statSummaryRequest, err := BuildStatSummaryRequest(
|
|
StatSummaryRequestParams{
|
|
TimeWindow: timeWindow,
|
|
ResourceType: k8s.KubernetesDeployments,
|
|
},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error from BuildStatSummaryRequest [%s => %s]", timeWindow, err)
|
|
}
|
|
if statSummaryRequest.TimeWindow != timeWindow {
|
|
t.Fatalf("Unexpected TimeWindow from BuildStatSummaryRequest [%s => %s]", timeWindow, statSummaryRequest.TimeWindow)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("Rejects invalid time windows", func(t *testing.T) {
|
|
expectations := map[string]string{
|
|
"1": "time: missing unit in duration 1",
|
|
"s": "time: invalid duration s",
|
|
}
|
|
|
|
for timeWindow, msg := range expectations {
|
|
_, err := BuildStatSummaryRequest(
|
|
StatSummaryRequestParams{
|
|
TimeWindow: timeWindow,
|
|
},
|
|
)
|
|
if err == nil {
|
|
t.Fatalf("BuildStatSummaryRequest(%s) unexpectedly succeeded, should have returned %s", timeWindow, msg)
|
|
}
|
|
if err.Error() != msg {
|
|
t.Fatalf("BuildStatSummaryRequest(%s) should have returned: %s but got unexpected message: %s", timeWindow, msg, err)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("Rejects invalid Kubernetes resource types", func(t *testing.T) {
|
|
expectations := map[string]string{
|
|
"foo": "cannot find Kubernetes canonical name from friendly name [foo]",
|
|
"": "cannot find Kubernetes canonical name from friendly name []",
|
|
}
|
|
|
|
for input, msg := range expectations {
|
|
_, err := BuildStatSummaryRequest(
|
|
StatSummaryRequestParams{
|
|
ResourceType: input,
|
|
},
|
|
)
|
|
if err == nil {
|
|
t.Fatalf("BuildStatSummaryRequest(%s) unexpectedly succeeded, should have returned %s", input, msg)
|
|
}
|
|
if err.Error() != msg {
|
|
t.Fatalf("BuildStatSummaryRequest(%s) should have returned: %s but got unexpected message: %s", input, msg, err)
|
|
}
|
|
}
|
|
})
|
|
}
|