Skip to content

Commit

Permalink
add node and node version test (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
luthermonson authored Sep 9, 2023
1 parent 15ed1f1 commit 37d0caa
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 8 deletions.
7 changes: 3 additions & 4 deletions nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ func (c *Client) Nodes() (ns NodeStatuses, err error) {
return ns, c.Get("/nodes", &ns)
}

func (c *Client) Node(name string) (*Node, error) {
var node Node
func (c *Client) Node(name string) (node *Node, err error) {
if err := c.Get(fmt.Sprintf("/nodes/%s/status", name), &node); err != nil {
return nil, err
}
node.Name = name
node.client = c

return &node, nil
return
}

func (n *Node) Version() (version *Version, err error) {
return version, n.client.Get("/nodes/%s/version", &version)
return version, n.client.Get(fmt.Sprintf("/nodes/%s/version", n.Name), &version)
}

func (n *Node) TermProxy() (vnc *VNC, err error) {
Expand Down
19 changes: 19 additions & 0 deletions nodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,22 @@ func TestClient_Nodes(t *testing.T) {
}
//assert.Equal(t, 6, len(testData))
}

func TestClient_Node(t *testing.T) {
mocks.On(mockConfig)
defer mocks.Off()
client := mockClient()

node, err := client.Node("node1")
assert.Nil(t, err)
assert.Equal(t, "node1", node.Name)
assert.NotNil(t, node.client)

v, err := node.Version()
assert.Nil(t, err)
assert.Equal(t, "7.4", v.Release)

node, err = client.Node("doesntexist")
assert.NotNil(t, err)
assert.Nil(t, node)
}
69 changes: 69 additions & 0 deletions tests/mocks/pve7x/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,4 +761,73 @@ func nodes() {
}
}`)

gock.New(config.C.URI).
Persist().
Get("^/nodes/node1/status$").
Reply(200).
JSON(`{
"data": {
"idle": 0,
"cpu": 0.00260552371026576,
"ksm": {
"shared": 0
},
"swap": {
"total": 0,
"free": 0,
"used": 0
},
"pveversion": "pve-manager/7.4-16/0f39f621",
"wait": 0,
"uptime": 2501631,
"kversion": "Linux 5.15.108-1-pve #1 SMP PVE 5.15.108-2 (2023-07-20T10:06Z)",
"cpuinfo": {
"mhz": "3400.000",
"model": "Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz",
"sockets": 1,
"flags": "fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single pti tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp",
"hvm": "1",
"cores": 4,
"user_hz": 100,
"cpus": 8
},
"loadavg": [
"0.00",
"0.00",
"0.00"
],
"memory": {
"total": 65919459328,
"free": 57824059392,
"used": 8095399936
},
"rootfs": {
"total": 948338819072,
"free": 937851224064,
"used": 10487595008,
"avail": 937851224064
}
}
}`)

gock.New(config.C.URI).
Persist().
Get("^/nodes/doesntexist/status$").
Reply(500).
JSON(`{
"data": null
}`)

gock.New(config.C.URI).
Persist().
Get("^/nodes/node1/version$").
Reply(200).
JSON(`{
"data": {
"release": "7.4",
"version": "7.4-16",
"repoid": "0f39f621"
}
}`)

}
8 changes: 4 additions & 4 deletions tests/mocks/pve7x/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ func version() {
}
}`
gock.New(config.C.URI).
Get("/version").
Get("^/version$").
Reply(200).
JSON(versionJSON)

gock.New(config.C.URI).
Post("/version"). // fake to test client Post method
Post("^/version$"). // fake to test client Post method
Reply(200).
JSON(versionJSON)

gock.New(config.C.URI).
Put("/version"). // fake to test client Put method
Put("^/version$"). // fake to test client Put method
Reply(200).
JSON(versionJSON)

gock.New(config.C.URI).
Delete("/version"). // fake to test client Delete method
Delete("^/version$"). // fake to test client Delete method
Reply(200).
JSON(versionJSON)
}

0 comments on commit 37d0caa

Please sign in to comment.