shad-go/distbuild/disttest/fixture.go

154 lines
3.4 KiB
Go
Raw Normal View History

2020-03-11 22:46:45 +00:00
package disttest
import (
"context"
2020-03-14 10:24:44 +00:00
"errors"
2020-03-11 22:46:45 +00:00
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
2020-03-29 16:03:07 +00:00
"gitlab.com/slon/shad-go/distbuild/pkg/api"
2020-03-11 22:46:45 +00:00
"gitlab.com/slon/shad-go/distbuild/pkg/artifact"
"gitlab.com/slon/shad-go/distbuild/pkg/client"
"gitlab.com/slon/shad-go/distbuild/pkg/dist"
"gitlab.com/slon/shad-go/distbuild/pkg/filecache"
"gitlab.com/slon/shad-go/distbuild/pkg/worker"
"gitlab.com/slon/shad-go/tools/testtool"
"go.uber.org/zap"
)
type env struct {
RootDir string
Logger *zap.Logger
Ctx context.Context
Client *client.Client
Coordinator *dist.Coordinator
Workers []*worker.Worker
HTTP *http.Server
}
2020-04-04 20:11:21 +00:00
const (
logToStderr = true
nWorkers = 1
)
2020-03-11 22:46:45 +00:00
func newEnv(t *testing.T) (e *env, cancel func()) {
cwd, err := os.Getwd()
require.NoError(t, err)
absCWD, err := filepath.Abs(cwd)
require.NoError(t, err)
env := &env{
RootDir: filepath.Join(absCWD, "workdir", t.Name()),
}
require.NoError(t, os.RemoveAll(env.RootDir))
require.NoError(t, os.MkdirAll(env.RootDir, 0777))
cfg := zap.NewDevelopmentConfig()
cfg.OutputPaths = []string{filepath.Join(env.RootDir, "test.log")}
2020-04-04 20:11:21 +00:00
if logToStderr {
cfg.OutputPaths = append(cfg.OutputPaths, "stderr")
}
2020-03-11 22:46:45 +00:00
env.Logger, err = cfg.Build()
require.NoError(t, err)
t.Helper()
t.Logf("test is running inside %s; see test.log file for more info", filepath.Join("workdir", t.Name()))
port, err := testtool.GetFreePort()
require.NoError(t, err)
addr := "127.0.0.1:" + port
coordinatorEndpoint := "http://" + addr + "/coordinator"
var cancelRootContext func()
env.Ctx, cancelRootContext = context.WithCancel(context.Background())
2020-03-29 16:24:18 +00:00
env.Client = client.NewClient(
env.Logger.Named("client"),
coordinatorEndpoint,
2020-04-04 21:21:55 +00:00
filepath.Join(absCWD, "testdata", t.Name()))
2020-03-11 22:46:45 +00:00
coordinatorCache, err := filecache.New(filepath.Join(env.RootDir, "coordinator", "filecache"))
require.NoError(t, err)
env.Coordinator = dist.NewCoordinator(
env.Logger.Named("coordinator"),
coordinatorCache,
)
2020-03-28 21:34:09 +00:00
router := http.NewServeMux()
router.Handle("/coordinator/", http.StripPrefix("/coordinator", env.Coordinator))
2020-03-11 22:46:45 +00:00
for i := 0; i < nWorkers; i++ {
workerName := fmt.Sprintf("worker%d", i)
workerDir := filepath.Join(env.RootDir, workerName)
2020-03-28 14:35:01 +00:00
var fileCache *filecache.Cache
fileCache, err = filecache.New(filepath.Join(workerDir, "filecache"))
2020-03-11 22:46:45 +00:00
require.NoError(t, err)
2020-03-28 14:35:01 +00:00
var artifacts *artifact.Cache
artifacts, err = artifact.NewCache(filepath.Join(workerDir, "artifacts"))
2020-03-11 22:46:45 +00:00
require.NoError(t, err)
2020-03-28 21:34:09 +00:00
workerPrefix := fmt.Sprintf("/worker/%d", i)
2020-03-29 16:03:07 +00:00
workerID := api.WorkerID("http://" + addr + workerPrefix)
2020-03-28 21:34:09 +00:00
2020-03-11 22:46:45 +00:00
w := worker.New(
2020-03-28 21:34:09 +00:00
workerID,
2020-03-11 22:46:45 +00:00
coordinatorEndpoint,
env.Logger.Named(workerName),
fileCache,
artifacts,
)
env.Workers = append(env.Workers, w)
2020-03-28 21:34:09 +00:00
router.Handle(workerPrefix+"/", http.StripPrefix(workerPrefix, w))
2020-03-11 22:46:45 +00:00
}
env.HTTP = &http.Server{
Addr: addr,
2020-03-28 21:34:09 +00:00
Handler: router,
2020-03-11 22:46:45 +00:00
}
lsn, err := net.Listen("tcp", env.HTTP.Addr)
require.NoError(t, err)
go func() {
2020-03-14 10:24:44 +00:00
err := env.HTTP.Serve(lsn)
if err != http.ErrServerClosed {
env.Logger.Fatal("http server stopped", zap.Error(err))
}
2020-03-11 22:46:45 +00:00
}()
2020-03-14 10:24:44 +00:00
for _, w := range env.Workers {
go func(w *worker.Worker) {
err := w.Run(env.Ctx)
if errors.Is(err, context.Canceled) {
return
}
env.Logger.Fatal("worker stopped", zap.Error(err))
}(w)
}
2020-03-11 22:46:45 +00:00
return env, func() {
cancelRootContext()
_ = env.HTTP.Shutdown(context.Background())
_ = env.Logger.Sync()
}
}