Skip to content

Commit

Permalink
refactor(gnostats): move osVersion retrieval to agent
Browse files Browse the repository at this point in the history
  • Loading branch information
aeddi committed Aug 27, 2024
1 parent fdfcab5 commit 83d08e5
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 262 deletions.
10 changes: 1 addition & 9 deletions contribs/gnostats/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ func randomStringOfLengthInRange(t *testing.T, random *mrand.Rand, min, max int)
func randomNodeInfo(t *testing.T, random *mrand.Rand) p2p.NodeInfo {
t.Helper()

goos := []string{"aix", "android", "darwin", "dragonfly", "freebsd", "illumos", "ios", "js", "linux", "netbsd", "openbsd", "plan9", "solaris", "windows"}
goarch := []string{"386", "amd64", "arm", "arm64", "mips", "mips64", "mips64le", "mipsle", "ppc64", "ppc64le", "riscv64", "s390x", "wasm"}

return p2p.NodeInfo{
Moniker: randomStringOfLengthInRange(t, random, 1, 128),
NetAddress: p2p.NewNetAddress(
Expand All @@ -113,10 +110,6 @@ func randomNodeInfo(t *testing.T, random *mrand.Rand) p2p.NodeInfo {
),
},
),
Other: p2p.NodeInfoOther{
OS: goos[randomIntInRange(t, random, 0, len(goos)-1)],
Arch: goarch[randomIntInRange(t, random, 0, len(goarch)-1)],
},
}
}

Expand Down Expand Up @@ -198,8 +191,7 @@ func TestAgent_E2E(t *testing.T) {

go agent.Start(ctx)
static := <-mockHub.static
osVersion := fmt.Sprintf("%s - %s", status.NodeInfo.Other.OS, status.NodeInfo.Other.Arch)
compareStatusRespToStaticInfo(t, status, osVersion, static)
compareStatusRespToStaticInfo(t, status, static)

// Test if the first five data pushes to the Hub work as expected
for i := 0; i < 5; i++ {
Expand Down
16 changes: 2 additions & 14 deletions contribs/gnostats/agent/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package agent
import (
"context"
"fmt"
"runtime"

ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types"

Expand Down Expand Up @@ -127,24 +128,11 @@ func (c *collector) CollectStatic(ctx context.Context) (*proto.StaticInfo, error
// Cast response to the appropriate type
status := results[0].(*ctypes.ResultStatus)

// Format the OsVersion string
var (
osVersion = ""
os = status.NodeInfo.Other.OS
arch = status.NodeInfo.Other.Arch
)
if os != "" && arch != "" {
osVersion = fmt.Sprintf("%s - %s", os, arch)
} else if os != "" {
osVersion = os
}

// Fill the StaticInfo fields with the corresponding values
return &proto.StaticInfo{
Address: status.NodeInfo.ID().String(),
GnoVersion: status.NodeInfo.Version,
OsVersion: osVersion,
Location: "", // @TODO: see comment in makeNodeInfo
OsVersion: fmt.Sprintf("%s - %s", runtime.GOOS, runtime.GOARCH),
}, nil
}

Expand Down
76 changes: 21 additions & 55 deletions contribs/gnostats/agent/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package agent
import (
"context"
"fmt"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -112,11 +113,6 @@ func getBatchResults(t *testing.T) []any {
str: "0.0.0.0",
},
),
Other: p2p.NodeInfoOther{
OS: "plan9",
Arch: "ppc64",
Location: "",
},
},
},

Expand Down Expand Up @@ -199,6 +195,7 @@ func TestCollector_DynamicSuccess(t *testing.T) {
mockBatch.On("Block", (*uint64)(nil)).Return(nil)
mockBatch.On("BlockResults", (*uint64)(nil)).Return(nil)

// Get predefined RPC batch results
ctx := context.Background()
results := getBatchResults(t)
mockBatch.On("Send", ctx).Return(results, nil)
Expand Down Expand Up @@ -260,69 +257,38 @@ func TestCollector_DynamicTimeout(t *testing.T) {
assert.Nil(t, dynamicInfo)
}

func compareStatusRespToStaticInfo(t *testing.T, status *ctypes.ResultStatus, osExpected string, staticInfo *proto.StaticInfo) {
func compareStatusRespToStaticInfo(t *testing.T, status *ctypes.ResultStatus, staticInfo *proto.StaticInfo) {
t.Helper()

assert.Equal(t, staticInfo.Address, status.NodeInfo.ID().String())
assert.Equal(t, staticInfo.GnoVersion, status.NodeInfo.Version)
assert.Equal(t, staticInfo.OsVersion, osExpected)
assert.Equal(t, staticInfo.Location, status.NodeInfo.Other.Location)
assert.Equal(t, staticInfo.OsVersion, fmt.Sprintf("%s - %s", runtime.GOOS, runtime.GOARCH))
}

func TestCollector_StaticSuccess(t *testing.T) {
t.Parallel()

// Get predefined OS and Arch values
var (
status = getBatchResults(t)[0].(*ctypes.ResultStatus)
os = status.NodeInfo.Other.OS
arch = status.NodeInfo.Other.Arch
)

// Setup multiple test cases (variation of the OsVersion)
testCases := []struct {
name string
os string
arch string
expected string
}{
{"Both OS and Arch", os, arch, fmt.Sprintf("%s - %s", os, arch)},
{"Only OS", os, "", os},
{"Only Arch", "", arch, ""},
{"Neither OS or Arch", "", "", ""},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()

// Setup RPC mocks
mockCaller := new(MockRPCClient)
mockBatch := new(MockRPCBatch)

mockCaller.On("NewBatch").Return(mockBatch)
mockBatch.On("Status").Return(nil)
// Setup RPC mocks
mockCaller := new(MockRPCClient)
mockBatch := new(MockRPCBatch)

// Get predefined RPC batch results
results := getBatchResults(t)
status := results[0].(*ctypes.ResultStatus)
mockCaller.On("NewBatch").Return(mockBatch)
mockBatch.On("Status").Return(nil)

// Override OS and Arch in the Status RPC results
status.NodeInfo.Other.OS = testCase.os
status.NodeInfo.Other.Arch = testCase.arch
ctx := context.Background()
mockBatch.On("Send", ctx).Return(results, nil)
// Get predefined RPC batch results
ctx := context.Background()
results := getBatchResults(t)
mockBatch.On("Send", ctx).Return(results, nil)

// Call the actual method to test (CollectStatic)
c := &collector{caller: mockCaller}
staticInfo, err := c.CollectStatic(ctx)
// Call the actual method to test (CollectStatic)
c := &collector{caller: mockCaller}
staticInfo, err := c.CollectStatic(ctx)

// Assert that all expectations were met
assert.NoError(t, err)
assert.NotNil(t, staticInfo)
compareStatusRespToStaticInfo(t, status, testCase.expected, staticInfo)
})
}
// Assert that all expectations were met
assert.NoError(t, err)
assert.NotNil(t, staticInfo)
status := results[0].(*ctypes.ResultStatus)
compareStatusRespToStaticInfo(t, status, staticInfo)
}

func TestCollector_StaticFail(t *testing.T) {
Expand Down
Loading

0 comments on commit 83d08e5

Please sign in to comment.