Exemplo n.º 1
0
// TestWorkerAncestry does basic tests on worker parents and children.
func TestWorkerAncestry(t *testing.T) {
	var (
		err                   error
		parent, child, worker coordinate.Worker
		kids                  []coordinate.Worker
	)

	sts := SimpleTestSetup{NamespaceName: "TestWorkerAncestry"}
	sts.SetUp(t)
	defer sts.TearDown(t)

	// start in the middle
	parent, err = sts.Namespace.Worker("parent")
	if !assert.NoError(t, err) {
		return
	}

	worker, err = parent.Parent()
	if assert.NoError(t, err) {
		assert.Nil(t, worker)
	}
	kids, err = parent.Children()
	if assert.NoError(t, err) {
		assert.Empty(t, kids)
	}

	// Create a child
	child, err = sts.Namespace.Worker("child")
	if !assert.NoError(t, err) {
		return
	}
	err = child.SetParent(parent)
	assert.NoError(t, err)

	// this should update the parent metadata
	worker, err = parent.Parent()
	if assert.NoError(t, err) {
		assert.Nil(t, worker)
	}
	kids, err = parent.Children()
	if assert.NoError(t, err) && assert.Len(t, kids, 1) {
		assert.Equal(t, "child", kids[0].Name())
	}

	// and also the child metadata
	worker, err = child.Parent()
	if assert.NoError(t, err) && assert.NotNil(t, worker) {
		assert.Equal(t, "parent", worker.Name())
	}
	kids, err = child.Children()
	if assert.NoError(t, err) {
		assert.Empty(t, kids)
	}
}
Exemplo n.º 2
0
// TestWorkerAdoption hands a child worker to a new parent.
func TestWorkerAdoption(t *testing.T) {
	var (
		err                                 error
		child, oldParent, newParent, worker coordinate.Worker
		kids                                []coordinate.Worker
	)

	sts := SimpleTestSetup{NamespaceName: "TestWorkerAdoption"}
	sts.SetUp(t)
	defer sts.TearDown(t)

	// Create the worker objects
	child, err = sts.Namespace.Worker("child")
	if !assert.NoError(t, err) {
		return
	}
	oldParent, err = sts.Namespace.Worker("old")
	if !assert.NoError(t, err) {
		return
	}
	newParent, err = sts.Namespace.Worker("new")
	if !assert.NoError(t, err) {
		return
	}

	// Set up the original ancestry
	err = child.SetParent(oldParent)
	assert.NoError(t, err)

	// Move it to the new parent
	err = child.SetParent(newParent)
	assert.NoError(t, err)

	// Checks
	worker, err = child.Parent()
	if assert.NoError(t, err) && assert.NotNil(t, worker) {
		assert.Equal(t, "new", worker.Name())
	}
	kids, err = child.Children()
	if assert.NoError(t, err) {
		assert.Empty(t, kids)
	}

	worker, err = oldParent.Parent()
	if assert.NoError(t, err) {
		assert.Nil(t, worker)
	}
	kids, err = oldParent.Children()
	if assert.NoError(t, err) {
		assert.Empty(t, kids)
	}

	worker, err = newParent.Parent()
	if assert.NoError(t, err) {
		assert.Nil(t, worker)
	}
	kids, err = newParent.Children()
	if assert.NoError(t, err) && assert.Len(t, kids, 1) {
		assert.Equal(t, "child", kids[0].Name())
	}
}
Exemplo n.º 3
0
func (api *restAPI) fillWorker(namespace coordinate.Namespace, worker coordinate.Worker, result *restdata.Worker) error {
	err := api.fillWorkerShort(namespace, worker, &result.WorkerShort)
	if err == nil {
		err = buildURLs(api.Router,
			"namespace", namespace.Name(),
			"worker", worker.Name(),
		).
			URL(&result.RequestAttemptsURL, "workerRequestAttempts").
			URL(&result.MakeAttemptURL, "workerMakeAttempt").
			URL(&result.ActiveAttemptsURL, "workerActiveAttempts").
			URL(&result.AllAttemptsURL, "workerAllAttempts").
			URL(&result.ChildAttemptsURL, "workerChildAttempts").
			Error
	}
	var parent coordinate.Worker
	if err == nil {
		parent, err = worker.Parent()
	}
	if err == nil && parent != nil {
		parentName := parent.Name()
		result.Parent = &parentName
		err = buildURLs(api.Router,
			"namespace", namespace.Name(),
			"worker", parent.Name(),
		).
			URL(&result.ParentURL, "worker").
			Error
	}
	var children []coordinate.Worker
	if err == nil {
		children, err = worker.Children()
	}
	if err == nil {
		result.ChildURLs = make([]string, len(children))
		for i, child := range children {
			err = buildURLs(api.Router,
				"namespace", namespace.Name(),
				"worker", child.Name(),
			).
				URL(&result.ChildURLs[i], "worker").
				Error
			if err != nil {
				break
			}
		}
	}
	if err == nil {
		result.Active, err = worker.Active()
	}
	if err == nil {
		result.Mode, err = worker.Mode()
	}
	if err == nil {
		result.Data, err = worker.Data()
	}
	if err == nil {
		result.Expiration, err = worker.Expiration()
	}
	if err == nil {
		result.LastUpdate, err = worker.LastUpdate()
	}
	return err
}