Start to port api test

This commit is contained in:
Jan Eitzinger 2022-09-06 09:31:52 +02:00
parent ac6785448e
commit 49dad5c8c5

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
@ -18,6 +19,7 @@ import (
"github.com/ClusterCockpit/cc-backend/internal/graph" "github.com/ClusterCockpit/cc-backend/internal/graph"
"github.com/ClusterCockpit/cc-backend/internal/metricdata" "github.com/ClusterCockpit/cc-backend/internal/metricdata"
"github.com/ClusterCockpit/cc-backend/internal/repository" "github.com/ClusterCockpit/cc-backend/internal/repository"
"github.com/ClusterCockpit/cc-backend/pkg/archive"
"github.com/ClusterCockpit/cc-backend/pkg/schema" "github.com/ClusterCockpit/cc-backend/pkg/schema"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@ -25,8 +27,15 @@ import (
) )
func setup(t *testing.T) *api.RestApi { func setup(t *testing.T) *api.RestApi {
const testconfig = `{
"addr": "0.0.0.0:80",
"archive": {
"kind": "file",
"path": "./var/job-archive"
}
}`
const testclusterJson = `{ const testclusterJson = `{
"name": "testcluster", "name": "testcluster",
"subClusters": [ "subClusters": [
{ {
"name": "sc0", "name": "sc0",
@ -93,22 +102,31 @@ func setup(t *testing.T) *api.RestApi {
} }
f.Close() f.Close()
cfgFilePath := filepath.Join(tmpdir, "config.json")
if err := os.WriteFile(cfgFilePath, []byte(testconfig), 0666); err != nil {
t.Fatal(err)
}
config.Init(cfgFilePath)
archiveCfg := fmt.Sprintf("{\"kind\":\"file\",\"path\":\"%s\"}", jobarchive)
config.Keys.Archive = []byte(archiveCfg)
repository.Connect("sqlite3", dbfilepath) repository.Connect("sqlite3", dbfilepath)
db := repository.GetConnection() db := repository.GetConnection()
if err := archive.Init(config.Keys.Archive); err != nil {
t.Fatal(err)
}
if err := metricdata.Init(config.Keys.DisableArchive); err != nil {
t.Fatal(err)
}
if _, err := db.DB.Exec(repository.JobsDBSchema); err != nil { if _, err := db.DB.Exec(repository.JobsDBSchema); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := config.Init(db.DB, false, map[string]interface{}{}, jobarchive); err != nil { jobRepo := repository.GetJobRepository()
t.Fatal(err)
}
if err := metricdata.Init(jobarchive, false); err != nil {
t.Fatal(err)
}
jobRepo := repository.GetRepository()
resolver := &graph.Resolver{DB: db.DB, Repo: jobRepo} resolver := &graph.Resolver{DB: db.DB, Repo: jobRepo}
return &api.RestApi{ return &api.RestApi{
@ -122,9 +140,9 @@ func cleanup() {
} }
/* /*
* This function starts a job, stops it, and then reads its data from the job-archive. * This function starts a job, stops it, and then reads its data from the job-archive.
* Do not run sub-tests in parallel! Tests should not be run in parallel at all, because * Do not run sub-tests in parallel! Tests should not be run in parallel at all, because
* at least `setup` modifies global state. Log-Output is redirected to /dev/null on purpose. * at least `setup` modifies global state. Log-Output is redirected to /dev/null on purpose.
*/ */
func TestRestApi(t *testing.T) { func TestRestApi(t *testing.T) {
restapi := setup(t) restapi := setup(t)
@ -155,7 +173,7 @@ func TestRestApi(t *testing.T) {
restapi.MountRoutes(r) restapi.MountRoutes(r)
const startJobBody string = `{ const startJobBody string = `{
"jobId": 123, "jobId": 123,
"user": "testuser", "user": "testuser",
"project": "testproj", "project": "testproj",
"cluster": "testcluster", "cluster": "testcluster",
@ -234,7 +252,7 @@ func TestRestApi(t *testing.T) {
} }
const stopJobBody string = `{ const stopJobBody string = `{
"jobId": 123, "jobId": 123,
"startTime": 123456789, "startTime": 123456789,
"cluster": "testcluster", "cluster": "testcluster",
@ -313,7 +331,7 @@ func TestRestApi(t *testing.T) {
func subtestLetJobFail(t *testing.T, restapi *api.RestApi, r *mux.Router) { func subtestLetJobFail(t *testing.T, restapi *api.RestApi, r *mux.Router) {
const startJobBody string = `{ const startJobBody string = `{
"jobId": 12345, "jobId": 12345,
"user": "testuser", "user": "testuser",
"project": "testproj", "project": "testproj",
"cluster": "testcluster", "cluster": "testcluster",
@ -350,7 +368,7 @@ func subtestLetJobFail(t *testing.T, restapi *api.RestApi, r *mux.Router) {
} }
const stopJobBody string = `{ const stopJobBody string = `{
"jobId": 12345, "jobId": 12345,
"cluster": "testcluster", "cluster": "testcluster",
"jobState": "failed", "jobState": "failed",