github.com/defang-io/defang/src@v0.0.0-20240505002154-bdf411911834/pkg/clouds/aws/ecs/fargate_test.go (about)

     1  package ecs
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  )
     7  
     8  func TestFixupFargateCPU(t *testing.T) {
     9  	tests := []struct {
    10  		vcpu    float64
    11  		wantCPU CpuUnits
    12  	}{
    13  		{0.0, 256},
    14  		{0.26, 512},
    15  		{111.0, 16384},
    16  	}
    17  
    18  	for _, tt := range tests {
    19  		if gotCPU := fixupFargateCPU(tt.vcpu); gotCPU != tt.wantCPU {
    20  			t.Errorf("fixupFargateCPU(%v) = %v, want %v", tt.vcpu, gotCPU, tt.wantCPU)
    21  		}
    22  	}
    23  }
    24  
    25  func TestFixupFargateMemory(t *testing.T) {
    26  	tests := []struct {
    27  		cpu     CpuUnits
    28  		memMiB  float64
    29  		wantMem MemoryMiB
    30  	}{
    31  		{256, 0, 512},
    32  		{256, 1023, 1024},
    33  		{256, 1024, 1024},
    34  		{256, 1025, 2048},
    35  	}
    36  
    37  	for _, tt := range tests {
    38  		if gotMem := fixupFargateMemory(tt.cpu, tt.memMiB); gotMem != tt.wantMem {
    39  			t.Errorf("fixupFargateMemory(%v, %v) = %v, want %v", tt.cpu, tt.memMiB, gotMem, tt.wantMem)
    40  		}
    41  	}
    42  }
    43  
    44  func TestMakeMinMaxCeil(t *testing.T) {
    45  	tests := []struct {
    46  		value float64
    47  		min   uint
    48  		max   uint
    49  		ceil  uint
    50  		want  uint
    51  	}{
    52  		{math.NaN(), 5, 100, 10, 5},
    53  		{0, 5, 100, 10, 5},
    54  		{6, 5, 100, 10, 10},
    55  		{1, 1, 100, 10, 1},
    56  		{1.1, 1, 100, 10, 10},
    57  		{89, 1, 100, 10, 90},
    58  		{90, 1, 100, 10, 90},
    59  		{91, 1, 100, 10, 100},
    60  	}
    61  
    62  	for _, tt := range tests {
    63  		if got := makeMinMaxCeil(tt.value, tt.min, tt.max, tt.ceil); got != tt.want {
    64  			t.Errorf("makeMinMaxCeil(%v, %v, %v, %v) = %v, want %v", tt.value, tt.min, tt.max, tt.ceil, got, tt.want)
    65  		}
    66  	}
    67  }
    68  
    69  func TestFixupFargateConfig(t *testing.T) {
    70  	tests := []struct {
    71  		vcpu    float64
    72  		memMiB  float64
    73  		wantCPU CpuUnits
    74  		wantMem MemoryMiB
    75  	}{
    76  		{0.0, 0.0, 256, 512},
    77  		{0.25, 0, 256, 512},
    78  		{0.0, 1024, 256, 1024},
    79  		{0.26, 0, 512, 1024},
    80  		{0.26, 1024, 512, 1024},
    81  		{111.0, 1024, 16384, 32768},
    82  	}
    83  
    84  	for _, tt := range tests {
    85  		gotCPU, gotMem := FixupFargateConfig(tt.vcpu, tt.memMiB)
    86  		if gotCPU != tt.wantCPU || gotMem != tt.wantMem {
    87  			t.Errorf("FixupFargateConfig(%v, %v) = %v, %v, want %v, %v", tt.vcpu, tt.memMiB, gotCPU, gotMem, tt.wantCPU, tt.wantMem)
    88  		}
    89  	}
    90  }