Skip to content

Commit

Permalink
Merge pull request #2 from Starttoaster/tests
Browse files Browse the repository at this point in the history
Add API method test framework and two tests
  • Loading branch information
Starttoaster authored Mar 26, 2024
2 parents 0241210 + ff37c28 commit fe6f9b7
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 2 deletions.
45 changes: 45 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package proxmox

import (
"crypto/tls"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
)

func setup(t *testing.T) (*http.ServeMux, *httptest.Server, *Client) {
mux := http.NewServeMux()
server := httptest.NewTLSServer(mux)

httpClient := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}

client, err := NewClient("test-token-id", "test-token",
WithBaseURL(fmt.Sprintf("%s/", server.URL)),
WithHTTPClient(&httpClient),
)
if err != nil {
t.Fatal(err)
}

return mux, server, client
}

func teardown(server *httptest.Server) {
server.Close()
}

func fixture(path string) string {
b, err := os.ReadFile("testdata/" + path)
if err != nil {
panic(err)
}
return string(b)
}
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ module github.com/starttoaster/go-proxmox

go 1.21

require github.com/google/go-querystring v1.1.0
require (
github.com/google/go-querystring v1.1.0
github.com/stretchr/testify v1.9.0
)

require github.com/google/go-cmp v0.6.0 // indirect
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
74 changes: 74 additions & 0 deletions nodes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package proxmox

import (
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/require"
)

func TestGetNodes(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

mux.HandleFunc("/api2/json/nodes", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprint(w, fixture("nodes/get_nodes.json"))
if err != nil {
return
}
})

want := GetNodesResponse{
Data: []GetNodesData{
{CPU: 0.0522061746389294, Disk: 5513633792, ID: "node/srv3", Level: "", MaxCPU: 8, MaxDisk: 100861726720, MaxMem: 16367738880, Mem: 14766223360, Node: "srv3", SslFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", Status: "online", Type: "node", Uptime: 418333},
{CPU: 0.0220677146311971, Disk: 5727686656, ID: "node/srv1", Level: "", MaxCPU: 16, MaxDisk: 100861726720, MaxMem: 134850498560, Mem: 45189853184, Node: "srv1", SslFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", Status: "online", Type: "node", Uptime: 418326},
{CPU: 0.0673548074849297, Disk: 5488590848, ID: "node/srv2", Level: "", MaxCPU: 8, MaxDisk: 100861726720, MaxMem: 16367742976, Mem: 13080690688, Node: "srv2", SslFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", Status: "online", Type: "node", Uptime: 418322},
},
}

r, resp, err := client.Nodes.GetNodes()
require.NoError(t, err)
require.NotNil(t, resp)
require.Equal(t, want, *r)
}

func TestGetNode(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

mux.HandleFunc("/api2/json/nodes/srv1/status", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprint(w, fixture("nodes/get_node_status.json"))
if err != nil {
return
}
})

want := GetNodeStatusResponse{
Data: GetNodeStatusData{
BootInfo: BootInfo{Mode: "efi", SecureBoot: 0},
CPU: 0.0282238002623366,
CPUInfo: CPUInfo{Cores: 50, Cpus: 200, 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 tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx avx512f avx512dq rdseed adx smap avx512ifma clflushopt intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid fsrm md_clear flush_l1d arch_capabilities", Hvm: "1", Mhz: "4886.225", Model: "99th Gen Intel(R) Core(TM) i19-9000 @ 6.50GHz", Sockets: 1, UserHz: 100},
CurrentKernel: CurrentKernel{Machine: "x86_64", Release: "6.5.11-8-pve", Sysname: "Linux", Version: "#1 SMP PREEMPT_DYNAMIC PMX 6.5.11-8 (2024-01-30T12:27Z)"},
Idle: 0,
Ksm: Ksm{Shared: 0},
Kversion: "Linux 6.5.11-8-pve #1 SMP PREEMPT_DYNAMIC PMX 6.5.11-8 (2024-01-30T12:27Z)",
LoadAvg: []string{"0.53", "0.46", "0.43"},
Memory: Memory{Free: 89574653952, Total: 134850498560, Used: 45275844608},
PveVersion: "pve-manager/0.0.0/0000000000000000",
RootFs: RootFs{Avail: 89962983424, Free: 95133720576, Total: 100861726720, Used: 5728006144},
Swap: Swap{Free: 8589930496, Total: 8589930496, Used: 0},
Uptime: 419090,
Wait: 0.00150768163794533,
},
}

r, resp, err := client.Nodes.GetNodeStatus("srv1")
require.NoError(t, err)
require.NotNil(t, resp)
require.Equal(t, want, *r)
}
54 changes: 54 additions & 0 deletions testdata/nodes/get_node_status.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"data":{
"idle":0,
"current-kernel":{
"version":"#1 SMP PREEMPT_DYNAMIC PMX 6.5.11-8 (2024-01-30T12:27Z)",
"sysname":"Linux",
"machine":"x86_64",
"release":"6.5.11-8-pve"
},
"swap":{
"free":8589930496,
"used":0,
"total":8589930496
},
"pveversion":"pve-manager/0.0.0/0000000000000000",
"cpuinfo":{
"cpus":200,
"model":"99th Gen Intel(R) Core(TM) i19-9000 @ 6.50GHz",
"cores":50,
"user_hz":100,
"mhz":"4886.225",
"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 tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx avx512f avx512dq rdseed adx smap avx512ifma clflushopt intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid fsrm md_clear flush_l1d arch_capabilities",
"hvm":"1"
},
"cpu":0.0282238002623366,
"boot-info":{
"secureboot":0,
"mode":"efi"
},
"memory":{
"total":134850498560,
"free":89574653952,
"used":45275844608
},
"ksm":{
"shared":0
},
"rootfs":{
"free":95133720576,
"used":5728006144,
"avail":89962983424,
"total":100861726720
},
"uptime":419090,
"kversion":"Linux 6.5.11-8-pve #1 SMP PREEMPT_DYNAMIC PMX 6.5.11-8 (2024-01-30T12:27Z)",
"loadavg":[
"0.53",
"0.46",
"0.43"
],
"wait":0.00150768163794533
}
}
49 changes: 49 additions & 0 deletions testdata/nodes/get_nodes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"data":[
{
"ssl_fingerprint":"00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00",
"level":"",
"uptime":418333,
"type":"node",
"cpu":0.0522061746389294,
"maxmem":16367738880,
"maxcpu":8,
"disk":5513633792,
"maxdisk":100861726720,
"node":"srv3",
"status":"online",
"mem":14766223360,
"id":"node/srv3"
},
{
"maxdisk":100861726720,
"mem":45189853184,
"status":"online",
"id":"node/srv1",
"node":"srv1",
"cpu":0.0220677146311971,
"level":"",
"ssl_fingerprint":"00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00",
"type":"node",
"uptime":418326,
"maxmem":134850498560,
"maxcpu":16,
"disk":5727686656
},
{
"mem":13080690688,
"status":"online",
"id":"node/srv2",
"node":"srv2",
"maxdisk":100861726720,
"maxmem":16367742976,
"disk":5488590848,
"maxcpu":8,
"cpu":0.0673548074849297,
"level":"",
"ssl_fingerprint":"00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00",
"type":"node",
"uptime":418322
}
]
}

0 comments on commit fe6f9b7

Please sign in to comment.