-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnomad_test.go
126 lines (108 loc) · 2.93 KB
/
nomad_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
package nomad
import (
"context"
"fmt"
"os"
"testing"
"github.com/google/uuid"
nomad "github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/testutil"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
// Client provides a client to the Nomad API
type Client struct {
config nomad.Config
}
func TestCreateGetDeletePod(t *testing.T) {
provider, err := makeProvider(t)
if err != nil {
t.Fatal("unable to create mock provider", err)
}
nomadClient, nomadServer := makeClient(t, nil)
defer nomadServer.Stop()
provider.nomadClient = nomadClient
provider.nomadAddress = nomadServer.HTTPAddr
podName := "pod-" + uuid.New().String()
podNamespace := "ns-" + uuid.New().String()
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Namespace: podNamespace,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Name: "nginx",
Image: "nginx",
LivenessProbe: &v1.Probe{
Handler: v1.Handler{
HTTPGet: &v1.HTTPGetAction{
Port: intstr.FromString("8080"),
Path: "/",
},
},
InitialDelaySeconds: 10,
PeriodSeconds: 5,
TimeoutSeconds: 60,
SuccessThreshold: 3,
FailureThreshold: 5,
},
},
},
},
}
// Create pod
err = provider.CreatePod(context.Background(), pod)
if err != nil {
t.Fatal("failed to create pod", err)
}
// Get pod
pod, err = provider.GetPod(context.Background(), podNamespace, podName)
if err != nil {
t.Fatal("failed to get pod", err)
}
// Get pod tests
// Validate pod spec
assert.Check(t, pod != nil, "pod cannot be nil")
assert.Check(t, pod.Spec.Containers != nil, "containers cannot be nil")
assert.Check(t, is.Nil(pod.Annotations), "pod annotations should be nil")
assert.Check(t, is.Equal(pod.Name, fmt.Sprintf("%s-%s", jobNamePrefix, podName)), "pod name should be equal")
// Get pods
pods, err := provider.GetPods(context.Background())
if err != nil {
t.Fatal("failed to get pods", err)
}
// TODO: finish adding a few more assertions
assert.Check(t, is.Len(pods, 1), "number of pods should be 1")
// Delete pod
err = provider.DeletePod(context.Background(), pod)
if err != nil {
t.Fatal("failed to delete pod", err)
}
}
func makeClient(t *testing.T, cb testutil.ServerConfigCallback) (*nomad.Client, *testutil.TestServer) {
// Make client config
conf := nomad.DefaultConfig()
// Create server
server := testutil.NewTestServer(t, cb)
conf.Address = "http://" + server.HTTPAddr
// Create client
client, err := nomad.NewClient(conf)
if err != nil {
t.Fatalf("err: %v", err)
}
return client, server
}
func makeProvider(t *testing.T) (*Provider, error) {
// Set default region
os.Setenv("NOMAD_REGION", "global")
provider, err := NewProvider(nil, "fakeNomadNode", "linux")
if err != nil {
return nil, err
}
return provider, nil
}