-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Node Drain events and Node Events (#8980)
Deployment status updates handle deployment status updates (paused, failed, resume) deployment alloc health
- Loading branch information
1 parent
54b4581
commit 2624098
Showing
12 changed files
with
631 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
package state | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/hashicorp/nomad/helper" | ||
"github.com/hashicorp/nomad/nomad/mock" | ||
"github.com/hashicorp/nomad/nomad/stream" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDeploymentEventFromChanges(t *testing.T) { | ||
t.Parallel() | ||
s := TestStateStoreCfg(t, TestStateStorePublisher(t)) | ||
defer s.StopEventPublisher() | ||
|
||
// setup | ||
setupTx := s.db.WriteTxn(10) | ||
|
||
j := mock.Job() | ||
e := mock.Eval() | ||
e.JobID = j.ID | ||
|
||
d := mock.Deployment() | ||
d.JobID = j.ID | ||
|
||
require.NoError(t, s.upsertJobImpl(10, j, false, setupTx)) | ||
require.NoError(t, s.upsertDeploymentImpl(10, d, setupTx)) | ||
|
||
setupTx.Txn.Commit() | ||
|
||
ctx := context.WithValue(context.Background(), CtxMsgType, structs.DeploymentStatusUpdateRequestType) | ||
|
||
req := &structs.DeploymentStatusUpdateRequest{ | ||
DeploymentUpdate: &structs.DeploymentStatusUpdate{ | ||
DeploymentID: d.ID, | ||
Status: structs.DeploymentStatusPaused, | ||
StatusDescription: structs.DeploymentStatusDescriptionPaused, | ||
}, | ||
Eval: e, | ||
// Exlude Job and assert its added | ||
} | ||
|
||
require.NoError(t, s.UpdateDeploymentStatus(ctx, 100, req)) | ||
|
||
events := WaitForEvents(t, s, 100, 1, 1*time.Second) | ||
require.Len(t, events, 1) | ||
|
||
got := events[0] | ||
require.Equal(t, uint64(100), got.Index) | ||
require.Equal(t, d.ID, got.Key) | ||
|
||
de := got.Payload.(*DeploymentEvent) | ||
require.Equal(t, structs.DeploymentStatusPaused, de.Deployment.Status) | ||
require.Equal(t, j, de.Job) | ||
|
||
} | ||
|
||
func TestDeploymentEventFromChanges_Promotion(t *testing.T) { | ||
t.Parallel() | ||
s := TestStateStoreCfg(t, TestStateStorePublisher(t)) | ||
defer s.StopEventPublisher() | ||
|
||
// setup | ||
setupTx := s.db.WriteTxn(10) | ||
|
||
j := mock.Job() | ||
tg1 := j.TaskGroups[0] | ||
tg2 := tg1.Copy() | ||
tg2.Name = "foo" | ||
j.TaskGroups = append(j.TaskGroups, tg2) | ||
require.NoError(t, s.upsertJobImpl(10, j, false, setupTx)) | ||
|
||
d := mock.Deployment() | ||
d.StatusDescription = structs.DeploymentStatusDescriptionRunningNeedsPromotion | ||
d.JobID = j.ID | ||
d.TaskGroups = map[string]*structs.DeploymentState{ | ||
"web": { | ||
DesiredTotal: 10, | ||
DesiredCanaries: 1, | ||
}, | ||
"foo": { | ||
DesiredTotal: 10, | ||
DesiredCanaries: 1, | ||
}, | ||
} | ||
require.NoError(t, s.upsertDeploymentImpl(10, d, setupTx)) | ||
|
||
// create set of allocs | ||
c1 := mock.Alloc() | ||
c1.JobID = j.ID | ||
c1.DeploymentID = d.ID | ||
d.TaskGroups[c1.TaskGroup].PlacedCanaries = append(d.TaskGroups[c1.TaskGroup].PlacedCanaries, c1.ID) | ||
c1.DeploymentStatus = &structs.AllocDeploymentStatus{ | ||
Healthy: helper.BoolToPtr(true), | ||
} | ||
c2 := mock.Alloc() | ||
c2.JobID = j.ID | ||
c2.DeploymentID = d.ID | ||
d.TaskGroups[c2.TaskGroup].PlacedCanaries = append(d.TaskGroups[c2.TaskGroup].PlacedCanaries, c2.ID) | ||
c2.TaskGroup = tg2.Name | ||
c2.DeploymentStatus = &structs.AllocDeploymentStatus{ | ||
Healthy: helper.BoolToPtr(true), | ||
} | ||
|
||
require.NoError(t, s.upsertAllocsImpl(10, []*structs.Allocation{c1, c2}, setupTx)) | ||
|
||
// commit setup transaction | ||
setupTx.Txn.Commit() | ||
|
||
e := mock.Eval() | ||
// Request to promote canaries | ||
ctx := context.WithValue(context.Background(), CtxMsgType, structs.DeploymentPromoteRequestType) | ||
req := &structs.ApplyDeploymentPromoteRequest{ | ||
DeploymentPromoteRequest: structs.DeploymentPromoteRequest{ | ||
DeploymentID: d.ID, | ||
All: true, | ||
}, | ||
Eval: e, | ||
} | ||
|
||
require.NoError(t, s.UpdateDeploymentPromotion(ctx, 100, req)) | ||
|
||
events := WaitForEvents(t, s, 100, 1, 1*time.Second) | ||
require.Len(t, events, 1) | ||
|
||
got := events[0] | ||
require.Equal(t, uint64(100), got.Index) | ||
require.Equal(t, d.ID, got.Key) | ||
|
||
de := got.Payload.(*DeploymentEvent) | ||
require.Equal(t, structs.DeploymentStatusRunning, de.Deployment.Status) | ||
require.Len(t, de.Allocs, 2) | ||
|
||
} | ||
|
||
func WaitForEvents(t *testing.T, s *StateStore, index uint64, want int, timeout time.Duration) []stream.Event { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
go func() { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-time.After(timeout): | ||
require.Fail(t, "timeout waiting for events") | ||
} | ||
}() | ||
|
||
maxAttempts := 10 | ||
for { | ||
got := EventsForIndex(t, s, index) | ||
if len(got) == want { | ||
return got | ||
} | ||
maxAttempts-- | ||
if maxAttempts == 0 { | ||
require.Fail(t, "reached max attempts waiting for desired event count") | ||
} | ||
time.Sleep(10 * time.Millisecond) | ||
} | ||
} | ||
|
||
func EventsForIndex(t *testing.T, s *StateStore, index uint64) []stream.Event { | ||
pub, err := s.EventPublisher() | ||
require.NoError(t, err) | ||
|
||
sub, err := pub.Subscribe(&stream.SubscribeRequest{ | ||
Topics: map[stream.Topic][]string{ | ||
"*": []string{"*"}, | ||
}, | ||
Index: index, | ||
}) | ||
defer sub.Unsubscribe() | ||
|
||
require.NoError(t, err) | ||
|
||
var events []stream.Event | ||
for { | ||
e, err := sub.NextNoBlock() | ||
require.NoError(t, err) | ||
if e == nil { | ||
break | ||
} | ||
events = append(events, e...) | ||
} | ||
return events | ||
} |
Oops, something went wrong.