-
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.
[SEC-11866] cws-instrumentation: add selftests command (#21445)
[SEC-11866] cws-instrumentation: add selftests command
- 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