Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pkg/collector/nodelogs_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ func (collector *NodeLogsCollector) Collect() error {
nodeLogs := strings.Fields(os.Getenv("DIAGNOSTIC_NODELOGS_LIST"))

for _, nodeLog := range nodeLogs {
normalizedNodeLog := strings.Replace(nodeLog, "/", "_", -1)
if normalizedNodeLog[0] == '_' {
normalizedNodeLog = normalizedNodeLog[1:]
}

output, err := utils.ReadFileContent(nodeLog)
if err != nil {
return err
}

collector.data["nodeLog"] = output

collector.data[normalizedNodeLog] = output
}

return nil
Expand Down
29 changes: 23 additions & 6 deletions pkg/collector/nodelogs_collector_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
package collector

import (
"fmt"
"os"
"testing"
)

func TestNodeLogsCollector(t *testing.T) {
// TODO: Avoid using real files for testing. These are chosen because they happen to exist in
// common Linux distros (including Ubuntu on WSL) as well as the Ubuntu GitHub workflow host,
// but they won't exist in every environment we might wish to run tests.
file1 := "/var/log/alternatives.log"
file1Key := "var_log_alternatives.log"

file2 := "/var/log/dpkg.log"
file2Key := "var_log_dpkg.log"

tests := []struct {
name string
want int
wantKeys []string
wantErr bool
collectorName string
}{
{
name: "get node logs",
want: 1,
wantKeys: []string{file1Key, file2Key},
wantErr: false,
collectorName: "nodelogs",
},
}

c := NewNodeLogsCollector()

if err := os.Setenv("DIAGNOSTIC_NODELOGS_LIST", "/var/log/cloud-init.log"); err != nil {
if err := os.Setenv("DIAGNOSTIC_NODELOGS_LIST", fmt.Sprintf("%s %s", file1, file2)); err != nil {
t.Fatalf("Setenv: %v", err)
}

Expand All @@ -33,10 +43,17 @@ func TestNodeLogsCollector(t *testing.T) {
if (err != nil) != tt.wantErr {
t.Errorf("Collect() error = %v, wantErr %v", err, tt.wantErr)
}
raw := c.GetData()

if len(raw) < tt.want {
t.Errorf("len(GetData()) = %v, want %v", len(raw), tt.want)
dataItems := c.GetData()
if len(dataItems) != len(tt.wantKeys) {
t.Errorf("len(GetData()) = %v, want %v", len(dataItems), len(tt.wantKeys))
}

for _, fileKey := range tt.wantKeys {
_, ok := dataItems[fileKey]
if !ok {
t.Errorf("Missing file key %s", fileKey)
}
}

name := c.GetName()
Expand Down