shad-go/distbuild/disttest/single_worker_test.go

137 lines
3.1 KiB
Go
Raw Normal View History

2020-03-28 13:54:43 +00:00
package disttest
import (
2020-03-28 14:22:24 +00:00
"fmt"
2020-03-28 13:54:43 +00:00
"io/ioutil"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/slon/shad-go/distbuild/pkg/build"
)
2020-04-05 12:00:33 +00:00
var singleWorkerConfig = &Config{WorkerCount: 1}
2020-03-28 13:54:43 +00:00
var echoGraph = build.Graph{
Jobs: []build.Job{
{
ID: build.ID{'a'},
Name: "echo",
Cmds: []build.Cmd{
{Exec: []string{"echo", "OK"}},
},
},
},
}
func TestSingleCommand(t *testing.T) {
2020-04-05 12:00:33 +00:00
env, cancel := newEnv(t, singleWorkerConfig)
2020-03-28 13:54:43 +00:00
defer cancel()
recorder := NewRecorder()
require.NoError(t, env.Client.Build(env.Ctx, echoGraph, recorder))
assert.Len(t, recorder.Jobs, 1)
assert.Equal(t, &JobResult{Stdout: "OK\n", Code: new(int)}, recorder.Jobs[build.ID{'a'}])
}
func TestJobCaching(t *testing.T) {
2020-04-05 12:00:33 +00:00
env, cancel := newEnv(t, singleWorkerConfig)
2020-03-28 13:54:43 +00:00
defer cancel()
tmpFile, err := ioutil.TempFile("", "")
require.NoError(t, err)
graph := build.Graph{
Jobs: []build.Job{
{
ID: build.ID{'a'},
Name: "echo",
Cmds: []build.Cmd{
{CatTemplate: "OK\n", CatOutput: tmpFile.Name()}, // No-hermetic, for testing purposes.
{Exec: []string{"echo", "OK"}},
},
},
},
}
recorder := NewRecorder()
require.NoError(t, env.Client.Build(env.Ctx, graph, recorder))
assert.Len(t, recorder.Jobs, 1)
assert.Equal(t, &JobResult{Stdout: "OK\n", Code: new(int)}, recorder.Jobs[build.ID{'a'}])
// Second build must get results from cache.
require.NoError(t, env.Client.Build(env.Ctx, graph, NewRecorder()))
2020-04-04 21:13:45 +00:00
require.NoError(t, ioutil.WriteFile(tmpFile.Name(), []byte("NOTOK\n"), 0666))
2020-03-28 13:54:43 +00:00
output, err := ioutil.ReadAll(tmpFile)
require.NoError(t, err)
2020-04-04 21:13:45 +00:00
require.Equal(t, []byte("NOTOK\n"), output)
2020-03-28 13:54:43 +00:00
}
2020-03-28 14:05:30 +00:00
var sourceFilesGraph = build.Graph{
SourceFiles: map[build.ID]string{
2020-03-28 14:22:24 +00:00
{'a'}: "a.txt",
{'c'}: "b/c.txt",
2020-03-28 14:05:30 +00:00
},
Jobs: []build.Job{
{
ID: build.ID{'a'},
Name: "echo",
Cmds: []build.Cmd{
{Exec: []string{"cat", "{{.SourceDir}}/a.txt"}},
{Exec: []string{"bash", "-c", "cat {{.SourceDir}}/b/c.txt > /dev/stderr"}},
},
Inputs: []string{
"a.txt",
"b/c.txt",
},
},
},
}
2020-03-28 13:54:43 +00:00
func TestSourceFiles(t *testing.T) {
2020-04-05 12:00:33 +00:00
env, cancel := newEnv(t, singleWorkerConfig)
2020-03-28 14:05:30 +00:00
defer cancel()
recorder := NewRecorder()
require.NoError(t, env.Client.Build(env.Ctx, sourceFilesGraph, recorder))
2020-03-28 13:54:43 +00:00
2020-03-28 14:05:30 +00:00
assert.Len(t, recorder.Jobs, 1)
assert.Equal(t, &JobResult{Stdout: "foo", Stderr: "bar", Code: new(int)}, recorder.Jobs[build.ID{'a'}])
2020-03-28 13:54:43 +00:00
}
2020-03-28 14:22:24 +00:00
var artifactTransferGraph = build.Graph{
Jobs: []build.Job{
{
ID: build.ID{'a'},
Name: "write",
Cmds: []build.Cmd{
{CatTemplate: "OK", CatOutput: "{{.OutputDir}}/out.txt"},
},
},
{
ID: build.ID{'b'},
Name: "cat",
Cmds: []build.Cmd{
{Exec: []string{"cat", fmt.Sprintf("{{index .Deps %q}}/out.txt", build.ID{'a'})}},
},
Deps: []build.ID{{'a'}},
},
},
}
2020-04-04 20:11:21 +00:00
func TestArtifactTransferBetweenJobs(t *testing.T) {
2020-04-05 12:00:33 +00:00
env, cancel := newEnv(t, singleWorkerConfig)
2020-03-28 14:22:24 +00:00
defer cancel()
recorder := NewRecorder()
require.NoError(t, env.Client.Build(env.Ctx, artifactTransferGraph, recorder))
assert.Len(t, recorder.Jobs, 2)
assert.Equal(t, &JobResult{Stdout: "OK", Code: new(int)}, recorder.Jobs[build.ID{'b'}])
}