-
Notifications
You must be signed in to change notification settings - Fork 70
[BUG] make PicoD execute tests portable #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
krrishrastogi05
wants to merge
1
commit into
volcano-sh:main
Choose a base branch
from
krrishrastogi05:krrish/picod-portable-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+202
−24
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,171 @@ | ||
| /* | ||
| Copyright The Volcano 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 picod | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "runtime" | ||
| "strconv" | ||
| "strings" | ||
| "syscall" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| const testHelperProcessEnv = "GO_WANT_PICOD_HELPER_PROCESS" | ||
|
|
||
| func init() { | ||
| if os.Getenv(testHelperProcessEnv) == "1" { | ||
| picodHelperProcessMain() | ||
| os.Exit(0) | ||
| } | ||
| } | ||
|
|
||
| func testCommand(args ...string) []string { | ||
| exe, err := os.Executable() | ||
| if err != nil { | ||
| exe = os.Args[0] | ||
| } | ||
| command := []string{exe, "--"} | ||
| return append(command, args...) | ||
| } | ||
|
|
||
| func requireSymlink(t *testing.T, oldname, newname string) { | ||
| t.Helper() | ||
| if err := os.Symlink(oldname, newname); err != nil { | ||
| if isWindowsSymlinkPrivilegeError(err) { | ||
| t.Skipf("creating symlinks requires privileges on Windows: %v", err) | ||
| } | ||
| t.Fatalf("create symlink: %v", err) | ||
| } | ||
|
Comment on lines
+49
to
+56
|
||
| } | ||
|
|
||
| func isWindowsSymlinkPrivilegeError(err error) bool { | ||
| if runtime.GOOS != "windows" { | ||
| return false | ||
| } | ||
| var linkErr *os.LinkError | ||
| if errors.As(err, &linkErr) { | ||
| var errno syscall.Errno | ||
| if errors.As(linkErr.Err, &errno) { | ||
| return errno == syscall.Errno(1314) | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func picodHelperProcessArgs() []string { | ||
| args := os.Args | ||
| for len(args) > 0 && args[0] != "--" { | ||
| args = args[1:] | ||
| } | ||
| if len(args) == 0 { | ||
| fmt.Fprintln(os.Stderr, "missing helper separator") | ||
| os.Exit(2) | ||
| } | ||
| args = args[1:] | ||
| if len(args) == 0 { | ||
| fmt.Fprintln(os.Stderr, "missing helper command") | ||
| os.Exit(2) | ||
| } | ||
| return args | ||
| } | ||
|
|
||
| func picodHelperProcessMain() { | ||
| args := picodHelperProcessArgs() | ||
| switch args[0] { | ||
| case "echo": | ||
| fmt.Println(strings.Join(args[1:], " ")) | ||
| case "env": | ||
| helperPrintEnv(args) | ||
| case "echo-env": | ||
| helperEchoEnv(args) | ||
| case "exit": | ||
| os.Exit(helperExitCode(args)) | ||
| case "pwd": | ||
| helperPrintWorkingDirectory() | ||
| case "sleep": | ||
| helperSleep(args) | ||
| case "stderr-exit": | ||
| helperStderrExit(args) | ||
| default: | ||
| fmt.Fprintf(os.Stderr, "unknown helper command: %s\n", args[0]) | ||
| os.Exit(2) | ||
| } | ||
| } | ||
|
|
||
| func helperPrintEnv(args []string) { | ||
| if len(args) != 2 { | ||
| fmt.Fprintln(os.Stderr, "env helper requires variable name") | ||
| os.Exit(2) | ||
| } | ||
| fmt.Println(os.Getenv(args[1])) | ||
| } | ||
|
|
||
| func helperEchoEnv(args []string) { | ||
| values := make([]string, 0, len(args)-1) | ||
| for _, key := range args[1:] { | ||
| values = append(values, os.Getenv(key)) | ||
| } | ||
| fmt.Println(strings.Join(values, " ")) | ||
| } | ||
|
|
||
| func helperExitCode(args []string) int { | ||
| if len(args) != 2 { | ||
| fmt.Fprintln(os.Stderr, "exit helper requires code") | ||
| os.Exit(2) | ||
| } | ||
| code, err := strconv.Atoi(args[1]) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "invalid exit code: %v\n", err) | ||
| os.Exit(2) | ||
| } | ||
| return code | ||
| } | ||
|
|
||
| func helperPrintWorkingDirectory() { | ||
| wd, err := os.Getwd() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "getwd: %v\n", err) | ||
| os.Exit(2) | ||
| } | ||
| fmt.Println(wd) | ||
| } | ||
|
|
||
| func helperSleep(args []string) { | ||
| if len(args) != 2 { | ||
| fmt.Fprintln(os.Stderr, "sleep helper requires duration") | ||
| os.Exit(2) | ||
| } | ||
| duration, err := time.ParseDuration(args[1]) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "invalid duration: %v\n", err) | ||
| os.Exit(2) | ||
| } | ||
| time.Sleep(duration) | ||
| } | ||
|
|
||
| func helperStderrExit(args []string) { | ||
| if len(args) != 3 { | ||
| fmt.Fprintln(os.Stderr, "stderr-exit helper requires message and code") | ||
| os.Exit(2) | ||
| } | ||
| fmt.Fprintln(os.Stderr, args[1]) | ||
| os.Exit(helperExitCode([]string{"exit", args[2]})) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure i understand this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @hzxuzhonghu ,
This is for the child helper process used by the execute tests. The tests re-run the package’s own test binary as the command being executed, and GO_WANT_PICOD_HELPER_PROCESS=1 tells that child process to run the helper logic instead of the normal tests.
I put it in init() so it runs before the Go test runner writes anything to stdout/stderr. That keeps the captured output clean for assertions like exact resp.Stdout checks.
I’ve added a short comment above this block to make the intent clearer. If you’d prefer another helper-test pattern here, I’m happy to adjust.