Feat: successful parse test

This commit is contained in:
kayos@tcp.direct 2022-05-05 05:16:55 -07:00
parent cb36b147fa
commit 56a209e5bd
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
4 changed files with 166 additions and 29 deletions

View File

@ -2,9 +2,6 @@ package buyvm
import (
"sync"
"time"
"inet.af/netaddr"
)
type Action uint8
@ -30,7 +27,7 @@ func NewVM(name, key, hash string) *VM {
Key: key,
Hash: hash,
},
Status: Status{},
Status: Response{},
}
return blinkies.m[name]
}
@ -55,20 +52,7 @@ func Use(name string) (vm *VM, ok bool) {
type VM struct {
Name string
Access Secrets
Status Status
}
// Status represents SolusVM status values.
type Status struct {
Status string `xml:"vmstat"`
Hostname string `xml:"hostname"`
MainIP netaddr.IP `xml:"ipaddress"`
AllIPs []netaddr.IP `xml:"ipaddr"`
Node string `xml:"node"`
BandwidthUsed int `xml:"bw"`
Location string `xml:"location"`
RDNS map[netaddr.IP]string
Stamp time.Time
Status Response
}
// Secrets represents a frantech API key

View File

@ -1,12 +1,102 @@
package buyvm
import "encoding/xml"
import (
"bytes"
"encoding/xml"
"strconv"
"strings"
"time"
"inet.af/netaddr"
)
type ipList []netaddr.IP
func (ipl *ipList) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var content string
if err := d.DecodeElement(&content, &start); err != nil {
return err
}
ipstrs := strings.Split(content, ",")
for _, ipstr := range ipstrs {
var ip netaddr.IP
var err error
if ip, err = netaddr.ParseIP(ipstr); err != nil {
return err
}
*ipl = append(*ipl, ip)
}
return nil
}
type bwUsed int64
func (bw *bwUsed) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var content string
if err := d.DecodeElement(&content, &start); err != nil {
return err
}
bwi, err := strconv.Atoi(strings.Split(content, ",")[0])
if err != nil {
return err
}
*bw = bwUsed(bwi)
return nil
}
type ipAddr netaddr.IP
func (ipa *ipAddr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var content string
if err := d.DecodeElement(&content, &start); err != nil {
return err
}
ip, err := netaddr.ParseIP(content)
if err != nil {
return err
}
*ipa = ipAddr(ip)
return nil
}
type errMsg struct {
content string
}
func (err *errMsg) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var content string
if e := d.DecodeElement(&content, &start); e != nil {
return e
}
e := *err
e.content = content
return nil
}
func (err errMsg) Error() string {
return err.content
}
func ParseResponse(b []byte) (r Response, err error) {
start := "<Response>"
end := "</Response>"
b = []byte(start + string(b) + end)
dec := xml.NewDecoder(bytes.NewReader(b))
dec.Strict = false
dec.DefaultSpace = "Response"
err = dec.Decode(&r)
return
}
// Response represents SolusVM status values.
type Response struct {
Status string `xml:"status"`
StatusMsg string `xml:"statusmsg"`
}
func parseResponse(b []byte) {
xml.Unmarshal(b, Status{})
timestamp time.Time
Status string `xml:"vmstat"`
Err errMsg `xml:"statusmsg"`
Hostname string `xml:"hostname"`
MainIP ipAddr `xml:"ipaddress"`
AllIPs ipList `xml:"ipaddr"`
Node string `xml:"node"`
BandwidthUsed bwUsed `xml:"bw"`
Location string `xml:"location"`
}

63
buyvm/parse_test.go Normal file
View File

@ -0,0 +1,63 @@
package buyvm
import (
"encoding/json"
"testing"
"inet.af/netaddr"
)
const testData = `
<type>kvm</type>
<hostname>yeet.club</hostname>
<ipaddress>127.0.0.1</ipaddress>
<node>KVM-69.FT.BUYVM.NET</node>
<vmstat>online</vmstat>
<bw>2199023255552,0,2199023255552,0</bw>
<ipaddr>127.0.0.1,192.168.69.69,172.16.184.137,2605:6400:0020:082c:0555:5555:5555:5555,2605:6400:0020:082c:6969:6969:6969:6969</ipaddr>
<location>Fuck Town, Washington, US</location>
`
func TestParseResposne(t *testing.T) {
t.Logf("%v", testData)
resp, err := ParseResponse([]byte(testData))
if err != nil {
t.Fatalf(err.Error())
}
testips := []string{"127.0.0.1", "192.168.69.69", "172.16.184.137",
"2605:6400:0020:082c:0555:5555:5555:5555", "2605:6400:0020:082c:6969:6969:6969:6969",
}
var ips []netaddr.IP
for _, ipstr := range testips {
ips = append(ips, netaddr.MustParseIP(ipstr))
}
switch {
case resp.Hostname != "yeet.club":
t.Fatalf("bad value for hostname, wanted %s, got %s", "yeet.club", resp.Hostname)
case resp.Node != "KVM-69.FT.BUYVM.NET":
t.Fatalf("bad value for node, wanted %s, got %s", "KVM-69.FT.BUYVM.NET", resp.Node)
case resp.BandwidthUsed != bwUsed(2199023255552):
t.Fatalf("bad value for bandwidth used, wanted %d, got %d", bwUsed(2199023255552), resp.BandwidthUsed)
case resp.MainIP != ipAddr(netaddr.MustParseIP("127.0.0.1")):
t.Fatalf("bad value for main IP, wanted %v, got %v", ipAddr(netaddr.MustParseIP("127.0.0.1")), resp.MainIP)
default:
//
}
for i, ip := range resp.AllIPs {
//goland:noinspection GoNilness
if ips[i] != ip {
t.Fatalf("missing ip: %v", ips[i])
}
}
var rj []byte
if rj, err = json.MarshalIndent(resp, "", "\t"); err != nil {
t.Fatalf(err.Error())
}
t.Logf("%v", string(rj))
}

View File

@ -60,9 +60,9 @@ func (d *Database) Replace(key string, value []byte) error {
}
// GetVMStatus gets Secrets status history from our database.
func (d *Database) GetVMStatus(key string) (slice []buyvm.Status, err error) {
func (d *Database) GetVMStatus(key string) (slice []buyvm.Response, err error) {
var verbytes []byte
slice = []buyvm.Status{}
slice = []buyvm.Response{}
if verbytes, err = d.Backend.Get([]byte(key)); err != nil {
return
@ -73,9 +73,9 @@ func (d *Database) GetVMStatus(key string) (slice []buyvm.Status, err error) {
}
// AppendVMHistoryStatus appends a given string to a slice in our JSON database.
func (d *Database) AppendVMHistoryStatus(vmid uint32, addition buyvm.Status) (err error) {
func (d *Database) AppendVMHistoryStatus(vmid uint32, addition buyvm.Response) (err error) {
var existing []byte
var statuses []buyvm.Status
var statuses []buyvm.Response
var newbytes []byte
vmidb := Uint32ToBytes(vmid)