-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
{"baseBranch":"main","baseCommit":"57c758bf428462d2d74149ed0f9311f812de9d36","createdAt":"2023-12-18T16:08:49.869157Z","headSha":"6b7fd100809a245d1bcdcf4c4a98f619a1ae7aeb","id":"ae9c3566-5670-4b46-881a-00980a9c4dae","priority":"200","pullRequestNumber":"21445","queuedAt":"2023-12-18T16:08:49.867012Z","status":"STATUS_QUEUED"}
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
cmd/cws-instrumentation/subcommands/selftestscmd/selftests.go
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 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
//go:build linux | ||
|
||
// Package selftestscmd holds the selftests command of CWS injector | ||
package selftestscmd | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
type execParams struct { | ||
enabled bool | ||
path string | ||
args string | ||
} | ||
|
||
type openParams struct { | ||
enabled bool | ||
path string | ||
} | ||
|
||
type selftestsCliParams struct { | ||
exec execParams | ||
open openParams | ||
} | ||
|
||
// Command returns the commands for the selftests subcommand | ||
func Command() []*cobra.Command { | ||
var params selftestsCliParams | ||
|
||
selftestsCmd := &cobra.Command{ | ||
Use: "selftests", | ||
Short: "run selftests against the tracer", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var err error | ||
if params.exec.enabled { | ||
err = errors.Join(err, selftestExec(¶ms.exec)) | ||
} | ||
if params.open.enabled { | ||
err = errors.Join(err, selftestOpen(¶ms.open)) | ||
} | ||
return err | ||
}, | ||
} | ||
|
||
selftestsCmd.Flags().BoolVar(¶ms.exec.enabled, "exec", false, "run the exec selftest") | ||
selftestsCmd.Flags().StringVar(¶ms.exec.path, "exec.path", "/usr/bin/date", "path to the file to execute") | ||
selftestsCmd.Flags().StringVar(¶ms.exec.args, "exec.args", "", "arguments to pass to the executable") | ||
selftestsCmd.Flags().BoolVar(¶ms.open.enabled, "open", false, "run the open selftest") | ||
selftestsCmd.Flags().StringVar(¶ms.open.path, "open.path", "/tmp/open.test", "path to the file to open") | ||
|
||
return []*cobra.Command{selftestsCmd} | ||
} | ||
|
||
func selftestExec(params *execParams) error { | ||
if params.args != "" { | ||
return exec.Command(params.path, strings.Split(params.args, " ")...).Run() | ||
} | ||
return exec.Command(params.path).Run() | ||
} | ||
|
||
func selftestOpen(params *openParams) error { | ||
f, createErr := os.OpenFile(params.path, os.O_CREATE|os.O_EXCL, 0400) | ||
if createErr != nil { | ||
f, openErr := os.Open(params.path) | ||
if openErr != nil { | ||
return errors.Join(createErr, openErr) | ||
} | ||
return f.Close() | ||
} | ||
return errors.Join(f.Close(), os.Remove(params.path)) | ||
} |
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