Skip to content

Commit 2caee4d

Browse files
committed
feat: Go implement GetDebugInfo
Signed-off-by: Fred Rolland <[email protected]>
1 parent 6099691 commit 2caee4d

File tree

2 files changed

+200
-2
lines changed

2 files changed

+200
-2
lines changed

entrypoint/internal/utils/host/host.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,33 @@ func (h *host) GetOSType(ctx context.Context) (string, error) {
117117

118118
// GetDebugInfo is the default implementation of the host.Interface.
119119
func (h *host) GetDebugInfo(ctx context.Context) (string, error) {
120-
// TODO: add implementation
121-
return "", nil
120+
var debugInfo strings.Builder
121+
122+
// Get OS release information
123+
osReleaseContent, err := h.os.ReadFile("/etc/os-release")
124+
if err != nil {
125+
debugInfo.WriteString(fmt.Sprintf("[os-release]: Error reading /etc/os-release: %v\n", err))
126+
} else {
127+
debugInfo.WriteString(fmt.Sprintf("[os-release]: %s\n", string(osReleaseContent)))
128+
}
129+
130+
// Get kernel information
131+
stdout, stderr, err := h.cmd.RunCommand(ctx, "uname", "-a")
132+
if err != nil {
133+
debugInfo.WriteString(fmt.Sprintf("[uname -a]: Error executing uname -a: %v (stderr: %s)\n", err, stderr))
134+
} else {
135+
debugInfo.WriteString(fmt.Sprintf("[uname -a]: %s\n", stdout))
136+
}
137+
138+
// Get memory information
139+
stdout, stderr, err = h.cmd.RunCommand(ctx, "free", "-m")
140+
if err != nil {
141+
debugInfo.WriteString(fmt.Sprintf("[free -m]: Error executing free -m: %v (stderr: %s)\n", err, stderr))
142+
} else {
143+
debugInfo.WriteString(fmt.Sprintf("[free -m]: %s\n", stdout))
144+
}
145+
146+
return debugInfo.String(), nil
122147
}
123148

124149
// LoadedModule contains information about loaded kernel module.

entrypoint/internal/utils/host/host_test.go

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,179 @@ VERSION_ID="invalid-version"`
362362
})
363363
})
364364

365+
Context("GetDebugInfo", func() {
366+
var (
367+
h Interface
368+
cmdMock *cmdMockPkg.Interface
369+
osMock *osMockPkg.OSWrapper
370+
)
371+
372+
BeforeEach(func() {
373+
cmdMock = cmdMockPkg.NewInterface(GinkgoT())
374+
osMock = osMockPkg.NewOSWrapper(GinkgoT())
375+
h = New(cmdMock, osMock)
376+
})
377+
378+
It("should return debug info with all successful operations", func() {
379+
osReleaseContent := `PRETTY_NAME="Ubuntu 22.04.3 LTS"
380+
NAME="Ubuntu"
381+
VERSION_ID="22.04"
382+
ID=ubuntu`
383+
unameOutput := "Linux hostname 5.15.0-91-generic #101-Ubuntu SMP Thu Nov 16 18:13:39 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux"
384+
freeOutput := ` total used free shared buff/cache available
385+
Mem: 15947 1234 5678 123 8901 14000
386+
Swap: 2048 0 2048`
387+
388+
osMock.EXPECT().ReadFile("/etc/os-release").Return([]byte(osReleaseContent), nil)
389+
cmdMock.EXPECT().RunCommand(context.Background(), "uname", "-a").Return(unameOutput, "", nil)
390+
cmdMock.EXPECT().RunCommand(context.Background(), "free", "-m").Return(freeOutput, "", nil)
391+
392+
debugInfo, err := h.GetDebugInfo(context.Background())
393+
Expect(err).ToNot(HaveOccurred())
394+
Expect(debugInfo).To(ContainSubstring("[os-release]: " + osReleaseContent))
395+
Expect(debugInfo).To(ContainSubstring("[uname -a]: " + unameOutput))
396+
Expect(debugInfo).To(ContainSubstring("[free -m]: " + freeOutput))
397+
})
398+
399+
It("should handle os-release read error gracefully", func() {
400+
unameOutput := "Linux hostname 5.15.0-91-generic #101-Ubuntu SMP Thu Nov 16 18:13:39 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux"
401+
freeOutput := ` total used free shared buff/cache available
402+
Mem: 15947 1234 5678 123 8901 14000
403+
Swap: 2048 0 2048`
404+
405+
osMock.EXPECT().ReadFile("/etc/os-release").Return(nil, assert.AnError)
406+
cmdMock.EXPECT().RunCommand(context.Background(), "uname", "-a").Return(unameOutput, "", nil)
407+
cmdMock.EXPECT().RunCommand(context.Background(), "free", "-m").Return(freeOutput, "", nil)
408+
409+
debugInfo, err := h.GetDebugInfo(context.Background())
410+
Expect(err).ToNot(HaveOccurred())
411+
Expect(debugInfo).To(ContainSubstring("[os-release]: Error reading /etc/os-release: assert.AnError general error for testing"))
412+
Expect(debugInfo).To(ContainSubstring("[uname -a]: " + unameOutput))
413+
Expect(debugInfo).To(ContainSubstring("[free -m]: " + freeOutput))
414+
})
415+
416+
It("should handle uname command error gracefully", func() {
417+
osReleaseContent := `PRETTY_NAME="Ubuntu 22.04.3 LTS"
418+
NAME="Ubuntu"
419+
ID=ubuntu`
420+
freeOutput := ` total used free shared buff/cache available
421+
Mem: 15947 1234 5678 123 8901 14000
422+
Swap: 2048 0 2048`
423+
424+
osMock.EXPECT().ReadFile("/etc/os-release").Return([]byte(osReleaseContent), nil)
425+
cmdMock.EXPECT().RunCommand(context.Background(), "uname", "-a").Return("", "command not found", assert.AnError)
426+
cmdMock.EXPECT().RunCommand(context.Background(), "free", "-m").Return(freeOutput, "", nil)
427+
428+
debugInfo, err := h.GetDebugInfo(context.Background())
429+
Expect(err).ToNot(HaveOccurred())
430+
Expect(debugInfo).To(ContainSubstring("[os-release]: " + osReleaseContent))
431+
Expect(debugInfo).To(ContainSubstring("[uname -a]: Error executing uname -a: assert.AnError general error for testing (stderr: command not found)"))
432+
Expect(debugInfo).To(ContainSubstring("[free -m]: " + freeOutput))
433+
})
434+
435+
It("should handle free command error gracefully", func() {
436+
osReleaseContent := `PRETTY_NAME="Ubuntu 22.04.3 LTS"
437+
NAME="Ubuntu"
438+
ID=ubuntu`
439+
unameOutput := "Linux hostname 5.15.0-91-generic #101-Ubuntu SMP Thu Nov 16 18:13:39 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux"
440+
441+
osMock.EXPECT().ReadFile("/etc/os-release").Return([]byte(osReleaseContent), nil)
442+
cmdMock.EXPECT().RunCommand(context.Background(), "uname", "-a").Return(unameOutput, "", nil)
443+
cmdMock.EXPECT().RunCommand(context.Background(), "free", "-m").Return("", "permission denied", assert.AnError)
444+
445+
debugInfo, err := h.GetDebugInfo(context.Background())
446+
Expect(err).ToNot(HaveOccurred())
447+
Expect(debugInfo).To(ContainSubstring("[os-release]: " + osReleaseContent))
448+
Expect(debugInfo).To(ContainSubstring("[uname -a]: " + unameOutput))
449+
Expect(debugInfo).To(ContainSubstring("[free -m]: Error executing free -m: assert.AnError general error for testing (stderr: permission denied)"))
450+
})
451+
452+
It("should handle all operations failing gracefully", func() {
453+
osMock.EXPECT().ReadFile("/etc/os-release").Return(nil, assert.AnError)
454+
cmdMock.EXPECT().RunCommand(context.Background(), "uname", "-a").Return("", "command not found", assert.AnError)
455+
cmdMock.EXPECT().RunCommand(context.Background(), "free", "-m").Return("", "permission denied", assert.AnError)
456+
457+
debugInfo, err := h.GetDebugInfo(context.Background())
458+
Expect(err).ToNot(HaveOccurred())
459+
Expect(debugInfo).To(ContainSubstring("[os-release]: Error reading /etc/os-release: assert.AnError general error for testing"))
460+
Expect(debugInfo).To(ContainSubstring("[uname -a]: Error executing uname -a: assert.AnError general error for testing (stderr: command not found)"))
461+
Expect(debugInfo).To(ContainSubstring("[free -m]: Error executing free -m: assert.AnError general error for testing (stderr: permission denied)"))
462+
})
463+
464+
It("should handle empty outputs from commands", func() {
465+
osReleaseContent := `PRETTY_NAME="Ubuntu 22.04.3 LTS"
466+
NAME="Ubuntu"
467+
ID=ubuntu`
468+
469+
osMock.EXPECT().ReadFile("/etc/os-release").Return([]byte(osReleaseContent), nil)
470+
cmdMock.EXPECT().RunCommand(context.Background(), "uname", "-a").Return("", "", nil)
471+
cmdMock.EXPECT().RunCommand(context.Background(), "free", "-m").Return("", "", nil)
472+
473+
debugInfo, err := h.GetDebugInfo(context.Background())
474+
Expect(err).ToNot(HaveOccurred())
475+
Expect(debugInfo).To(ContainSubstring("[os-release]: " + osReleaseContent))
476+
Expect(debugInfo).To(ContainSubstring("[uname -a]: \n"))
477+
Expect(debugInfo).To(ContainSubstring("[free -m]: \n"))
478+
})
479+
480+
It("should handle multiline os-release content", func() {
481+
osReleaseContent := `PRETTY_NAME="Red Hat Enterprise Linux 9.2 (Plow)"
482+
NAME="Red Hat Enterprise Linux"
483+
VERSION="9.2 (Plow)"
484+
ID="rhel"
485+
ID_LIKE="fedora"
486+
VERSION_ID="9.2"
487+
PLATFORM_ID="platform:el9"
488+
PRETTY_NAME="Red Hat Enterprise Linux 9.2 (Plow)"
489+
ANSI_COLOR="0;31"
490+
LOGO="fedora-logo-icon"
491+
CPE_NAME="cpe:/o:redhat:enterprise_linux:9::baseos"
492+
HOME_URL="https://www.redhat.com/"
493+
DOCUMENTATION_URL="https://access.redhat.com/documentation/red_hat_enterprise_linux/9/"
494+
SUPPORT_URL="https://access.redhat.com/support"
495+
BUG_REPORT_URL="https://bugzilla.redhat.com/"
496+
REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 9"
497+
REDHAT_BUGZILLA_PRODUCT_VERSION=9.2
498+
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
499+
REDHAT_SUPPORT_PRODUCT_VERSION="9.2"`
500+
unameOutput := "Linux rhel-host 5.14.0-284.11.1.el9_2.x86_64 #1 SMP PREEMPT_DYNAMIC Tue May 9 10:23:07 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux"
501+
freeOutput := ` total used free shared buff/cache available
502+
Mem: 32000 8000 12000 500 12000 23000
503+
Swap: 4096 0 4096`
504+
505+
osMock.EXPECT().ReadFile("/etc/os-release").Return([]byte(osReleaseContent), nil)
506+
cmdMock.EXPECT().RunCommand(context.Background(), "uname", "-a").Return(unameOutput, "", nil)
507+
cmdMock.EXPECT().RunCommand(context.Background(), "free", "-m").Return(freeOutput, "", nil)
508+
509+
debugInfo, err := h.GetDebugInfo(context.Background())
510+
Expect(err).ToNot(HaveOccurred())
511+
Expect(debugInfo).To(ContainSubstring("[os-release]: " + osReleaseContent))
512+
Expect(debugInfo).To(ContainSubstring("[uname -a]: " + unameOutput))
513+
Expect(debugInfo).To(ContainSubstring("[free -m]: " + freeOutput))
514+
})
515+
516+
It("should handle commands with stderr output but no error", func() {
517+
osReleaseContent := `PRETTY_NAME="Ubuntu 22.04.3 LTS"
518+
NAME="Ubuntu"
519+
ID=ubuntu`
520+
unameOutput := "Linux hostname 5.15.0-91-generic #101-Ubuntu SMP Thu Nov 16 18:13:39 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux"
521+
unameStderr := "warning: some warning message"
522+
freeOutput := ` total used free shared buff/cache available
523+
Mem: 15947 1234 5678 123 8901 14000
524+
Swap: 2048 0 2048`
525+
526+
osMock.EXPECT().ReadFile("/etc/os-release").Return([]byte(osReleaseContent), nil)
527+
cmdMock.EXPECT().RunCommand(context.Background(), "uname", "-a").Return(unameOutput, unameStderr, nil)
528+
cmdMock.EXPECT().RunCommand(context.Background(), "free", "-m").Return(freeOutput, "", nil)
529+
530+
debugInfo, err := h.GetDebugInfo(context.Background())
531+
Expect(err).ToNot(HaveOccurred())
532+
Expect(debugInfo).To(ContainSubstring("[os-release]: " + osReleaseContent))
533+
Expect(debugInfo).To(ContainSubstring("[uname -a]: " + unameOutput))
534+
Expect(debugInfo).To(ContainSubstring("[free -m]: " + freeOutput))
535+
})
536+
})
537+
365538
Context("Caching behavior", func() {
366539
var (
367540
h Interface

0 commit comments

Comments
 (0)