Skip to content

Commit

Permalink
Merge pull request #1068 from wzshiming/feat/stage-test
Browse files Browse the repository at this point in the history
Add test for stages
  • Loading branch information
wzshiming authored May 13, 2024
2 parents 9fca551 + bdd2b3a commit 3b622ae
Show file tree
Hide file tree
Showing 24 changed files with 734 additions and 94 deletions.
81 changes: 81 additions & 0 deletions hack/test_stage/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Copyright 2024 The Kubernetes Authors.
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 main implements a simple stage tester.
package main

import (
"context"
"fmt"
"os"

"sigs.k8s.io/kwok/pkg/apis/internalversion"
"sigs.k8s.io/kwok/pkg/config"
"sigs.k8s.io/kwok/pkg/tools/stage"
"sigs.k8s.io/kwok/pkg/utils/yaml"
)

func main() {
args := os.Args[1:]
if len(args) < 2 {
fmt.Fprintf(os.Stderr, "usage: %s <resource file> <stage file...>\n", os.Args[0])
os.Exit(1)
}

ctx := context.Background()
err := run(ctx, args[0], args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}

func run(ctx context.Context, resourceFile string, stageFile []string) error {
rio, err := config.LoadUnstructured(resourceFile)
if err != nil {
return err
}
if len(rio) != 1 {
return fmt.Errorf("expected exactly one resource in %s, got %d", resourceFile, len(rio))
}

sio, err := config.Load(ctx, stageFile...)
if err != nil {
return err
}

ue := config.FilterWithoutType[*internalversion.Stage](sio)
if len(ue) != 0 {
return fmt.Errorf("expected only stage, got %d non-stage", len(ue))
}

stages := config.FilterWithType[*internalversion.Stage](sio)
if len(stages) == 0 {
return fmt.Errorf("expected at least one stage, got 0")
}

out, err := stage.TestingStages(ctx, rio[0], stages)
if err != nil {
return err
}

err = yaml.NewEncoder(os.Stdout).Encode(out)
if err != nil {
return err
}

return nil
}
5 changes: 5 additions & 0 deletions hack/update-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ if [[ "${UPDATE_SPELLING:-true}" == "true" ]]; then
"${ROOT_DIR}"/hack/update-spelling.sh || failed+=(spelling)
fi

if [[ "${UPDATE_STAGES:-true}" == "true" ]]; then
echo "[*] Update stages..."
"${ROOT_DIR}"/hack/update-stages.sh || failed+=(stages)
fi

if [[ "${#failed[@]}" != 0 ]]; then
echo "Update failed for: ${failed[*]}"
exit 1
Expand Down
56 changes: 56 additions & 0 deletions hack/update-stages.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash
# Copyright 2024 The Kubernetes Authors.
#
# 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.

set -o errexit
set -o nounset
set -o pipefail

DIR="$(dirname "${BASH_SOURCE[0]}")"

ROOT_DIR="$(realpath "${DIR}/..")"

function update() {
mapfile -t files < <(
find . \
-iname "*.input.yaml"
)

for file in "${files[@]}"; do
update_stage "${file}"
done
}

function update_stage() {
local file="${1}"
local stages=()
local rel_path
local base_dir
local from
base_dir="$(dirname "${file}")"

from="$(grep '# @Stage: ' "${file}" | awk '{print $3}')"
for line in ${from}; do
rel_path="${line}"
stages+=("${base_dir}/${rel_path}")
done

if [[ ${#stages[@]} -eq 0 ]]; then
return
fi

go run ./hack/test_stage "${file}" "${stages[@]}" >"${file%.input.yaml}.output.yaml"
}

cd "${ROOT_DIR}" && update
5 changes: 5 additions & 0 deletions hack/verify-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ if [[ "${VERIFY_SPELLING:-true}" == "true" ]]; then
"${ROOT_DIR}"/hack/verify-spelling.sh || failed+=(spelling)
fi

if [[ "${VERIFY_STAGES:-true}" == "true" ]]; then
echo "[*] Verifying stages..."
"${ROOT_DIR}"/hack/verify-stages.sh || failed+=(stages)
fi

# exit based on verify scripts
if [[ "${#failed[@]}" != 0 ]]; then
echo "Verify failed for: ${failed[*]}"
Expand Down
30 changes: 30 additions & 0 deletions hack/verify-stages.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
# Copyright 2024 The Kubernetes Authors.
#
# 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.

set -o errexit
set -o nounset
set -o pipefail

DIR="$(dirname "${BASH_SOURCE[0]}")"

ROOT_DIR="$(realpath "${DIR}/..")"

function check() {
rm -rf "${ROOT_DIR}"/kustomize/stage/**/testdata/*.output.yaml
"${ROOT_DIR}"/hack/update-stages.sh
git --no-pager diff --exit-code
}

cd "${ROOT_DIR}" && check
5 changes: 5 additions & 0 deletions kustomize/stage/node/fast/testdata/node-pending.input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @Stage: ../node-initialize.yaml
apiVersion: v1
kind: Node
metadata:
name: node-pending
59 changes: 59 additions & 0 deletions kustomize/stage/node/fast/testdata/node-pending.output.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
apiGroup: v1
kind: Node
name: node-pending
stages:
- next:
- data:
status:
addresses:
- address: <NodeIP>
type: InternalIP
- address: <NodeName>
type: Hostname
allocatable:
cpu: 1k
memory: 1Ti
pods: 1M
capacity:
cpu: 1k
memory: 1Ti
pods: 1M
conditions:
- lastHeartbeatTime: <Now>
lastTransitionTime: <Now>
message: kubelet is posting ready status
reason: KubeletReady
status: "True"
type: Ready
- lastHeartbeatTime: <Now>
lastTransitionTime: <Now>
message: kubelet has sufficient memory available
reason: KubeletHasSufficientMemory
status: "False"
type: MemoryPressure
- lastHeartbeatTime: <Now>
lastTransitionTime: <Now>
message: kubelet has no disk pressure
reason: KubeletHasNoDiskPressure
status: "False"
type: DiskPressure
- lastHeartbeatTime: <Now>
lastTransitionTime: <Now>
message: kubelet has sufficient PID available
reason: KubeletHasSufficientPID
status: "False"
type: PIDPressure
- lastHeartbeatTime: <Now>
lastTransitionTime: <Now>
message: RouteController created a route
reason: RouteCreated
status: "False"
type: NetworkUnavailable
daemonEndpoints:
kubeletEndpoint:
Port: <NodePort>
phase: Running
kind: patch
subresource: status
type: application/merge-patch+json
stage: node-initialize
12 changes: 12 additions & 0 deletions kustomize/stage/pod/fast/testdata/pod-pending.input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# @Stage: ../pod-ready.yaml
# @Stage: ../pod-delete.yaml
# @Stage: ../pod-complete.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-pending
spec:
containers:
- name: container
image: image
nodeName: node
34 changes: 34 additions & 0 deletions kustomize/stage/pod/fast/testdata/pod-pending.output.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apiGroup: v1
kind: Pod
name: pod-pending
stages:
- next:
- data:
status:
conditions:
- lastTransitionTime: <Now>
status: "True"
type: Initialized
- lastTransitionTime: <Now>
status: "True"
type: Ready
- lastTransitionTime: <Now>
status: "True"
type: ContainersReady
containerStatuses:
- image: image
name: container
ready: true
restartCount: 0
state:
running:
startedAt: <Now>
hostIP: <NodeIPWith("node")>
initContainerStatuses: null
phase: Running
podIP: <PodIPWith("node", false, "", "pod-pending", "")>
startTime: <Now>
kind: patch
subresource: status
type: application/merge-patch+json
stage: pod-ready
28 changes: 28 additions & 0 deletions kustomize/stage/pod/fast/testdata/pod-running-with-job.input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# @Stage: ../pod-ready.yaml
# @Stage: ../pod-delete.yaml
# @Stage: ../pod-complete.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-running-with-job
ownerReferences:
- apiVersion: batch/v1
kind: Job
name: job
uid: uid
spec:
containers:
- name: container
image: image
nodeName: node
status:
containerStatuses:
- image: image
name: container
ready: true
restartCount: 0
state:
running:
startedAt: <Now>
podIP: 10.0.0.1
phase: Running
24 changes: 24 additions & 0 deletions kustomize/stage/pod/fast/testdata/pod-running-with-job.output.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiGroup: v1
kind: Pod
name: pod-running-with-job
stages:
- next:
- data:
status:
containerStatuses:
- image: image
name: container
ready: false
restartCount: 0
started: false
state:
terminated:
exitCode: 0
finishedAt: <Now>
reason: Completed
startedAt: <Now>
phase: Succeeded
kind: patch
subresource: status
type: application/merge-patch+json
stage: pod-complete
13 changes: 13 additions & 0 deletions kustomize/stage/pod/fast/testdata/pod-terminating.input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @Stage: ../pod-ready.yaml
# @Stage: ../pod-delete.yaml
# @Stage: ../pod-complete.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-terminating
deletionTimestamp: "2006-01-02T15:04:05Z"
spec:
containers:
- name: container
image: image
nodeName: node
7 changes: 7 additions & 0 deletions kustomize/stage/pod/fast/testdata/pod-terminating.output.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiGroup: v1
kind: Pod
name: pod-terminating
stages:
- next:
- kind: delete
stage: pod-delete
Loading

0 comments on commit 3b622ae

Please sign in to comment.