Skip to content

Commit

Permalink
Merge pull request #10752 from rifelpet/lifecycle-integration-test
Browse files Browse the repository at this point in the history
Add overrides testing in lifecycle integration tests
  • Loading branch information
k8s-ci-robot authored Feb 11, 2021
2 parents d725765 + dd1ebb8 commit 63baa5b
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 129 deletions.
61 changes: 61 additions & 0 deletions cloudmock/aws/mockautoscaling/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,67 @@ func (m *MockAutoscaling) CreateAutoScalingGroup(input *autoscaling.CreateAutoSc
return &autoscaling.CreateAutoScalingGroupOutput{}, nil
}

func (m *MockAutoscaling) UpdateAutoScalingGroup(request *autoscaling.UpdateAutoScalingGroupInput) (*autoscaling.UpdateAutoScalingGroupOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
klog.V(2).Infof("Mock UpdateAutoScalingGroup %v", request)

if _, ok := m.Groups[*request.AutoScalingGroupName]; !ok {
return nil, fmt.Errorf("Autoscaling group not found: %v", *request.AutoScalingGroupName)
}
group := m.Groups[*request.AutoScalingGroupName]

if request.AvailabilityZones != nil {
group.AvailabilityZones = request.AvailabilityZones
}
if request.CapacityRebalance != nil {
group.CapacityRebalance = request.CapacityRebalance
}
if request.DesiredCapacity != nil {
group.DesiredCapacity = request.DesiredCapacity
}
if request.HealthCheckGracePeriod != nil {
group.HealthCheckGracePeriod = request.HealthCheckGracePeriod
}
if request.HealthCheckType != nil {
group.HealthCheckType = request.HealthCheckType
}
if request.LaunchConfigurationName != nil {
group.LaunchConfigurationName = request.LaunchConfigurationName
}
if request.LaunchTemplate != nil {
group.LaunchTemplate = request.LaunchTemplate
}
if request.MaxInstanceLifetime != nil {
group.MaxInstanceLifetime = request.MaxInstanceLifetime
}
if request.MaxSize != nil {
group.MaxSize = request.MaxSize
}
if request.MinSize != nil {
group.MinSize = request.MinSize
}
if request.MixedInstancesPolicy != nil {
group.MixedInstancesPolicy = request.MixedInstancesPolicy
}
if request.NewInstancesProtectedFromScaleIn != nil {
group.NewInstancesProtectedFromScaleIn = request.NewInstancesProtectedFromScaleIn
}
if request.PlacementGroup != nil {
group.PlacementGroup = request.PlacementGroup
}
if request.ServiceLinkedRoleARN != nil {
group.ServiceLinkedRoleARN = request.ServiceLinkedRoleARN
}
if request.TerminationPolicies != nil {
group.TerminationPolicies = request.TerminationPolicies
}
if request.VPCZoneIdentifier != nil {
group.VPCZoneIdentifier = request.VPCZoneIdentifier
}
return &autoscaling.UpdateAutoScalingGroupOutput{}, nil
}

func (m *MockAutoscaling) EnableMetricsCollection(request *autoscaling.EnableMetricsCollectionInput) (*autoscaling.EnableMetricsCollectionOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
Expand Down
159 changes: 95 additions & 64 deletions cloudmock/aws/mockec2/launch_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import (
)

type launchTemplateInfo struct {
data *ec2.ResponseLaunchTemplateData
name *string
data *ec2.ResponseLaunchTemplateData
name *string
version int
}

// DescribeLaunchTemplatesPages mocks the describing the launch templates
Expand Down Expand Up @@ -132,38 +133,91 @@ func (m *MockEC2) CreateLaunchTemplate(request *ec2.CreateLaunchTemplateInput) (
if m.LaunchTemplates[id] != nil {
return nil, fmt.Errorf("duplicate LaunchTemplateId %s", id)
}
resp := &ec2.ResponseLaunchTemplateData{
DisableApiTermination: request.LaunchTemplateData.DisableApiTermination,
EbsOptimized: request.LaunchTemplateData.EbsOptimized,
ImageId: request.LaunchTemplateData.ImageId,
InstanceType: request.LaunchTemplateData.InstanceType,
KeyName: request.LaunchTemplateData.KeyName,
SecurityGroupIds: request.LaunchTemplateData.SecurityGroupIds,
SecurityGroups: request.LaunchTemplateData.SecurityGroups,
UserData: request.LaunchTemplateData.UserData,
}
m.LaunchTemplates[id] = &launchTemplateInfo{
data: resp,
name: request.LaunchTemplateName,
data: responseLaunchTemplateData(request.LaunchTemplateData),
name: request.LaunchTemplateName,
version: 1,
}
m.addTags(id, tagSpecificationsToTags(request.TagSpecifications, ec2.ResourceTypeLaunchTemplate)...)

return &ec2.CreateLaunchTemplateOutput{
LaunchTemplate: &ec2.LaunchTemplate{
LaunchTemplateId: aws.String(id),
},
}, nil
}

func (m *MockEC2) CreateLaunchTemplateVersion(request *ec2.CreateLaunchTemplateVersionInput) (*ec2.CreateLaunchTemplateVersionOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

klog.V(2).Infof("Mock CreateLaunchTemplateVersion: %v", request)

name := request.LaunchTemplateName
found := false
for _, ltInfo := range m.LaunchTemplates {
if aws.StringValue(ltInfo.name) == aws.StringValue(name) {
found = true
ltInfo.data = responseLaunchTemplateData(request.LaunchTemplateData)
ltInfo.version++
}
}
if !found {
return nil, nil // TODO: error
}
return &ec2.CreateLaunchTemplateVersionOutput{}, nil
}

if request.LaunchTemplateData.MetadataOptions != nil {
// DeleteLaunchTemplate mocks the deletion of a launch template
func (m *MockEC2) DeleteLaunchTemplate(request *ec2.DeleteLaunchTemplateInput) (*ec2.DeleteLaunchTemplateOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

klog.V(2).Infof("Mock DeleteLaunchTemplate: %v", request)

o := &ec2.DeleteLaunchTemplateOutput{}

if m.LaunchTemplates == nil {
return o, nil
}
for id := range m.LaunchTemplates {
if id == aws.StringValue(request.LaunchTemplateId) {
delete(m.LaunchTemplates, id)
}
}

return o, nil
}

func responseLaunchTemplateData(req *ec2.RequestLaunchTemplateData) *ec2.ResponseLaunchTemplateData {
resp := &ec2.ResponseLaunchTemplateData{
DisableApiTermination: req.DisableApiTermination,
EbsOptimized: req.EbsOptimized,
ImageId: req.ImageId,
InstanceType: req.InstanceType,
KeyName: req.KeyName,
SecurityGroupIds: req.SecurityGroupIds,
SecurityGroups: req.SecurityGroups,
UserData: req.UserData,
}

if req.MetadataOptions != nil {
resp.MetadataOptions = &ec2.LaunchTemplateInstanceMetadataOptions{
HttpTokens: request.LaunchTemplateData.MetadataOptions.HttpTokens,
HttpPutResponseHopLimit: request.LaunchTemplateData.MetadataOptions.HttpPutResponseHopLimit,
HttpTokens: req.MetadataOptions.HttpTokens,
HttpPutResponseHopLimit: req.MetadataOptions.HttpPutResponseHopLimit,
}
}
if request.LaunchTemplateData.Monitoring != nil {
resp.Monitoring = &ec2.LaunchTemplatesMonitoring{Enabled: request.LaunchTemplateData.Monitoring.Enabled}
if req.Monitoring != nil {
resp.Monitoring = &ec2.LaunchTemplatesMonitoring{Enabled: req.Monitoring.Enabled}
}
if request.LaunchTemplateData.CpuOptions != nil {
if req.CpuOptions != nil {
resp.CpuOptions = &ec2.LaunchTemplateCpuOptions{
CoreCount: request.LaunchTemplateData.CpuOptions.CoreCount,
ThreadsPerCore: request.LaunchTemplateData.CpuOptions.ThreadsPerCore,
CoreCount: req.CpuOptions.CoreCount,
ThreadsPerCore: req.CpuOptions.ThreadsPerCore,
}
}
if len(request.LaunchTemplateData.BlockDeviceMappings) > 0 {
for _, x := range request.LaunchTemplateData.BlockDeviceMappings {
if len(req.BlockDeviceMappings) > 0 {
for _, x := range req.BlockDeviceMappings {
var ebs *ec2.LaunchTemplateEbsBlockDevice
if x.Ebs != nil {
ebs = &ec2.LaunchTemplateEbsBlockDevice{
Expand All @@ -185,29 +239,29 @@ func (m *MockEC2) CreateLaunchTemplate(request *ec2.CreateLaunchTemplateInput) (
})
}
}
if request.LaunchTemplateData.CreditSpecification != nil {
resp.CreditSpecification = &ec2.CreditSpecification{CpuCredits: request.LaunchTemplateData.CreditSpecification.CpuCredits}
if req.CreditSpecification != nil {
resp.CreditSpecification = &ec2.CreditSpecification{CpuCredits: req.CreditSpecification.CpuCredits}
}
if request.LaunchTemplateData.IamInstanceProfile != nil {
if req.IamInstanceProfile != nil {
resp.IamInstanceProfile = &ec2.LaunchTemplateIamInstanceProfileSpecification{
Arn: request.LaunchTemplateData.IamInstanceProfile.Arn,
Name: request.LaunchTemplateData.IamInstanceProfile.Name,
Arn: req.IamInstanceProfile.Arn,
Name: req.IamInstanceProfile.Name,
}
}
if request.LaunchTemplateData.InstanceMarketOptions != nil {
if req.InstanceMarketOptions != nil {
resp.InstanceMarketOptions = &ec2.LaunchTemplateInstanceMarketOptions{
MarketType: request.LaunchTemplateData.InstanceMarketOptions.MarketType,
MarketType: req.InstanceMarketOptions.MarketType,
SpotOptions: &ec2.LaunchTemplateSpotMarketOptions{
BlockDurationMinutes: request.LaunchTemplateData.InstanceMarketOptions.SpotOptions.BlockDurationMinutes,
InstanceInterruptionBehavior: request.LaunchTemplateData.InstanceMarketOptions.SpotOptions.InstanceInterruptionBehavior,
MaxPrice: request.LaunchTemplateData.InstanceMarketOptions.SpotOptions.MaxPrice,
SpotInstanceType: request.LaunchTemplateData.InstanceMarketOptions.SpotOptions.SpotInstanceType,
ValidUntil: request.LaunchTemplateData.InstanceMarketOptions.SpotOptions.ValidUntil,
BlockDurationMinutes: req.InstanceMarketOptions.SpotOptions.BlockDurationMinutes,
InstanceInterruptionBehavior: req.InstanceMarketOptions.SpotOptions.InstanceInterruptionBehavior,
MaxPrice: req.InstanceMarketOptions.SpotOptions.MaxPrice,
SpotInstanceType: req.InstanceMarketOptions.SpotOptions.SpotInstanceType,
ValidUntil: req.InstanceMarketOptions.SpotOptions.ValidUntil,
},
}
}
if len(request.LaunchTemplateData.NetworkInterfaces) > 0 {
for _, x := range request.LaunchTemplateData.NetworkInterfaces {
if len(req.NetworkInterfaces) > 0 {
for _, x := range req.NetworkInterfaces {
resp.NetworkInterfaces = append(resp.NetworkInterfaces, &ec2.LaunchTemplateInstanceNetworkInterfaceSpecification{
AssociatePublicIpAddress: x.AssociatePublicIpAddress,
DeleteOnTermination: x.DeleteOnTermination,
Expand All @@ -223,36 +277,13 @@ func (m *MockEC2) CreateLaunchTemplate(request *ec2.CreateLaunchTemplateInput) (
})
}
}
if len(request.LaunchTemplateData.TagSpecifications) > 0 {
for _, x := range request.LaunchTemplateData.TagSpecifications {
if len(req.TagSpecifications) > 0 {
for _, x := range req.TagSpecifications {
resp.TagSpecifications = append(resp.TagSpecifications, &ec2.LaunchTemplateTagSpecification{
ResourceType: x.ResourceType,
Tags: x.Tags,
})
}
}
m.addTags(id, tagSpecificationsToTags(request.TagSpecifications, ec2.ResourceTypeLaunchTemplate)...)

return &ec2.CreateLaunchTemplateOutput{}, nil
}

// DeleteLaunchTemplate mocks the deletion of a launch template
func (m *MockEC2) DeleteLaunchTemplate(request *ec2.DeleteLaunchTemplateInput) (*ec2.DeleteLaunchTemplateOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()

klog.V(2).Infof("Mock DeleteLaunchTemplate: %v", request)

o := &ec2.DeleteLaunchTemplateOutput{}

if m.LaunchTemplates == nil {
return o, nil
}
for id := range m.LaunchTemplates {
if id == aws.StringValue(request.LaunchTemplateId) {
delete(m.LaunchTemplates, id)
}
}

return o, nil
return resp
}
1 change: 1 addition & 0 deletions cmd/kops/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ go_test(
"//cloudmock/aws/mockec2:go_default_library",
"//cmd/kops/util:go_default_library",
"//pkg/apis/kops:go_default_library",
"//pkg/commands:go_default_library",
"//pkg/featureflag:go_default_library",
"//pkg/jsonutils:go_default_library",
"//pkg/kopscodecs:go_default_library",
Expand Down
Loading

0 comments on commit 63baa5b

Please sign in to comment.