Skip to content

Commit

Permalink
Merge pull request #1937 from cyli/fix-force-new-cluster
Browse files Browse the repository at this point in the history
Fix how TestForceNewCluster checks if a service is ready
  • Loading branch information
LK4D4 authored Feb 8, 2017
2 parents ed384f3 + eca7b97 commit 991bf36
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
5 changes: 4 additions & 1 deletion agent/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,10 @@ func (w *worker) taskManager(ctx context.Context, tx *bolt.Tx, task *api.Task) (
}

func (w *worker) newTaskManager(ctx context.Context, tx *bolt.Tx, task *api.Task) (*taskManager, error) {
ctx = log.WithLogger(ctx, log.G(ctx).WithField("task.id", task.ID))
ctx = log.WithLogger(ctx, log.G(ctx).WithFields(logrus.Fields{
"task.id": task.ID,
"service.id": task.ServiceID,
}))

ctlr, status, err := exec.Resolve(ctx, task, w.executor)
if err := w.updateTaskStatus(ctx, tx, task.ID, status); err != nil {
Expand Down
27 changes: 19 additions & 8 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -117,19 +118,29 @@ func pollClusterReady(t *testing.T, c *testCluster, numWorker, numManager int) {
require.NoError(t, err)
}

func pollServiceReady(t *testing.T, c *testCluster, sid string) {
func pollServiceReady(t *testing.T, c *testCluster, sid string, replicas int) {
pollFunc := func() error {
req := &api.ListTasksRequest{}
req := &api.ListTasksRequest{Filters: &api.ListTasksRequest_Filters{
ServiceIDs: []string{sid},
}}
res, err := c.api.ListTasks(context.Background(), req)
require.NoError(t, err)

if len(res.Tasks) == 0 {
return fmt.Errorf("tasks list is empty")
}
var running int
var states []string
for _, task := range res.Tasks {
if task.Status.State != api.TaskStateRunning {
return fmt.Errorf("task %s is not running, status %s", task.ID, task.Status.State)
if task.Status.State == api.TaskStateRunning {
running++
}
states = append(states, fmt.Sprintf("[task %s: %s]", task.ID, task.Status.State))
}
if running != replicas {
return fmt.Errorf("only %d running tasks, but expecting %d replicas: %s", running, replicas, strings.Join(states, ", "))
}

return nil
}
require.NoError(t, raftutils.PollFuncWithTimeout(nil, pollFunc, opsTimeout))
Expand Down Expand Up @@ -177,7 +188,7 @@ func TestServiceCreateLateBind(t *testing.T) {

sid, err := cl.CreateService("test_service", 60)
require.NoError(t, err)
pollServiceReady(t, cl, sid)
pollServiceReady(t, cl, sid, 60)
}

func TestServiceCreate(t *testing.T) {
Expand All @@ -191,7 +202,7 @@ func TestServiceCreate(t *testing.T) {

sid, err := cl.CreateService("test_service", 60)
require.NoError(t, err)
pollServiceReady(t, cl, sid)
pollServiceReady(t, cl, sid, 60)
}

func TestNodeOps(t *testing.T) {
Expand Down Expand Up @@ -490,7 +501,7 @@ func TestForceNewCluster(t *testing.T) {

sid, err := cl.CreateService("test_service", 2)
require.NoError(t, err)
pollServiceReady(t, cl, sid)
pollServiceReady(t, cl, sid, 2)

// generate an expired certificate
rootKey, err := helpers.ParsePrivateKeyPEM(rootCA.Key)
Expand Down Expand Up @@ -519,7 +530,7 @@ func TestForceNewCluster(t *testing.T) {
require.NoError(t, ioutil.WriteFile(managerCertFile, expiredCertPEM, 0644))
require.NoError(t, cl.StartNode(nodeID))
pollClusterReady(t, cl, numWorker, numManager)
pollServiceReady(t, cl, sid)
pollServiceReady(t, cl, sid, 2)

err = raftutils.PollFuncWithTimeout(nil, func() error {
certBytes, err := ioutil.ReadFile(managerCertFile)
Expand Down

0 comments on commit 991bf36

Please sign in to comment.