Skip to content

Commit

Permalink
update ut
Browse files Browse the repository at this point in the history
Signed-off-by: l1b0k <[email protected]>
  • Loading branch information
l1b0k committed Jan 9, 2025
1 parent 4ab7a38 commit c4c33cd
Show file tree
Hide file tree
Showing 16 changed files with 1,204 additions and 12 deletions.
7 changes: 7 additions & 0 deletions pkg/aliyun/client/errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,10 @@ func TestErrorIs(t *testing.T) {
// Test case 3: Check if no check functions are provided
assert.False(t, ErrorIs(err))
}

func TestErrorIsReturnsFalseWhenNoCheckErrMatches(t *testing.T) {
err := apiErr.NewServerError(403, "{\"Code\": \"err\"}", "")
checkFunc1 := WarpFn("anotherErr")
checkFunc2 := WarpFn("yetAnotherErr")
assert.False(t, ErrorIs(err, checkFunc1, checkFunc2))
}
227 changes: 227 additions & 0 deletions pkg/controller/pod-eni/eni_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
package podeni

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

aliyunClient "github.com/AliyunContainerService/terway/pkg/aliyun/client"
"github.com/AliyunContainerService/terway/pkg/apis/network.alibabacloud.com/v1beta1"
register "github.com/AliyunContainerService/terway/pkg/controller"
"github.com/AliyunContainerService/terway/pkg/controller/mocks"
)

func TestReconcilePodENI_podRequirePodENI(t *testing.T) {
type fields struct {
client client.Client
scheme *runtime.Scheme
aliyun register.Interface
record record.EventRecorder
trunkMode bool
crdMode bool
}
type args struct {
ctx context.Context
pod *corev1.Pod
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "normal pod",
fields: fields{
client: func() client.Client {
nodeReader := fake.NewClientBuilder()
nodeReader.WithObjects(&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Spec: corev1.NodeSpec{},
Status: corev1.NodeStatus{},
})
return nodeReader.Build()
}(),
scheme: nil,
aliyun: nil,
record: nil,
trunkMode: false,
crdMode: false,
},
args: args{
ctx: context.Background(),
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
},
Spec: corev1.PodSpec{
NodeName: "foo",
},
Status: corev1.PodStatus{},
},
},
want: false,
},
{
name: "trunk pod",
fields: fields{
client: func() client.Client {
nodeReader := fake.NewClientBuilder()
nodeReader.WithObjects(&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Spec: corev1.NodeSpec{},
Status: corev1.NodeStatus{},
})
return nodeReader.Build()
}(),
scheme: nil,
aliyun: nil,
record: nil,
trunkMode: false,
crdMode: false,
},
args: args{
ctx: context.Background(),
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
Annotations: map[string]string{
"k8s.aliyun.com/pod-eni": "true",
},
},
Spec: corev1.PodSpec{
NodeName: "foo",
},
Status: corev1.PodStatus{},
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &ReconcilePodENI{
client: tt.fields.client,
scheme: tt.fields.scheme,
aliyun: tt.fields.aliyun,
record: tt.fields.record,
trunkMode: tt.fields.trunkMode,
crdMode: tt.fields.crdMode,
}
if got := m.podRequirePodENI(tt.args.ctx, tt.args.pod); got != tt.want {
t.Errorf("podRequirePodENI() = %v, want %v", got, tt.want)
}
})
}
}

func TestReconcilePodENI_deleteMemberENI(t *testing.T) {
m := mocks.NewInterface(t)
m.On("DeleteNetworkInterface", mock.Anything, "foo").Return(nil)

c := &ReconcilePodENI{
client: fake.NewClientBuilder().Build(),
aliyun: m,
record: record.NewFakeRecorder(10),
}
err := c.deleteMemberENI(context.Background(), &v1beta1.PodENI{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Spec: v1beta1.PodENISpec{
Allocations: []v1beta1.Allocation{
{
ENI: v1beta1.ENI{ID: "foo"},
},
},
Zone: "",
},
})
assert.NoError(t, err)
}

func TestReconcilePodENI_detachMemberENI(t *testing.T) {
m := mocks.NewInterface(t)
m.On("WaitForNetworkInterface", mock.Anything, "foo", mock.Anything, mock.Anything, mock.Anything).Return(nil, nil)
m.On("DetachNetworkInterface", mock.Anything, "foo", "i-x", "eni-x").Return(nil)

c := &ReconcilePodENI{
client: fake.NewClientBuilder().Build(),
aliyun: m,
record: record.NewFakeRecorder(10),
}
err := c.detachMemberENI(context.Background(), &v1beta1.PodENI{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Spec: v1beta1.PodENISpec{
Allocations: []v1beta1.Allocation{
{
ENI: v1beta1.ENI{ID: "foo"},
},
},
Zone: "",
},
Status: v1beta1.PodENIStatus{
Phase: v1beta1.ENIPhaseBind,
InstanceID: "i-x",
TrunkENIID: "eni-x",
Msg: "",
PodLastSeen: metav1.Time{},
ENIInfos: nil,
},
})
assert.NoError(t, err)
}

func TestReconcilePodENI_attachENI(t *testing.T) {
m := mocks.NewInterface(t)
m.On("WaitForNetworkInterface", mock.Anything, "foo", mock.Anything, mock.Anything, mock.Anything).Return(&aliyunClient.NetworkInterface{
NetworkInterfaceID: "foo",
}, nil)
m.On("AttachNetworkInterface", mock.Anything, "foo", "i-x", "eni-x").Return(nil)

c := &ReconcilePodENI{
client: fake.NewClientBuilder().Build(),
aliyun: m,
record: record.NewFakeRecorder(10),
}
podENI := &v1beta1.PodENI{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Spec: v1beta1.PodENISpec{
Allocations: []v1beta1.Allocation{
{
ENI: v1beta1.ENI{ID: "foo"},
},
},
Zone: "",
},
Status: v1beta1.PodENIStatus{
InstanceID: "i-x",
TrunkENIID: "eni-x",
Msg: "",
PodLastSeen: metav1.Time{},
ENIInfos: nil,
},
}
err := c.attachENI(context.Background(), podENI)
assert.NoError(t, err)

assert.Equal(t, v1beta1.ENIStatusBind, podENI.Status.ENIInfos["foo"].Status)
}
2 changes: 0 additions & 2 deletions pkg/controller/pod/pod_controller_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build test_env

package pod

import (
Expand Down
2 changes: 0 additions & 2 deletions pkg/controller/pod/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build test_env

package pod

import (
Expand Down
62 changes: 62 additions & 0 deletions pkg/eni/crdv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
pkgclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

networkv1beta1 "github.com/AliyunContainerService/terway/pkg/apis/network.alibabacloud.com/v1beta1"
Expand Down Expand Up @@ -470,3 +471,64 @@ func Test_removeDeleted(t *testing.T) {
})
}
}

func TestSyncNodeRuntimeReturnsNilWhenNoDeletedPods(t *testing.T) {
r := &CRDV2{
deletedPods: make(map[string]*networkv1beta1.RuntimePodStatus),
}
err := r.syncNodeRuntime(context.Background())
assert.NoError(t, err)
}

func TestSyncNodeRuntimeReturnsErrorWhenGetRuntimeNodeFails(t *testing.T) {
client := fake.NewClientBuilder().WithScheme(types.Scheme).Build()
r := &CRDV2{
client: client,
deletedPods: map[string]*networkv1beta1.RuntimePodStatus{"pod-1": {PodID: "pod-1"}},
}
err := r.syncNodeRuntime(context.Background())
assert.Error(t, err)
}

func TestSyncNodeRuntimeUpdatesDeletedPods(t *testing.T) {
n := &networkv1beta1.NodeRuntime{
ObjectMeta: metav1.ObjectMeta{Name: "node1"},
Spec: networkv1beta1.NodeRuntimeSpec{},
Status: networkv1beta1.NodeRuntimeStatus{Pods: map[string]*networkv1beta1.RuntimePodStatus{}},
}
client := fake.NewClientBuilder().WithScheme(types.Scheme).
WithStatusSubresource(n).
WithObjects(n).Build()
r := &CRDV2{
client: client,
nodeName: "node1",
deletedPods: map[string]*networkv1beta1.RuntimePodStatus{"pod-1": {PodID: "pod-1"}},
}

err := r.syncNodeRuntime(context.Background())
assert.NoError(t, err)

nodeRuntime := &networkv1beta1.NodeRuntime{}
err = client.Get(context.Background(), pkgclient.ObjectKey{Name: "node1"}, nodeRuntime)
assert.NoError(t, err)
assert.Contains(t, nodeRuntime.Status.Pods, "pod-1")
assert.Contains(t, nodeRuntime.Status.Pods["pod-1"].Status, networkv1beta1.CNIStatusDeleted)
}

func TestSyncNodeRuntimeClearsDeletedPods(t *testing.T) {
n := &networkv1beta1.NodeRuntime{
ObjectMeta: metav1.ObjectMeta{Name: "node1"},
Spec: networkv1beta1.NodeRuntimeSpec{},
Status: networkv1beta1.NodeRuntimeStatus{Pods: map[string]*networkv1beta1.RuntimePodStatus{}},
}

client := fake.NewClientBuilder().WithScheme(types.Scheme).WithStatusSubresource(n).WithObjects(n).Build()
r := &CRDV2{
client: client,
nodeName: "node1",
deletedPods: map[string]*networkv1beta1.RuntimePodStatus{"pod-1": {PodID: "pod-1"}},
}
err := r.syncNodeRuntime(context.Background())
assert.NoError(t, err)
assert.Empty(t, r.deletedPods)
}
Loading

0 comments on commit c4c33cd

Please sign in to comment.