Fix tests

This commit is contained in:
Jan Eitzinger 2023-03-27 14:41:00 +02:00
parent 8447d011ad
commit b5b5feb850
4 changed files with 32 additions and 16 deletions

View File

@ -15,6 +15,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/ClusterCockpit/cc-backend/internal/config" "github.com/ClusterCockpit/cc-backend/internal/config"
@ -75,20 +76,20 @@ func (fsa *FsArchive) Init(rawConfig json.RawMessage) (int, error) {
} }
fsa.path = config.Path fsa.path = config.Path
b, err := os.ReadFile(fmt.Sprintf("%s/version.txt", fsa.path)) b, err := os.ReadFile(filepath.Join(fsa.path, "version.txt"))
if err != nil { if err != nil {
fmt.Println("Err") fmt.Println("Err")
return 0, err return 0, err
} }
version, err := strconv.Atoi(string(b)) version, err := strconv.Atoi(strings.TrimSuffix(string(b), "\n"))
if err != nil { if err != nil {
log.Errorf("fsBackend Init()- %v", err) log.Errorf("fsBackend Init()- %v", err)
return 0, err return 0, err
} }
if version != Version { if version != Version {
return version, fmt.Errorf("Unsupported version %d, need %d", version, Version) return version, fmt.Errorf("unsupported version %d, need %d", version, Version)
} }
entries, err := os.ReadDir(fsa.path) entries, err := os.ReadDir(fsa.path)
@ -98,6 +99,9 @@ func (fsa *FsArchive) Init(rawConfig json.RawMessage) (int, error) {
} }
for _, de := range entries { for _, de := range entries {
if !de.IsDir() {
continue
}
fsa.clusters = append(fsa.clusters, de.Name()) fsa.clusters = append(fsa.clusters, de.Name())
} }
@ -147,7 +151,7 @@ func (fsa *FsArchive) LoadClusterCfg(name string) (*schema.Cluster, error) {
} }
if config.Keys.Validate { if config.Keys.Validate {
if err := schema.Validate(schema.ClusterCfg, bytes.NewReader(b)); err != nil { if err := schema.Validate(schema.ClusterCfg, bytes.NewReader(b)); err != nil {
return &schema.Cluster{}, fmt.Errorf("Validate cluster config: %v\n", err) return &schema.Cluster{}, fmt.Errorf("validate cluster config: %v", err)
} }
} }
return DecodeCluster(bytes.NewReader(b)) return DecodeCluster(bytes.NewReader(b))
@ -163,6 +167,9 @@ func (fsa *FsArchive) Iter() <-chan *schema.JobMeta {
} }
for _, clusterDir := range clustersDir { for _, clusterDir := range clustersDir {
if !clusterDir.IsDir() {
continue
}
lvl1Dirs, err := os.ReadDir(filepath.Join(fsa.path, clusterDir.Name())) lvl1Dirs, err := os.ReadDir(filepath.Join(fsa.path, clusterDir.Name()))
if err != nil { if err != nil {
log.Fatalf("Reading jobs failed: %s", err.Error()) log.Fatalf("Reading jobs failed: %s", err.Error())
@ -278,6 +285,9 @@ func (fsa *FsArchive) ImportJob(
// } // }
f, err = os.Create(path.Join(dir, "data.json")) f, err = os.Create(path.Join(dir, "data.json"))
if err != nil {
return err
}
if err := EncodeJobData(f, jobData); err != nil { if err := EncodeJobData(f, jobData); err != nil {
return err return err
} }

View File

@ -15,7 +15,7 @@ import (
func TestInitEmptyPath(t *testing.T) { func TestInitEmptyPath(t *testing.T) {
var fsa FsArchive var fsa FsArchive
err := fsa.Init(json.RawMessage("{\"kind\":\"../../test/archive\"}")) _, err := fsa.Init(json.RawMessage("{\"kind\":\"../../test/archive\"}"))
if err == nil { if err == nil {
t.Fatal(err) t.Fatal(err)
} }
@ -23,14 +23,14 @@ func TestInitEmptyPath(t *testing.T) {
func TestInitNoJson(t *testing.T) { func TestInitNoJson(t *testing.T) {
var fsa FsArchive var fsa FsArchive
err := fsa.Init(json.RawMessage("\"path\":\"../../test/archive\"}")) _, err := fsa.Init(json.RawMessage("\"path\":\"../../test/archive\"}"))
if err == nil { if err == nil {
t.Fatal(err) t.Fatal(err)
} }
} }
func TestInitNotExists(t *testing.T) { func TestInitNotExists(t *testing.T) {
var fsa FsArchive var fsa FsArchive
err := fsa.Init(json.RawMessage("{\"path\":\"../../test/job-archive\"}")) _, err := fsa.Init(json.RawMessage("{\"path\":\"../../test/job-archive\"}"))
if err == nil { if err == nil {
t.Fatal(err) t.Fatal(err)
} }
@ -38,15 +38,16 @@ func TestInitNotExists(t *testing.T) {
func TestInit(t *testing.T) { func TestInit(t *testing.T) {
var fsa FsArchive var fsa FsArchive
err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) version, err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if fsa.path != "../../test/archive" { if fsa.path != "../../test/archive" {
t.Fail() t.Fail()
} }
if version != 1 {
t.Fail()
}
if len(fsa.clusters) != 1 || fsa.clusters[0] != "emmy" { if len(fsa.clusters) != 1 || fsa.clusters[0] != "emmy" {
t.Fail() t.Fail()
} }
@ -54,7 +55,7 @@ func TestInit(t *testing.T) {
func TestLoadJobMetaInternal(t *testing.T) { func TestLoadJobMetaInternal(t *testing.T) {
var fsa FsArchive var fsa FsArchive
err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) _, err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -77,7 +78,7 @@ func TestLoadJobMetaInternal(t *testing.T) {
func TestLoadJobMeta(t *testing.T) { func TestLoadJobMeta(t *testing.T) {
var fsa FsArchive var fsa FsArchive
err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) _, err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -105,7 +106,7 @@ func TestLoadJobMeta(t *testing.T) {
func TestLoadJobData(t *testing.T) { func TestLoadJobData(t *testing.T) {
var fsa FsArchive var fsa FsArchive
err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) _, err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -131,7 +132,7 @@ func TestLoadJobData(t *testing.T) {
func TestLoadCluster(t *testing.T) { func TestLoadCluster(t *testing.T) {
var fsa FsArchive var fsa FsArchive
err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) _, err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -148,7 +149,7 @@ func TestLoadCluster(t *testing.T) {
func TestIter(t *testing.T) { func TestIter(t *testing.T) {
var fsa FsArchive var fsa FsArchive
err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) _, err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

1
test/archive/version.txt Normal file
View File

@ -0,0 +1 @@
1

View File

@ -298,6 +298,10 @@ func setup(t *testing.T) *api.RestApi {
t.Fatal(err) t.Fatal(err)
} }
if err := os.WriteFile(filepath.Join(jobarchive, "version.txt"), []byte(fmt.Sprintf("%d", 1)), 0666); err != nil {
t.Fatal(err)
}
if err := os.Mkdir(filepath.Join(jobarchive, "testcluster"), 0777); err != nil { if err := os.Mkdir(filepath.Join(jobarchive, "testcluster"), 0777); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -389,7 +393,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",