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.Nomad.URL().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") } }