feat: share proxmox cluster client with worker
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"forgejo.digital-droplets.de/philschlo/proxui/platform/cluster"
|
||||
"forgejo.digital-droplets.de/philschlo/proxui/platform/proxmox"
|
||||
)
|
||||
|
||||
func TestClusterTaskClientResolverLoadsClusterAndAdaptsStatus(t *testing.T) {
|
||||
repository := &stubClusterRepository{
|
||||
cluster: cluster.Cluster{
|
||||
ID: "cluster-1",
|
||||
APIEndpoint: "https://pve.example.test:8006",
|
||||
TLSFingerprint: "fingerprint",
|
||||
TokenID: "root@pam!proxui",
|
||||
TokenSecret: "secret-token",
|
||||
},
|
||||
found: true,
|
||||
}
|
||||
factoryClient := &stubPlatformTaskClient{
|
||||
status: proxmox.TaskStatus{Status: "stopped", ExitStatus: "OK"},
|
||||
}
|
||||
resolver := NewClusterTaskClientResolver(repository, func(cluster cluster.Cluster) (PlatformProxmoxTaskClient, error) {
|
||||
if cluster.ID != "cluster-1" {
|
||||
t.Fatalf("cluster ID = %q, want cluster-1", cluster.ID)
|
||||
}
|
||||
return factoryClient, nil
|
||||
})
|
||||
|
||||
client, err := resolver.ResolveTaskClient(context.Background(), "cluster-1")
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveTaskClient() error = %v", err)
|
||||
}
|
||||
|
||||
status, err := client.GetTaskStatus(context.Background(), "pve", "UPID:pve:1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTaskStatus() error = %v", err)
|
||||
}
|
||||
if status.Status != "stopped" || status.ExitStatus != "OK" {
|
||||
t.Fatalf("status = %+v, want stopped/OK", status)
|
||||
}
|
||||
if repository.id != "cluster-1" {
|
||||
t.Fatalf("repository id = %q, want cluster-1", repository.id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClusterTaskClientResolverReturnsClusterNotFound(t *testing.T) {
|
||||
resolver := NewClusterTaskClientResolver(&stubClusterRepository{}, nil)
|
||||
|
||||
if _, err := resolver.ResolveTaskClient(context.Background(), "missing"); !errors.Is(err, ErrClusterNotFound) {
|
||||
t.Fatalf("ResolveTaskClient() error = %v, want ErrClusterNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
type stubClusterRepository struct {
|
||||
id string
|
||||
cluster cluster.Cluster
|
||||
found bool
|
||||
err error
|
||||
}
|
||||
|
||||
func (s *stubClusterRepository) GetCluster(_ context.Context, id string) (cluster.Cluster, bool, error) {
|
||||
s.id = id
|
||||
return s.cluster, s.found, s.err
|
||||
}
|
||||
|
||||
type stubPlatformTaskClient struct {
|
||||
status proxmox.TaskStatus
|
||||
err error
|
||||
}
|
||||
|
||||
func (s *stubPlatformTaskClient) GetTaskStatus(context.Context, string, string) (proxmox.TaskStatus, error) {
|
||||
return s.status, s.err
|
||||
}
|
||||
Reference in New Issue
Block a user