Skip to content

Commit

Permalink
Fix e2e TestAntctl on windows testbed
Browse files Browse the repository at this point in the history
TestAntctl/testAntctlControllerRemoteAccess failed
on all windows testbed because it cannot get
output of `antctl get memberlist` while collecting
supportbundle from outside on windows testbed as
memberlist is not running.

Fixes #4659

Signed-off-by: Kumar Atish <atish.iaf@gmail.com>
  • Loading branch information
Atish-iaf committed Mar 2, 2023
1 parent dfbfa41 commit f0bdd93
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 26 deletions.
4 changes: 4 additions & 0 deletions pkg/agent/apiserver/handlers/memberlist/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func generateResponse(node *v1.Node, aliveNodes sets.String) Response {
// HandleFunc returns the function which can handle queries issued by the memberlist command.
func HandleFunc(aq querier.AgentQuerier) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if aq.GetMemberlistCluster() == nil {
http.Error(w, "memberlist is not running", http.StatusServiceUnavailable)
return
}
var memberlist []Response
allNodes, _ := aq.GetNodeLister().List(labels.Everything())
aliveNodes := aq.GetMemberlistCluster().AliveNodes()
Expand Down
92 changes: 66 additions & 26 deletions pkg/agent/apiserver/handlers/memberlist/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,35 +68,75 @@ func TestMemberlistQuery(t *testing.T) {
informerFactory.Start(stopCh)
informerFactory.WaitForCacheSync(stopCh)

ctrl := gomock.NewController(t)
q := queriertest.NewMockAgentQuerier(ctrl)
memberlistInterface := memberlisttest.NewMockInterface(ctrl)
q.EXPECT().GetNodeLister().Return(nodeLister)
q.EXPECT().GetMemberlistCluster().Return(memberlistInterface)
memberlistInterface.EXPECT().AliveNodes().Return(sets.NewString("node1"))
handler := HandleFunc(q)

req, err := http.NewRequest(http.MethodGet, "", nil)
require.NoError(t, err)

recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusOK, recorder.Code)

expectedResponse := []Response{
tests := []struct {
name string
mockNodeLister func(*queriertest.MockAgentQuerier)
mockMemberlistCluster func(*queriertest.MockAgentQuerier, *memberlisttest.MockInterface)
mockAliveNodes func(*memberlisttest.MockInterface)
expectedStatus int
expectedResponse []Response
}{
{
NodeName: "node1",
IP: "172.16.0.11",
Status: "Alive",
name: "memberlist not running",
mockMemberlistCluster: func(q *queriertest.MockAgentQuerier, m *memberlisttest.MockInterface) {
q.EXPECT().GetMemberlistCluster().Return(nil)
},
expectedStatus: http.StatusServiceUnavailable,
},
{
NodeName: "node2",
IP: "172.16.0.12",
Status: "Dead",
name: "get memberlist",
mockNodeLister: func(q *queriertest.MockAgentQuerier) {
q.EXPECT().GetNodeLister().Return(nodeLister)
},
mockMemberlistCluster: func(q *queriertest.MockAgentQuerier, m *memberlisttest.MockInterface) {
q.EXPECT().GetMemberlistCluster().Return(m).Times(2)
},
mockAliveNodes: func(m *memberlisttest.MockInterface) {
m.EXPECT().AliveNodes().Return(sets.NewString("node1"))
},
expectedStatus: http.StatusOK,
expectedResponse: []Response{
{
NodeName: "node1",
IP: "172.16.0.11",
Status: "Alive",
},
{
NodeName: "node2",
IP: "172.16.0.12",
Status: "Dead",
},
},
},
}
var received []Response
err = json.Unmarshal(recorder.Body.Bytes(), &received)
require.NoError(t, err)
assert.ElementsMatch(t, expectedResponse, received)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
q := queriertest.NewMockAgentQuerier(ctrl)
if tt.expectedStatus == http.StatusOK {
memberlistInterface := memberlisttest.NewMockInterface(ctrl)
tt.mockNodeLister(q)
tt.mockMemberlistCluster(q, memberlistInterface)
tt.mockAliveNodes(memberlistInterface)
} else {
tt.mockMemberlistCluster(q, nil)
}
handler := HandleFunc(q)

req, err := http.NewRequest(http.MethodGet, "", nil)
require.NoError(t, err)

recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
assert.Equal(t, tt.expectedStatus, recorder.Code)

if tt.expectedStatus == http.StatusOK {
var received []Response
err = json.Unmarshal(recorder.Body.Bytes(), &received)
require.NoError(t, err)
assert.ElementsMatch(t, tt.expectedResponse, received)
}
})
}
}

0 comments on commit f0bdd93

Please sign in to comment.