generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1068 from wzshiming/feat/stage-test
Add test for stages
- Loading branch information
Showing
24 changed files
with
734 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
kustomize/stage/node/fast/testdata/node-pending.output.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
kustomize/stage/pod/fast/testdata/pod-running-with-job.input.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
kustomize/stage/pod/fast/testdata/pod-running-with-job.output.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
kustomize/stage/pod/fast/testdata/pod-terminating.input.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
7
kustomize/stage/pod/fast/testdata/pod-terminating.output.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.