|  | 
|  | 1 | +package realizestate | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"testing" | 
|  | 5 | +	"time" | 
|  | 6 | + | 
|  | 7 | +	"github.com/agiledragon/gomonkey/v2" | 
|  | 8 | +	"github.com/stretchr/testify/assert" | 
|  | 9 | +	"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" | 
|  | 10 | +	"k8s.io/apimachinery/pkg/util/wait" | 
|  | 11 | + | 
|  | 12 | +	"github.com/vmware-tanzu/nsx-operator/pkg/config" | 
|  | 13 | +	"github.com/vmware-tanzu/nsx-operator/pkg/nsx" | 
|  | 14 | +	"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common" | 
|  | 15 | +) | 
|  | 16 | + | 
|  | 17 | +type fakeRealizedEntitiesClient struct{} | 
|  | 18 | + | 
|  | 19 | +func (c *fakeRealizedEntitiesClient) List(orgIdParam string, projectIdParam string, intentPathParam string, sitePathParam *string) (model.GenericPolicyRealizedResourceListResult, error) { | 
|  | 20 | +	return model.GenericPolicyRealizedResourceListResult{ | 
|  | 21 | +		Results: []model.GenericPolicyRealizedResource{}, | 
|  | 22 | +	}, nil | 
|  | 23 | +} | 
|  | 24 | + | 
|  | 25 | +func TestRealizeStateService_CheckRealizeState(t *testing.T) { | 
|  | 26 | +	commonService := common.Service{ | 
|  | 27 | +		NSXClient: &nsx.Client{ | 
|  | 28 | +			RealizedEntitiesClient: &fakeRealizedEntitiesClient{}, | 
|  | 29 | +			NsxConfig: &config.NSXOperatorConfig{ | 
|  | 30 | +				CoeConfig: &config.CoeConfig{ | 
|  | 31 | +					Cluster: "k8scl-one:test", | 
|  | 32 | +				}, | 
|  | 33 | +			}, | 
|  | 34 | +		}, | 
|  | 35 | +		NSXConfig: &config.NSXOperatorConfig{ | 
|  | 36 | +			CoeConfig: &config.CoeConfig{ | 
|  | 37 | +				Cluster: "k8scl-one:test", | 
|  | 38 | +			}, | 
|  | 39 | +		}, | 
|  | 40 | +	} | 
|  | 41 | +	s := &RealizeStateService{ | 
|  | 42 | +		Service: commonService, | 
|  | 43 | +	} | 
|  | 44 | +	patches := gomonkey.ApplyFunc((*fakeRealizedEntitiesClient).List, func(c *fakeRealizedEntitiesClient, orgIdParam string, projectIdParam string, intentPathParam string, sitePathParam *string) (model.GenericPolicyRealizedResourceListResult, error) { | 
|  | 45 | +		return model.GenericPolicyRealizedResourceListResult{ | 
|  | 46 | +			Results: []model.GenericPolicyRealizedResource{ | 
|  | 47 | +				{ | 
|  | 48 | +					State: common.String(model.GenericPolicyRealizedResource_STATE_ERROR), | 
|  | 49 | +					Alarms: []model.PolicyAlarmResource{ | 
|  | 50 | +						{Message: common.String("mocked error")}, | 
|  | 51 | +					}, | 
|  | 52 | +					EntityType: common.String("RealizedLogicalPort"), | 
|  | 53 | +				}, | 
|  | 54 | +			}, | 
|  | 55 | +		}, nil | 
|  | 56 | +	}) | 
|  | 57 | +	defer patches.Reset() | 
|  | 58 | + | 
|  | 59 | +	backoff := wait.Backoff{ | 
|  | 60 | +		Duration: 1 * time.Second, | 
|  | 61 | +		Factor:   2.0, | 
|  | 62 | +		Jitter:   0, | 
|  | 63 | +		Steps:    6, | 
|  | 64 | +	} | 
|  | 65 | +	err := s.CheckRealizeState(backoff, "/orgs/default/projects/default/vpcs/vpc/subnets/subnet/ports/port", "RealizedLogicalPort") | 
|  | 66 | + | 
|  | 67 | +	realizeStateError, ok := err.(*RealizeStateError) | 
|  | 68 | +	assert.True(t, ok) | 
|  | 69 | +	assert.Equal(t, realizeStateError.Error(), "RealizedLogicalPort realized with errors: [mocked error]") | 
|  | 70 | +} | 
0 commit comments