2019-08-23 21:26:46 +08:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/goodrain/rainbond/worker/server/pb"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
2019-09-01 10:30:14 +08:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2019-08-23 21:26:46 +08:00
|
|
|
)
|
|
|
|
|
2019-09-01 10:30:14 +08:00
|
|
|
func eventsFromJSONFile(t *testing.T, filename string) *corev1.EventList {
|
|
|
|
jsonfile, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read file '%s': %v", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result corev1.EventList
|
|
|
|
if err := json.Unmarshal(jsonfile, &result); err != nil {
|
|
|
|
t.Fatalf("file: %s; failed to unmarshalling json: %v", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2019-08-23 21:26:46 +08:00
|
|
|
func TestDescribePodStatus(t *testing.T) {
|
|
|
|
tests := []struct {
|
2019-09-01 10:30:14 +08:00
|
|
|
name, podfilename, eventsfilename string
|
|
|
|
expstatus pb.PodStatus_Type
|
2019-08-23 21:26:46 +08:00
|
|
|
}{
|
2019-09-01 10:30:14 +08:00
|
|
|
{name: "insufficient-memory", podfilename: "testdata/insufficient-memory.json", expstatus: pb.PodStatus_SCHEDULING},
|
|
|
|
{name: "containercreating", podfilename: "testdata/containercreating.json", expstatus: pb.PodStatus_NOTREADY},
|
|
|
|
{name: "crashloopbackoff", podfilename: "testdata/crashloopbackoff.json", expstatus: pb.PodStatus_ABNORMAL},
|
|
|
|
{name: "initiating", podfilename: "testdata/initiating.json", expstatus: pb.PodStatus_INITIATING},
|
|
|
|
{name: "notready", podfilename: "testdata/notready.json", expstatus: pb.PodStatus_NOTREADY},
|
|
|
|
{name: "liveness", podfilename: "testdata/liveness.json", eventsfilename: "testdata/livenessprobefailed.json", expstatus: pb.PodStatus_UNHEALTHY},
|
|
|
|
{name: "readiness", podfilename: "testdata/readiness.json", eventsfilename: "testdata/readinessprobefailed.json", expstatus: pb.PodStatus_UNHEALTHY},
|
2020-03-09 17:44:42 +08:00
|
|
|
{name: "initc-notready-mainc-ready", podfilename: "testdata/initc-notready-mainc-ready.json", expstatus: pb.PodStatus_RUNNING},
|
2019-08-23 21:26:46 +08:00
|
|
|
}
|
|
|
|
for idx := range tests {
|
|
|
|
tc := tests[idx]
|
2019-09-01 10:30:14 +08:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
jsonfile, err := ioutil.ReadFile(tc.podfilename)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to read file '%s': %v", tc.podfilename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var pod corev1.Pod
|
|
|
|
if err := json.Unmarshal(jsonfile, &pod); err != nil {
|
|
|
|
t.Fatalf("file: %s; failed to unmarshalling json: %v", tc.podfilename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
listEventsByPodFunc := func(clientset kubernetes.Interface, pod *corev1.Pod) *corev1.EventList {
|
|
|
|
if tc.eventsfilename != "" {
|
|
|
|
events := eventsFromJSONFile(t, tc.eventsfilename)
|
|
|
|
return events
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
podStatus := &pb.PodStatus{}
|
|
|
|
DescribePodStatus(nil, &pod, podStatus, listEventsByPodFunc)
|
|
|
|
if podStatus.Type != tc.expstatus {
|
|
|
|
t.Errorf("Expected %s for pod status type, but returned %s", tc.expstatus.String(), podStatus.Type.String())
|
|
|
|
}
|
|
|
|
})
|
2019-08-23 21:26:46 +08:00
|
|
|
}
|
|
|
|
}
|