File: setup_test.go

package info (click to toggle)
golang-github-containerd-nydus-snapshotter 0.13.4-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,824 kB
  • sloc: sh: 470; makefile: 129
file content (67 lines) | stat: -rw-r--r-- 1,575 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * Copyright (c) 2022. Nydus Developers. All rights reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

package logging

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"github.com/containerd/containerd/log"
	"github.com/sirupsen/logrus"
	"gotest.tools/assert"
)

const (
	TestLogDirName  = "test-rotate-logs"
	TestRootDirName = "test-root"
)

func GetRotateLogFileNumbers(testLogDir string, suffix string) int {
	i := 0
	err := filepath.Walk(testLogDir, func(fname string, fi os.FileInfo, err error) error {
		if !fi.IsDir() && strings.HasSuffix(fname, suffix) {
			i++
		}
		return nil
	})
	if err != nil {
		log.L.Fatal("walk path")
	}
	return i
}

func TestSetUp(t *testing.T) {
	// Try to clean previously created test directory.
	os.RemoveAll(TestLogDirName)

	logRotateArgs := &RotateLogArgs{
		RotateLogMaxSize:    1, // 1MB
		RotateLogMaxBackups: 5,
		RotateLogMaxAge:     0,
		RotateLogLocalTime:  true,
		RotateLogCompress:   true,
	}
	logLevel := logrus.InfoLevel.String()

	err := SetUp(logLevel, true, TestLogDirName, nil)
	assert.NilError(t, err, nil)

	err = SetUp(logLevel, false, TestLogDirName, nil)
	assert.ErrorContains(t, err, "logRotateArgs is needed when logToStdout is false")

	err = SetUp(logLevel, false, TestLogDirName, logRotateArgs)
	assert.NilError(t, err)
	for i := 0; i < 100000; i++ { // total 9.1MB
		log.L.Infof("test log, now: %s", time.Now().Format("2006-01-02 15:04:05"))
	}
	assert.Equal(t, GetRotateLogFileNumbers(TestLogDirName, "log.gz"), logRotateArgs.RotateLogMaxBackups)

	os.RemoveAll(TestLogDirName)
}