-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Runservice: added logCleanerLoop for deleting older logs
- Loading branch information
1 parent
d0d219c
commit 7d4313e
Showing
6 changed files
with
136 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package runservice | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"agola.io/agola/internal/objectstorage" | ||
"agola.io/agola/internal/services/config" | ||
"agola.io/agola/internal/services/runservice/common" | ||
"agola.io/agola/internal/services/runservice/store" | ||
"agola.io/agola/internal/testutil" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zaptest" | ||
) | ||
|
||
func setupEtcd(t *testing.T, logger *zap.Logger, dir string) *testutil.TestEmbeddedEtcd { | ||
tetcd, err := testutil.NewTestEmbeddedEtcd(t, logger, dir) | ||
if err != nil { | ||
t.Fatalf("unexpected err: %v", err) | ||
} | ||
if err := tetcd.Start(); err != nil { | ||
t.Fatalf("unexpected err: %v", err) | ||
} | ||
if err := tetcd.WaitUp(30 * time.Second); err != nil { | ||
t.Fatalf("error waiting on etcd up: %v", err) | ||
} | ||
return tetcd | ||
} | ||
|
||
func shutdownEtcd(tetcd *testutil.TestEmbeddedEtcd) { | ||
if tetcd.Etcd != nil { | ||
_ = tetcd.Kill() | ||
} | ||
} | ||
|
||
func setupRunservice(t *testing.T, ctx context.Context, dir string) (*Runservice, *testutil.TestEmbeddedEtcd, error) { | ||
logger := zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel)) | ||
|
||
etcdDir := filepath.Join(dir, "etcd") | ||
tetcd := setupEtcd(t, logger, etcdDir) | ||
|
||
config := config.Runservice{ | ||
DataDir: filepath.Join(dir, "runservice"), | ||
Etcd: config.Etcd{ | ||
Endpoints: "", | ||
}, | ||
ObjectStorage: config.ObjectStorage{ | ||
Type: "posix", | ||
Path: filepath.Join(dir, "runservice/ost"), | ||
}, | ||
RunLogExpireInterval: time.Millisecond, | ||
} | ||
|
||
config.Etcd.Endpoints = tetcd.Endpoint | ||
|
||
rs, err := NewRunservice(ctx, logger, &config) | ||
return rs, tetcd, err | ||
} | ||
|
||
func TestLogleaner(t *testing.T) { | ||
dir, err := ioutil.TempDir("", "agola") | ||
if err != nil { | ||
t.Fatalf("unexpected err: %v", err) | ||
} | ||
defer os.RemoveAll(dir) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
rs, tetcd, err := setupRunservice(t, ctx, dir) | ||
if err != nil { | ||
t.Fatalf("unexpected err: %v", err) | ||
} | ||
|
||
defer shutdownEtcd(tetcd) | ||
|
||
body := ioutil.NopCloser(bytes.NewBufferString("log test")) | ||
logPath := store.OSTRunTaskStepLogPath("tast01", 0) | ||
|
||
err = rs.ost.WriteObject(logPath, body, -1, false) | ||
if err != nil { | ||
t.Fatalf("unexpected err: %v", err) | ||
} | ||
|
||
_, err = rs.ost.ReadObject(logPath) | ||
if err != nil { | ||
t.Fatalf("unexpected err: %v", err) | ||
} | ||
|
||
err = rs.objectsCleaner(ctx, store.OSTLogsBaseDir(), common.EtcdLogCleanerLockKey, 1*time.Millisecond) | ||
if err != nil { | ||
t.Fatalf("unexpected err: %v", err) | ||
} | ||
|
||
_, err = rs.ost.ReadObject(logPath) | ||
if err == nil || !objectstorage.IsNotExist(err) { | ||
t.Fatalf("expected err NotExists, got: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters