tested real proxmox connection

--> passed
This commit is contained in:
Philipp
2026-06-12 14:25:34 +02:00
parent 1bfc46f03b
commit 452d1fb8b3
9 changed files with 271 additions and 42 deletions
+97 -2
View File
@@ -41,6 +41,36 @@ func TestClientAcceptsMatchingFingerprintAndSendsTokenHeader(t *testing.T) {
}
}
func TestClientAcceptsPinnedSelfSignedCertificate(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api2/json/version" {
t.Fatalf("path = %q, want /api2/json/version", r.URL.Path)
}
_, _ = w.Write([]byte(`{"data":{"version":"9.2"}}`))
}))
defer server.Close()
client, err := NewClient(cluster.Cluster{
APIEndpoint: server.URL + "/api2/json",
TLSFingerprint: fingerprintForServer(server),
TokenID: "root@pam!proxui",
TokenSecret: "secret-token",
}, WithTimeout(time.Second), WithRetries(0))
if err != nil {
t.Fatalf("NewClient() error = %v", err)
}
response, err := client.Get(context.Background(), "/version")
if err != nil {
t.Fatalf("Get() error = %v", err)
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
t.Fatalf("status = %d, want %d", response.StatusCode, http.StatusOK)
}
}
func TestClientRejectsMismatchedFingerprint(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
@@ -86,8 +116,9 @@ func TestClientRetriesServerErrors(t *testing.T) {
}
func TestClientGetsTaskStatus(t *testing.T) {
upid := "UPID:pve:1:2:3:qmstart:100:root@pam!ui:"
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api2/json/nodes/pve/tasks/UPID:pve:1/status" {
if r.URL.Path != "/api2/json/nodes/pve/tasks/"+upid+"/status" {
t.Fatalf("path = %q", r.URL.Path)
}
_, _ = w.Write([]byte(`{"data":{"status":"stopped","exitstatus":"OK"}}`))
@@ -95,7 +126,7 @@ func TestClientGetsTaskStatus(t *testing.T) {
defer server.Close()
client := newTestClient(t, server, fingerprintForServer(server))
status, err := client.GetTaskStatus(context.Background(), "pve", "UPID:pve:1")
status, err := client.GetTaskStatus(context.Background(), "pve", upid)
if err != nil {
t.Fatalf("GetTaskStatus() error = %v", err)
}
@@ -149,6 +180,70 @@ func TestClientGetsVMStatus(t *testing.T) {
}
}
func TestClientConfiguresHardware(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Fatalf("method = %s, want POST", r.Method)
}
if r.URL.Path != "/api2/json/nodes/pve/qemu/100/config" {
t.Fatalf("path = %q", r.URL.Path)
}
body, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("read body: %v", err)
}
if !strings.Contains(string(body), `"cores":4`) {
t.Fatalf("body = %s, want cores", body)
}
if !strings.Contains(string(body), `"memory":6144`) {
t.Fatalf("body = %s, want memory", body)
}
_, _ = w.Write([]byte(`{"data":"UPID:pve:config"}`))
}))
defer server.Close()
client := newTestClient(t, server, fingerprintForServer(server))
upid, err := client.ConfigureHardware(context.Background(), "pve", 100, HardwareConfig{Cores: 4, Memory: 6144})
if err != nil {
t.Fatalf("ConfigureHardware() error = %v", err)
}
if upid != "UPID:pve:config" {
t.Fatalf("UPID = %q, want UPID:pve:config", upid)
}
}
func TestClientResizesDisk(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut {
t.Fatalf("method = %s, want PUT", r.Method)
}
if r.URL.Path != "/api2/json/nodes/pve/qemu/100/resize" {
t.Fatalf("path = %q", r.URL.Path)
}
body, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("read body: %v", err)
}
if !strings.Contains(string(body), `"disk":"scsi0"`) {
t.Fatalf("body = %s, want disk", body)
}
if !strings.Contains(string(body), `"size":"30G"`) {
t.Fatalf("body = %s, want size", body)
}
_, _ = w.Write([]byte(`{"data":"UPID:pve:resize"}`))
}))
defer server.Close()
client := newTestClient(t, server, fingerprintForServer(server))
upid, err := client.ResizeDisk(context.Background(), "pve", 100, "scsi0", 30)
if err != nil {
t.Fatalf("ResizeDisk() error = %v", err)
}
if upid != "UPID:pve:resize" {
t.Fatalf("UPID = %q, want UPID:pve:resize", upid)
}
}
func TestNewClientRejectsInvalidFingerprint(t *testing.T) {
_, err := NewClient(cluster.Cluster{
APIEndpoint: "https://pve.example.test:8006/api2/json",