-
Notifications
You must be signed in to change notification settings - Fork 3
/
batch_test.go
180 lines (148 loc) · 4.91 KB
/
batch_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package azurebatch
import (
"context"
"crypto/md5"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
"github.com/Azure/azure-sdk-for-go/services/batch/2017-09-01.6.0/batch"
"github.com/Azure/go-autorest/autorest"
"github.com/virtual-kubelet/virtual-kubelet/node/api"
apiv1 "k8s.io/api/core/v1"
)
func Test_deletePod(t *testing.T) {
podNamespace := "bob"
podName := "marley"
concatName := []byte(fmt.Sprintf("%s-%s", podNamespace, podName))
expectedDeleteTaskID := fmt.Sprintf("%x", md5.Sum(concatName))
provider := Provider{}
provider.deleteTask = func(taskID string) (autorest.Response, error) {
if taskID != expectedDeleteTaskID {
t.Errorf("Deleted wrong task! Expected delete: %v Actual: %v", taskID, expectedDeleteTaskID)
}
return autorest.Response{}, nil
}
pod := &apiv1.Pod{}
pod.Name = podName
pod.Namespace = podNamespace
err := provider.DeletePod(context.Background(), pod)
if err != nil {
t.Error(err)
}
}
func Test_deletePod_doesntExist(t *testing.T) {
pod := &apiv1.Pod{}
pod.Namespace = "bob"
pod.Name = "marley"
provider := Provider{}
provider.deleteTask = func(taskID string) (autorest.Response, error) {
return autorest.Response{}, fmt.Errorf("Task doesn't exist")
}
err := provider.DeletePod(context.Background(), pod)
if err == nil {
t.Error("Expected error but none seen")
}
}
func Test_createPod(t *testing.T) {
pod := &apiv1.Pod{}
pod.Namespace = "bob"
pod.Name = "marley"
provider := Provider{}
provider.addTask = func(task batch.TaskAddParameter) (autorest.Response, error) {
if task.CommandLine == nil || *task.CommandLine == "" {
t.Error("Missing commandline args")
}
derefVars := *task.EnvironmentSettings
if len(derefVars) != 1 || *derefVars[0].Name != podJSONKey {
t.Error("Missing pod json")
}
return autorest.Response{}, nil
}
err := provider.CreatePod(context.Background(), pod)
if err != nil {
t.Errorf("Unexpected error creating pod %v", err)
}
}
func Test_createPod_errorResponse(t *testing.T) {
pod := &apiv1.Pod{}
pod.Namespace = "bob"
pod.Name = "marley"
provider := Provider{}
provider.addTask = func(task batch.TaskAddParameter) (autorest.Response, error) {
return autorest.Response{}, fmt.Errorf("Failed creating task")
}
err := provider.CreatePod(context.Background(), pod)
if err == nil {
t.Error("Expected error but none seen")
}
}
func Test_readLogs_404Response_expectReturnStartupLogs(t *testing.T) {
pod := &apiv1.Pod{}
pod.Namespace = "bob"
pod.Name = "marley"
containerName := "sam"
provider := Provider{}
provider.getFileFromTask = func(taskID, path string) (batch.ReadCloser, error) {
if path == "wd/sam.log" {
// Autorest - Seriously? Can't find a better way to make a 404 :(
return batch.ReadCloser{Response: autorest.Response{Response: &http.Response{StatusCode: 404}}}, nil
} else if path == "stderr.txt" {
response := ioutil.NopCloser(strings.NewReader("stderrResponse"))
return batch.ReadCloser{Value: &response}, nil
} else if path == "stdout.txt" {
response := ioutil.NopCloser(strings.NewReader("stdoutResponse"))
return batch.ReadCloser{Value: &response}, nil
} else {
t.Errorf("Unexpected Filepath: %v", path)
}
return batch.ReadCloser{}, fmt.Errorf("Failed in test mock of getFileFromTask")
}
logs, err := provider.GetContainerLogs(context.Background(), pod.Namespace, pod.Name, containerName, api.ContainerLogOpts{})
if err != nil {
t.Fatalf("GetContainerLogs return error: %v", err)
}
defer logs.Close()
r, err := ioutil.ReadAll(logs)
if err != nil {
t.Fatal(err)
}
result := string(r)
if !strings.Contains(result, "stderrResponse") || !strings.Contains(result, "stdoutResponse") {
t.Errorf("Result didn't contain expected content have: %v", result)
}
}
func Test_readLogs_JsonResponse_expectFormattedLogs(t *testing.T) {
pod := &apiv1.Pod{}
pod.Namespace = "bob"
pod.Name = "marley"
containerName := "sam"
provider := Provider{}
provider.getFileFromTask = func(taskID, path string) (batch.ReadCloser, error) {
if path == "wd/sam.log" {
fileReader, err := os.Open("./testdata/logresponse.json")
if err != nil {
t.Error(err)
}
readCloser := ioutil.NopCloser(fileReader)
return batch.ReadCloser{Value: &readCloser, Response: autorest.Response{Response: &http.Response{StatusCode: 200}}}, nil
}
t.Errorf("Unexpected Filepath: %v", path)
return batch.ReadCloser{}, fmt.Errorf("Failed in test mock of getFileFromTask")
}
logs, err := provider.GetContainerLogs(context.Background(), pod.Namespace, pod.Name, containerName, api.ContainerLogOpts{})
if err != nil {
t.Errorf("GetContainerLogs return error: %v", err)
}
defer logs.Close()
r, err := ioutil.ReadAll(logs)
if err != nil {
t.Fatal(err)
}
result := string(r)
if !strings.Contains(string(result), "Copy output data from the CUDA device to the host memory") || strings.Contains(result, "{") {
t.Errorf("Result didn't contain expected content have or had json: %v", result)
}
}