@@ -101,6 +101,17 @@ var createDefaultPort = func(r *recorders) {
101101 }, nil )
102102}
103103
104+ var createDefaultPortFails = func (r * recorders ) {
105+ createOpts := ports.CreateOpts {
106+ Name : openStackServerName + "-0" ,
107+ NetworkID : networkUUID ,
108+ }
109+ portsBuilder := portsbinding.CreateOptsExt {
110+ CreateOptsBuilder : createOpts ,
111+ }
112+ r .network .CreatePort (portsBuilder ).Return (nil , fmt .Errorf ("Error creating port" ))
113+ }
114+
104115var createDefaultServer = func (r * recorders ) {
105116 // Mock any server creation
106117 r .compute .CreateServer (gomock .Any (), gomock .Any ()).Return (& servers.Server {ID : instanceUUID }, nil )
@@ -468,9 +479,11 @@ func Test_OpenStackServerReconcileDelete(t *testing.T) {
468479
469480func Test_OpenStackServerReconcileCreate (t * testing.T ) {
470481 tests := []struct {
471- name string
472- osServer infrav1alpha1.OpenStackServer
473- expect func (r * recorders )
482+ name string
483+ osServer infrav1alpha1.OpenStackServer
484+ expect func (r * recorders )
485+ wantErr error
486+ wantCondition * clusterv1beta1.Condition
474487 }{
475488 {
476489 name : "Minimal server spec creating port and server" ,
@@ -518,6 +531,36 @@ func Test_OpenStackServerReconcileCreate(t *testing.T) {
518531 listDefaultServerFound (r )
519532 },
520533 },
534+ {
535+ name : "Port created with error" ,
536+ osServer : infrav1alpha1.OpenStackServer {
537+ Spec : infrav1alpha1.OpenStackServerSpec {
538+ Flavor : ptr .To (defaultFlavor ),
539+ Image : defaultImage ,
540+ Ports : defaultPortOpts ,
541+ },
542+ Status : infrav1alpha1.OpenStackServerStatus {
543+ Resolved : & infrav1alpha1.ResolvedServerSpec {
544+ ImageID : imageUUID ,
545+ FlavorID : flavorUUID ,
546+ Ports : defaultResolvedPorts ,
547+ },
548+ },
549+ },
550+ expect : func (r * recorders ) {
551+ listDefaultPortsNotFound (r )
552+ listDefaultPortsNotFound (r )
553+ createDefaultPortFails (r )
554+ },
555+ wantErr : fmt .Errorf ("creating ports: %w" , fmt .Errorf ("Error creating port" )),
556+ wantCondition : & clusterv1beta1.Condition {
557+ Type : infrav1 .InstanceReadyCondition ,
558+ Status : corev1 .ConditionFalse ,
559+ Severity : clusterv1beta1 .ConditionSeverityError ,
560+ Reason : infrav1 .PortCreateFailedReason ,
561+ Message : "Error creating port" ,
562+ },
563+ },
521564 }
522565 for i := range tests {
523566 tt := & tests [i ]
@@ -545,7 +588,29 @@ func Test_OpenStackServerReconcileCreate(t *testing.T) {
545588 osServer .Finalizers = []string {infrav1alpha1 .OpenStackServerFinalizer }
546589
547590 _ , err := reconciler .reconcileNormal (ctx , scopeWithLogger , & tt .osServer )
548- g .Expect (err ).ToNot (HaveOccurred ())
591+
592+ // Check error result
593+ if tt .wantErr != nil {
594+ g .Expect (err ).To (Equal (tt .wantErr ))
595+ } else {
596+ g .Expect (err ).NotTo (HaveOccurred ())
597+ }
598+
599+ // Check the condition is set correctly
600+ if tt .wantCondition != nil {
601+ // print openstackServer conditions
602+ for _ , condition := range tt .osServer .Status .Conditions {
603+ t .Logf ("Condition: %s, Status: %s, Reason: %s" , condition .Type , condition .Status , condition .Reason )
604+ }
605+ unstructuredServer , err := tt .osServer .ToUnstructured ()
606+ g .Expect (err ).ToNot (HaveOccurred ())
607+ conditionType , err := conditions .UnstructuredGet (unstructuredServer , string (tt .wantCondition .Type ))
608+ g .Expect (err ).ToNot (HaveOccurred ())
609+ g .Expect (conditionType ).ToNot (BeNil ())
610+ g .Expect (string (conditionType .Status )).To (Equal (string (tt .wantCondition .Status )))
611+ g .Expect (conditionType .Reason ).To (Equal (tt .wantCondition .Reason ))
612+ g .Expect (conditionType .Message ).To (Equal (tt .wantCondition .Message ))
613+ }
549614 })
550615 }
551616}
0 commit comments