From 6d8a3aa256af5ead8c2702cbd4c783b04f9ffb2a Mon Sep 17 00:00:00 2001 From: Jan Eitzinger Date: Wed, 28 Jun 2023 07:39:39 +0200 Subject: [PATCH] Fix bug in fsBackend and add tests for file utils --- .gitignore | 1 + internal/util/fstat.go | 4 ++- internal/util/util_test.go | 71 +++++++++++++++++++++++++++++++++++++ pkg/archive/archive_test.go | 2 +- pkg/archive/fsBackend.go | 2 +- 5 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 internal/util/util_test.go diff --git a/.gitignore b/.gitignore index 0e18666..2f7c206 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ var/job.db-shm var/job.db-wal dist/ +*.db diff --git a/internal/util/fstat.go b/internal/util/fstat.go index 3361e39..0c84014 100644 --- a/internal/util/fstat.go +++ b/internal/util/fstat.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg. +// Copyright (C) 2023 NHR@FAU, University Erlangen-Nuremberg. // All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -20,6 +20,7 @@ func GetFilesize(filePath string) int64 { fileInfo, err := os.Stat(filePath) if err != nil { log.Errorf("Error on Stat %s: %v", filePath, err) + return 0 } return fileInfo.Size() } @@ -28,6 +29,7 @@ func GetFilecount(path string) int { files, err := os.ReadDir(path) if err != nil { log.Errorf("Error on ReadDir %s: %v", path, err) + return 0 } return len(files) diff --git a/internal/util/util_test.go b/internal/util/util_test.go new file mode 100644 index 0000000..a318649 --- /dev/null +++ b/internal/util/util_test.go @@ -0,0 +1,71 @@ +// Copyright (C) 2023 NHR@FAU, University Erlangen-Nuremberg. +// All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. +package util_test + +import ( + "fmt" + "os" + "path/filepath" + "testing" + + "github.com/ClusterCockpit/cc-backend/internal/util" +) + +func TestCheckFileExists(t *testing.T) { + tmpdir := t.TempDir() + filePath := filepath.Join(tmpdir, "version.txt") + + if err := os.WriteFile(filePath, []byte(fmt.Sprintf("%d", 1)), 0666); err != nil { + t.Fatal(err) + } + if !util.CheckFileExists(filePath) { + t.Fatal("expected true, got false") + } + + filePath = filepath.Join(tmpdir, "version-test.txt") + if util.CheckFileExists(filePath) { + t.Fatal("expected false, got true") + } +} + +func TestGetFileSize(t *testing.T) { + tmpdir := t.TempDir() + filePath := filepath.Join(tmpdir, "data.json") + + if s := util.GetFilesize(filePath); s > 0 { + t.Fatalf("expected 0, got %d", s) + } + + if err := os.WriteFile(filePath, []byte(fmt.Sprintf("%d", 1)), 0666); err != nil { + t.Fatal(err) + } + if s := util.GetFilesize(filePath); s == 0 { + t.Fatal("expected not 0, got 0") + } +} + +func TestGetFileCount(t *testing.T) { + tmpdir := t.TempDir() + + if c := util.GetFilecount(tmpdir); c != 0 { + t.Fatalf("expected 0, got %d", c) + } + + filePath := filepath.Join(tmpdir, "data-1.json") + if err := os.WriteFile(filePath, []byte(fmt.Sprintf("%d", 1)), 0666); err != nil { + t.Fatal(err) + } + filePath = filepath.Join(tmpdir, "data-2.json") + if err := os.WriteFile(filePath, []byte(fmt.Sprintf("%d", 1)), 0666); err != nil { + t.Fatal(err) + } + if c := util.GetFilecount(tmpdir); c != 2 { + t.Fatalf("expected 2, got %d", c) + } + + if c := util.GetFilecount(filePath); c != 0 { + t.Fatalf("expected 0, got %d", c) + } +} diff --git a/pkg/archive/archive_test.go b/pkg/archive/archive_test.go index b41a033..5523a21 100644 --- a/pkg/archive/archive_test.go +++ b/pkg/archive/archive_test.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg. +// Copyright (C) 2023 NHR@FAU, University Erlangen-Nuremberg. // All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. diff --git a/pkg/archive/fsBackend.go b/pkg/archive/fsBackend.go index b58891f..cb8ed69 100644 --- a/pkg/archive/fsBackend.go +++ b/pkg/archive/fsBackend.go @@ -348,7 +348,7 @@ func (fsa *FsArchive) Compress(jobs []*schema.Job) { for _, job := range jobs { fileIn := getPath(job, fsa.path, "data.json") - if !util.CheckFileExists(fileIn) && util.GetFilesize(fileIn) > 2000 { + if util.CheckFileExists(fileIn) && util.GetFilesize(fileIn) > 2000 { util.CompressFile(fileIn, getPath(job, fsa.path, "data.json.gz")) cnt++ }