/
consultemplate.go
152 lines (123 loc) · 3.93 KB
/
consultemplate.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
package consultemplate
import (
"time"
capi "github.com/hashicorp/consul/api"
"github.com/hashicorp/nomad/e2e/framework"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/jobspec"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/stretchr/testify/require"
. "github.com/onsi/gomega"
)
type ConsulTemplateTest struct {
framework.TC
jobIds []string
}
func init() {
framework.AddSuites(&framework.TestSuite{
Component: "Consul Template",
CanRunLocal: true,
Consul: true,
Cases: []framework.TestCase{
new(ConsulTemplateTest),
},
})
}
func (tc *ConsulTemplateTest) TestUpdatesRestartTasks(f *framework.F) {
require := require.New(f.T())
g := NewGomegaWithT(f.T())
nomadClient := tc.Nomad()
consulClient := tc.Consul()
// Ensure consultemplatetest does not exist
_, err := consulClient.KV().Delete("consultemplatetest", nil)
require.NoError(err)
// Parse job
job, err := jobspec.ParseFile("consultemplate/input/docker.nomad")
require.Nil(err)
uuid := uuid.Generate()
jobId := helper.StringToPtr("cltp" + uuid[:8])
job.ID = jobId
tc.jobIds = append(tc.jobIds, *jobId)
// Register job
jobs := nomadClient.Jobs()
resp, _, err := jobs.Register(job, nil)
require.Nil(err)
require.NotEmpty(resp.EvalID)
waitForTaskState := func(taskState string) {
g.Eventually(func() string {
allocs, _, _ := jobs.Allocations(*job.ID, false, nil)
if len(allocs) != 1 {
return ""
}
first := allocs[0]
taskState := first.TaskStates["test"]
if taskState == nil {
return ""
}
return taskState.State
}, 5*time.Second, time.Second).Should(Equal(taskState), "Incorrect task state")
}
waitForClientAllocStatus := func(allocState string) {
g.Eventually(func() string {
allocSummaries, _, _ := jobs.Allocations(*job.ID, false, nil)
if len(allocSummaries) != 1 {
return ""
}
alloc, _, _ := nomadClient.Allocations().Info(allocSummaries[0].ID, nil)
if alloc == nil {
return ""
}
return alloc.ClientStatus
}, 5*time.Second, time.Second).Should(Equal(allocState), "Incorrect alloc state")
}
waitForRestartCount := func(count uint64) {
g.Eventually(func() uint64 {
allocs, _, _ := jobs.Allocations(*job.ID, false, nil)
if len(allocs) != 1 {
return 0
}
first := allocs[0]
return first.TaskStates["test"].Restarts
}, 10*time.Second, time.Second).Should(Equal(count), "Incorrect restart count")
}
// Wrap in retry to wait until placement
waitForTaskState(structs.TaskStatePending)
// Client should be pending
waitForClientAllocStatus(structs.AllocClientStatusPending)
// Alloc should have a blocked event
g.Eventually(func() []string {
allocSummaries, _, _ := jobs.Allocations(*job.ID, false, nil)
events := allocSummaries[0].TaskStates["test"].Events
messages := []string{}
for _, event := range events {
messages = append(messages, event.DisplayMessage)
}
return messages
}, 5*time.Second, time.Second).Should(ContainElement(ContainSubstring("kv.block")))
// Insert consultemplatetest
_, err = consulClient.KV().Put(&capi.KVPair{Key: "consultemplatetest", Value: []byte("bar")}, nil)
require.Nil(err)
// Placement should start running
waitForClientAllocStatus(structs.AllocClientStatusRunning)
// Ensure restart count 0 -- we should be going from blocked to running.
waitForRestartCount(0)
// Update consultemplatetest
_, err = consulClient.KV().Put(&capi.KVPair{Key: "consultemplatetest", Value: []byte("baz")}, nil)
require.Nil(err)
// Wrap in retry to wait until restart
waitForRestartCount(1)
}
func (tc *ConsulTemplateTest) AfterEach(f *framework.F) {
nomadClient := tc.Nomad()
consulClient := tc.Consul()
jobs := nomadClient.Jobs()
// Stop all jobs in test
for _, id := range tc.jobIds {
jobs.Deregister(id, true, nil)
}
// Garbage collect
nomadClient.System().GarbageCollect()
// Ensure consultemplatetest does not exist
consulClient.KV().Delete("consultemplatetest", nil)
}