Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Nov 25, 2024
1 parent 68f8d2a commit 8322271
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
63 changes: 48 additions & 15 deletions test/e2e/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ limitations under the License.
package e2e

import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os/exec"
"strconv"
"strings"
Expand All @@ -30,23 +33,54 @@ import (
)

func waitResource(ctx context.Context, t *testing.T, kwokctlPath, name, resource, reason string, want, gap, tolerance int) error {
var prev int
ctx, cancel := context.WithCancel(ctx)
defer cancel()

cmd := exec.CommandContext(ctx, kwokctlPath, "--name", name, "kubectl", "get", "--no-headers", "--watch", resource)

outpipe, err := cmd.StdoutPipe()
if err != nil {
return err
}

err = cmd.Start()
if err != nil {
return err
}

uniq := map[string]struct{}{}
prev := 0
got := 0
var latestTime time.Time
reader := bufio.NewReader(outpipe)
for {
if ctx.Err() != nil {
return ctx.Err()
}
cmd := exec.Command(kwokctlPath, "--name", name, "kubectl", "get", "--no-headers", resource) // #nosec G204
output, err := cmd.Output()
line, _, err := reader.ReadLine()
if err != nil {
if err != io.EOF {
return nil
}
return err
}
raw := string(output)
got := strings.Count(raw, reason)

if bytes.Contains(line, []byte(reason)) {
got++
}

uniq[string(line[:bytes.IndexByte(line, byte(' '))])] = struct{}{}

if got == want {
return nil
}
all := strings.Count(raw, "\n")
t.Logf("%s %d/%d => %d, %v\n", resource, got, all, want, time.Now())

if time.Since(latestTime) < time.Second {
continue
}

latestTime = time.Now()

all := len(uniq)

t.Logf("%s %d/%d => %d, %v\n", resource, got, all, want, latestTime)
if prev != 0 && got == prev {
return fmt.Errorf("resource %s not changed", resource)
}
Expand All @@ -60,7 +94,6 @@ func waitResource(ctx context.Context, t *testing.T, kwokctlPath, name, resource
return fmt.Errorf("gap too large for resource %s", resource)
}
}
time.Sleep(1 * time.Second)
}
}

Expand All @@ -87,19 +120,19 @@ func scaleCreatePod(ctx context.Context, t *testing.T, kwokctlPath string, name
return fmt.Errorf("failed to start scale command: %w", err)
}

if err := waitResource(ctx, t, kwokctlPath, name, "Pod", "Running", size, 5, 20); err != nil {
if err := waitResource(ctx, t, kwokctlPath, name, "Pod", "Running", size, 5, 50); err != nil {
return fmt.Errorf("failed to wait for resource: %w", err)
}
return nil
}

func scaleDeletePod(ctx context.Context, t *testing.T, kwokctlPath string, name string, _ int) error {
func scaleDeletePod(ctx context.Context, t *testing.T, kwokctlPath string, name string, size int) error {
scaleCmd := exec.CommandContext(ctx, kwokctlPath, "--name", name, "scale", "pod", "fake-pod", "--replicas", strconv.Itoa(0)) // #nosec G204
if err := scaleCmd.Start(); err != nil {
return fmt.Errorf("failed to start scale command: %w", err)
}

if err := waitResource(ctx, t, kwokctlPath, name, "Pod", "fake-pod-", 0, 0, 0); err != nil {
if err := waitResource(ctx, t, kwokctlPath, name, "Pod", "fake-pod-", size, 0, 0); err != nil {
return fmt.Errorf("failed to wait for resource: %w", err)
}
return nil
Expand All @@ -111,7 +144,7 @@ func scaleCreateNode(ctx context.Context, t *testing.T, kwokctlPath string, name
return fmt.Errorf("failed to start scale command: %w", err)
}

if err := waitResource(ctx, t, kwokctlPath, name, "Node", "Ready", size, 10, 20); err != nil {
if err := waitResource(ctx, t, kwokctlPath, name, "Node", "Ready", size, 10, 50); err != nil {
return fmt.Errorf("failed to wait for resource: %w", err)
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/benchmark_hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func scaleCreatePodWithHack(ctx context.Context, t *testing.T, kwokctlPath strin
return fmt.Errorf("failed to start scale command: %w", err)
}

if err := waitResource(ctx, t, kwokctlPath, name, "Pod", "Running", size, 5, 20); err != nil {
if err := waitResource(ctx, t, kwokctlPath, name, "Pod", "Running", size, 5, 50); err != nil {
return fmt.Errorf("failed to wait for resource: %w", err)
}
return nil
Expand Down Expand Up @@ -106,7 +106,7 @@ func scaleDeletePodWithHack(ctx context.Context, t *testing.T, kwokctlPath strin
return fmt.Errorf("failed to start scale command: %w", err)
}

if err := waitResource(ctx, t, kwokctlPath, name, "Pod", "Running", 0, 5, 10); err != nil {
if err := waitResource(ctx, t, kwokctlPath, name, "Pod", "fake-pod-", size, 5, 10); err != nil {
return fmt.Errorf("failed to wait for resource: %w", err)
}
return nil
Expand Down Expand Up @@ -161,7 +161,7 @@ func scaleCreateNodeWithHack(ctx context.Context, t *testing.T, kwokctlPath stri
return fmt.Errorf("failed to start scale command: %w", err)
}

if err := waitResource(ctx, t, kwokctlPath, name, "Node", "Ready", size, 10, 20); err != nil {
if err := waitResource(ctx, t, kwokctlPath, name, "Node", "Ready", size, 10, 50); err != nil {
return fmt.Errorf("failed to wait for resource: %w", err)
}
return nil
Expand Down

0 comments on commit 8322271

Please sign in to comment.