Skip to content

Commit

Permalink
add test for successCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
YZ775 committed Nov 20, 2024
1 parent 52d692d commit f847d21
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
2 changes: 2 additions & 0 deletions mtest/cke-cluster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ repair:
command_timeout_seconds: 30
need_drain: true
watch_seconds: 30
success_command: ["sh", "-c", "touch /tmp/mtest-repair-success-$1", "success"]
success_command_timeout_seconds: 30
health_check_command: ["sh", "-c", "test -f /tmp/mtest-repair-$1 && echo true", "health_check"]
options:
kube-api:
Expand Down
34 changes: 32 additions & 2 deletions mtest/repair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ func repairShouldNotProceed() {
}).WithTimeout(time.Second * 60).Should(Succeed())
}

func repairSuccessCommandSuccess(node string) {
cmdSuccess := false
for _, host := range []string{host1, host2} {
_, _, err := execAt(host, "docker", "exec", "cke", "test", "-f", "/tmp/mtest-repair-success-"+node)
if err == nil {
cmdSuccess = true
}
}
Expect(cmdSuccess).To(BeTrue())
}

func testRepairOperations() {
// this will run:
// - RepairDrainStartOp
Expand Down Expand Up @@ -110,15 +121,34 @@ func testRepairOperations() {
repairQueueAdd(node1)
waitRepairSuccess(cluster)
nodesShouldBeSchedulable(node1)
repairSuccessCommandSuccess(node1)

ckecliSafe("repair-queue", "delete-finished")
waitRepairEmpty(cluster)

By("setting erroneous success command")
originalSuceessCommand := cluster.Repair.RepairProcedures[0].RepairOperations[0].SuccessCommand
cluster.Repair.RepairProcedures[0].RepairOperations[0].SuccessCommand = []string{"false"}
_, err := ckecliClusterSet(cluster)
Expect(err).NotTo(HaveOccurred())
time.Sleep(time.Second * 3)

repairQueueAdd(node1)
waitRepairFailure(cluster)

ckecliSafe("repair-queue", "delete-finished")
waitRepairEmpty(cluster)

By("restoring success command")
cluster.Repair.RepairProcedures[0].RepairOperations[0].SuccessCommand = originalSuceessCommand
_, err = ckecliClusterSet(cluster)
Expect(err).NotTo(HaveOccurred())
time.Sleep(time.Second * 3)

By("setting erroneous repair command")
originalRepairCommand := cluster.Repair.RepairProcedures[0].RepairOperations[0].RepairSteps[0].RepairCommand

cluster.Repair.RepairProcedures[0].RepairOperations[0].RepairSteps[0].RepairCommand = []string{"false"}
_, err := ckecliClusterSet(cluster)
_, err = ckecliClusterSet(cluster)
Expect(err).NotTo(HaveOccurred())
time.Sleep(time.Second * 3)

Expand Down
29 changes: 12 additions & 17 deletions op/repair_finish.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,19 @@ func repairFinish(ctx context.Context, inf cke.Infrastructure, entry *cke.Repair
return err
}
if op.SuccessCommand != nil {
err := func() error {
ctx := ctx
timeout := cke.DefaultRepairSuccessCommandTimeoutSeconds
if op.SuccessCommandTimeout != nil {
timeout = *op.SuccessCommandTimeout
}
if timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Second*time.Duration(timeout))
defer cancel()
}
args := append(op.SuccessCommand[1:], entry.Address)
command := well.CommandContext(ctx, op.SuccessCommand[0], args...)
return command.Run()
}()
if err != nil {
return err
ctx := ctx
timeout := cke.DefaultRepairSuccessCommandTimeoutSeconds
if op.SuccessCommandTimeout != nil {
timeout = *op.SuccessCommandTimeout
}
if timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Second*time.Duration(timeout))
defer cancel()
}
args := append(op.SuccessCommand[1:], entry.Address)
command := well.CommandContext(ctx, op.SuccessCommand[0], args...)
return command.Run()
}
return nil
}()
Expand Down

0 comments on commit f847d21

Please sign in to comment.