diff --git a/pkg/archive/fsBackend.go b/pkg/archive/fsBackend.go index 1ee7394..9668e5d 100644 --- a/pkg/archive/fsBackend.go +++ b/pkg/archive/fsBackend.go @@ -15,6 +15,7 @@ import ( "path" "path/filepath" "strconv" + "strings" "time" "github.com/ClusterCockpit/cc-backend/internal/config" @@ -75,20 +76,20 @@ func (fsa *FsArchive) Init(rawConfig json.RawMessage) (int, error) { } 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 { fmt.Println("Err") return 0, err } - version, err := strconv.Atoi(string(b)) + version, err := strconv.Atoi(strings.TrimSuffix(string(b), "\n")) if err != nil { log.Errorf("fsBackend Init()- %v", err) return 0, err } 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) @@ -98,6 +99,9 @@ func (fsa *FsArchive) Init(rawConfig json.RawMessage) (int, error) { } for _, de := range entries { + if !de.IsDir() { + continue + } 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 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)) @@ -163,6 +167,9 @@ func (fsa *FsArchive) Iter() <-chan *schema.JobMeta { } for _, clusterDir := range clustersDir { + if !clusterDir.IsDir() { + continue + } lvl1Dirs, err := os.ReadDir(filepath.Join(fsa.path, clusterDir.Name())) if err != nil { 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")) + if err != nil { + return err + } if err := EncodeJobData(f, jobData); err != nil { return err } diff --git a/pkg/archive/fsBackend_test.go b/pkg/archive/fsBackend_test.go index 8d37b6f..72055e6 100644 --- a/pkg/archive/fsBackend_test.go +++ b/pkg/archive/fsBackend_test.go @@ -15,7 +15,7 @@ import ( func TestInitEmptyPath(t *testing.T) { var fsa FsArchive - err := fsa.Init(json.RawMessage("{\"kind\":\"../../test/archive\"}")) + _, err := fsa.Init(json.RawMessage("{\"kind\":\"../../test/archive\"}")) if err == nil { t.Fatal(err) } @@ -23,14 +23,14 @@ func TestInitEmptyPath(t *testing.T) { func TestInitNoJson(t *testing.T) { var fsa FsArchive - err := fsa.Init(json.RawMessage("\"path\":\"../../test/archive\"}")) + _, err := fsa.Init(json.RawMessage("\"path\":\"../../test/archive\"}")) if err == nil { t.Fatal(err) } } func TestInitNotExists(t *testing.T) { var fsa FsArchive - err := fsa.Init(json.RawMessage("{\"path\":\"../../test/job-archive\"}")) + _, err := fsa.Init(json.RawMessage("{\"path\":\"../../test/job-archive\"}")) if err == nil { t.Fatal(err) } @@ -38,15 +38,16 @@ func TestInitNotExists(t *testing.T) { func TestInit(t *testing.T) { var fsa FsArchive - err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) + version, err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) if err != nil { t.Fatal(err) } - if fsa.path != "../../test/archive" { t.Fail() } - + if version != 1 { + t.Fail() + } if len(fsa.clusters) != 1 || fsa.clusters[0] != "emmy" { t.Fail() } @@ -54,7 +55,7 @@ func TestInit(t *testing.T) { func TestLoadJobMetaInternal(t *testing.T) { var fsa FsArchive - err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) + _, err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) if err != nil { t.Fatal(err) } @@ -77,7 +78,7 @@ func TestLoadJobMetaInternal(t *testing.T) { func TestLoadJobMeta(t *testing.T) { var fsa FsArchive - err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) + _, err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) if err != nil { t.Fatal(err) } @@ -105,7 +106,7 @@ func TestLoadJobMeta(t *testing.T) { func TestLoadJobData(t *testing.T) { var fsa FsArchive - err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) + _, err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) if err != nil { t.Fatal(err) } @@ -131,7 +132,7 @@ func TestLoadJobData(t *testing.T) { func TestLoadCluster(t *testing.T) { var fsa FsArchive - err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) + _, err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) if err != nil { t.Fatal(err) } @@ -148,7 +149,7 @@ func TestLoadCluster(t *testing.T) { func TestIter(t *testing.T) { var fsa FsArchive - err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) + _, err := fsa.Init(json.RawMessage("{\"path\":\"../../test/archive\"}")) if err != nil { t.Fatal(err) } diff --git a/test/archive/version.txt b/test/archive/version.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/test/archive/version.txt @@ -0,0 +1 @@ +1 diff --git a/test/integration_test.go b/test/integration_test.go index cd01f04..607907b 100644 --- a/test/integration_test.go +++ b/test/integration_test.go @@ -298,6 +298,10 @@ func setup(t *testing.T) *api.RestApi { 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 { t.Fatal(err) } @@ -389,7 +393,7 @@ func TestRestApi(t *testing.T) { restapi.MountRoutes(r) const startJobBody string = `{ -"jobId": 123, + "jobId": 123, "user": "testuser", "project": "testproj", "cluster": "testcluster",