Skip to content

Commit ca8752a

Browse files
committed
test: Add dedicated unit tests for getInstanceCPUOptionsRequest
The unit tests for `getInstanceCPUOptionsRequest` are more lightweight and faster than testing various CPU option configurations through the larger `CreateInstance` function. This commit refactors the existing tests by moving the specific CPU option test cases from `TestCreateInstance` into a new, more focused `TestGetInstanceCPUOptionsRequest` function. A single test case remains in `TestCreateInstance` to ensure the integration between the functions is correct. Signed-off-by: Fangge Jin <[email protected]>
1 parent 15a2d14 commit ca8752a

File tree

1 file changed

+42
-250
lines changed

1 file changed

+42
-250
lines changed

pkg/cloud/services/ec2/instances_test.go

Lines changed: 42 additions & 250 deletions
Original file line numberDiff line numberDiff line change
@@ -5905,256 +5905,6 @@ func TestCreateInstance(t *testing.T) {
59055905
}
59065906
},
59075907
},
5908-
{
5909-
name: "with AMD SEV-SNP disabled",
5910-
machine: &clusterv1.Machine{
5911-
ObjectMeta: metav1.ObjectMeta{
5912-
Labels: map[string]string{"set": "node"},
5913-
},
5914-
Spec: clusterv1.MachineSpec{
5915-
Bootstrap: clusterv1.Bootstrap{
5916-
DataSecretName: ptr.To[string]("bootstrap-data"),
5917-
},
5918-
},
5919-
},
5920-
machineConfig: &infrav1.AWSMachineSpec{
5921-
AMI: infrav1.AMIReference{
5922-
ID: aws.String("abc"),
5923-
},
5924-
InstanceType: "m6a.large",
5925-
CPUOptions: infrav1.CPUOptions{
5926-
ConfidentialCompute: infrav1.AWSConfidentialComputePolicy("Disabled"),
5927-
},
5928-
},
5929-
awsCluster: &infrav1.AWSCluster{
5930-
ObjectMeta: metav1.ObjectMeta{Name: "test"},
5931-
Spec: infrav1.AWSClusterSpec{
5932-
NetworkSpec: infrav1.NetworkSpec{
5933-
Subnets: infrav1.Subnets{
5934-
infrav1.SubnetSpec{
5935-
ID: "subnet-1",
5936-
IsPublic: false,
5937-
},
5938-
infrav1.SubnetSpec{
5939-
IsPublic: false,
5940-
},
5941-
},
5942-
VPC: infrav1.VPCSpec{
5943-
ID: "vpc-test",
5944-
},
5945-
},
5946-
},
5947-
Status: infrav1.AWSClusterStatus{
5948-
Network: infrav1.NetworkStatus{
5949-
SecurityGroups: map[infrav1.SecurityGroupRole]infrav1.SecurityGroup{
5950-
infrav1.SecurityGroupControlPlane: {
5951-
ID: "1",
5952-
},
5953-
infrav1.SecurityGroupNode: {
5954-
ID: "2",
5955-
},
5956-
infrav1.SecurityGroupLB: {
5957-
ID: "3",
5958-
},
5959-
},
5960-
APIServerELB: infrav1.LoadBalancer{
5961-
DNSName: "test-apiserver.us-east-1.aws",
5962-
},
5963-
},
5964-
},
5965-
},
5966-
expect: func(m *mocks.MockEC2APIMockRecorder) {
5967-
m.
5968-
DescribeInstanceTypes(context.TODO(), gomock.Eq(&ec2.DescribeInstanceTypesInput{
5969-
InstanceTypes: []types.InstanceType{
5970-
types.InstanceTypeM6aLarge,
5971-
},
5972-
})).
5973-
Return(&ec2.DescribeInstanceTypesOutput{
5974-
InstanceTypes: []types.InstanceTypeInfo{
5975-
{
5976-
ProcessorInfo: &types.ProcessorInfo{
5977-
SupportedArchitectures: []types.ArchitectureType{
5978-
types.ArchitectureTypeX8664,
5979-
},
5980-
},
5981-
},
5982-
},
5983-
}, nil)
5984-
m. // TODO: Restore these parameters, but with the tags as well
5985-
RunInstances(context.TODO(), gomock.Any()).
5986-
DoAndReturn(func(ctx context.Context, input *ec2.RunInstancesInput, optFns ...func(*ec2.Options)) (*ec2.RunInstancesOutput, error) {
5987-
if input.CpuOptions == nil {
5988-
t.Fatalf("expected AMD SEV-SNP to be disabled, but got no CpuOptions")
5989-
} else if input.CpuOptions.AmdSevSnp != types.AmdSevSnpSpecificationDisabled {
5990-
t.Fatalf("expected AMD SEV-SNP to be disabled, but got %s", input.CpuOptions.AmdSevSnp)
5991-
}
5992-
return &ec2.RunInstancesOutput{
5993-
Instances: []types.Instance{
5994-
{
5995-
State: &types.InstanceState{
5996-
Name: types.InstanceStateNamePending,
5997-
},
5998-
IamInstanceProfile: &types.IamInstanceProfile{
5999-
Arn: aws.String("arn:aws:iam::123456789012:instance-profile/foo"),
6000-
},
6001-
InstanceId: aws.String("two"),
6002-
InstanceType: types.InstanceTypeM5Large,
6003-
SubnetId: aws.String("subnet-1"),
6004-
ImageId: aws.String("ami-1"),
6005-
RootDeviceName: aws.String("device-1"),
6006-
BlockDeviceMappings: []types.InstanceBlockDeviceMapping{
6007-
{
6008-
DeviceName: aws.String("device-1"),
6009-
Ebs: &types.EbsInstanceBlockDevice{
6010-
VolumeId: aws.String("volume-1"),
6011-
},
6012-
},
6013-
},
6014-
Placement: &types.Placement{
6015-
AvailabilityZone: &az,
6016-
},
6017-
},
6018-
},
6019-
}, nil
6020-
})
6021-
m.
6022-
DescribeNetworkInterfaces(context.TODO(), gomock.Any()).
6023-
Return(&ec2.DescribeNetworkInterfacesOutput{
6024-
NetworkInterfaces: []types.NetworkInterface{},
6025-
NextToken: nil,
6026-
}, nil)
6027-
},
6028-
check: func(instance *infrav1.Instance, err error) {
6029-
if err != nil {
6030-
t.Fatalf("did not expect error: %v", err)
6031-
}
6032-
},
6033-
},
6034-
{
6035-
name: "with AMD SEV-SNP unspecified",
6036-
machine: &clusterv1.Machine{
6037-
ObjectMeta: metav1.ObjectMeta{
6038-
Labels: map[string]string{"set": "node"},
6039-
},
6040-
Spec: clusterv1.MachineSpec{
6041-
Bootstrap: clusterv1.Bootstrap{
6042-
DataSecretName: ptr.To[string]("bootstrap-data"),
6043-
},
6044-
},
6045-
},
6046-
machineConfig: &infrav1.AWSMachineSpec{
6047-
AMI: infrav1.AMIReference{
6048-
ID: aws.String("abc"),
6049-
},
6050-
InstanceType: "m6a.large",
6051-
CPUOptions: infrav1.CPUOptions{
6052-
ConfidentialCompute: "",
6053-
},
6054-
},
6055-
awsCluster: &infrav1.AWSCluster{
6056-
ObjectMeta: metav1.ObjectMeta{Name: "test"},
6057-
Spec: infrav1.AWSClusterSpec{
6058-
NetworkSpec: infrav1.NetworkSpec{
6059-
Subnets: infrav1.Subnets{
6060-
infrav1.SubnetSpec{
6061-
ID: "subnet-1",
6062-
IsPublic: false,
6063-
},
6064-
infrav1.SubnetSpec{
6065-
IsPublic: false,
6066-
},
6067-
},
6068-
VPC: infrav1.VPCSpec{
6069-
ID: "vpc-test",
6070-
},
6071-
},
6072-
},
6073-
Status: infrav1.AWSClusterStatus{
6074-
Network: infrav1.NetworkStatus{
6075-
SecurityGroups: map[infrav1.SecurityGroupRole]infrav1.SecurityGroup{
6076-
infrav1.SecurityGroupControlPlane: {
6077-
ID: "1",
6078-
},
6079-
infrav1.SecurityGroupNode: {
6080-
ID: "2",
6081-
},
6082-
infrav1.SecurityGroupLB: {
6083-
ID: "3",
6084-
},
6085-
},
6086-
APIServerELB: infrav1.LoadBalancer{
6087-
DNSName: "test-apiserver.us-east-1.aws",
6088-
},
6089-
},
6090-
},
6091-
},
6092-
expect: func(m *mocks.MockEC2APIMockRecorder) {
6093-
m.
6094-
DescribeInstanceTypes(context.TODO(), gomock.Eq(&ec2.DescribeInstanceTypesInput{
6095-
InstanceTypes: []types.InstanceType{
6096-
types.InstanceTypeM6aLarge,
6097-
},
6098-
})).
6099-
Return(&ec2.DescribeInstanceTypesOutput{
6100-
InstanceTypes: []types.InstanceTypeInfo{
6101-
{
6102-
ProcessorInfo: &types.ProcessorInfo{
6103-
SupportedArchitectures: []types.ArchitectureType{
6104-
types.ArchitectureTypeX8664,
6105-
},
6106-
},
6107-
},
6108-
},
6109-
}, nil)
6110-
m. // TODO: Restore these parameters, but with the tags as well
6111-
RunInstances(context.TODO(), gomock.Any()).
6112-
DoAndReturn(func(ctx context.Context, input *ec2.RunInstancesInput, optFns ...func(*ec2.Options)) (*ec2.RunInstancesOutput, error) {
6113-
if input.CpuOptions != nil {
6114-
t.Fatalf("expected no CpuOptions, but got %+v", input.CpuOptions)
6115-
}
6116-
return &ec2.RunInstancesOutput{
6117-
Instances: []types.Instance{
6118-
{
6119-
State: &types.InstanceState{
6120-
Name: types.InstanceStateNamePending,
6121-
},
6122-
IamInstanceProfile: &types.IamInstanceProfile{
6123-
Arn: aws.String("arn:aws:iam::123456789012:instance-profile/foo"),
6124-
},
6125-
InstanceId: aws.String("two"),
6126-
InstanceType: types.InstanceTypeM5Large,
6127-
SubnetId: aws.String("subnet-1"),
6128-
ImageId: aws.String("ami-1"),
6129-
RootDeviceName: aws.String("device-1"),
6130-
BlockDeviceMappings: []types.InstanceBlockDeviceMapping{
6131-
{
6132-
DeviceName: aws.String("device-1"),
6133-
Ebs: &types.EbsInstanceBlockDevice{
6134-
VolumeId: aws.String("volume-1"),
6135-
},
6136-
},
6137-
},
6138-
Placement: &types.Placement{
6139-
AvailabilityZone: &az,
6140-
},
6141-
},
6142-
},
6143-
}, nil
6144-
})
6145-
m.
6146-
DescribeNetworkInterfaces(context.TODO(), gomock.Any()).
6147-
Return(&ec2.DescribeNetworkInterfacesOutput{
6148-
NetworkInterfaces: []types.NetworkInterface{},
6149-
NextToken: nil,
6150-
}, nil)
6151-
},
6152-
check: func(instance *infrav1.Instance, err error) {
6153-
if err != nil {
6154-
t.Fatalf("did not expect error: %v", err)
6155-
}
6156-
},
6157-
},
61585908
}
61595909
for _, tc := range testcases {
61605910
t.Run(tc.name, func(t *testing.T) {
@@ -6766,3 +6516,45 @@ func TestGetCapacityReservationSpecification(t *testing.T) {
67666516
})
67676517
}
67686518
}
6519+
6520+
func TestGetInstanceCPUOptionsRequest (t *testing.T) {
6521+
testCases := []struct {
6522+
name string
6523+
cpuOptions *infrav1.CPUOptions
6524+
expectedRequest *types.CpuOptionsRequest
6525+
}{
6526+
{
6527+
name: "with ConfidentialCompute set to AMD SEV-SNP",
6528+
expectedRequest: &types.CpuOptionsRequest{
6529+
AmdSevSnp: types.AmdSevSnpSpecificationEnabled,
6530+
},
6531+
instance: &infrav1.CPUOptions{
6532+
ConfidentialCompute: infrav1.AWSConfidentialComputePolicy("AMDEncryptedVirtualizationNestedPaging"),
6533+
},
6534+
},
6535+
{
6536+
name: "with ConfidentialCompute disabled",
6537+
expectedRequest: &types.CpuOptionsRequest{
6538+
AmdSevSnp: types.AmdSevSnpSpecificationDisabled,
6539+
},
6540+
instance: &infrav1.CPUOptions{
6541+
ConfidentialCompute: infrav1.AWSConfidentialComputePolicy("Disabled"),
6542+
},
6543+
},
6544+
{
6545+
name: "with ConfidentialCompute empty",
6546+
expectedRequest: nil,
6547+
instance: &infrav1.CPUOptions{
6548+
ConfidentialCompute: "",
6549+
},
6550+
},
6551+
}
6552+
6553+
for _, tc := range testCases {
6554+
t.Run(tc.name, func(t *testing.T) {
6555+
request := getInstanceCPUOptionsRequest(tc.cpuOptions)
6556+
g := NewWithT(t)
6557+
g.Expect(request).To(Equal(tc.expectedRequest))
6558+
})
6559+
}
6560+
}

0 commit comments

Comments
 (0)