From a533916e943ecb192506f07e044b01ad2ed4f0be Mon Sep 17 00:00:00 2001 From: Starttoaster Date: Mon, 25 Mar 2024 23:05:32 -0700 Subject: [PATCH 1/3] Add a test pattern base --- client_test.go | 45 +++++++++++++++++++++++++++++++++++ go.mod | 12 ++++++++-- go.sum | 10 ++++++++ nodes_test.go | 32 +++++++++++++++++++++++++ testdata/nodes/get_nodes.json | 1 + 5 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 client_test.go create mode 100644 nodes_test.go create mode 100644 testdata/nodes/get_nodes.json diff --git a/client_test.go b/client_test.go new file mode 100644 index 0000000..7165b39 --- /dev/null +++ b/client_test.go @@ -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) +} diff --git a/go.mod b/go.mod index 712f44e..2742b8e 100755 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum index b2abdf1..d04d123 100755 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/nodes_test.go b/nodes_test.go new file mode 100644 index 0000000..0d94761 --- /dev/null +++ b/nodes_test.go @@ -0,0 +1,32 @@ +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("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{}, + } + + r, resp, err := client.Nodes.GetNodes() + require.NoError(t, err) + require.NotNil(t, resp) + require.Equal(t, want, *r) +} diff --git a/testdata/nodes/get_nodes.json b/testdata/nodes/get_nodes.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/testdata/nodes/get_nodes.json @@ -0,0 +1 @@ +{} From c504906b433092e07d506b76a5db37caa01e1563 Mon Sep 17 00:00:00 2001 From: Starttoaster Date: Tue, 26 Mar 2024 00:00:31 -0700 Subject: [PATCH 2/3] Add some base test data and a test --- nodes_test.go | 23 ++++++++++++ testdata/nodes/get_node_status.json | 54 +++++++++++++++++++++++++++++ testdata/nodes/get_nodes.json | 50 +++++++++++++++++++++++++- 3 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 testdata/nodes/get_node_status.json diff --git a/nodes_test.go b/nodes_test.go index 0d94761..c469e76 100644 --- a/nodes_test.go +++ b/nodes_test.go @@ -30,3 +30,26 @@ func TestGetNodes(t *testing.T) { require.NotNil(t, resp) require.Equal(t, want, *r) } + +func TestGetNode(t *testing.T) { + mux, server, client := setup(t) + defer teardown(server) + + mux.HandleFunc("nodes/srv1", 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{}, + } + + r, resp, err := client.Nodes.GetNodeStatus("srv1") + require.NoError(t, err) + require.NotNil(t, resp) + require.Equal(t, want, *r) +} diff --git a/testdata/nodes/get_node_status.json b/testdata/nodes/get_node_status.json new file mode 100644 index 0000000..efbd9ee --- /dev/null +++ b/testdata/nodes/get_node_status.json @@ -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 + } +} diff --git a/testdata/nodes/get_nodes.json b/testdata/nodes/get_nodes.json index 0967ef4..5146d2f 100644 --- a/testdata/nodes/get_nodes.json +++ b/testdata/nodes/get_nodes.json @@ -1 +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 + } + ] +} From ff37c28ac2974a1440271174aef8f7f7b34aaf10 Mon Sep 17 00:00:00 2001 From: Starttoaster Date: Tue, 26 Mar 2024 00:24:32 -0700 Subject: [PATCH 3/3] Add comparison struct to tests --- nodes_test.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/nodes_test.go b/nodes_test.go index c469e76..9ea7a42 100644 --- a/nodes_test.go +++ b/nodes_test.go @@ -12,7 +12,7 @@ func TestGetNodes(t *testing.T) { mux, server, client := setup(t) defer teardown(server) - mux.HandleFunc("nodes", func(w http.ResponseWriter, r *http.Request) { + 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")) @@ -22,7 +22,11 @@ func TestGetNodes(t *testing.T) { }) want := GetNodesResponse{ - Data: []GetNodesData{}, + 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() @@ -35,7 +39,7 @@ func TestGetNode(t *testing.T) { mux, server, client := setup(t) defer teardown(server) - mux.HandleFunc("nodes/srv1", func(w http.ResponseWriter, r *http.Request) { + 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")) @@ -45,7 +49,22 @@ func TestGetNode(t *testing.T) { }) want := GetNodeStatusResponse{ - Data: GetNodeStatusData{}, + 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")