Port api test. Fix Bugs

This commit is contained in:
Jan Eitzinger 2022-02-07 14:20:44 +01:00
parent e39bf6da78
commit cb70711c15
3 changed files with 22 additions and 8 deletions

View File

@ -206,6 +206,7 @@ func (api *RestApi) startJob(rw http.ResponseWriter, r *http.Request) {
// Check if combination of (job_id, cluster_id, start_time) already exists:
job, err := api.JobRepository.Find(req.JobID, req.Cluster, req.StartTime)
if err != nil {
print("ERROR in Find\n")
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
@ -305,7 +306,7 @@ func (api *RestApi) stopJob(rw http.ResponseWriter, r *http.Request) {
return err
}
api.JobRepository.Stop(job.JobID, job.Duration, req.State, jobMeta.Statistics)
api.JobRepository.Stop(job.ID, job.Duration, req.State, jobMeta.Statistics)
log.Printf("job stopped and archived (dbid: %d)", job.ID)
return nil
}

View File

@ -18,6 +18,7 @@ import (
"github.com/ClusterCockpit/cc-backend/config"
"github.com/ClusterCockpit/cc-backend/graph"
"github.com/ClusterCockpit/cc-backend/metricdata"
"github.com/ClusterCockpit/cc-backend/repository"
"github.com/ClusterCockpit/cc-backend/schema"
"github.com/gorilla/mux"
"github.com/jmoiron/sqlx"
@ -122,7 +123,7 @@ func setup(t *testing.T) *api.RestApi {
t.Fatal(err)
}
return &api.RestApi{
DB: db,
JobRepository: &repository.JobRepository{db},
Resolver: resolver,
}
}
@ -200,7 +201,7 @@ func TestRestApi(t *testing.T) {
t.Fatal(response.Status, recorder.Body.String())
}
var res api.StartJobApiRespone
var res api.StartJobApiResponse
if err := json.Unmarshal(recorder.Body.Bytes(), &res); err != nil {
t.Fatal(err)
}
@ -260,6 +261,7 @@ func TestRestApi(t *testing.T) {
}
if job.State != schema.JobStateCompleted {
print("STATE:" + job.State)
t.Fatal("expected job to be completed")
}

View File

@ -27,12 +27,17 @@ func (r *JobRepository) Find(
Where("job.cluster = ?", cluster).
Where("job.start_time = ?", startTime)
sql, args, err := qb.ToSql()
sqlQuery, args, err := qb.ToSql()
if err != nil {
return nil, err
}
job, err := schema.ScanJob(r.DB.QueryRowx(sql, args...))
job, err := schema.ScanJob(r.DB.QueryRowx(sqlQuery, args...))
// Reset error if no job is found as this is indicated by nil job ptr
if err == sql.ErrNoRows {
err = nil
}
return job, err
}
@ -42,13 +47,18 @@ func (r *JobRepository) Find(
// If the job was not found nil is returned for the job pointer.
func (r *JobRepository) FindById(
jobId int64) (*schema.Job, error) {
sql, args, err := sq.Select(schema.JobColumns...).
sqlQuery, args, err := sq.Select(schema.JobColumns...).
From("job").Where("job.id = ?", jobId).ToSql()
if err != nil {
return nil, err
}
job, err := schema.ScanJob(r.DB.QueryRowx(sql, args...))
job, err := schema.ScanJob(r.DB.QueryRowx(sqlQuery, args...))
// Reset error if no job is found as this is indicated by nil job ptr
if err == sql.ErrNoRows {
err = nil
}
return job, err
}
@ -92,6 +102,7 @@ func (r *JobRepository) Stop(
}
sql, args, err := stmt.ToSql()
if err != nil {
log.Errorf("archiving job (dbid: %d) failed: %s", jobId, err.Error())
}