Skip to content
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

feat: entrypoint #4154

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion api/v1/testkube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3850,7 +3850,11 @@ components:
postRunScript:
type: string
description: script to run after test execution (not supported for container executors)
example: "sleep 30"
example: "sleep 30"
containerEntrypoint:
type: string
description: script to run as an entry point for container executors
example: "cypress run"
runningContext:
$ref: "#/components/schemas/RunningContext"
description: running context for the test execution
Expand Down Expand Up @@ -4354,6 +4358,10 @@ components:
type: string
description: script to run after test execution (not supported for container executors)
example: "sleep 30"
containerEntrypoint:
type: string
description: script to run as an entry point for container executors
example: "cypress run"
scraperTemplate:
type: string
description: scraper template extensions
Expand Down
22 changes: 14 additions & 8 deletions cmd/kubectl-testkube/commands/crds/tests_crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func processPostmanFiles(cmd *cobra.Command, args []string) error {
testEnvs := make(map[string]map[string]string, 0)
testSecretEnvs := make(map[string]map[string]string, 0)

var preRunScript, postRunScript []byte
var preRunScript, postRunScript, containerEntrypoint []byte

err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
Expand Down Expand Up @@ -153,6 +153,11 @@ func processPostmanFiles(cmd *cobra.Command, args []string) error {
postRunScriptBody = fmt.Sprintf("%q", strings.TrimSpace(postRunScriptBody))
}

containerEntrypointBody := string(containerEntrypoint)
if containerEntrypointBody != "" {
containerEntrypointBody = fmt.Sprintf("%q", strings.TrimSpace(containerEntrypointBody))
}

for key, value := range flags.Envs {
if value != "" {
flags.Envs[key] = fmt.Sprintf("%q", value)
Expand All @@ -172,13 +177,14 @@ func processPostmanFiles(cmd *cobra.Command, args []string) error {
}

test.ExecutionRequest = &testkube.ExecutionRequest{
Command: flags.Command,
Args: flags.ExecutorArgs,
ArgsMode: flags.ArgsMode,
Envs: flags.Envs,
Variables: vars,
PreRunScript: preRunScriptBody,
PostRunScript: postRunScriptBody,
Command: flags.Command,
Args: flags.ExecutorArgs,
ArgsMode: flags.ArgsMode,
Envs: flags.Envs,
Variables: vars,
PreRunScript: preRunScriptBody,
PostRunScript: postRunScriptBody,
ContainerEntrypoint: containerEntrypointBody,
}
detectedTests[testName] = test
return nil
Expand Down
28 changes: 28 additions & 0 deletions cmd/kubectl-testkube/commands/tests/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,17 @@ func newExecutionRequestFromFlags(cmd *cobra.Command) (request *testkube.Executi
postRunScriptContent = string(b)
}

containerEntrypoiintContent := ""
containerEntrypoint := cmd.Flag("container-entrypoint").Value.String()
if containerEntrypoint != "" {
b, err := os.ReadFile(containerEntrypoint)
if err != nil {
return nil, err
}

containerEntrypoiintContent = string(b)
}

scraperTemplateContent := ""
scraperTemplate := cmd.Flag("scraper-template").Value.String()
if scraperTemplate != "" {
Expand Down Expand Up @@ -457,6 +468,7 @@ func newExecutionRequestFromFlags(cmd *cobra.Command) (request *testkube.Executi
CronJobTemplate: cronJobTemplateContent,
PreRunScript: preRunScriptContent,
PostRunScript: postRunScriptContent,
ContainerEntrypoint: containerEntrypoiintContent,
ScraperTemplate: scraperTemplateContent,
NegativeTest: negativeTest,
EnvConfigMaps: envConfigMaps,
Expand Down Expand Up @@ -968,6 +980,22 @@ func newExecutionUpdateRequestFromFlags(cmd *cobra.Command) (request *testkube.E
nonEmpty = true
}

if cmd.Flag("container-entrypoint").Changed {
containerEntrypoiintContent := ""
containerEntrypoint := cmd.Flag("container-entrypoint").Value.String()
if containerEntrypoint != "" {
b, err := os.ReadFile(containerEntrypoint)
if err != nil {
return nil, err
}

containerEntrypoiintContent = string(b)
}

request.ContainerEntrypoint = &containerEntrypoiintContent
nonEmpty = true
}

if cmd.Flag("scraper-template").Changed {
scraperTemplateContent := ""
scraperTemplate := cmd.Flag("scraper-template").Value.String()
Expand Down
2 changes: 2 additions & 0 deletions cmd/kubectl-testkube/commands/tests/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type CreateCommonFlags struct {
CronJobTemplate string
PreRunScript string
PostRunScript string
ContainerEntrypoint string
ScraperTemplate string
NegativeTest bool
MountConfigMaps map[string]string
Expand Down Expand Up @@ -210,6 +211,7 @@ func AddCreateFlags(cmd *cobra.Command, flags *CreateCommonFlags) {
cmd.Flags().StringVar(&flags.CronJobTemplate, "cronjob-template", "", "cron job template file path for extensions to cron job template")
cmd.Flags().StringVarP(&flags.PreRunScript, "prerun-script", "", "", "path to script to be run before test execution")
cmd.Flags().StringVarP(&flags.PostRunScript, "postrun-script", "", "", "path to script to be run after test execution")
cmd.Flags().StringVarP(&flags.ContainerEntrypoint, "container-entrypoint", "", "", "path to container entrypoint script to be run instead of default one")
cmd.Flags().StringVar(&flags.ScraperTemplate, "scraper-template", "", "scraper template file path for extensions to scraper template")
cmd.Flags().BoolVar(&flags.NegativeTest, "negative-test", false, "negative test, if enabled, makes failure an expected and correct test result. If the test fails the result will be set to success, and vice versa")
cmd.Flags().StringToStringVarP(&flags.MountConfigMaps, "mount-configmap", "", map[string]string{}, "config map value pair for mounting it to executor pod: --mount-configmap configmap_name=configmap_mountpath")
Expand Down
4 changes: 4 additions & 0 deletions cmd/kubectl-testkube/commands/tests/renderer/test_obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func TestRenderer(ui *ui.UI, obj interface{}) error {
ui.Warn(" Post run script: ", "\n", test.ExecutionRequest.PostRunScript)
}

if test.ExecutionRequest.ContainerEntrypoint != "" {
ui.Warn(" Container entrypoint: ", "\n", test.ExecutionRequest.ContainerEntrypoint)
}

if test.ExecutionRequest.ScraperTemplate != "" {
ui.Warn(" Scraper template: ", "\n", test.ExecutionRequest.ScraperTemplate)
}
Expand Down
10 changes: 10 additions & 0 deletions cmd/kubectl-testkube/commands/tests/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func NewRunTestCmd() *cobra.Command {
gitWorkingDir string
preRunScript string
postRunScript string
containerEntrypoint string
scraperTemplate string
negativeTest bool
mountConfigMaps map[string]string
Expand Down Expand Up @@ -101,6 +102,13 @@ func NewRunTestCmd() *cobra.Command {
postRunScriptContent = string(b)
}

containerEntrypointContent := ""
if containerEntrypoint != "" {
b, err := os.ReadFile(containerEntrypoint)
ui.ExitOnError("reading container entrypoint", err)
containerEntrypointContent = string(b)
}

scraperTemplateContent := ""
if scraperTemplate != "" {
b, err := os.ReadFile(scraperTemplate)
Expand Down Expand Up @@ -131,6 +139,7 @@ func NewRunTestCmd() *cobra.Command {
JobTemplate: jobTemplateContent,
PreRunScriptContent: preRunScriptContent,
PostRunScriptContent: postRunScriptContent,
ContainerEntrypointContent: containerEntrypointContent,
ScraperTemplate: scraperTemplateContent,
IsNegativeTestChangedOnRun: false,
EnvConfigMaps: envConfigMaps,
Expand Down Expand Up @@ -286,6 +295,7 @@ func NewRunTestCmd() *cobra.Command {
cmd.Flags().StringVarP(&gitWorkingDir, "git-working-dir", "", "", "if repository contains multiple directories with tests (like monorepo) and one starting directory we can set working directory parameter")
cmd.Flags().StringVarP(&preRunScript, "prerun-script", "", "", "path to script to be run before test execution")
cmd.Flags().StringVarP(&postRunScript, "postrun-script", "", "", "path to script to be run after test execution")
cmd.Flags().StringVarP(&containerEntrypoint, "container-entrypoint", "", "", "path to container entrypoint script to be run instead of default one")
cmd.Flags().StringVar(&scraperTemplate, "scraper-template", "", "scraper template file path for extensions to scraper template")
cmd.Flags().BoolVar(&negativeTest, "negative-test", false, "negative test, if enabled, makes failure an expected and correct test result. If the test fails the result will be set to success, and vice versa")
cmd.Flags().StringToStringVarP(&mountConfigMaps, "mount-configmap", "", map[string]string{}, "config map value pair for mounting it to executor pod: --mount-configmap configmap_name=configmap_mountpath")
Expand Down
2 changes: 2 additions & 0 deletions cmd/kubectl-testkube/commands/tests/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func NewUpdateTestsCmd() *cobra.Command {
cronJobTemplate string
preRunScript string
postRunScript string
containerEntrypoint string
scraperTemplate string
negativeTest bool
mountConfigMaps map[string]string
Expand Down Expand Up @@ -132,6 +133,7 @@ func NewUpdateTestsCmd() *cobra.Command {
cmd.Flags().StringVar(&cronJobTemplate, "cronjob-template", "", "cron job template file path for extensions to cron job template")
cmd.Flags().StringVarP(&preRunScript, "prerun-script", "", "", "path to script to be run before test execution")
cmd.Flags().StringVarP(&postRunScript, "postrun-script", "", "", "path to script to be run after test execution")
cmd.Flags().StringVarP(&containerEntrypoint, "container-entrypoint", "", "", "path to container entrypoint script to be run instead of default one")
cmd.Flags().StringVar(&scraperTemplate, "scraper-template", "", "scraper template file path for extensions to scraper template")
cmd.Flags().BoolVar(&negativeTest, "negative-test", false, "negative test, if enabled, makes failure an expected and correct test result. If the test fails the result will be set to success, and vice versa")
cmd.Flags().StringToStringVarP(&mountConfigMaps, "mount-configmap", "", map[string]string{}, "config map value pair for mounting it to executor pod: --mount-configmap configmap_name=configmap_mountpath")
Expand Down
2 changes: 1 addition & 1 deletion contrib/executor/init/build/agent/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FROM ubuntu
COPY init /bin/runner
RUN apt-get update
RUN apt-get install ca-certificates git -y
RUN apt-get install ca-certificates git skopeo -y
WORKDIR /root/
RUN git config --global user.name "testkube"
RUN chmod a=rw /root/.gitconfig
Expand Down
19 changes: 19 additions & 0 deletions contrib/executor/init/pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ func (r *InitRunner) Run(ctx context.Context, execution testkube.Execution) (res
return result, errors.Errorf("could not fetch test content: %v", err)
}

if execution.PreRunScript != "" || execution.PostRunScript != "" || execution.ContainerEntrypoint != "" {
output.PrintLogf("%s Creating entrypoint script...", ui.IconWorld)
file := filepath.Join(r.dir, "entrypoint.sh")
scripts := []string{execution.PreRunScript, execution.ContainerEntrypoint, execution.PostRunScript}
var data string
for _, script := range scripts {
data += script
if script != "" {
data += "\n"
}
}

if err = os.WriteFile(file, []byte(data), 0755); err != nil {
output.PrintLogf("%s Could not create entrypoint script %s: %s", ui.IconCross, file, err.Error())
return result, errors.Errorf("could not create entrypoint script %s: %v", file, err)
}
output.PrintLogf("%s Entrypoint script created", ui.IconCheckMark)
}

// TODO: write a proper cloud implementation
// add copy files in case object storage is set
if r.Params.Endpoint != "" && !r.Params.CloudMode {
Expand Down
1 change: 1 addition & 0 deletions docs/docs/cli/testkube_create_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ testkube create test [flags]
--artifact-storage-class-name string artifact storage class name for container executor
--artifact-volume-mount-path string artifact volume mount path for container executor
--command stringArray command passed to image in executor
--container-entrypoint string path to container entrypoint script to be run instead of default one
--copy-files stringArray file path mappings from host to pod of form source:destination
--cronjob-template string cron job template file path for extensions to cron job template
--env stringToString envs in a form of name1=val1 passed to executor (default [])
Expand Down
1 change: 1 addition & 0 deletions docs/docs/cli/testkube_generate_tests-crds.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ testkube generate tests-crds <manifestDirectory> [flags]
--artifact-storage-class-name string artifact storage class name for container executor
--artifact-volume-mount-path string artifact volume mount path for container executor
--command stringArray command passed to image in executor
--container-entrypoint string path to container entrypoint script to be run instead of default one
--copy-files stringArray file path mappings from host to pod of form source:destination
--cronjob-template string cron job template file path for extensions to cron job template
--env stringToString envs in a form of name1=val1 passed to executor (default [])
Expand Down
1 change: 1 addition & 0 deletions docs/docs/cli/testkube_run_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ testkube run test <testName> [flags]
--artifact-volume-mount-path string artifact volume mount path for container executor
--command stringArray command passed to image in executor
--concurrency int concurrency level for multiple test execution (default 10)
--container-entrypoint string path to container entrypoint script to be run instead of default one
--context string running context description for test execution
--copy-files stringArray file path mappings from host to pod of form source:destination
-d, --download-artifacts downlaod artifacts automatically
Expand Down
1 change: 1 addition & 0 deletions docs/docs/cli/testkube_update_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ testkube update test [flags]
--artifact-storage-class-name string artifact storage class name for container executor
--artifact-volume-mount-path string artifact volume mount path for container executor
--command stringArray command passed to image in executor
--container-entrypoint string path to container entrypoint script to be run instead of default one
--copy-files stringArray file path mappings from host to pod of form source:destination
--cronjob-template string cron job template file path for extensions to cron job template
--execution-name string execution name, if empty will be autogenerated
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/joshdk/go-junit v1.0.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/kubeshop/testkube-operator v1.10.8-0.20230706105320-8815bd839764
github.com/kubeshop/testkube-operator v1.10.8-0.20230710121945-7485c5e73eca
github.com/minio/minio-go/v7 v7.0.47
github.com/montanaflynn/stats v0.6.6
github.com/moogar0880/problems v0.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubeshop/testkube-operator v1.10.8-0.20230706105320-8815bd839764 h1:r6JErrRK9Tpzi/JlwAKMeaBlJIkFyZVYtDbd2UA0QP4=
github.com/kubeshop/testkube-operator v1.10.8-0.20230706105320-8815bd839764/go.mod h1:6Rs8MugOzaMcthGzobf6GBlRzbOFiK/GJiuYN6MCfEw=
github.com/kubeshop/testkube-operator v1.10.8-0.20230710121945-7485c5e73eca h1:UggeVVvPvv0nt1mdbUMLcvI1kMAJJ5IYAAJnJLn9B3o=
github.com/kubeshop/testkube-operator v1.10.8-0.20230710121945-7485c5e73eca/go.mod h1:6Rs8MugOzaMcthGzobf6GBlRzbOFiK/GJiuYN6MCfEw=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
Expand Down
1 change: 1 addition & 0 deletions pkg/api/v1/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ type ExecuteTestOptions struct {
ContentRequest *testkube.TestContentRequest
PreRunScriptContent string
PostRunScriptContent string
ContainerEntrypointContent string
ScraperTemplate string
NegativeTest bool
IsNegativeTestChangedOnRun bool
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/v1/client/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func (c TestClient) ExecuteTest(id, executionName string, options ExecuteTestOpt
ContentRequest: options.ContentRequest,
PreRunScript: options.PreRunScriptContent,
PostRunScript: options.PostRunScriptContent,
ContainerEntrypoint: options.ContainerEntrypointContent,
ScraperTemplate: options.ScraperTemplate,
NegativeTest: options.NegativeTest,
IsNegativeTestChangedOnRun: options.IsNegativeTestChangedOnRun,
Expand Down Expand Up @@ -189,6 +190,7 @@ func (c TestClient) ExecuteTests(selector string, concurrencyLevel int, options
ContentRequest: options.ContentRequest,
PreRunScript: options.PreRunScriptContent,
PostRunScript: options.PostRunScriptContent,
ContainerEntrypoint: options.ContainerEntrypointContent,
ScraperTemplate: options.ScraperTemplate,
NegativeTest: options.NegativeTest,
IsNegativeTestChangedOnRun: options.IsNegativeTestChangedOnRun,
Expand Down
6 changes: 4 additions & 2 deletions pkg/api/v1/testkube/model_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type Execution struct {
// script to run before test execution (not supported for container executors)
PreRunScript string `json:"preRunScript,omitempty"`
// script to run after test execution (not supported for container executors)
PostRunScript string `json:"postRunScript,omitempty"`
RunningContext *RunningContext `json:"runningContext,omitempty"`
PostRunScript string `json:"postRunScript,omitempty"`
// script to run as an entry point for container executors
ContainerEntrypoint string `json:"containerEntrypoint,omitempty"`
RunningContext *RunningContext `json:"runningContext,omitempty"`
}
2 changes: 2 additions & 0 deletions pkg/api/v1/testkube/model_execution_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type ExecutionRequest struct {
PreRunScript string `json:"preRunScript,omitempty"`
// script to run after test execution (not supported for container executors)
PostRunScript string `json:"postRunScript,omitempty"`
// script to run as an entry point for container executors
ContainerEntrypoint string `json:"containerEntrypoint,omitempty"`
// scraper template extensions
ScraperTemplate string `json:"scraperTemplate,omitempty"`
// config map references
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/v1/testkube/model_execution_update_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type ExecutionUpdateRequest struct {
PreRunScript *string `json:"preRunScript,omitempty"`
// script to run after test execution (not supported for container executors)
PostRunScript *string `json:"postRunScript,omitempty"`
// script to run as an entry point for container executors
ContainerEntrypoint *string `json:"containerEntrypoint,omitempty"`
// scraper template extensions
ScraperTemplate *string `json:"scraperTemplate,omitempty"`
// config *map references
Expand Down
1 change: 1 addition & 0 deletions pkg/api/v1/testkube/model_test_base_extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (test *Test) QuoteTestTextFields() {
&test.ExecutionRequest.CronJobTemplate,
&test.ExecutionRequest.PreRunScript,
&test.ExecutionRequest.PostRunScript,
&test.ExecutionRequest.ContainerEntrypoint,
&test.ExecutionRequest.ScraperTemplate,
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/api/v1/testkube/model_test_trigger_condition_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ type TestTriggerConditionSpec struct {
Conditions []TestTriggerCondition `json:"conditions,omitempty"`
// duration in seconds the test trigger waits for conditions, until its stopped
Timeout int32 `json:"timeout,omitempty"`
// duration in seconds the test trigger waits between condition check
// duration in seconds the test trigger waits between condition checks
Delay int32 `json:"delay,omitempty"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (test *TestUpsertRequest) QuoteTestTextFields() {
&test.ExecutionRequest.CronJobTemplate,
&test.ExecutionRequest.PreRunScript,
&test.ExecutionRequest.PostRunScript,
&test.ExecutionRequest.ContainerEntrypoint,
&test.ExecutionRequest.ScraperTemplate,
}

Expand Down
Loading
Loading