Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Nov 22, 2024
1 parent 809adfd commit a0c4200
Showing 1 changed file with 70 additions and 20 deletions.
90 changes: 70 additions & 20 deletions test/e2e/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package e2e
import (
"context"
"fmt"
"io"
"os/exec"
"strconv"
"strings"
Expand Down Expand Up @@ -64,30 +65,39 @@ func waitResource(ctx context.Context, t *testing.T, kwokctlPath, name, resource
}
}

func scaleCreatePod(ctx context.Context, t *testing.T, kwokctlPath string, name string, size int) error {
cmd := exec.CommandContext(ctx, kwokctlPath, "--name", name, "kubectl", "get", "node", "-o", "jsonpath={.items.*.metadata.name}") // #nosec G204
out, err := cmd.Output()
if err != nil {
return fmt.Errorf("failed to run command: %w", err)
}
nodeName := ""
nodes := strings.Split(string(out), " ")
for _, node := range nodes {
if strings.Contains(node, "fake-") {
nodeName = node
break
func readerPodYaml(size int) io.Reader {
r, w := io.Pipe()
go func() {
defer w.Close()

Check failure on line 71 in test/e2e/benchmark.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

Error return value of `w.Close` is not checked (errcheck)
for i := 0; i < size; i++ {
_, _ = fmt.Fprintf(w, podYaml, i)
}
}
if nodeName == "" {
return fmt.Errorf("no fake- node found")
}
}()
return r
}

var podYaml = `
apiVersion: v1
kind: Pod
metadata:
name: pod-%d
namespace: default
spec:
containers:
- image: busybox
name: container-0
nodeName: node-0
---
`

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

if err := waitResource(ctx, t, kwokctlPath, name, "Pod", "Running", size, 5, 1); err != nil {
if err := waitResource(ctx, t, kwokctlPath, name, "Pod", "Running", size, 5, 10); err != nil {
return fmt.Errorf("failed to wait for resource: %w", err)
}
return nil
Expand All @@ -105,13 +115,53 @@ func scaleDeletePod(ctx context.Context, t *testing.T, kwokctlPath string, name
return nil
}

func readerNodeYaml(size int) io.Reader {
r, w := io.Pipe()
go func() {
defer w.Close()

Check failure on line 121 in test/e2e/benchmark.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

Error return value of `w.Close` is not checked (errcheck)
for i := 0; i < size; i++ {
_, _ = fmt.Fprintf(w, nodeYaml, i)
}
}()
return r
}

var nodeYaml = `
apiVersion: v1
kind: Node
metadata:
annotations:
kwok.x-k8s.io/node: fake
node.alpha.kubernetes.io/ttl: "0"
labels:
beta.kubernetes.io/arch: amd64
beta.kubernetes.io/os: linux
kubernetes.io/arch: amd64
kubernetes.io/os: linux
kubernetes.io/role: agent
node-role.kubernetes.io/agent: ""
type: kwok
name: node-%d
status:
allocatable:
cpu: "32"
memory: 256Gi
pods: "110"
capacity:
cpu: "32"
memory: 256Gi
pods: "110"
---
`

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

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

0 comments on commit a0c4200

Please sign in to comment.