Skip to content

Commit

Permalink
Merge pull request #46 from chuntaojun/feat_report_client
Browse files Browse the repository at this point in the history
[ISSUE #18] Add TestSuit for location-plugin & reportHandler
  • Loading branch information
andrewshan authored Apr 26, 2022
2 parents ddddfe5 + 0af33ba commit c801785
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 6 deletions.
1 change: 1 addition & 0 deletions pkg/plugin/register/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ import (
_ "github.com/polarismesh/polaris-go/plugin/reporthandler/location"

// 注册 location 地址插件
_ "github.com/polarismesh/polaris-go/plugin/location/env"
_ "github.com/polarismesh/polaris-go/plugin/location/tencent"
)
4 changes: 3 additions & 1 deletion plugin/location/env/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package env
import (
"os"

"github.com/polarismesh/polaris-go/pkg/log"
"github.com/polarismesh/polaris-go/pkg/model"
"github.com/polarismesh/polaris-go/pkg/plugin"
"github.com/polarismesh/polaris-go/pkg/plugin/common"
Expand All @@ -46,13 +47,14 @@ type LocationProvider struct {

// Init 初始化插件
func (p *LocationProvider) Init(ctx *plugin.InitContext) error {
log.GetBaseLogger().Infof("start use env location provider")
p.PluginBase = plugin.NewPluginBase(ctx)
return nil
}

// Destroy 销毁插件,可用于释放资源
func (p *LocationProvider) Destroy() error {
return nil
return p.PluginBase.Destroy()
}

// Type 插件类型
Expand Down
4 changes: 3 additions & 1 deletion plugin/location/tencent/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type LocationProvider struct {

// Init 初始化插件
func (p *LocationProvider) Init(ctx *plugin.InitContext) error {
log.GetBaseLogger().Infof("start use tencent location provider")
p.PluginBase = plugin.NewPluginBase(ctx)
return nil
}
Expand All @@ -64,7 +65,8 @@ func (p *LocationProvider) Name() string {

// Destroy 销毁插件,可用于释放资源
func (p *LocationProvider) Destroy() error {
return nil

return p.PluginBase.Destroy()
}

// GetLocation 获取地理位置信息
Expand Down
4 changes: 4 additions & 0 deletions plugin/reporthandler/location/reporthandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func (h *ReportHandler) HandleResponse(resp *model.ReportClientResponse, err err
} else {
loc = _loc
}

resp.Region = loc.Region
resp.Zone = loc.Zone
resp.Campus = loc.Campus
}
h.updateLocation(loc, nil)
}
Expand Down
12 changes: 8 additions & 4 deletions test/all_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@
package test

import (
"log"
"net/http"
_ "net/http/pprof"
"testing"

"github.com/polarismesh/polaris-go/test/circuitbreak"
"github.com/polarismesh/polaris-go/test/discover"
"github.com/polarismesh/polaris-go/test/loadbalance"
"github.com/polarismesh/polaris-go/test/observability"
"github.com/polarismesh/polaris-go/test/ratelimit"
"github.com/polarismesh/polaris-go/test/reporthandler"
"github.com/polarismesh/polaris-go/test/serviceroute"
"github.com/polarismesh/polaris-go/test/stability"
"github.com/polarismesh/polaris-go/test/subscribe"
"log"
"net/http"
_ "net/http/pprof"
"testing"

"github.com/polarismesh/polaris-go/api"
. "gopkg.in/check.v1"
Expand Down Expand Up @@ -93,6 +95,8 @@ func init() {
Suite(&loadbalance.InnerServiceLBTestingSuite{})
// 基础本地限流用例测试
Suite(&ratelimit.LocalNormalTestingSuite{})
// ReportClient相关测试用例
Suite(&reporthandler.ReporthandlerTestingSuite{})

// 基础远程限流用例测试
// Suite(&ratelimit.RemoteNormalTestingSuite{})
Expand Down
90 changes: 90 additions & 0 deletions test/reporthandler/reporthandler_suit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* Tencent is pleased to support the open source community by making polaris-go available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package reporthandler

import (
"log"
"os"

"github.com/polarismesh/polaris-go/api"
"github.com/polarismesh/polaris-go/pkg/config"
"github.com/polarismesh/polaris-go/pkg/flow/data"
"github.com/polarismesh/polaris-go/pkg/model"
"github.com/polarismesh/polaris-go/test/util"
"gopkg.in/check.v1"
)

type ReporthandlerTestingSuite struct {
}

// GetName 套件名字
func (t *ReporthandlerTestingSuite) GetName() string {
return "Reporthandler"
}

// SetUpSuite 启动测试套程序
func (t *ReporthandlerTestingSuite) SetUpSuite(c *check.C) {

}

func (t *ReporthandlerTestingSuite) TestHandlerClientReport(c *check.C) {
log.Printf("Start TestHandlerClientReport")

envKeyRegion := "POLARIS_INSTANCE_REGION"
envKeyZone := "POLARIS_INSTANCE_ZONE"
envKeyCampus := "POLARIS_INSTANCE_CAMPUS"

if err := os.Setenv(envKeyRegion, envKeyRegion); err != nil {
c.Fatal(err)
}
if err := os.Setenv(envKeyZone, envKeyZone); err != nil {
c.Fatal(err)
}
if err := os.Setenv(envKeyCampus, envKeyCampus); err != nil {
c.Fatal(err)
}

cfg, err := config.LoadConfigurationByFile("testdata/consumer.yaml")
c.Assert(err, check.IsNil)
cfg.GetGlobal().GetLocation().SetProvider("env")
sdkCtx, err := api.InitContextByConfig(cfg)
c.Assert(err, check.IsNil)

reportChain, err := data.GetReportChain(nil, sdkCtx.GetPlugins())
c.Assert(err, check.IsNil)
c.Assert(reportChain, check.NotNil)
c.Assert(len(reportChain.Chain), check.Equals, 1)

req := &model.ReportClientRequest{}
resp := &model.ReportClientResponse{}

for i := range reportChain.Chain {
handler := reportChain.Chain[i]
handler.HandleRequest(req)
handler.HandleResponse(resp, nil)
}

c.Assert(resp.Region, check.Equals, envKeyRegion)
c.Assert(resp.Zone, check.Equals, envKeyZone)
c.Assert(resp.Campus, check.Equals, envKeyCampus)
}

// TearDownSuite 结束测试套程序
func (t *ReporthandlerTestingSuite) TearDownSuite(c *check.C) {
util.InsertLog(t, c.GetTestLog())
}

0 comments on commit c801785

Please sign in to comment.