mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-13 13:55:41 +00:00
Add a unit test for heuristicsCopyFileLogs
Signed-off-by: Stephen Kitt <skitt@redhat.com>
This commit is contained in:
parent
7b730fcd73
commit
7918e9f0cb
@ -343,7 +343,7 @@ func copyFileLogs(ctx context.Context, w io.Writer, services []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, service := range services {
|
for _, service := range services {
|
||||||
heuristicsCopyFileLogs(ctx, w, service)
|
heuristicsCopyFileLogs(ctx, w, nodeLogDir, service)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -352,7 +352,7 @@ func copyFileLogs(ctx context.Context, w io.Writer, services []string) {
|
|||||||
// /var/log/service.log or
|
// /var/log/service.log or
|
||||||
// /var/log/service/service.log or
|
// /var/log/service/service.log or
|
||||||
// in that order stopping on first success.
|
// in that order stopping on first success.
|
||||||
func heuristicsCopyFileLogs(ctx context.Context, w io.Writer, service string) {
|
func heuristicsCopyFileLogs(ctx context.Context, w io.Writer, logDir, service string) {
|
||||||
logFileNames := [3]string{
|
logFileNames := [3]string{
|
||||||
service,
|
service,
|
||||||
fmt.Sprintf("%s.log", service),
|
fmt.Sprintf("%s.log", service),
|
||||||
@ -362,7 +362,7 @@ func heuristicsCopyFileLogs(ctx context.Context, w io.Writer, service string) {
|
|||||||
var err error
|
var err error
|
||||||
for _, logFileName := range logFileNames {
|
for _, logFileName := range logFileNames {
|
||||||
var logFile string
|
var logFile string
|
||||||
logFile, err = securejoin.SecureJoin(nodeLogDir, logFileName)
|
logFile, err = securejoin.SecureJoin(logDir, logFileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,11 @@ limitations under the License.
|
|||||||
package kubelet
|
package kubelet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
@ -221,3 +225,64 @@ func Test_nodeLogQuery_validate(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_heuristicsCopyFileLogs(t *testing.T) {
|
||||||
|
ctx := context.TODO()
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
|
dir, err := os.MkdirTemp("", "logs")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer func() { _ = os.RemoveAll(dir) }()
|
||||||
|
|
||||||
|
// Check missing logs
|
||||||
|
heuristicsCopyFileLogs(ctx, buf, dir, "service.log")
|
||||||
|
if !strings.Contains(buf.String(), "log not found for service.log") {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
|
// Check missing service logs
|
||||||
|
heuristicsCopyFileLogs(ctx, buf, dir, "service")
|
||||||
|
if !strings.Contains(buf.String(), "log not found for service") {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
|
// Check explicitly-named files
|
||||||
|
if err := os.WriteFile(filepath.Join(dir, "service.log"), []byte("valid logs"), 0o444); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
heuristicsCopyFileLogs(ctx, buf, dir, "service.log")
|
||||||
|
if buf.String() != "valid logs" {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
|
// Check service logs
|
||||||
|
heuristicsCopyFileLogs(ctx, buf, dir, "service")
|
||||||
|
if buf.String() != "valid logs" {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
|
// Check that a directory doesn't cause errors
|
||||||
|
if err := os.Mkdir(filepath.Join(dir, "service"), 0o755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
heuristicsCopyFileLogs(ctx, buf, dir, "service")
|
||||||
|
if buf.String() != "valid logs" {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
|
// Check that service logs return the first matching file
|
||||||
|
if err := os.WriteFile(filepath.Join(dir, "service", "service.log"), []byte("error"), 0o444); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
heuristicsCopyFileLogs(ctx, buf, dir, "service")
|
||||||
|
if buf.String() != "valid logs" {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user