-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change expected code to 200, since DNC returns 200 (#2301)
* change expected code to 200, since DNC returns 200 * Use http client interface instead of passing in a client directly * Write test case * Write test case * Write test cases * Write test cases * Write test cases * Write test cases * Write test cases * Write test cases * Write test cases * fix linter issues
- Loading branch information
Showing
2 changed files
with
99 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/Azure/azure-container-networking/cns" | ||
"github.com/Azure/azure-container-networking/cns/fakes" | ||
"github.com/Azure/azure-container-networking/cns/logger" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// MockHTTPClient is a mock implementation of HTTPClient | ||
type MockHTTPClient struct { | ||
Response *http.Response | ||
Err error | ||
} | ||
|
||
// Post is the implementation of the Post method for MockHTTPClient | ||
func (m *MockHTTPClient) Do(_ *http.Request) (*http.Response, error) { | ||
return m.Response, m.Err | ||
} | ||
|
||
func TestSendRegisterNodeRequest_StatusOK(t *testing.T) { | ||
ctx := context.Background() | ||
logger.InitLogger("testlogs", 0, 0, "./") | ||
httpServiceFake := fakes.NewHTTPServiceFake() | ||
nodeRegisterReq := cns.NodeRegisterRequest{ | ||
NumCores: 2, | ||
NmAgentSupportedApis: nil, | ||
} | ||
|
||
url := "https://localhost:9000/api" | ||
|
||
// Create a mock HTTP client | ||
mockResponse := &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: io.NopCloser(bytes.NewBufferString(`{"status": "success", "OrchestratorType": "Kubernetes", "DncPartitionKey": "1234", "NodeID": "5678"}`)), | ||
Header: make(http.Header), | ||
} | ||
|
||
mockClient := &MockHTTPClient{Response: mockResponse, Err: nil} | ||
|
||
assert.NoError(t, sendRegisterNodeRequest(ctx, mockClient, httpServiceFake, nodeRegisterReq, url)) | ||
} | ||
|
||
func TestSendRegisterNodeRequest_StatusAccepted(t *testing.T) { | ||
ctx := context.Background() | ||
logger.InitLogger("testlogs", 0, 0, "./") | ||
httpServiceFake := fakes.NewHTTPServiceFake() | ||
nodeRegisterReq := cns.NodeRegisterRequest{ | ||
NumCores: 2, | ||
NmAgentSupportedApis: nil, | ||
} | ||
|
||
url := "https://localhost:9000/api" | ||
|
||
// Create a mock HTTP client | ||
mockResponse := &http.Response{ | ||
StatusCode: http.StatusAccepted, | ||
Body: io.NopCloser(bytes.NewBufferString(`{"status": "accepted", "OrchestratorType": "Kubernetes", "DncPartitionKey": "1234", "NodeID": "5678"}`)), | ||
Header: make(http.Header), | ||
} | ||
|
||
mockClient := &MockHTTPClient{Response: mockResponse, Err: nil} | ||
|
||
assert.Error(t, sendRegisterNodeRequest(ctx, mockClient, httpServiceFake, nodeRegisterReq, url)) | ||
} |