readysite / pkg / platform / providers / digitalocean / digitalocean_test.go
2.5 KB
digitalocean_test.go
package digitalocean

import (
	"testing"

	"github.com/readysite/readysite/pkg/platform"
)

func TestNewWithValidToken(t *testing.T) {
	p, err := New("test-token-12345")
	if err != nil {
		t.Fatalf("New() with valid token returned error: %v", err)
	}
	if p == nil {
		t.Fatal("New() with valid token returned nil platform")
	}

	// Verify it implements the Backend interface by checking it's a *platform.Platform
	var _ *platform.Platform = p
}

func TestNewWithEmptyToken(t *testing.T) {
	p, err := New("")
	if err == nil {
		t.Fatal("New() with empty token should return error")
	}
	if p != nil {
		t.Fatal("New() with empty token should return nil platform")
	}
	if err.Error() != "token required" {
		t.Errorf("expected error message 'token required', got %q", err.Error())
	}
}

func TestNewReturnsPlatformWithBackend(t *testing.T) {
	p, err := New("valid-token")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	// The platform should have a non-nil Backend
	if p.Backend == nil {
		t.Fatal("expected platform to have a non-nil Backend")
	}
}

func TestRegionMappings(t *testing.T) {
	expected := map[platform.Region]string{
		platform.NYC: "nyc1",
		platform.SFO: "sfo3",
		platform.TOR: "tor1",
		platform.LON: "lon1",
		platform.AMS: "ams3",
		platform.FRA: "fra1",
		platform.SGP: "sgp1",
		platform.SYD: "syd1",
		platform.BLR: "blr1",
	}

	for region, doSlug := range expected {
		actual, ok := regions[region]
		if !ok {
			t.Errorf("region %q not found in mappings", region)
			continue
		}
		if actual != doSlug {
			t.Errorf("region %q: expected DO slug %q, got %q", region, doSlug, actual)
		}
	}
}

func TestRegionMappingsComplete(t *testing.T) {
	allRegions := platform.AllRegions()
	for _, region := range allRegions {
		if _, ok := regions[region]; !ok {
			t.Errorf("platform region %q has no DigitalOcean mapping", region)
		}
	}
}

func TestSizeMappings(t *testing.T) {
	expected := map[platform.Size]string{
		platform.Micro:  "s-1vcpu-1gb",
		platform.Small:  "s-1vcpu-2gb",
		platform.Medium: "s-2vcpu-4gb",
		platform.Large:  "s-4vcpu-8gb",
		platform.XLarge: "s-8vcpu-16gb",
	}

	for size, doSlug := range expected {
		actual, ok := sizes[size]
		if !ok {
			t.Errorf("size %q not found in mappings", size)
			continue
		}
		if actual != doSlug {
			t.Errorf("size %q: expected DO slug %q, got %q", size, doSlug, actual)
		}
	}
}

func TestSizeMappingsComplete(t *testing.T) {
	allSizes := platform.AllSizes()
	for _, size := range allSizes {
		if _, ok := sizes[size]; !ok {
			t.Errorf("platform size %q has no DigitalOcean mapping", size)
		}
	}
}
← Back