Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(task): add ut for task (pingcap#201)
Browse files Browse the repository at this point in the history
Signed-off-by: liubo02 <liubo02@pingcap.com>
liubog2008 authored Dec 16, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 7d67729 commit c063e8e
Showing 8 changed files with 793 additions and 184 deletions.
10 changes: 5 additions & 5 deletions pkg/controllers/pd/builder.go
Original file line number Diff line number Diff line change
@@ -25,23 +25,23 @@ func (r *Reconciler) NewRunner(ctx *tasks.ReconcileContext, reporter task.TaskRe
// get pd
common.TaskContextPD(ctx, r.Client),
// if it's deleted just return
task.NewSwitchTask(common.CondPDHasBeenDeleted(ctx)),
task.IfBreak(common.CondPDHasBeenDeleted(ctx)),

// get info from pd
tasks.TaskContextInfoFromPD(ctx, r.PDClientManager),
task.NewSwitchTask(common.CondPDIsDeleting(ctx),
task.IfBreak(common.CondPDIsDeleting(ctx),
tasks.TaskFinalizerDel(ctx, r.Client),
),

// get cluster and check whether it's paused
common.TaskContextCluster(ctx, r.Client),
task.NewSwitchTask(
task.IfBreak(
common.CondClusterIsPaused(ctx),
),

// get pod and check whether the cluster is suspending
common.TaskContextPod(ctx, r.Client),
task.NewSwitchTask(
task.IfBreak(
common.CondClusterIsSuspending(ctx),
tasks.TaskFinalizerAdd(ctx, r.Client),
common.TaskSuspendPod(ctx, r.Client),
@@ -55,7 +55,7 @@ func (r *Reconciler) NewRunner(ctx *tasks.ReconcileContext, reporter task.TaskRe
tasks.TaskPVC(ctx, r.Logger, r.Client, r.VolumeModifier),
tasks.TaskPod(ctx, r.Logger, r.Client),
// If pd client has not been registered yet, do not update status of the pd
task.NewSwitchTask(tasks.CondPDClientIsNotRegisterred(ctx),
task.IfBreak(tasks.CondPDClientIsNotRegisterred(ctx),
tasks.TaskStatusUnknown(),
),
tasks.TaskStatus(ctx, r.Logger, r.Client),
4 changes: 2 additions & 2 deletions pkg/controllers/pd/tasks/pvc.go
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ import (
)

func TaskPVC(ctx *ReconcileContext, logger logr.Logger, c client.Client, vm volumes.Modifier) task.Task {
return task.NameTaskFunc("PVC", task.TaskFunc(func() task.Result {
return task.NameTaskFunc("PVC", func() task.Result {
pvcs := newPVCs(ctx.PD)
if wait, err := volumes.SyncPVCs(ctx, c, pvcs, vm, logger); err != nil {
return task.Fail().With("failed to sync pvcs: %v", err)
@@ -38,7 +38,7 @@ func TaskPVC(ctx *ReconcileContext, logger logr.Logger, c client.Client, vm volu
// TODO: check config updation

return task.Complete().With("pvcs are synced")
}))
})
}

func newPVCs(pd *v1alpha1.PD) []*corev1.PersistentVolumeClaim {
38 changes: 25 additions & 13 deletions pkg/utils/task/v3/result.go
Original file line number Diff line number Diff line change
@@ -57,12 +57,12 @@ type Result interface {

type NamedResult interface {
Result
Name() string
name() string
}

type AggregateResult interface {
Result
Results() []Result
results() []Result
}

// WithMessage defines an interface to set message into task result
@@ -127,32 +127,38 @@ func Wait() WithMessage {

type namedResult struct {
Result
name string
n string
}

func AnnotateName(name string, r Result) Result {
if _, ok := r.(AggregateResult); ok {
return r
}
func nameResult(name string, r Result) Result {
return &namedResult{
Result: r,
name: name,
n: name,
}
}

func (r *namedResult) Name() string {
return r.name
func (r *namedResult) name() string {
return r.n
}

type aggregateResult struct {
rs []Result
}

func NewAggregateResult(rs ...Result) AggregateResult {
return &aggregateResult{rs: rs}
func newAggregate(rs ...Result) AggregateResult {
var nrs []Result
for _, r := range rs {
ar, ok := r.(AggregateResult)
if ok {
nrs = append(nrs, ar.results()...)
} else {
nrs = append(nrs, r)
}
}
return &aggregateResult{rs: nrs}
}

func (r *aggregateResult) Results() []Result {
func (r *aggregateResult) results() []Result {
return r.rs
}

@@ -195,7 +201,13 @@ func (r *aggregateResult) RequeueAfter() time.Duration {
func (r *aggregateResult) Message() string {
sb := strings.Builder{}
for _, res := range r.rs {
named, ok := res.(NamedResult)
if ok {
sb.WriteString(named.name())
sb.WriteString(": ")
}
sb.WriteString(res.Message())
sb.WriteString("\n")
}

return sb.String()
169 changes: 169 additions & 0 deletions pkg/utils/task/v3/result_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// Copyright 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package task

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestAggregateResult(t *testing.T) {
cases := []struct {
desc string
rs []Result
expectedResults []Result
expectedStatus Status
expectedRequeueAfter time.Duration
expectedMessage string
}{
{
desc: "no result",
rs: nil,
expectedResults: nil,
expectedStatus: SComplete,
expectedRequeueAfter: 0,
expectedMessage: "",
},
{
desc: "only complete result",
rs: []Result{
Complete().With("success"),
Complete().With("success"),
},
expectedResults: []Result{
Complete().With("success"),
Complete().With("success"),
},
expectedStatus: SComplete,
expectedRequeueAfter: 0,
expectedMessage: "success\nsuccess\n",
},
{
desc: "contains a fail result",
rs: []Result{
Complete().With("success"),
Retry(1).With("retry"),
Wait().With("wait"),
Fail().With("fail"),
},
expectedResults: []Result{
Complete().With("success"),
Retry(1).With("retry"),
Wait().With("wait"),
Fail().With("fail"),
},
expectedStatus: SFail,
expectedRequeueAfter: 1,
expectedMessage: "success\nretry(requeue after 1ns)\nwait\nfail\n",
},
{
desc: "contains a retry result and no fail result",
rs: []Result{
Complete().With("success"),
Retry(1).With("retry"),
Wait().With("wait"),
},
expectedResults: []Result{
Complete().With("success"),
Retry(1).With("retry"),
Wait().With("wait"),
},
expectedStatus: SRetry,
expectedRequeueAfter: 1,
expectedMessage: "success\nretry(requeue after 1ns)\nwait\n",
},
{
desc: "contains two retry results",
rs: []Result{
Retry(1).With("retry"),
Retry(2).With("retry"),
},
expectedResults: []Result{
Retry(1).With("retry"),
Retry(2).With("retry"),
},
expectedStatus: SRetry,
expectedRequeueAfter: 2,
expectedMessage: "retry(requeue after 1ns)\nretry(requeue after 2ns)\n",
},
{
desc: "contains a wait result and no fail and retry result",
rs: []Result{
Complete().With("success"),
Wait().With("wait"),
Complete().With("success"),
},
expectedResults: []Result{
Complete().With("success"),
Wait().With("wait"),
Complete().With("success"),
},
expectedStatus: SWait,
expectedRequeueAfter: 0,
expectedMessage: "success\nwait\nsuccess\n",
},
{
desc: "contains an aggregate result",
rs: []Result{
Complete().With("success"),
newAggregate(
Wait().With("wait"),
Complete().With("success"),
),
},
expectedResults: []Result{
Complete().With("success"),
Wait().With("wait"),
Complete().With("success"),
},
expectedStatus: SWait,
expectedRequeueAfter: 0,
expectedMessage: "success\nwait\nsuccess\n",
},
{
desc: "contains a named result",
rs: []Result{
nameResult("aaa", Complete().With("success")),
newAggregate(
Wait().With("wait"),
Complete().With("success"),
),
},
expectedResults: []Result{
nameResult("aaa", Complete().With("success")),
Wait().With("wait"),
Complete().With("success"),
},
expectedStatus: SWait,
expectedRequeueAfter: 0,
expectedMessage: "aaa: success\nwait\nsuccess\n",
},
}

for i := range cases {
c := &cases[i]
t.Run(c.desc, func(tt *testing.T) {
tt.Parallel()

ar := newAggregate(c.rs...)
assert.Equal(tt, c.expectedResults, ar.results(), c.desc)
assert.Equal(tt, c.expectedStatus, ar.Status(), c.desc)
assert.Equal(tt, c.expectedRequeueAfter, ar.RequeueAfter(), c.desc)
assert.Equal(tt, c.expectedMessage, ar.Message(), c.desc)
})
}
}
Loading

0 comments on commit c063e8e

Please sign in to comment.