Skip to content

Commit

Permalink
Added more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenkndinh authored and saurav-agarwalla committed May 11, 2022
1 parent e3ff5fd commit 7fa05e3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
4 changes: 2 additions & 2 deletions pkg/controllers/tagging/tagging_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
cloudprovider "k8s.io/cloud-provider"
opt "k8s.io/cloud-provider-aws/pkg/controllers/options"
awsv1 "k8s.io/cloud-provider-aws/pkg/providers/v1"
"k8s.io/klog/v2"
"k8s.io/klog"
"strings"
"time"
)
Expand Down Expand Up @@ -252,7 +252,7 @@ func (tc *TaggingController) untagEc2Instance(node *v1.Node) error {
}
}

klog.Infof("Successfully tagged %s with %v", instanceId, tc.tags)
klog.Infof("Successfully untagged %s with %v", instanceId, tc.tags)

return nil
}
Expand Down
57 changes: 36 additions & 21 deletions pkg/controllers/tagging/tagging_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,33 @@ limitations under the License.
package tagging

import (
"bytes"
"context"
"flag"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
coreinformers "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
awsv1 "k8s.io/cloud-provider-aws/pkg/providers/v1"
"k8s.io/klog/v2"
"k8s.io/klog"
"os"
"strings"
"testing"
"time"
)

const TestClusterID = "clusterid.test"

func Test_NodesJoiningAndLeaving(t *testing.T) {
klog.InitFlags(nil)
flag.CommandLine.Parse([]string{"--logtostderr=false"})
testcases := []struct {
name string
currNode *v1.Node
noOfItemLeft int
toBeTagged bool
name string
currNode *v1.Node
toBeTagged bool
expectedMessages []string
}{
{
name: "node0 joins the cluster, but fail to tag.",
Expand All @@ -48,8 +53,8 @@ func Test_NodesJoiningAndLeaving(t *testing.T) {
ProviderID: "i-error",
},
},
noOfItemLeft: 1,
toBeTagged: true,
toBeTagged: true,
expectedMessages: []string{"Error occurred while processing ToBeTagged:node0"},
},
{
name: "node0 joins the cluster.",
Expand All @@ -62,11 +67,11 @@ func Test_NodesJoiningAndLeaving(t *testing.T) {
ProviderID: "i-0001",
},
},
noOfItemLeft: 0,
toBeTagged: true,
toBeTagged: true,
expectedMessages: []string{"Successfully tagged i-0001"},
},
{
name: "node0 leaves the cluster, failed to tag.",
name: "node0 leaves the cluster, failed to untag.",
currNode: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node0",
Expand All @@ -76,8 +81,8 @@ func Test_NodesJoiningAndLeaving(t *testing.T) {
ProviderID: "i-error",
},
},
noOfItemLeft: 1,
toBeTagged: false,
toBeTagged: false,
expectedMessages: []string{"Error in untagging EC2 instance for node node0"},
},
{
name: "node0 leaves the cluster.",
Expand All @@ -90,8 +95,8 @@ func Test_NodesJoiningAndLeaving(t *testing.T) {
ProviderID: "i-0001",
},
},
noOfItemLeft: 0,
toBeTagged: false,
toBeTagged: false,
expectedMessages: []string{"Successfully untagged i-0001"},
},
}

Expand All @@ -100,6 +105,12 @@ func Test_NodesJoiningAndLeaving(t *testing.T) {

for _, testcase := range testcases {
t.Run(testcase.name, func(t *testing.T) {
var logBuf bytes.Buffer
klog.SetOutput(&logBuf)
defer func() {
klog.SetOutput(os.Stderr)
}()

clientset := fake.NewSimpleClientset(testcase.currNode)
informer := informers.NewSharedInformerFactory(clientset, time.Second)
nodeInformer := informer.Core().V1().Nodes()
Expand All @@ -108,7 +119,7 @@ func Test_NodesJoiningAndLeaving(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}

eventBroadcaster := record.NewBroadcaster()
//eventBroadcaster := record.NewBroadcaster()
tc := &TaggingController{
nodeInformer: nodeInformer,
kubeClient: clientset,
Expand All @@ -119,14 +130,18 @@ func Test_NodesJoiningAndLeaving(t *testing.T) {
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "Tagging"),
}

w := eventBroadcaster.StartLogging(klog.Infof)
defer w.Stop()

tc.enqueueNode(testcase.currNode, testcase.toBeTagged)
tc.Process()

if tc.workqueue.Len() != testcase.noOfItemLeft {
t.Fatalf("workqueue not processed properly, expected %d left, got %d.", testcase.noOfItemLeft, tc.workqueue.Len())
for _, msg := range testcase.expectedMessages {
if !strings.Contains(logBuf.String(), msg) {
t.Errorf("\nMsg %q not found in log: \n%v\n", msg, logBuf.String())
}
if strings.Contains(logBuf.String(), "error tagging ") || strings.Contains(logBuf.String(), "error untagging ") {
if !strings.Contains(logBuf.String(), ", requeuing") {
t.Errorf("\nFailed to tag or untag but logs do not contain 'requeueing': \n%v\n", logBuf.String())
}
}
}
})
}
Expand Down

0 comments on commit 7fa05e3

Please sign in to comment.