Skip to content

Commit

Permalink
[support] Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Sep 15, 2024
1 parent 8394fc4 commit b9bb86c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
6 changes: 5 additions & 1 deletion support/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ type Check struct {

// ////////////////////////////////////////////////////////////////////////////////// //

var buildInfoProvider = debug.ReadBuildInfo

// ////////////////////////////////////////////////////////////////////////////////// //

// Collect collects basic info about system
func Collect(app, ver string) *Info {
bin, _ := os.Executable()
Expand Down Expand Up @@ -435,7 +439,7 @@ func (i *Info) appendBuildInfo() {

bin, _ := os.Executable()
binSHA := hash.FileHash(bin)
info, ok := debug.ReadBuildInfo()
info, ok := buildInfoProvider()

if ok {
for _, s := range info.Settings {
Expand Down
56 changes: 55 additions & 1 deletion support/support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package support

import (
"os"
"runtime/debug"
"testing"

"github.com/essentialkaos/ek/v13/fmtc"
Expand Down Expand Up @@ -51,6 +52,8 @@ require (
func (s *SupportSuite) TestCollect(c *C) {
i := Collect("test", "1.2.3")

c.Assert(i, NotNil)

os.Setenv("SUPPORT_VAR", "123")

chks := []Check{
Expand Down Expand Up @@ -85,7 +88,29 @@ func (s *SupportSuite) TestCollect(c *C) {
App{},
}

c.Assert(i, NotNil)
resources := &ResourcesInfo{
CPU: []CPUInfo{
{
Model: "Virtual",
Threads: 4,
Cores: 2,
},
},
MemTotal: 8 * 1024 * 1024,
MemFree: 6 * 1024 * 1024,
MemUsed: 2 * 1024 * 1024,
SwapTotal: 2 * 1024 * 1024,
SwapFree: 1 * 1024 * 1024,
SwapUsed: 1 * 1024 * 1024,
}

params := []KernelParam{
{"fs.inotify.max_user_watches", "27024"},
{"kernel.random.boot_id", "3cfe3c24-c698-42fc-b232-af5345f828f7"},
}

i.Build.CGO = true

c.Assert(i.WithDeps(deps), NotNil)
c.Assert(i.WithRevision(""), NotNil)
c.Assert(i.WithRevision("1234567"), NotNil)
Expand All @@ -96,6 +121,14 @@ func (s *SupportSuite) TestCollect(c *C) {
c.Assert(i.WithEnvVars("", "SUPPORT_VAR", "TERM", "CI"), NotNil)
c.Assert(i.WithNetwork(&NetworkInfo{PublicIP: "192.168.1.1", Hostname: "test.loc"}), NotNil)
c.Assert(i.WithFS([]FSInfo{FSInfo{}, FSInfo{"/", "/dev/vda1", "ext4", 1000, 10000}}), NotNil)
c.Assert(i.WithResources(resources), NotNil)
c.Assert(i.WithKernel(params), NotNil)

i.Print()

i.Resources.SwapTotal = 0
i.Resources.SwapFree = 0
i.Resources.SwapUsed = 0

i.Print()

Expand All @@ -109,6 +142,8 @@ func (s *SupportSuite) TestCollect(c *C) {
i.Network = nil
i.FS = nil
i.Deps = nil
i.Resources = nil
i.Kernel = nil

i.Print()

Expand Down Expand Up @@ -136,6 +171,8 @@ func (s *SupportSuite) TestNil(c *C) {
c.Assert(i.WithEnvVars(""), IsNil)
c.Assert(i.WithNetwork(nil), IsNil)
c.Assert(i.WithFS(nil), IsNil)
c.Assert(i.WithResources(nil), IsNil)
c.Assert(i.WithKernel(nil), IsNil)

c.Assert(func() { i.Print() }, NotPanics)
}
Expand Down Expand Up @@ -163,3 +200,20 @@ func (s *SupportSuite) TestSizeCalc(c *C) {
FSInfo{Device: "/dev/sda1"}, FSInfo{Device: "/dev/test/test"}, FSInfo{Device: "/dev"},
}), Equals, 14)
}

func (s *SupportSuite) TestBuildInfo(c *C) {
buildInfoProvider = func() (*debug.BuildInfo, bool) {
return &debug.BuildInfo{
Settings: []debug.BuildSetting{
{"CGO_ENABLED", "1"},
{"vcs.revision", "8b6e70d9dce17f98595dd364bd6f699f8608b46e"},
},
}, true
}

i := Collect("test", "1.2.3")

c.Assert(i, NotNil)

buildInfoProvider = debug.ReadBuildInfo
}

0 comments on commit b9bb86c

Please sign in to comment.