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

Adding workspace to tests. #5677

Merged
merged 1 commit into from
Apr 15, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions docs/content/en/schemas/v2beta14.json
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,8 @@
"type": "string"
},
"type": "array",
"description": "should be set to the file dependencies for this command, so that the skaffold file watcher knows when to retest and perform file synchronization.",
"x-intellij-html-description": "should be set to the file dependencies for this command, so that the skaffold file watcher knows when to retest and perform file synchronization.",
"description": "locates the file dependencies for the command relative to workspace. Paths should be set to the file dependencies for this command, so that the skaffold file watcher knows when to retest and perform file synchronization.",
"x-intellij-html-description": "locates the file dependencies for the command relative to workspace. Paths should be set to the file dependencies for this command, so that the skaffold file watcher knows when to retest and perform file synchronization.",
"default": "[]",
"examples": [
"[\"src/test/**\"]"
Expand Down Expand Up @@ -3005,6 +3005,12 @@
"image"
],
"properties": {
"context": {
"type": "string",
"description": "directory containing the test sources.",
"x-intellij-html-description": "directory containing the test sources.",
"default": "."
},
"custom": {
"items": {
"$ref": "#/definitions/CustomTest"
Expand Down Expand Up @@ -3036,6 +3042,7 @@
},
"preferredOrder": [
"image",
"context",
"custom",
"structureTests"
],
Expand Down
10 changes: 10 additions & 0 deletions pkg/skaffold/schema/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func Set(c *latest.SkaffoldConfig) error {
setDefaultAddress(pf)
}

setDefaultTestWorkspace(c)
return nil
}

Expand Down Expand Up @@ -384,3 +385,12 @@ func setDefaultArtifactDependencyAlias(d *latest.ArtifactDependency) {
d.Alias = d.ImageName
}
}

func setDefaultTestWorkspace(c *latest.SkaffoldConfig) {
for _, tc := range c.Test {
if tc == nil {
continue
}
tc.Workspace = valueOrDefault(tc.Workspace, ".")
}
}
9 changes: 7 additions & 2 deletions pkg/skaffold/schema/latest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,10 @@ type TestCase struct {
// For example: `gcr.io/k8s-skaffold/example`.
ImageName string `yaml:"image" yamltags:"required"`

// Workspace is the directory containing the test sources.
// Defaults to `.`.
Workspace string `yaml:"context,omitempty" skaffold:"filepath"`

// CustomTests lists the set of custom tests to run after an artifact is built.
CustomTests []CustomTest `yaml:"custom,omitempty"`

Expand Down Expand Up @@ -1046,13 +1050,14 @@ type CustomTestDependencies struct {
// Command represents a command that skaffold executes to obtain dependencies. The output of this command *must* be a valid JSON array.
Command string `yaml:"command,omitempty" yamltags:"oneOf=dependency"`

// Paths locates the file dependencies for the command relative to workspace.
// Paths should be set to the file dependencies for this command, so that the skaffold file watcher knows when to retest and perform file synchronization.
// For example: `["src/test/**"]`
Paths []string `yaml:"paths,omitempty" yamltags:"oneOf=dependency" skaffold:"filepath"`
Paths []string `yaml:"paths,omitempty" yamltags:"oneOf=dependency"`

// Ignore specifies the paths that should be ignored by skaffold's file watcher. If a file exists in both `paths` and in `ignore`, it will be ignored, and will be excluded from both retest and file synchronization.
// Will only work in conjunction with `paths`.
Ignore []string `yaml:"ignore,omitempty" skaffold:"filepath"`
Ignore []string `yaml:"ignore,omitempty"`
}

// DockerfileDependency *beta* is used to specify a custom build artifact that is built from a Dockerfile. This allows skaffold to determine dependencies from the Dockerfile.
Expand Down
20 changes: 10 additions & 10 deletions pkg/skaffold/test/custom/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ var (
const Windows string = "windows"

type Runner struct {
customTest latest.CustomTest
imageName string
testWorkingDir string
customTest latest.CustomTest
imageName string
workspace string
}

// New creates a new custom.Runner.
func New(cfg docker.Config, imageName string, wd string, ct latest.CustomTest) (*Runner, error) {
func New(cfg docker.Config, imageName string, ws string, ct latest.CustomTest) (*Runner, error) {
return &Runner{
imageName: imageName,
customTest: ct,
testWorkingDir: wd,
imageName: imageName,
customTest: ct,
workspace: ws,
}, nil
}

Expand Down Expand Up @@ -146,7 +146,7 @@ func (ct *Runner) TestDependencies() ([]string, error) {
return deps, nil

case test.Dependencies.Paths != nil:
return list.Files(ct.testWorkingDir, test.Dependencies.Paths, test.Dependencies.Ignore)
return list.Files(ct.workspace, test.Dependencies.Paths, test.Dependencies.Ignore)
}
}
return nil, nil
Expand All @@ -169,7 +169,7 @@ func (ct *Runner) retrieveCmd(ctx context.Context, out io.Writer, command string
}
cmd.Env = env

dir, err := testContext(ct.testWorkingDir)
dir, err := testContext(ct.workspace)
if err != nil {
return nil, fmt.Errorf("getting context for test: %w", err)
}
Expand All @@ -179,7 +179,7 @@ func (ct *Runner) retrieveCmd(ctx context.Context, out io.Writer, command string
}

func (ct *Runner) getEnv(imageTag string) ([]string, error) {
testContext, err := testContext(ct.testWorkingDir)
testContext, err := testContext(ct.workspace)
if err != nil {
return nil, fmt.Errorf("getting absolute path for test context: %w", err)
}
Expand Down
31 changes: 15 additions & 16 deletions pkg/skaffold/test/custom/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ func TestNewCustomTestRunner(t *testing.T) {

testCase := &latest.TestCase{
ImageName: "image",
Workspace: tmpDir.Root(),
CustomTests: []latest.CustomTest{custom},
}

cfg := &mockConfig{
workingDir: tmpDir.Root(),
tests: []*latest.TestCase{testCase},
tests: []*latest.TestCase{testCase},
}
testEvent.InitializeState([]latest.Pipeline{{}})

testRunner, err := New(cfg, testCase.ImageName, cfg.workingDir, custom)
testRunner, err := New(cfg, testCase.ImageName, testCase.Workspace, custom)
t.CheckNoError(err)
err = testRunner.Test(context.Background(), ioutil.Discard, "image:tag")

Expand Down Expand Up @@ -110,16 +110,16 @@ func TestCustomCommandError(t *testing.T) {

testCase := &latest.TestCase{
ImageName: "image",
Workspace: tmpDir.Root(),
CustomTests: []latest.CustomTest{test.custom},
}

cfg := &mockConfig{
workingDir: tmpDir.Root(),
tests: []*latest.TestCase{testCase},
tests: []*latest.TestCase{testCase},
}
testEvent.InitializeState([]latest.Pipeline{{}})

testRunner, err := New(cfg, testCase.ImageName, cfg.workingDir, test.custom)
testRunner, err := New(cfg, testCase.ImageName, testCase.Workspace, test.custom)
t.CheckNoError(err)
err = testRunner.Test(context.Background(), ioutil.Discard, "image:tag")

Expand All @@ -145,12 +145,12 @@ func TestTestDependenciesCommand(t *testing.T) {

testCase := &latest.TestCase{
ImageName: "image",
Workspace: tmpDir.Root(),
CustomTests: []latest.CustomTest{custom},
}

cfg := &mockConfig{
workingDir: tmpDir.Root(),
tests: []*latest.TestCase{testCase},
tests: []*latest.TestCase{testCase},
}
testEvent.InitializeState([]latest.Pipeline{{}})

Expand All @@ -167,7 +167,7 @@ func TestTestDependenciesCommand(t *testing.T) {
}

expected := []string{"file1", "file2", "file3"}
testRunner, err := New(cfg, testCase.ImageName, cfg.workingDir, custom)
testRunner, err := New(cfg, testCase.ImageName, testCase.Workspace, custom)
t.CheckNoError(err)
deps, err := testRunner.TestDependencies()

Expand Down Expand Up @@ -229,16 +229,16 @@ func TestTestDependenciesPaths(t *testing.T) {

testCase := &latest.TestCase{
ImageName: "image",
Workspace: tmpDir.Root(),
CustomTests: []latest.CustomTest{custom},
}

cfg := &mockConfig{
workingDir: tmpDir.Root(),
tests: []*latest.TestCase{testCase},
tests: []*latest.TestCase{testCase},
}
testEvent.InitializeState([]latest.Pipeline{{}})

testRunner, err := New(cfg, testCase.ImageName, cfg.workingDir, custom)
testRunner, err := New(cfg, testCase.ImageName, testCase.Workspace, custom)
t.CheckNoError(err)
deps, err := testRunner.TestDependencies()

Expand Down Expand Up @@ -282,16 +282,16 @@ func TestGetEnv(t *testing.T) {

testCase := &latest.TestCase{
ImageName: "image",
Workspace: tmpDir.Root(),
CustomTests: []latest.CustomTest{custom},
}

cfg := &mockConfig{
workingDir: tmpDir.Root(),
tests: []*latest.TestCase{testCase},
tests: []*latest.TestCase{testCase},
}
testEvent.InitializeState([]latest.Pipeline{{}})

testRunner, err := New(cfg, testCase.ImageName, cfg.workingDir, custom)
testRunner, err := New(cfg, testCase.ImageName, testCase.Workspace, custom)
t.CheckNoError(err)
actual, err := testRunner.getEnv(test.tag)

Expand All @@ -303,6 +303,5 @@ func TestGetEnv(t *testing.T) {

type mockConfig struct {
runcontext.RunContext // Embedded to provide the default values.
workingDir string
tests []*latest.TestCase
}
8 changes: 4 additions & 4 deletions pkg/skaffold/test/structure/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ type Runner struct {
structureTests []string
imageName string
imageIsLocal bool
testWorkingDir string
workspace string
localDaemon docker.LocalDaemon
}

// New creates a new structure.Runner.
func New(cfg docker.Config, wd string, tc *latest.TestCase, imageIsLocal bool) (*Runner, error) {
func New(cfg docker.Config, tc *latest.TestCase, imageIsLocal bool) (*Runner, error) {
localDaemon, err := docker.NewAPIClient(cfg)
if err != nil {
return nil, err
}
return &Runner{
structureTests: tc.StructureTests,
imageName: tc.ImageName,
testWorkingDir: wd,
workspace: tc.Workspace,
localDaemon: localDaemon,
imageIsLocal: imageIsLocal,
}, nil
Expand Down Expand Up @@ -101,7 +101,7 @@ func (cst *Runner) runStructureTests(ctx context.Context, out io.Writer, imageTa

// TestDependencies returns dependencies listed for the structure tests
func (cst *Runner) TestDependencies() ([]string, error) {
files, err := util.ExpandPathsGlob(cst.testWorkingDir, cst.structureTests)
files, err := util.ExpandPathsGlob(cst.workspace, cst.structureTests)
if err != nil {
return nil, expandingFilePathsErr(err)
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/skaffold/test/structure/structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,21 @@ func TestNewRunner(t *testing.T) {
t.Override(&util.DefaultExecCommand, testutil.CmdRun("container-structure-test test -v warn --image "+imageName+" --config "+tmpDir.Path("test.yaml")))

cfg := &mockConfig{
workingDir: tmpDir.Root(),
tests: []*latest.TestCase{{
ImageName: "image",
Workspace: tmpDir.Root(),
StructureTests: []string{"test.yaml"},
}},
}

testCase := &latest.TestCase{
ImageName: "image",
Workspace: tmpDir.Root(),
StructureTests: []string{"test.yaml"},
}
testEvent.InitializeState([]latest.Pipeline{{}})

testRunner, err := New(cfg, cfg.workingDir, testCase, true)
testRunner, err := New(cfg, testCase, true)
t.CheckNoError(err)
err = testRunner.Test(context.Background(), ioutil.Discard, "image:tag")
t.CheckNoError(err)
Expand All @@ -74,31 +75,31 @@ func TestIgnoreDockerNotFound(t *testing.T) {
})

cfg := &mockConfig{
workingDir: tmpDir.Root(),
tests: []*latest.TestCase{{
ImageName: "image",
Workspace: tmpDir.Root(),
StructureTests: []string{"test.yaml"},
}},
}

testCase := &latest.TestCase{
ImageName: "image",
Workspace: tmpDir.Root(),
StructureTests: []string{"test.yaml"},
}

testRunner, err := New(cfg, cfg.workingDir, testCase, true)
testRunner, err := New(cfg, testCase, true)
t.CheckError(true, err)
t.CheckNil(testRunner)
})
}

type mockConfig struct {
runcontext.RunContext // Embedded to provide the default values.
workingDir string
tests []*latest.TestCase
muted config.Muted
}

func (c *mockConfig) Muted() config.Muted { return c.muted }
func (c *mockConfig) GetWorkingDir() string { return c.workingDir }
func (c *mockConfig) Muted() config.Muted { return c.muted }

func (c *mockConfig) TestCases() []*latest.TestCase { return c.tests }
7 changes: 3 additions & 4 deletions pkg/skaffold/test/test_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type Config interface {
docker.Config

TestCases() []*latest.TestCase
GetWorkingDir() string
Muted() config.Muted
}

Expand Down Expand Up @@ -114,7 +113,7 @@ func (t FullTester) runTests(ctx context.Context, out io.Writer, bRes []graph.Ar
return nil
}

func getImageTesters(cfg Config, imagesAreLocal func(imageName string) (bool, error), tcs []*latest.TestCase) (ImageTesters, error) {
func getImageTesters(cfg docker.Config, imagesAreLocal func(imageName string) (bool, error), tcs []*latest.TestCase) (ImageTesters, error) {
runners := make(map[string][]ImageTester)
for _, tc := range tcs {
isLocal, err := imagesAreLocal(tc.ImageName)
Expand All @@ -123,15 +122,15 @@ func getImageTesters(cfg Config, imagesAreLocal func(imageName string) (bool, er
}

if len(tc.StructureTests) != 0 {
structureRunner, err := structure.New(cfg, cfg.GetWorkingDir(), tc, isLocal)
structureRunner, err := structure.New(cfg, tc, isLocal)
if err != nil {
return nil, err
}
runners[tc.ImageName] = append(runners[tc.ImageName], structureRunner)
}

for _, customTest := range tc.CustomTests {
customRunner, err := custom.New(cfg, tc.ImageName, cfg.GetWorkingDir(), customTest)
customRunner, err := custom.New(cfg, tc.ImageName, tc.Workspace, customTest)
if err != nil {
return nil, err
}
Expand Down
Loading