@@ -425,7 +425,7 @@ func TestBuildLinuxResourceSpecWithoutTaskCPULimits(t *testing.T) {
425
425
},
426
426
},
427
427
}
428
- expectedCPUShares := uint64 (minimumCPUShare )
428
+ expectedCPUShares := uint64 (minimumCPUShares )
429
429
expectedLinuxResourceSpec := specs.LinuxResources {
430
430
CPU : & specs.LinuxCPU {
431
431
Shares : & expectedCPUShares ,
@@ -449,7 +449,7 @@ func TestBuildLinuxResourceSpecWithoutTaskCPULimits_WithPidLimits(t *testing.T)
449
449
},
450
450
},
451
451
}
452
- expectedCPUShares := uint64 (minimumCPUShare )
452
+ expectedCPUShares := uint64 (minimumCPUShares )
453
453
expectedLinuxResourceSpec := specs.LinuxResources {
454
454
CPU : & specs.LinuxCPU {
455
455
Shares : & expectedCPUShares ,
@@ -490,7 +490,7 @@ func TestBuildLinuxResourceSpecWithoutTaskCPUWithContainerCPULimits(t *testing.T
490
490
}
491
491
492
492
// TestBuildLinuxResourceSpecWithoutTaskCPUWithLessThanMinimumContainerCPULimits validates behavior of CPU Shares
493
- // when container CPU share is 1 (less than the current minimumCPUShare which is 2)
493
+ // when container CPU share is 1 (less than the current minimumCPUShares which is 2)
494
494
func TestBuildLinuxResourceSpecWithoutTaskCPUWithLessThanMinimumContainerCPULimits (t * testing.T ) {
495
495
task := & Task {
496
496
Arn : validTaskArn ,
@@ -1578,3 +1578,145 @@ func TestBuildCNIBridgeModeWithServiceConnect(t *testing.T) {
1578
1578
})
1579
1579
}
1580
1580
}
1581
+
1582
+ // TestBuildImplicitLinuxCPUSpec tests the task's CPU spec generation when "task.CPU" is not explicitly set.
1583
+ func TestBuildImplicitLinuxCPUSpec (t * testing.T ) {
1584
+ testCases := []struct {
1585
+ name string
1586
+ task * Task
1587
+ expectedTaskCPUSpec specs.LinuxCPU
1588
+ }{
1589
+ {
1590
+ name : "2 containers: each with CPU within permitted range" ,
1591
+ task : & Task {
1592
+ Arn : validTaskArn ,
1593
+ Containers : []* apicontainer.Container {
1594
+ {
1595
+ CPU : uint (10 ),
1596
+ },
1597
+ {
1598
+ CPU : uint (20 ),
1599
+ },
1600
+ },
1601
+ },
1602
+ expectedTaskCPUSpec : specs.LinuxCPU {
1603
+ Shares : aws .Uint64 (30 ),
1604
+ },
1605
+ },
1606
+ {
1607
+ name : "2 containers: 1 of them with CPU below min" ,
1608
+ task : & Task {
1609
+ Arn : validTaskArn ,
1610
+ Containers : []* apicontainer.Container {
1611
+ {
1612
+ CPU : uint (1 ),
1613
+ },
1614
+ {
1615
+ CPU : uint (10 ),
1616
+ },
1617
+ },
1618
+ },
1619
+ expectedTaskCPUSpec : specs.LinuxCPU {
1620
+ Shares : aws .Uint64 (uint64 (12 )),
1621
+ },
1622
+ },
1623
+ {
1624
+ name : "2 containers: 1 of them with CPU above max" ,
1625
+ task : & Task {
1626
+ Arn : validTaskArn ,
1627
+ Containers : []* apicontainer.Container {
1628
+ {
1629
+ CPU : uint (1 ),
1630
+ },
1631
+ {
1632
+ CPU : uint (364544 ),
1633
+ },
1634
+ },
1635
+ },
1636
+ expectedTaskCPUSpec : specs.LinuxCPU {
1637
+ Shares : aws .Uint64 (uint64 (maximumCPUShares )),
1638
+ },
1639
+ },
1640
+ {
1641
+ name : "3 containers: 1 with CPU within permitted range, 1 above max, and 1 below min" ,
1642
+ task : & Task {
1643
+ Arn : validTaskArn ,
1644
+ Containers : []* apicontainer.Container {
1645
+ {
1646
+ CPU : uint (5 ),
1647
+ },
1648
+ {
1649
+ CPU : uint (1 ),
1650
+ },
1651
+ {
1652
+ CPU : uint (364544 ),
1653
+ },
1654
+ },
1655
+ },
1656
+ expectedTaskCPUSpec : specs.LinuxCPU {
1657
+ Shares : aws .Uint64 (uint64 (maximumCPUShares )),
1658
+ },
1659
+ },
1660
+ }
1661
+
1662
+ for _ , tc := range testCases {
1663
+ t .Run (tc .name , func (t * testing.T ) {
1664
+ result := tc .task .buildImplicitLinuxCPUSpec ()
1665
+ assert .Equal (t , tc .expectedTaskCPUSpec , result )
1666
+ })
1667
+ }
1668
+ }
1669
+
1670
+ // TestDockerCPUShares tests the dockerCPUShares() conversion method,
1671
+ // which is used to send the container shares info is sent to Docker
1672
+ func TestDockerCPUShares (t * testing.T ) {
1673
+ testCases := []struct {
1674
+ name string
1675
+ task * Task
1676
+ expectedContainerShares int64
1677
+ }{
1678
+ {
1679
+ name : "container CPU within permitted range" ,
1680
+ task : & Task {
1681
+ Arn : validTaskArn ,
1682
+ Containers : []* apicontainer.Container {
1683
+ {
1684
+ CPU : uint (10 ),
1685
+ },
1686
+ },
1687
+ },
1688
+ expectedContainerShares : int64 (10 ),
1689
+ },
1690
+ {
1691
+ name : "container CPU below min" ,
1692
+ task : & Task {
1693
+ Arn : validTaskArn ,
1694
+ Containers : []* apicontainer.Container {
1695
+ {
1696
+ CPU : uint (1 ),
1697
+ },
1698
+ },
1699
+ },
1700
+ expectedContainerShares : int64 (minimumCPUShares ),
1701
+ },
1702
+ {
1703
+ name : "container CPU above max" ,
1704
+ task : & Task {
1705
+ Arn : validTaskArn ,
1706
+ Containers : []* apicontainer.Container {
1707
+ {
1708
+ CPU : uint (364544 ),
1709
+ },
1710
+ },
1711
+ },
1712
+ expectedContainerShares : int64 (maximumCPUShares ),
1713
+ },
1714
+ }
1715
+
1716
+ for _ , tc := range testCases {
1717
+ t .Run (tc .name , func (t * testing.T ) {
1718
+ result := tc .task .dockerCPUShares (tc .task .Containers [0 ].CPU )
1719
+ assert .Equal (t , tc .expectedContainerShares , result )
1720
+ })
1721
+ }
1722
+ }
0 commit comments