Skip to content

Commit

Permalink
add a few failure cases for demo controller in UT
Browse files Browse the repository at this point in the history
  • Loading branch information
WeichengWang1 committed Sep 21, 2023
1 parent d5fd71a commit 124f7e2
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 58 deletions.
2 changes: 1 addition & 1 deletion pkg/controllers/resourceconsist/consister.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (r *Consist) syncEmployer(ctx context.Context, employer client.Object, expe
return false, false, fmt.Errorf("syncDelete failed, err: %s", err.Error())
}

isClean := len(toCudEmployer.Unchanged) == 0 && len(toCudEmployer.ToCreate) == 0 && len(toCudEmployer.ToUpdate) == 0 && len(toCudEmployer.ToDelete) == 0
isClean := len(toCudEmployer.Unchanged) == 0 && len(toCudEmployer.ToCreate) == 0 && len(toCudEmployer.ToUpdate) == 0 && len(failDelete) == 0
cudFailedExist := len(failCreate) > 0 || len(failUpdate) > 0 || len(failDelete) > 0
return isClean, cudFailedExist, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (r *Consist) Reconcile(ctx context.Context, request reconcile.Request) (rec
}
isCleanEmployee, syncEmployeeFailedExist, err := r.syncEmployees(ctx, employer, expectEmployees, currentEmployees)
if err != nil {
r.Logger.Error(err, "sync employees status failed: %s")
r.Logger.Error(err, "sync employees status failed")
r.Recorder.Eventf(employer, corev1.EventTypeWarning, "syncEmployeesFailed",
"sync employees status failed: %s", err.Error())
return reconcile.Result{}, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (r *DemoReconcile) GetExpectedEmployer(ctx context.Context, employer client
}
var expect []IEmployer
expect = append(expect, DemoServiceStatus{
EmployerId: "demo-expect-employer-id",
EmployerId: employer.GetName(),
EmployerStatuses: DemoServiceDetails{
RemoteVIP: "demo-remote-VIP",
RemoteVIPQPS: 100,
Expand Down Expand Up @@ -104,6 +104,10 @@ func (r *DemoReconcile) GetCurrentEmployer(ctx context.Context, employer client.
}

func (r *DemoReconcile) CreateEmployer(ctx context.Context, employer client.Object, toCreates []IEmployer) ([]IEmployer, []IEmployer, error) {
if toCreates == nil || len(toCreates) == 0 {
return toCreates, nil, nil
}

toCreateDemoServiceStatus := make([]DemoServiceStatus, len(toCreates))
for idx, create := range toCreates {
createDemoServiceStatus, ok := create.(DemoServiceStatus)
Expand All @@ -123,6 +127,10 @@ func (r *DemoReconcile) CreateEmployer(ctx context.Context, employer client.Obje
}

func (r *DemoReconcile) UpdateEmployer(ctx context.Context, employer client.Object, toUpdates []IEmployer) ([]IEmployer, []IEmployer, error) {
if toUpdates == nil || len(toUpdates) == 0 {
return toUpdates, nil, nil
}

toUpdateDemoServiceStatus := make([]DemoServiceStatus, len(toUpdates))
for idx, update := range toUpdates {
updateDemoServiceStatus, ok := update.(DemoServiceStatus)
Expand All @@ -142,6 +150,10 @@ func (r *DemoReconcile) UpdateEmployer(ctx context.Context, employer client.Obje
}

func (r *DemoReconcile) DeleteEmployer(ctx context.Context, employer client.Object, toDeletes []IEmployer) ([]IEmployer, []IEmployer, error) {
if toDeletes == nil || len(toDeletes) == 0 {
return toDeletes, nil, nil
}

toDeleteDemoServiceStatus := make([]DemoServiceStatus, len(toDeletes))
for idx, update := range toDeletes {
deleteDemoServiceStatus, ok := update.(DemoServiceStatus)
Expand Down Expand Up @@ -227,6 +239,9 @@ func (r *DemoReconcile) GetCurrentEmployee(ctx context.Context, employer client.
}

func (r *DemoReconcile) CreateEmployees(ctx context.Context, employer client.Object, toCreates []IEmployee) ([]IEmployee, []IEmployee, error) {
if toCreates == nil || len(toCreates) == 0 {
return toCreates, nil, nil
}
toCreateDemoPodStatuses := make([]DemoPodStatus, len(toCreates))

for idx, toCreate := range toCreates {
Expand All @@ -248,6 +263,10 @@ func (r *DemoReconcile) CreateEmployees(ctx context.Context, employer client.Obj
}

func (r *DemoReconcile) UpdateEmployees(ctx context.Context, employer client.Object, toUpdates []IEmployee) ([]IEmployee, []IEmployee, error) {
if toUpdates == nil || len(toUpdates) == 0 {
return toUpdates, nil, nil
}

toUpdateDemoPodStatuses := make([]DemoPodStatus, len(toUpdates))

for idx, toUpdate := range toUpdates {
Expand All @@ -269,6 +288,10 @@ func (r *DemoReconcile) UpdateEmployees(ctx context.Context, employer client.Obj
}

func (r *DemoReconcile) DeleteEmployees(ctx context.Context, employer client.Object, toDeletes []IEmployee) ([]IEmployee, []IEmployee, error) {
if toDeletes == nil || len(toDeletes) == 0 {
return toDeletes, nil, nil
}

toDeleteDemoPodStatuses := make([]DemoPodStatus, len(toDeletes))

for idx, toDelete := range toDeletes {
Expand Down Expand Up @@ -370,13 +393,13 @@ type DemoResourceProviderClient struct {
}

type DemoResourceVipOps struct {
VipStatuses []DemoServiceStatus `json:"vipStatuses,omitempty"`
MockData bool `json:"mockData,omitempty"`
VipStatuses []DemoServiceStatus
MockData bool
}

type DemoResourceRsOps struct {
RsStatuses []DemoPodStatus `json:"rsStatuses,omitempty"`
MockData bool `json:"mockData,omitempty"`
RsStatuses []DemoPodStatus
MockData bool
}

func (d *DemoResourceProviderClient) CreateVip(req *DemoResourceVipOps) (*DemoResourceVipOps, error) {
Expand Down
Loading

0 comments on commit 124f7e2

Please sign in to comment.