Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"abtest_test.go",
"dao_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/resource/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"abtest.go",
"dao.go",
],
importpath = "go-common/app/service/main/resource/dao/abtest",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/resource/conf:go_default_library",
"//app/service/main/resource/model:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,32 @@
package abtest
import (
"context"
"fmt"
"net/url"
"go-common/app/service/main/resource/model"
"go-common/library/log"
)
// AbTest get abtest data from data-platform.
func (d *Dao) AbTest(c context.Context, names, ipaddr string) (adr []*model.AbTest, err error) {
params := url.Values{}
params.Set("groupNames", names)
var res struct {
Code int `json:"code"`
Data []*model.AbTest `json:"expItems"`
Msg string `json:"msg"`
}
if err = d.httpClient.Get(c, d.testURL, ipaddr, params, &res); err != nil {
log.Error("AbTest url(%s) error(%v)", d.testURL+"?"+params.Encode(), err)
return
}
if res.Code != 0 {
err = fmt.Errorf("AbTest api failed(%d)", res.Code)
log.Error("CpmsApp url(%s) res code(%d) or res.data(%v)", d.testURL+"?"+params.Encode(), res.Code, res.Data)
return
}
adr = res.Data
return
}

View File

@@ -0,0 +1,39 @@
package abtest
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestAbtestAbTest(t *testing.T) {
convey.Convey("Abtest", t, func() {
var (
c = context.Background()
names = "test"
ipaddr = "0.0.0.0"
)
convey.Convey("When everything is correct", func(ctx convey.C) {
adr, err := d.AbTest(context.TODO(), "", "")
ctx.Convey("Then error should be niladr should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(adr, convey.ShouldNotBeNil)
})
})
convey.Convey("When http request gets 404 error", func(ctx convey.C) {
httpMock("GET", d.testURL).Reply(404)
_, err := d.AbTest(c, names, ipaddr)
ctx.Convey("Then error should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
convey.Convey("When http request gets code != 0", func(ctx convey.C) {
httpMock("GET", d.testURL).Reply(200).JSON(`{"code":-3,"message":"faild","data":{}}`)
_, err := d.AbTest(c, names, ipaddr)
ctx.Convey("Then error should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,28 @@
package abtest
import (
"go-common/app/service/main/resource/conf"
httpx "go-common/library/net/http/blademaster"
)
// Dao define db struct
type Dao struct {
c *conf.Config
// cpt
httpClient *httpx.Client
testURL string
}
const (
_abTestURL = "/abserver/v1/app/query-exp"
)
// New init mysql db
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
httpClient: httpx.NewClient(c.HTTPClient),
testURL: c.Host.DataPlat + _abTestURL,
}
return
}

View File

@@ -0,0 +1,46 @@
package abtest
import (
"flag"
"os"
"strings"
"testing"
"go-common/app/service/main/resource/conf"
"gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.resource-service")
flag.Set("conf_token", "y79sErNhxggjvULS0O8Czas9PaxHBF5o")
flag.Set("tree_id", "3232")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../../cmd/resource-service-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
d.httpClient.SetTransport(gock.DefaultTransport)
m.Run()
os.Exit(0)
}
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
return r
}