Skip to content

Commit 829f2fa

Browse files
committed
e2e: add test for unencrypted userdata with ignition
1 parent 6f56ed2 commit 829f2fa

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

test/e2e/suites/unmanaged/helpers_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package unmanaged
2121

2222
import (
2323
"context"
24+
"encoding/base64"
2425
"fmt"
2526
"io"
2627
"net/http"
@@ -629,6 +630,22 @@ func assertInstanceMetadataOptions(instanceID string, expected infrav1.InstanceM
629630
Expect(metadataOptions.HttpPutResponseHopLimit).To(HaveValue(Equal(expected.HTTPPutResponseHopLimit)))
630631
}
631632

633+
func assertUnencryptedUserDataIgnition(instanceID string, expected string) {
634+
ginkgo.By(fmt.Sprintf("Finding EC2 instance with ID: %s", instanceID))
635+
ec2Client := ec2.New(e2eCtx.AWSSession)
636+
input := &ec2.DescribeInstanceAttributeInput{
637+
Attribute: aws.String(ec2.InstanceAttributeNameUserData),
638+
InstanceId: aws.String(instanceID[strings.LastIndex(instanceID, "/")+1:]),
639+
}
640+
641+
result, err := ec2Client.DescribeInstanceAttribute(input)
642+
Expect(err).ToNot(HaveOccurred(), "expected DescribeInstanceAttribute call to succeed")
643+
644+
userData, err := base64.StdEncoding.DecodeString(*result.UserData.Value)
645+
Expect(err).ToNot(HaveOccurred(), "expected ec2 instance user data to be base64 decodable")
646+
Expect(string(userData)).To(HaveValue(MatchJSON(expected)), "expected userdata to match")
647+
}
648+
632649
func terminateInstance(instanceID string) {
633650
ginkgo.By(fmt.Sprintf("Terminating EC2 instance with ID: %s", instanceID))
634651
ec2Client := ec2.New(e2eCtx.AWSSession)
@@ -868,3 +885,22 @@ func createPodWithEFSMount(clusterClient crclient.Client) {
868885
}
869886
Expect(clusterClient.Create(context.TODO(), pod)).NotTo(HaveOccurred())
870887
}
888+
889+
func getRawBootstrapDataWithFormat(c crclient.Client, m clusterv1.Machine) ([]byte, string, error) {
890+
if m.Spec.Bootstrap.DataSecretName == nil {
891+
return nil, "", fmt.Errorf("error retrieving bootstrap data: linked Machine's bootstrap.dataSecretName is nil")
892+
}
893+
894+
secret := &corev1.Secret{}
895+
key := apimachinerytypes.NamespacedName{Namespace: m.Namespace, Name: *m.Spec.Bootstrap.DataSecretName}
896+
if err := c.Get(context.TODO(), key, secret); err != nil {
897+
return nil, "", fmt.Errorf("failed to retrieve bootstrap data secret for AWSMachine %s/%s: %v", m.Namespace, m.Name, err)
898+
}
899+
900+
value, ok := secret.Data["value"]
901+
if !ok {
902+
return nil, "", fmt.Errorf("error retrieving bootstrap data: secret value key is missing")
903+
}
904+
905+
return value, string(secret.Data["format"]), nil
906+
}

test/e2e/suites/unmanaged/unmanaged_functional_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,46 @@ var _ = ginkgo.Context("[unmanaged] [functional]", func() {
10701070
awsCluster, err := GetAWSClusterByName(ctx, namespace.Name, clusterName)
10711071
Expect(err).To(BeNil())
10721072

1073+
ginkgo.By("Creating a MachineDeployment bootstrapped via Ignition with StorageType UnencryptedUserData")
1074+
unencryptedMDName := clusterName + "-md-unencrypted-userdata"
1075+
unencryptedUDMachineTemplate := makeAWSMachineTemplate(namespace.Name, unencryptedMDName, e2eCtx.E2EConfig.GetVariable(shared.AwsNodeMachineType), nil)
1076+
unencryptedUDMachineTemplate.Spec.Template.Spec.ImageLookupBaseOS = "flatcar-stable"
1077+
unencryptedUDMachineTemplate.Spec.Template.Spec.Ignition = &infrav1.Ignition{
1078+
StorageType: infrav1.IgnitionStorageTypeOptionUnencryptedUserData,
1079+
}
1080+
1081+
unencryptedUDMachineDeployment := makeMachineDeployment(namespace.Name, unencryptedMDName, clusterName, nil, int32(1))
1082+
// Use the same bootstrap configuration from one of the existing worker machines,
1083+
// as that already contains an ignition bootstrap configuration.
1084+
unencryptedUDMachineDeployment.Spec.Template.Spec.Bootstrap.ConfigRef = md[0].Spec.Template.Spec.Bootstrap.ConfigRef
1085+
1086+
framework.CreateMachineDeployment(ctx, framework.CreateMachineDeploymentInput{
1087+
Creator: e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
1088+
MachineDeployment: unencryptedUDMachineDeployment,
1089+
BootstrapConfigTemplate: makeJoinBootstrapConfigTemplate(namespace.Name, unencryptedMDName),
1090+
InfraMachineTemplate: unencryptedUDMachineTemplate,
1091+
})
1092+
1093+
framework.WaitForMachineDeploymentNodesToExist(ctx, framework.WaitForMachineDeploymentNodesToExistInput{
1094+
Lister: e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
1095+
Cluster: result.Cluster,
1096+
MachineDeployment: unencryptedUDMachineDeployment,
1097+
}, e2eCtx.E2EConfig.GetIntervals("", "wait-worker-nodes")...)
1098+
1099+
unencryptedUDWorkerMachines := framework.GetMachinesByMachineDeployments(ctx, framework.GetMachinesByMachineDeploymentsInput{
1100+
Lister: e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
1101+
ClusterName: clusterName,
1102+
Namespace: namespace.Name,
1103+
MachineDeployment: *unencryptedUDMachineDeployment,
1104+
})
1105+
Expect(len(unencryptedUDWorkerMachines)).To(Equal(1))
1106+
// There is only one machine.
1107+
m := unencryptedUDWorkerMachines[0]
1108+
machineUserData, userDataFormat, err := getRawBootstrapDataWithFormat(e2eCtx.Environment.BootstrapClusterProxy.GetClient(), m)
1109+
Expect(err).NotTo(HaveOccurred())
1110+
Expect(userDataFormat).To(Equal("ignition"))
1111+
assertUnencryptedUserDataIgnition(*m.Spec.ProviderID, string(machineUserData))
1112+
10731113
ginkgo.By("Validating the s3 endpoint was created")
10741114
vpc, err := shared.GetVPCByName(e2eCtx, clusterName+"-vpc")
10751115
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)