8000 Add skipIfNoCache to tests by maroshii · Pull Request #4612 · okteto/okteto · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add skipIfNoCache to tests #4612

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

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/test/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,11 @@ func doRun(ctx context.Context, servicesToTest []string, options *Options, ioCtr
Caches: test.Caches,
IgnoreRules: testIgnoreRules,
Artifacts: test.Artifacts,
NoCache: options.NoCache,
Hosts: test.Hosts,
}
if test.SkipIfNoFileChanges && !options.NoCache {
params.CacheInvalidationKey = "const"
}

ioCtrl.Out().Infof("Executing test container '%s'", name)
if err := runner.Run(ctx, params); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/model/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func Test_getStructKeys(t *testing.T) {
"model.StorageResource": {"size", "class"},
"model.Sync": {"folders", "rescanInterval", "compression", "verbose"},
"model.SyncFolder": {"localPath", "remotePath"},
"model.Test": {"image", "context", "commands", "depends_on", "caches", "artifacts", "hosts"},
"model.Test": {"image", "context", "commands", "depends_on", "caches", "artifacts", "hosts", "skipIfNoFileChanges"},
"model.TestCommand": {"name", "command"},
"model.Timeout": {"default", "resources"},
"model.VolumeSpec": {"labels", "annotations", "size", "class"},
Expand Down
15 changes: 8 additions & 7 deletions pkg/model/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import (
)

type Test struct {
Image string `yaml:"image,omitempty"`
Context string `yaml:"context,omitempty"`
Commands []TestCommand `yaml:"commands,omitempty"`
DependsOn []string `yaml:"depends_on,omitempty"`
Caches []string `yaml:"caches,omitempty"`
Artifacts []Artifact `yaml:"artifacts,omitempty"`
Hosts []Host `yaml:"hosts,omitempty"`
Image string `yaml:"image,omitempty"`
Context string `yaml:"context,omitempty"`
Commands []TestCommand `yaml:"commands,omitempty"`
DependsOn []string `yaml:"depends_on,omitempty"`
Caches []string `yaml:"caches,omitempty"`
Artifacts []Artifact `yaml:"artifacts,omitempty"`
Hosts []Host `yaml:"hosts,omitempty"`
SkipIfNoFileChanges bool `yaml:"skipIfNoFileChanges,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should include this field in the JSON schema. Ask @andreafalzetti if you need help to know how to do it

}

type Host struct {
Expand Down
26 changes: 16 additions & 10 deletions pkg/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,13 @@ type Params struct {
ExecutionEnvVars map[string]string
Manifest *model.Manifest
Command string
TemplateName string
DockerfileName string
BaseImage string
// CacheInvalidationKey is the value use to invalidate the cache. Defaults
// to a random value which essentially means no-cache. Setting this to a
// static or known value will reuse the build cache
CacheInvalidationKey string
TemplateName string
DockerfileName string
BaseImage string
// ContextAbsolutePathOverride is the absolute path for the build context. Optional.
// If this values is not defined it will default to the folder location of the
// okteto manifest which is resolved through params.ManifestPathFlag
Expand All @@ -186,8 +190,6 @@ type Params struct {
// UseOktetoDeployIgnoreFile if enabled loads the docker ignore file from an
// .oktetodeployignore file. Disabled by default
UseOktetoDeployIgnoreFile bool

NoCache bool
}

// dockerfileTemplateProperties internal struct with the information needed by the Dockerfile template
Expand Down Expand Up @@ -297,11 +299,15 @@ func (r *Runner) Run(ctx context.Context, params *Params) error {
return err
}

randomNumber, err := rand.Int(rand.Reader, big.NewInt(1000000))
if err != nil {
return err
cacheKey := params.CacheInvalidationKey
if cacheKey == "" {

randomNumber, err := rand.Int(rand.Reader, big.NewInt(1000000))
if err != nil {
return err
}
cacheKey = strconv.Itoa(int(randomNumber.Int64()))
}
cacheKey := strconv.Itoa(int(randomNumber.Int64()))

b, err := yaml.Marshal(params.Deployable)
if err != nil {
Expand All @@ -320,7 +326,7 @@ func (r *Runner) Run(ctx context.Context, params *Params) error {
outputMode = buildCmd.DeployOutputModeOnBuild
}

buildOptions := buildCmd.OptsFromBuildInfoForRemoteDeploy(buildInfo, &types.BuildOptions{OutputMode: outputMode, NoCache: params.NoCache})
buildOptions := buildCmd.OptsFromBuildInfoForRemoteDeploy(buildInfo, &types.BuildOptions{OutputMode: outputMode})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the cache option was not passed to the remote, was this intentionally? we do not use cache for remote?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NoCache had no effect even if passed in. See the implementation of OptsFromBuildInfoForRemoteDeploy:

https://github.com/okteto/okteto/blob/master/pkg/cmd/build/build.go#L327-L335

buildOptions.Manifest = params.Manifest
buildOptions.BuildArgs = append(
buildOptions.BuildArgs,
Expand Down
6 changes: 6 additions & 0 deletions pkg/schema/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ func (test) JSONSchema() *jsonschema.Schema {
Description: "Base image used to run your test.",
})

testProps.Set("skipIfNoFileChanges", &jsonschema.Schema{
Type: &jsonschema.Type{Types: []string{"boolean"}},
Title: "skipIfNoFileChanges",
Description: "Skip the test execution if no files have changed since the last test run. This is useful to avoid running tests when the code hasn't changed.",
})

return &jsonschema.Schema{
Type: &jsonschema.Type{Types: []string{"object"}},
Title: "test",
Expand Down
5 changes: 5 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,11 @@
"description": "Base image used to run your test.",
"title": "image",
"type": "string"
},
"skipIfNoFileChanges": {
"description": "Skip the test execution if no files have changed since the last test run. This is useful to avoid running tests when the code hasn't changed.",
"title": "skipIfNoFileChanges",
"type": "boolean"
}
},
"required": [
Expand Down
Loading
0