@@ -106,6 +106,17 @@ var createDefaultPort = func(r *recorders) {
106106 }, nil )
107107}
108108
109+ var createDefaultPortFails = func (r * recorders ) {
110+ createOpts := ports.CreateOpts {
111+ Name : openStackServerName + "-0" ,
112+ NetworkID : networkUUID ,
113+ }
114+ portsBuilder := portsbinding.CreateOptsExt {
115+ CreateOptsBuilder : createOpts ,
116+ }
117+ r .network .CreatePort (portsBuilder ).Return (nil , fmt .Errorf ("Error creating port" ))
118+ }
119+
109120var createDefaultServer = func (r * recorders ) {
110121 // Mock any server creation
111122 r .compute .CreateServer (gomock .Any (), gomock .Any ()).Return (& servers.Server {ID : instanceUUID }, nil )
@@ -659,9 +670,11 @@ func Test_OpenStackServerReconcileDelete(t *testing.T) {
659670
660671func Test_OpenStackServerReconcileCreate (t * testing.T ) {
661672 tests := []struct {
662- name string
663- osServer infrav1alpha1.OpenStackServer
664- expect func (r * recorders )
673+ name string
674+ osServer infrav1alpha1.OpenStackServer
675+ expect func (r * recorders )
676+ wantErr error
677+ wantCondition * clusterv1beta1.Condition
665678 }{
666679 {
667680 name : "Minimal server spec creating port and server" ,
@@ -709,6 +722,36 @@ func Test_OpenStackServerReconcileCreate(t *testing.T) {
709722 listDefaultServerFound (r )
710723 },
711724 },
725+ {
726+ name : "Port created with error" ,
727+ osServer : infrav1alpha1.OpenStackServer {
728+ Spec : infrav1alpha1.OpenStackServerSpec {
729+ Flavor : ptr .To (defaultFlavor ),
730+ Image : defaultImage ,
731+ Ports : defaultPortOpts ,
732+ },
733+ Status : infrav1alpha1.OpenStackServerStatus {
734+ Resolved : & infrav1alpha1.ResolvedServerSpec {
735+ ImageID : imageUUID ,
736+ FlavorID : flavorUUID ,
737+ Ports : defaultResolvedPorts ,
738+ },
739+ },
740+ },
741+ expect : func (r * recorders ) {
742+ listDefaultPortsNotFound (r )
743+ listDefaultPortsNotFound (r )
744+ createDefaultPortFails (r )
745+ },
746+ wantErr : fmt .Errorf ("creating ports: %w" , fmt .Errorf ("Error creating port" )),
747+ wantCondition : & clusterv1beta1.Condition {
748+ Type : infrav1 .InstanceReadyCondition ,
749+ Status : corev1 .ConditionFalse ,
750+ Severity : clusterv1beta1 .ConditionSeverityError ,
751+ Reason : infrav1 .PortCreateFailedReason ,
752+ Message : "Error creating port" ,
753+ },
754+ },
712755 }
713756 for i := range tests {
714757 tt := & tests [i ]
@@ -736,7 +779,29 @@ func Test_OpenStackServerReconcileCreate(t *testing.T) {
736779 osServer .Finalizers = []string {infrav1alpha1 .OpenStackServerFinalizer }
737780
738781 _ , err := reconciler .reconcileNormal (ctx , scopeWithLogger , & tt .osServer )
739- g .Expect (err ).ToNot (HaveOccurred ())
782+
783+ // Check error result
784+ if tt .wantErr != nil {
785+ g .Expect (err ).To (Equal (tt .wantErr ))
786+ } else {
787+ g .Expect (err ).NotTo (HaveOccurred ())
788+ }
789+
790+ // Check the condition is set correctly
791+ if tt .wantCondition != nil {
792+ // print openstackServer conditions
793+ for _ , condition := range tt .osServer .Status .Conditions {
794+ t .Logf ("Condition: %s, Status: %s, Reason: %s" , condition .Type , condition .Status , condition .Reason )
795+ }
796+ unstructuredServer , err := tt .osServer .ToUnstructured ()
797+ g .Expect (err ).ToNot (HaveOccurred ())
798+ conditionType , err := conditions .UnstructuredGet (unstructuredServer , string (tt .wantCondition .Type ))
799+ g .Expect (err ).ToNot (HaveOccurred ())
800+ g .Expect (conditionType ).ToNot (BeNil ())
801+ g .Expect (string (conditionType .Status )).To (Equal (string (tt .wantCondition .Status )))
802+ g .Expect (conditionType .Reason ).To (Equal (tt .wantCondition .Reason ))
803+ g .Expect (conditionType .Message ).To (Equal (tt .wantCondition .Message ))
804+ }
740805 })
741806 }
742807}
0 commit comments