Skip to content

Commit 745523b

Browse files
committed
added more test
1 parent 534dc4c commit 745523b

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

clients/fetch/fetch_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package fetch
2+
3+
import (
4+
"device-chronicle-client/models"
5+
"github.com/stretchr/testify/assert"
6+
"github.com/stretchr/testify/mock"
7+
"runtime"
8+
"testing"
9+
)
10+
11+
// MockOS is a mock that simulates the OS interface
12+
type MockOS struct {
13+
mock.Mock
14+
}
15+
16+
// Linux mocks the os.Linux function
17+
func (m *MockOS) Linux() (*models.System, error) {
18+
args := m.Called()
19+
if args.Get(0) == nil {
20+
return nil, args.Error(1)
21+
}
22+
return args.Get(0).(*models.System), args.Error(1)
23+
}
24+
25+
// TestFetchData tests the FetchData function
26+
func TestFetchData(t *testing.T) {
27+
// We can only test the expected behavior for the current OS
28+
system, err := FetchData()
29+
30+
if runtime.GOOS == "linux" {
31+
assert.NoError(t, err)
32+
assert.NotNil(t, system)
33+
34+
// Verify system has expected structure
35+
assert.NotEmpty(t, system.Hostname)
36+
assert.NotNil(t, system.CPUCores)
37+
} else {
38+
assert.Error(t, err)
39+
assert.Nil(t, system)
40+
assert.Contains(t, err.Error(), "unsupported OS")
41+
}
42+
}

clients/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
1818
github.com/pmezard/go-difflib v1.0.0 // indirect
1919
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
20+
github.com/stretchr/objx v0.5.2 // indirect
2021
github.com/tklauser/go-sysconf v0.3.12 // indirect
2122
github.com/tklauser/numcpus v0.6.1 // indirect
2223
github.com/yusufpapurcu/wmi v1.2.4 // indirect

clients/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF
2424
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
2525
github.com/shirou/gopsutil/v4 v4.24.11 h1:WaU9xqGFKvFfsUv94SXcUPD7rCkU0vr/asVdQOBZNj8=
2626
github.com/shirou/gopsutil/v4 v4.24.11/go.mod h1:s4D/wg+ag4rG0WO7AiTj2BeYCRhym0vM7DHbZRxnIT8=
27+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
28+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
2729
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
2830
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
2931
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=

0 commit comments

Comments
 (0)