readysite / pkg / platform / providers / aws / aws_test.go
4.2 KB
aws_test.go
package aws

import (
	"testing"

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

func TestNewWithValidCredentials(t *testing.T) {
	p, err := New("AKIAIOSFODNN7EXAMPLE", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", "")
	if err != nil {
		t.Fatalf("New() with valid credentials returned error: %v", err)
	}
	if p == nil {
		t.Fatal("New() with valid credentials returned nil platform")
	}

	var _ *platform.Platform = p
}

func TestNewWithExplicitRegion(t *testing.T) {
	p, err := New("AKIAIOSFODNN7EXAMPLE", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", "us-west-2")
	if err != nil {
		t.Fatalf("New() with explicit region returned error: %v", err)
	}
	if p == nil {
		t.Fatal("New() with explicit region returned nil platform")
	}
}

func TestNewWithEmptyAccessKey(t *testing.T) {
	p, err := New("", "secret", "us-east-1")
	if err == nil {
		t.Fatal("New() with empty accessKey should return error")
	}
	if p != nil {
		t.Fatal("New() with empty accessKey should return nil platform")
	}
	if err.Error() != "accessKey and secretKey required" {
		t.Errorf("expected error message 'accessKey and secretKey required', got %q", err.Error())
	}
}

func TestNewWithEmptySecretKey(t *testing.T) {
	p, err := New("access", "", "us-east-1")
	if err == nil {
		t.Fatal("New() with empty secretKey should return error")
	}
	if p != nil {
		t.Fatal("New() with empty secretKey should return nil platform")
	}
}

func TestNewWithBothKeysEmpty(t *testing.T) {
	p, err := New("", "", "")
	if err == nil {
		t.Fatal("New() with both keys empty should return error")
	}
	if p != nil {
		t.Fatal("New() with both keys empty should return nil platform")
	}
}

func TestNewReturnsPlatformWithBackend(t *testing.T) {
	p, err := New("access-key", "secret-key", "")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	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: "us-east-1",
		platform.SFO: "us-west-1",
		platform.TOR: "ca-central-1",
		platform.LON: "eu-west-2",
		platform.AMS: "eu-west-1",
		platform.FRA: "eu-central-1",
		platform.SGP: "ap-southeast-1",
		platform.SYD: "ap-southeast-2",
		platform.BLR: "ap-south-1",
	}

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

func TestSizeMappings(t *testing.T) {
	expected := map[platform.Size]string{
		platform.Micro:  "t3.micro",
		platform.Small:  "t3.small",
		platform.Medium: "t3.medium",
		platform.Large:  "t3.large",
		platform.XLarge: "t3.xlarge",
	}

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

func TestSplitTag(t *testing.T) {
	tests := []struct {
		input    string
		expected []string
	}{
		{"key=value", []string{"key", "value"}},
		{"env=production", []string{"env", "production"}},
		{"name=my=server", []string{"name", "my=server"}},
		{"noequals", nil},
		{"=value", []string{"", "value"}},
		{"key=", []string{"key", ""}},
	}

	for _, tt := range tests {
		result := splitTag(tt.input)
		if tt.expected == nil {
			if result != nil {
				t.Errorf("splitTag(%q) = %v, expected nil", tt.input, result)
			}
			continue
		}
		if len(result) != len(tt.expected) {
			t.Errorf("splitTag(%q) = %v, expected %v", tt.input, result, tt.expected)
			continue
		}
		for i := range result {
			if result[i] != tt.expected[i] {
				t.Errorf("splitTag(%q)[%d] = %q, expected %q", tt.input, i, result[i], tt.expected[i])
			}
		}
	}
}
← Back