Skip to content

improve unit test coverage #3816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: 2.x-master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/k8s-bigip-ctlr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@ func initController(
true,
agentParams,
nil,
nil,
)
return ctlr
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/agent/agentAS3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type (
RespIndex int
}

responceCtx struct {
responseCtx struct {
tenant string
status float64
body string
Expand All @@ -35,13 +35,13 @@ func newMockPostManager() *mockPostManager {
return mockPM
}

func (mockPM *mockPostManager) SetResponses(responces []responceCtx, method string) {
func (mockPM *mockPostManager) SetResponses(responses []responseCtx, method string) {
var body string

responseMap := make(mockhc.ResponseConfigMap)
responseMap[method] = &mockhc.ResponseConfig{}

for _, resp := range responces {
for _, resp := range responses {
if resp.body == "" {
if resp.status == http.StatusOK {
body = fmt.Sprintf(`{"results":[{"code":%f,"message":"none", "tenant": "%s"}]}`,
Expand Down Expand Up @@ -81,7 +81,7 @@ var _ = Describe("Agent AS3 Tests", func() {
Expect(ok).To(BeTrue())
// Test Get BIGIP Reg key
tnt := "test"
mockPM.SetResponses([]responceCtx{{
mockPM.SetResponses([]responseCtx{{
tenant: tnt,
status: http.StatusOK,
body: `{"registrationKey": "sfiifhanji"}`,
Expand All @@ -90,7 +90,7 @@ var _ = Describe("Agent AS3 Tests", func() {
// test clean function
Expect(ag.Clean("test")).To(BeNil())
// set invalid status
mockPM.SetResponses([]responceCtx{{
mockPM.SetResponses([]responseCtx{{
tenant: tnt,
status: http.StatusServiceUnavailable,
body: `{"registrationKey": "sfiifhanji"}`,
Expand Down
18 changes: 9 additions & 9 deletions pkg/agent/as3/as3Manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,15 @@ var _ = Describe("AS3Manager Tests", func() {
It("Check BigIP App services available", func() {
mockPM := newMockPostManger()
mockMgr.PostManager = mockPM.PostManager
mockPM.setResponses([]responceCtx{
mockPM.setResponses([]responseCtx{
{
tenant: "test",
status: http.StatusOK,
body: `{"version":"3.18.1", "release":"r1", "schemaCurrent":"test"}`,
},
}, http.MethodGet)
Expect(mockMgr.IsBigIPAppServicesAvailable()).To(BeNil())
mockPM.setResponses([]responceCtx{
mockPM.setResponses([]responseCtx{
{
tenant: "test",
status: http.StatusOK,
Expand All @@ -341,7 +341,7 @@ var _ = Describe("AS3Manager Tests", func() {
Expect(mockMgr.as3Version).To(Equal(defaultAS3Version))
Expect(mockMgr.as3Release).To(Equal(defaultAS3Version + "-" + defaultAS3Build))
Expect(mockMgr.as3SchemaVersion).To(Equal(fmt.Sprintf("%.2f.0", as3Version)))
mockPM.setResponses([]responceCtx{
mockPM.setResponses([]responseCtx{
{
tenant: "test",
status: http.StatusOK,
Expand Down Expand Up @@ -374,7 +374,7 @@ var _ = Describe("AS3Manager Tests", func() {
mockPM.AS3PostDelay = 2
mockMgr.PostManager = mockPM.PostManager
go mockMgr.ConfigDeployer()
mockPM.setResponses([]responceCtx{{
mockPM.setResponses([]responseCtx{{
tenant: "test",
status: http.StatusOK,
body: "",
Expand Down Expand Up @@ -409,7 +409,7 @@ var _ = Describe("AS3Manager Tests", func() {
tnt := "test"
mockPM := newMockPostManger()
mockMgr.PostManager = mockPM.PostManager
mockPM.setResponses([]responceCtx{{
mockPM.setResponses([]responseCtx{{
tenant: tnt,
status: http.StatusOK,
body: "",
Expand All @@ -435,7 +435,7 @@ var _ = Describe("AS3Manager Tests", func() {
mockMgr.as3ActiveConfig.tenantMap["Tenant2"] = true
mockPM := newMockPostManger()
mockMgr.PostManager = mockPM.PostManager
mockPM.setResponses([]responceCtx{{
mockPM.setResponses([]responseCtx{{
tenant: "Tenant1",
status: http.StatusOK,
body: ""},
Expand Down Expand Up @@ -483,7 +483,7 @@ var _ = Describe("AS3Manager Tests", func() {
It("Clean CIS managed Partition", func() {
mockPM := newMockPostManger()
mockMgr.PostManager = mockPM.PostManager
mockPM.setResponses([]responceCtx{{
mockPM.setResponses([]responseCtx{{
tenant: "Tenant1",
status: http.StatusOK,
body: ""},
Expand All @@ -495,7 +495,7 @@ var _ = Describe("AS3Manager Tests", func() {
It("Handle Post Failures", func() {
mockPM := newMockPostManger()
mockMgr.PostManager = mockPM.PostManager
mockPM.setResponses([]responceCtx{{
mockPM.setResponses([]responseCtx{{
tenant: "Tenant1",
status: http.StatusOK,
body: ""},
Expand All @@ -507,7 +507,7 @@ var _ = Describe("AS3Manager Tests", func() {
mockMgr.FilterTenants = true
// setting failed tenant context
mockMgr.failedContext.failedTenants["Tenant1"] = as3Declaration("")
mockPM.setResponses([]responceCtx{{
mockPM.setResponses([]responseCtx{{
tenant: "Tenant1",
status: http.StatusOK,
body: ""},
Expand Down
30 changes: 15 additions & 15 deletions pkg/agent/as3/postManager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type (
RespIndex int
}

responceCtx struct {
responseCtx struct {
tenant string
status float64
body string
Expand All @@ -33,13 +33,13 @@ func newMockPostManger() *mockPostManager {
return mockPM
}

func (mockPM *mockPostManager) setResponses(responces []responceCtx, method string) {
func (mockPM *mockPostManager) setResponses(responses []responseCtx, method string) {
var body string

responseMap := make(mockhc.ResponseConfigMap)
responseMap[method] = &mockhc.ResponseConfig{}

for _, resp := range responces {
for _, resp := range responses {
if resp.body == "" {
if resp.status == http.StatusOK {
body = fmt.Sprintf(`{"results":[{"code":%f,"message":"none", "tenant": "%s"}]}`,
Expand Down Expand Up @@ -86,7 +86,7 @@ var _ = Describe("PostManager Tests", func() {

It("Handle HTTP Status OK, Accepted & Created", func() {
tnt := "test"
mockPM.setResponses([]responceCtx{
mockPM.setResponses([]responseCtx{
{
tenant: tnt,
status: http.StatusOK,
Expand All @@ -109,7 +109,7 @@ var _ = Describe("PostManager Tests", func() {

It("Handle Expected HTTP Response Errors", func() {
tnt := "test"
mockPM.setResponses([]responceCtx{
mockPM.setResponses([]responseCtx{
{
tenant: tnt,
status: http.StatusServiceUnavailable,
Expand All @@ -128,7 +128,7 @@ var _ = Describe("PostManager Tests", func() {

It("Handle Unexpected HTTP Response Errors", func() {
tnt := "test"
mockPM.setResponses([]responceCtx{
mockPM.setResponses([]responseCtx{
{
tenant: tnt,
status: http.StatusRequestTimeout,
Expand All @@ -152,7 +152,7 @@ var _ = Describe("PostManager Tests", func() {

It("Handle HTTP Response Errors: StatusServiceUnavailable", func() {
tnt := "test"
mockPM.setResponses([]responceCtx{
mockPM.setResponses([]responseCtx{
{
tenant: tnt,
status: http.StatusServiceUnavailable,
Expand All @@ -165,7 +165,7 @@ var _ = Describe("PostManager Tests", func() {
})
It("Handle HTTP Response Errors: StatusNotFound", func() {
tnt := "test"
mockPM.setResponses([]responceCtx{
mockPM.setResponses([]responseCtx{
{
tenant: tnt,
status: http.StatusNotFound,
Expand All @@ -178,7 +178,7 @@ var _ = Describe("PostManager Tests", func() {
})
It("Handle HTTP Response Errors: StatusUnprocessableEntity", func() {
tnt := "test"
mockPM.setResponses([]responceCtx{
mockPM.setResponses([]responseCtx{
{
tenant: tnt,
status: http.StatusUnprocessableEntity,
Expand All @@ -199,7 +199,7 @@ var _ = Describe("PostManager Tests", func() {
})

It("Get BIG-IP AS3 Version", func() {
mockPM.setResponses([]responceCtx{
mockPM.setResponses([]responseCtx{
{
tenant: "test",
status: http.StatusOK,
Expand All @@ -211,7 +211,7 @@ var _ = Describe("PostManager Tests", func() {
})

It("Validation1: Get BIG-IP AS3 Version", func() {
mockPM.setResponses([]responceCtx{
mockPM.setResponses([]responseCtx{
{
tenant: "test",
status: http.StatusNotFound,
Expand All @@ -223,7 +223,7 @@ var _ = Describe("PostManager Tests", func() {
})

It("Validation2: Get BIG-IP AS3 Version", func() {
mockPM.setResponses([]responceCtx{
mockPM.setResponses([]responseCtx{
{
tenant: "test",
status: http.StatusNotFound,
Expand All @@ -235,7 +235,7 @@ var _ = Describe("PostManager Tests", func() {
})

It("Validation3: Get BIG-IP AS3 Version", func() {
mockPM.setResponses([]responceCtx{
mockPM.setResponses([]responseCtx{
{
tenant: "test",
status: http.StatusNotFound,
Expand All @@ -250,7 +250,7 @@ var _ = Describe("PostManager Tests", func() {
Describe("Get BIGIP Registration key", func() {
It("Get Registration key successfully", func() {
tnt := "test"
mockPM.setResponses([]responceCtx{{
mockPM.setResponses([]responseCtx{{
tenant: tnt,
status: http.StatusOK,
body: `{"registrationKey": "sfiifhanji"}`,
Expand All @@ -261,7 +261,7 @@ var _ = Describe("PostManager Tests", func() {
})
It("Handle Failures while Getting Registration key", func() {
tnt := "test"
mockPM.setResponses([]responceCtx{
mockPM.setResponses([]responseCtx{
{
tenant: tnt,
status: http.StatusNotFound,
Expand Down
97 changes: 56 additions & 41 deletions pkg/controller/apiHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ type PostManagerInterface interface {
setupBIGIPRESTClient()
}

func NewGTMAPIHandler(params AgentParams, respChan chan *agentPostConfig) *GTMAPIHandler {
gtm := &GTMAPIHandler{
BaseAPIHandler: NewBaseAPIHandler(params, GTMBigIP, respChan),
Partition: DEFAULT_GTM_PARTITION,
func NewGTMAPIHandler(params AgentParams, baseAPIHandler *BaseAPIHandler, respChan chan *agentPostConfig) *GTMAPIHandler {
var gtm *GTMAPIHandler
if baseAPIHandler == nil {
gtm = &GTMAPIHandler{
BaseAPIHandler: NewBaseAPIHandler(params, GTMBigIP, respChan),
Partition: DEFAULT_GTM_PARTITION,
}
} else {
gtm = &GTMAPIHandler{
BaseAPIHandler: baseAPIHandler,
Partition: DEFAULT_GTM_PARTITION,
}
}
switch params.ApiType {
case AS3:
Expand Down Expand Up @@ -47,9 +55,16 @@ func NewBaseAPIHandler(params AgentParams, kind string, respChan chan *agentPost
}
}

func NewLTMAPIHandler(params AgentParams, kind string, respChan chan *agentPostConfig) *LTMAPIHandler {
ltm := &LTMAPIHandler{
BaseAPIHandler: NewBaseAPIHandler(params, kind, respChan),
func NewLTMAPIHandler(params AgentParams, kind string, baseAPIHandler *BaseAPIHandler, respChan chan *agentPostConfig) *LTMAPIHandler {
var ltm *LTMAPIHandler
if baseAPIHandler == nil {
ltm = &LTMAPIHandler{
BaseAPIHandler: NewBaseAPIHandler(params, kind, respChan),
}
} else {
ltm = &LTMAPIHandler{
BaseAPIHandler: baseAPIHandler,
}
}
// Initialize appropriate API handler based on type
switch params.ApiType {
Expand Down Expand Up @@ -142,43 +157,43 @@ func (api *BaseAPIHandler) IsBigIPAppServicesAvailable() error {
return api.APIHandler.getVersionsFromBigIPResponse(httpResp, responseMap)
}

func (api *BaseAPIHandler) GetDeclarationFromBigIP() (map[string]interface{}, error) {
url := api.APIHandler.getAPIURL([]string{})
// func (api *BaseAPIHandler) GetDeclarationFromBigIP() (map[string]interface{}, error) {
// url := api.APIHandler.getAPIURL([]string{})

req, err := api.createHTTPRequest(url, api.postManagerPrefix)
if err != nil {
return nil, fmt.Errorf("Internal Error")
}
httpResp, responseMap := api.httpReq(req)
if httpResp == nil || responseMap == nil {
return nil, fmt.Errorf("Internal Error")
}
// req, err := api.createHTTPRequest(url, api.postManagerPrefix)
// if err != nil {
// return nil, fmt.Errorf("Internal Error")
// }
// httpResp, responseMap := api.httpReq(req)
// if httpResp == nil || responseMap == nil {
// return nil, fmt.Errorf("Internal Error")
// }

return api.APIHandler.getDeclarationFromBigIPResponse(httpResp, responseMap)
// return api.APIHandler.getDeclarationFromBigIPResponse(httpResp, responseMap)

}
// }

func (gtmApi *GTMAPIHandler) GetDeclarationFromBigIP(postManagerPrefix string) (map[string]interface{}, error) {
url := gtmApi.APIHandler.getAPIURL([]string{})
// func (gtmApi *GTMAPIHandler) GetDeclarationFromBigIP(postManagerPrefix string) (map[string]interface{}, error) {
// url := gtmApi.APIHandler.getAPIURL([]string{})

// Create HTTP request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Errorf("[%s]%v Creating new HTTP request error: %v ", gtmApi.apiType, postManagerPrefix, err)
return nil, err
}
// // Create HTTP request
// req, err := http.NewRequest("GET", url, nil)
// if err != nil {
// log.Errorf("[%s]%v Creating new HTTP request error: %v ", gtmApi.apiType, postManagerPrefix, err)
// return nil, err
// }

// Set basic auth credentials
req.SetBasicAuth(gtmApi.BIGIPUsername, gtmApi.BIGIPPassword)
// // Set basic auth credentials
// req.SetBasicAuth(gtmApi.BIGIPUsername, gtmApi.BIGIPPassword)

// Make HTTP request
httpResp, responseMap := gtmApi.httpReq(req)
if httpResp == nil || responseMap == nil {
return nil, fmt.Errorf("Internal Error")
}
// // Make HTTP request
// httpResp, responseMap := gtmApi.httpReq(req)
// if httpResp == nil || responseMap == nil {
// return nil, fmt.Errorf("Internal Error")
// }

return gtmApi.APIHandler.getDeclarationFromBigIPResponse(httpResp, responseMap)
}
// return gtmApi.APIHandler.getDeclarationFromBigIPResponse(httpResp, responseMap)
// }

func (api *BaseAPIHandler) createHTTPRequest(url string, postManagerPrefix string) (*http.Request, error) {
// Create HTTP request
Expand All @@ -194,11 +209,11 @@ func (api *BaseAPIHandler) createHTTPRequest(url string, postManagerPrefix strin
return req, nil
}

func (api *BaseAPIHandler) publishConfig(cfg *agentPostConfig) {
log.Debugf("[%s]%v PostManager Accepted the configuration", api.apiType, api.postManagerPrefix)
// postConfig updates the tenantResponseMap with response codes
api.postConfig(cfg)
}
// func (api *BaseAPIHandler) publishConfig(cfg *agentPostConfig) {
// log.Debugf("[%s]%v PostManager Accepted the configuration", api.apiType, api.postManagerPrefix)
// // postConfig updates the tenantResponseMap with response codes
// api.postConfig(cfg)
// }

func (api *BaseAPIHandler) PopulateAPIVersion() {
version, build, schemaVersion, err := api.GetBigIPAPIVersion()
Expand Down
Loading
Loading