
We previously didn't really had any structure in our project apart from creating a new folder for each package in our project root. Now that we have accumulated some packages, we use the well-known Golang project layout in order to clearly communicate our intent with packages. See https://github.com/golang-standards/project-layout
97 lines
2.5 KiB
Go
97 lines
2.5 KiB
Go
package e2e
|
|
|
|
import (
|
|
"flag"
|
|
nomadApi "github.com/hashicorp/nomad/api"
|
|
"github.com/stretchr/testify/suite"
|
|
"gitlab.hpi.de/codeocean/codemoon/poseidon/internal/api"
|
|
"gitlab.hpi.de/codeocean/codemoon/poseidon/internal/config"
|
|
"gitlab.hpi.de/codeocean/codemoon/poseidon/pkg/dto"
|
|
"gitlab.hpi.de/codeocean/codemoon/poseidon/pkg/logging"
|
|
"gitlab.hpi.de/codeocean/codemoon/poseidon/tests"
|
|
"gitlab.hpi.de/codeocean/codemoon/poseidon/tests/helpers"
|
|
"net/http"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
/*
|
|
* # E2E Tests
|
|
*
|
|
* For the e2e tests a nomad cluster must be connected and poseidon must be running.
|
|
*/
|
|
|
|
var (
|
|
log = logging.GetLogger("e2e")
|
|
testDockerImage = flag.String("dockerImage", "", "Docker image to use in E2E tests")
|
|
nomadClient *nomadApi.Client
|
|
nomadNamespace string
|
|
)
|
|
|
|
type E2ETestSuite struct {
|
|
suite.Suite
|
|
}
|
|
|
|
func (s *E2ETestSuite) SetupTest() {
|
|
// Waiting one second before each test allows Nomad to rescale after tests requested runners.
|
|
<-time.After(time.Second)
|
|
}
|
|
|
|
func TestE2ETestSuite(t *testing.T) {
|
|
suite.Run(t, new(E2ETestSuite))
|
|
}
|
|
|
|
// Overwrite TestMain for custom setup.
|
|
func TestMain(m *testing.M) {
|
|
log.Info("Test Setup")
|
|
err := config.InitConfig()
|
|
if err != nil {
|
|
log.WithError(err).Fatal("Could not initialize configuration")
|
|
}
|
|
nomadNamespace = config.Config.Nomad.Namespace
|
|
nomadClient, err = nomadApi.NewClient(&nomadApi.Config{
|
|
Address: config.Config.NomadAPIURL().String(),
|
|
TLSConfig: &nomadApi.TLSConfig{},
|
|
Namespace: nomadNamespace,
|
|
})
|
|
if err != nil {
|
|
log.WithError(err).Fatal("Could not create Nomad client")
|
|
}
|
|
log.Info("Test Run")
|
|
createDefaultEnvironment()
|
|
|
|
// wait for environment to become ready
|
|
<-time.After(10 * time.Second)
|
|
|
|
code := m.Run()
|
|
cleanupJobsForEnvironment(&testing.T{}, "0")
|
|
os.Exit(code)
|
|
}
|
|
|
|
func createDefaultEnvironment() {
|
|
if *testDockerImage == "" {
|
|
log.Fatal("You must specify the -dockerImage flag!")
|
|
}
|
|
|
|
path := helpers.BuildURL(api.BasePath, api.EnvironmentsPath, tests.DefaultEnvironmentIDAsString)
|
|
|
|
request := dto.ExecutionEnvironmentRequest{
|
|
PrewarmingPoolSize: 10,
|
|
CPULimit: 100,
|
|
MemoryLimit: 100,
|
|
Image: *testDockerImage,
|
|
NetworkAccess: false,
|
|
ExposedPorts: nil,
|
|
}
|
|
|
|
resp, err := helpers.HTTPPutJSON(path, request)
|
|
if err != nil || resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusNoContent {
|
|
log.WithError(err).Fatal("Couldn't create default environment for e2e tests")
|
|
}
|
|
err = resp.Body.Close()
|
|
if err != nil {
|
|
log.Fatal("Failed closing body")
|
|
}
|
|
}
|