92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
package rainyun
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestServerList(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "GET" || r.URL.Path != "/product/rcs/" {
|
|
t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}
|
|
resp := map[string]any{
|
|
"code": 0,
|
|
"data": []map[string]any{
|
|
{
|
|
"id": 12001,
|
|
"name": "web-server",
|
|
"status": "running",
|
|
"ip": "1.2.3.4",
|
|
"vcpu": 2,
|
|
"memory": 4096,
|
|
"disk": 50,
|
|
"os": "Ubuntu 22.04",
|
|
"expire_date": "2026-08-15",
|
|
},
|
|
},
|
|
}
|
|
json.NewEncoder(w).Encode(resp)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
rain := &Rainyun{client: newClient(srv.URL, "sk-test")}
|
|
svc := rain.Server()
|
|
servers, err := svc.List(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("List() error: %v", err)
|
|
}
|
|
if len(servers) != 1 {
|
|
t.Fatalf("len = %d, want 1", len(servers))
|
|
}
|
|
s := servers[0]
|
|
if s.Name != "web-server" {
|
|
t.Errorf("Name = %q", s.Name)
|
|
}
|
|
if s.Status != "running" {
|
|
t.Errorf("Status = %q", s.Status)
|
|
}
|
|
if s.CPU != 2 {
|
|
t.Errorf("CPU = %d", s.CPU)
|
|
}
|
|
}
|
|
|
|
func TestServerStart(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "POST" || r.URL.Path != "/product/rcs/12001/start" {
|
|
t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}
|
|
json.NewEncoder(w).Encode(map[string]any{"code": 0})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
rain := &Rainyun{client: newClient(srv.URL, "sk-test")}
|
|
err := rain.Server().Start(context.Background(), "12001")
|
|
if err != nil {
|
|
t.Fatalf("Start() error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestServerVNC(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
resp := map[string]any{
|
|
"code": 0,
|
|
"data": map[string]any{"vnc_url": "https://vnc.example.com/abc"},
|
|
}
|
|
json.NewEncoder(w).Encode(resp)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
rain := &Rainyun{client: newClient(srv.URL, "sk-test")}
|
|
url, err := rain.Server().GetVNC(context.Background(), "12001")
|
|
if err != nil {
|
|
t.Fatalf("GetVNC() error: %v", err)
|
|
}
|
|
if url != "https://vnc.example.com/abc" {
|
|
t.Errorf("VNC url = %q", url)
|
|
}
|
|
}
|