From 77dc29894032c209b907284ba69c4c7b12842259 Mon Sep 17 00:00:00 2001 From: Luther Monson Date: Fri, 8 Sep 2023 21:14:28 -0700 Subject: [PATCH] add node and node version test --- nodes.go | 7 ++-- nodes_test.go | 19 ++++++++++ tests/mocks/pve7x/nodes.go | 69 ++++++++++++++++++++++++++++++++++++ tests/mocks/pve7x/version.go | 8 ++--- 4 files changed, 95 insertions(+), 8 deletions(-) diff --git a/nodes.go b/nodes.go index 665c33c..fddb51b 100644 --- a/nodes.go +++ b/nodes.go @@ -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) { diff --git a/nodes_test.go b/nodes_test.go index 50ec2f0..5c70b1b 100644 --- a/nodes_test.go +++ b/nodes_test.go @@ -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) +} diff --git a/tests/mocks/pve7x/nodes.go b/tests/mocks/pve7x/nodes.go index 282e4b6..751f6c7 100644 --- a/tests/mocks/pve7x/nodes.go +++ b/tests/mocks/pve7x/nodes.go @@ -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" + } +}`) + } diff --git a/tests/mocks/pve7x/version.go b/tests/mocks/pve7x/version.go index 0f5d72a..7db700f 100644 --- a/tests/mocks/pve7x/version.go +++ b/tests/mocks/pve7x/version.go @@ -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) }