readysite / pkg / platform / providers / gcp / gcp_test.go
3.5 KB
gcp_test.go
package gcp

import (
	"testing"

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

// Note: GCP's New() creates real REST clients that require credentials or
// a valid environment to connect. We cannot test New() construction directly
// without credentials. Instead, we test region/size mappings and helper functions.

func TestRegionMappings(t *testing.T) {
	expected := map[platform.Region]string{
		platform.NYC: "us-east4-a",
		platform.SFO: "us-west1-a",
		platform.TOR: "northamerica-northeast1-a",
		platform.LON: "europe-west2-a",
		platform.AMS: "europe-west4-a",
		platform.FRA: "europe-west3-a",
		platform.SGP: "asia-southeast1-a",
		platform.SYD: "australia-southeast1-a",
		platform.BLR: "asia-south1-a",
	}

	for region, gcpZone := range expected {
		actual, ok := regions[region]
		if !ok {
			t.Errorf("region %q not found in GCP mappings", region)
			continue
		}
		if actual != gcpZone {
			t.Errorf("region %q: expected GCP zone %q, got %q", region, gcpZone, 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 GCP mapping", region)
		}
	}
}

func TestSizeMappings(t *testing.T) {
	expected := map[platform.Size]string{
		platform.Micro:  "e2-micro",
		platform.Small:  "e2-small",
		platform.Medium: "e2-medium",
		platform.Large:  "e2-standard-4",
		platform.XLarge: "e2-standard-8",
	}

	for size, machineType := range expected {
		actual, ok := sizes[size]
		if !ok {
			t.Errorf("size %q not found in GCP mappings", size)
			continue
		}
		if actual != machineType {
			t.Errorf("size %q: expected GCP machine type %q, got %q", size, machineType, 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 GCP mapping", size)
		}
	}
}

func TestExtractZone(t *testing.T) {
	tests := []struct {
		input    string
		expected string
	}{
		{"https://www.googleapis.com/compute/v1/projects/my-project/zones/us-east4-a", "us-east4-a"},
		{"zones/us-west1-a", "us-west1-a"},
		{"us-east4-a", "us-east4-a"},
		{"", ""},
	}

	for _, tt := range tests {
		result := extractZone(tt.input)
		if result != tt.expected {
			t.Errorf("extractZone(%q) = %q, expected %q", tt.input, result, tt.expected)
		}
	}
}

func TestExtractMachineType(t *testing.T) {
	tests := []struct {
		input    string
		expected string
	}{
		{"https://www.googleapis.com/compute/v1/projects/my-project/zones/us-east4-a/machineTypes/e2-micro", "e2-micro"},
		{"machineTypes/e2-standard-4", "e2-standard-4"},
		{"e2-small", "e2-small"},
		{"", ""},
	}

	for _, tt := range tests {
		result := extractMachineType(tt.input)
		if result != tt.expected {
			t.Errorf("extractMachineType(%q) = %q, expected %q", tt.input, result, tt.expected)
		}
	}
}

func TestRegionMappingsAreZones(t *testing.T) {
	// GCP regions map to specific zones (ending with -a, -b, -c, etc.)
	for region, zone := range regions {
		lastChar := zone[len(zone)-1]
		if lastChar < 'a' || lastChar > 'f' {
			t.Errorf("region %q maps to %q which does not end with a zone letter", region, zone)
		}
	}
}

func TestSizeMappingsAreE2Family(t *testing.T) {
	// All current GCP size mappings use e2 family
	for size, machineType := range sizes {
		if len(machineType) < 2 || machineType[:2] != "e2" {
			t.Errorf("size %q maps to %q which is not in the e2 family", size, machineType)
		}
	}
}
← Back