Files
virtual-kubelet/providers/azurebatch/batch_status_test.go
Lawrence Gripper d6e8b3daf7 Create a provider to use Azure Batch (#133)
* Started work on provider

* WIP Adding batch provider

* Working basic call into pool client. Need to parameterize the baseurl

* Fixed job creation by manipulating the content-type

* WIP Kicking off containers. Dirty

* [wip] More meat around scheduling simple containers.

* Working on basic task wrapper to co-schedule pods

* WIP on task wrapper

* WIP

* Working pod minimal wrapper for batch

* Integrate pod template code into provider

* Cleaning up

* Move to docker without gpu

* WIP batch integration

* partially working

* Working logs

* Tidy code

* WIP: Testing and readme

* Added readme and terraform deployment for GPU Azure Batch pool.

* Update to enable low priority nodes for gpu

* Fix log formatting bug. Return node logs when container not yet started

* Moved to golang v1.10

* Fix cri test

* Fix up minor docs Issue. Add provider to readme. Add var for vk image.
2018-06-22 16:33:49 -07:00

167 lines
3.6 KiB
Go

package azurebatch
import (
"github.com/Azure/go-autorest/autorest/to"
"reflect"
"testing"
"github.com/Azure/azure-sdk-for-go/services/batch/2017-09-01.6.0/batch"
apiv1 "k8s.io/api/core/v1"
)
func Test_getPodFromTask(t *testing.T) {
type args struct {
task *batch.CloudTask
}
tests := []struct {
name string
task batch.CloudTask
wantPod *apiv1.Pod
wantErr bool
}{
{
name: "SimplePod",
task: batch.CloudTask{
EnvironmentSettings: &[]batch.EnvironmentSetting{
{
Name: to.StringPtr(podJSONKey),
Value: to.StringPtr(`{"metadata":{"creationTimestamp":null},"spec":{"containers":[{"name":"web","image":"nginx:1.12","ports":[{"name":"http","containerPort":80,"protocol":"TCP"}],"resources":{}}]},"status":{}}`),
},
},
},
wantPod: &apiv1.Pod{
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
{
Name: "web",
Image: "nginx:1.12",
Ports: []apiv1.ContainerPort{
{
Name: "http",
Protocol: apiv1.ProtocolTCP,
ContainerPort: 80,
},
},
},
},
},
},
},
{
name: "InvalidJson",
task: batch.CloudTask{
EnvironmentSettings: &[]batch.EnvironmentSetting{
{
Name: to.StringPtr(podJSONKey),
Value: to.StringPtr("---notjson--"),
},
},
},
wantErr: true,
},
{
name: "NilEnvironment",
task: batch.CloudTask{
EnvironmentSettings: nil,
},
wantErr: true,
},
{
name: "NilString",
task: batch.CloudTask{
EnvironmentSettings: &[]batch.EnvironmentSetting{
{
Name: to.StringPtr(podJSONKey),
Value: nil,
},
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotPod, err := getPodFromTask(&tt.task)
if (err != nil) != tt.wantErr {
t.Errorf("getPodFromTask() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotPod, tt.wantPod) {
t.Errorf("getPodFromTask() = %v, want %v", gotPod, tt.wantPod)
}
})
}
}
func Test_convertTaskStatusToPodPhase(t *testing.T) {
type args struct {
t *batch.CloudTask
}
tests := []struct {
name string
task batch.CloudTask
wantPodPhase apiv1.PodPhase
}{
{
name: "PreparingTask",
task: batch.CloudTask{
State: batch.TaskStatePreparing,
},
wantPodPhase: apiv1.PodPending,
},
{
//Active tasks are sitting in a queue waiting for a node
// so maps best to pending state
name: "ActiveTask",
task: batch.CloudTask{
State: batch.TaskStateActive,
},
wantPodPhase: apiv1.PodPending,
},
{
name: "RunningTask",
task: batch.CloudTask{
State: batch.TaskStateRunning,
},
wantPodPhase: apiv1.PodRunning,
},
{
name: "CompletedTask_ExitCode0",
task: batch.CloudTask{
State: batch.TaskStateCompleted,
ExecutionInfo: &batch.TaskExecutionInformation{
ExitCode: to.Int32Ptr(0),
},
},
wantPodPhase: apiv1.PodSucceeded,
},
{
name: "CompletedTask_ExitCode127",
task: batch.CloudTask{
State: batch.TaskStateCompleted,
ExecutionInfo: &batch.TaskExecutionInformation{
ExitCode: to.Int32Ptr(127),
},
},
wantPodPhase: apiv1.PodFailed,
},
{
name: "CompletedTask_nilExecInfo",
task: batch.CloudTask{
State: batch.TaskStateCompleted,
ExecutionInfo: nil,
},
wantPodPhase: apiv1.PodFailed,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotPodPhase := convertTaskStatusToPodPhase(&tt.task); !reflect.DeepEqual(gotPodPhase, tt.wantPodPhase) {
t.Errorf("convertTaskStatusToPodPhase() = %v, want %v", gotPodPhase, tt.wantPodPhase)
}
})
}
}