Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Yanjun Zhou <[email protected]>
  • Loading branch information
yanjunz97 committed Dec 26, 2024
1 parent 5535fe4 commit e0ad08b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
5 changes: 4 additions & 1 deletion pkg/controllers/pod/pod_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ func TestSubnetPortReconciler_GetSubnetPathForPod(t *testing.T) {
prepareFunc func(*testing.T, *PodReconciler) *gomonkey.Patches
expectedErr string
expectedSubnetPath string
expectedIsExisted bool
}{
{
name: "SubnetExisted",
Expand All @@ -398,6 +399,7 @@ func TestSubnetPortReconciler_GetSubnetPathForPod(t *testing.T) {
return patches
},
expectedSubnetPath: subnetPath,
expectedIsExisted: true,
},
{
name: "NoGetDefaultSubnetSet",
Expand Down Expand Up @@ -467,7 +469,7 @@ func TestSubnetPortReconciler_GetSubnetPathForPod(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
patches := tt.prepareFunc(t, r)
defer patches.Reset()
_, path, err := r.GetSubnetPathForPod(context.TODO(), &v1.Pod{
isExisted, path, err := r.GetSubnetPathForPod(context.TODO(), &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
Namespace: "ns-1",
Expand All @@ -478,6 +480,7 @@ func TestSubnetPortReconciler_GetSubnetPathForPod(t *testing.T) {
} else {
assert.Nil(t, err)
assert.Equal(t, subnetPath, path)
assert.Equal(t, tt.expectedIsExisted, isExisted)
}
})
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/controllers/subnetport/subnetport_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ func TestSubnetPortReconciler_CheckAndGetSubnetPathForSubnetPort(t *testing.T) {
expectedIsStale bool
expectedErr string
expectedSubnetPath string
expectedIsExisted bool
subnetport *v1alpha1.SubnetPort
}{
{
Expand All @@ -608,6 +609,7 @@ func TestSubnetPortReconciler_CheckAndGetSubnetPathForSubnetPort(t *testing.T) {
Namespace: "ns-1",
},
},
expectedIsExisted: true,
},
{
name: "FailedToDeleteSubnetPort",
Expand Down Expand Up @@ -886,8 +888,9 @@ func TestSubnetPortReconciler_CheckAndGetSubnetPathForSubnetPort(t *testing.T) {
ctx := context.TODO()
patches := tt.prepareFunc(t, r)
defer patches.Reset()
_, isStale, subnetPath, err := r.CheckAndGetSubnetPathForSubnetPort(ctx, tt.subnetport)
isExisted, isStale, subnetPath, err := r.CheckAndGetSubnetPathForSubnetPort(ctx, tt.subnetport)
assert.Equal(t, tt.expectedIsStale, isStale)
assert.Equal(t, tt.expectedIsExisted, isExisted)
if tt.expectedErr != "" {
assert.Contains(t, err.Error(), tt.expectedErr)
} else {
Expand Down
18 changes: 18 additions & 0 deletions pkg/nsx/services/subnetport/subnetport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,24 @@ func TestSubnetPortService_ListSubnetPortByPodName(t *testing.T) {
assert.Equal(t, subnetPort2, subnetPorts[0])
}

func TestSubnetPortService_AllocatePortFromSubnet(t *testing.T) {
subnetPath := "subnet-path-1"
subnetId := "subnet-id-1"
subnetPortService := createSubnetPortService()
ok := subnetPortService.AllocatePortFromSubnet(&model.VpcSubnet{
Ipv4SubnetSize: common.Int64(16),
IpAddresses: []string{"10.0.0.1/28"},
Path: &subnetPath,
Id: &subnetId,
})
assert.True(t, ok)
empty := subnetPortService.IsEmptySubnet(subnetId, subnetPath)
assert.False(t, empty)
subnetPortService.ReleasePortInSubnet(subnetPath)
empty = subnetPortService.IsEmptySubnet(subnetId, subnetPath)
assert.True(t, empty)
}

func createSubnetPortService() *SubnetPortService {
return &SubnetPortService{
SubnetPortStore: &SubnetPortStore{ResourceStore: common.ResourceStore{
Expand Down

0 comments on commit e0ad08b

Please sign in to comment.