Add a unit test for heuristicsCopyFileLogs

Signed-off-by: Stephen Kitt <skitt@redhat.com>
This commit is contained in:
Stephen Kitt 2024-10-03 16:01:55 +02:00
parent 7b730fcd73
commit 7918e9f0cb
No known key found for this signature in database
GPG Key ID: 1CC5FA453662A71D
2 changed files with 68 additions and 3 deletions

View File

@ -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
} }

View File

@ -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()
}
}