Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lyt122 committed Aug 6, 2024
1 parent f0d005c commit d7de52d
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 117 deletions.
5 changes: 4 additions & 1 deletion controller/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/nacos-group/nacos-sdk-go v1.1.4
github.com/onsi/ginkgo/v2 v2.17.2
github.com/onsi/gomega v1.33.0
github.com/smartystreets/goconvey v1.6.4
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.27.0
Expand Down Expand Up @@ -83,6 +84,7 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand All @@ -97,6 +99,7 @@ require (
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand All @@ -119,12 +122,12 @@ require (
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stoewer/go-strcase v1.2.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tchap/go-patricia/v2 v2.3.1 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
Expand Down
4 changes: 4 additions & 0 deletions controller/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6/go.mod h1:kf6iHlnVGwg
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
Expand Down Expand Up @@ -218,6 +219,7 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand Down Expand Up @@ -333,8 +335,10 @@ github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPx
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
Expand Down
57 changes: 16 additions & 41 deletions controller/registries/consul/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,17 @@ func init() {
name: om.Name,
softDeletedServices: map[consulService]bool{},
done: make(chan struct{}),
clientFactory: factory,
}
return reg, nil
})
}

var (
//RegistryType = "consul"
factory ClientFactory = &DefaultClientFactory{}
)

type consulCatalog interface {
Services(q *consulapi.QueryOptions) (map[string][]string, *consulapi.QueryMeta, error)
}

type ConsulAPI struct {
client *consulapi.Client
}

func (c *ConsulAPI) Services(q *consulapi.QueryOptions) (map[string][]string, *consulapi.QueryMeta, error) {
return c.client.Catalog().Services(q)
}

type Consul struct {
consul.RegistryType
logger log.RegistryLogger
store registry.ServiceEntryStore
name string
client *Client
clientFactory ClientFactory
logger log.RegistryLogger
store registry.ServiceEntryStore
name string
client *Client

lock sync.RWMutex
watchingServices map[consulService]bool
Expand All @@ -81,20 +62,14 @@ type Consul struct {

type Client struct {
consulClient *consulapi.Client
consulCatalog consulCatalog
consulCatalog *consulapi.Catalog

DataCenter string
NameSpace string
Token string
}

type ClientFactory interface {
NewClient(config *consul.Config) (*Client, error)
}

type DefaultClientFactory struct{}

func (f *DefaultClientFactory) NewClient(config *consul.Config) (*Client, error) {
func (reg *Consul) NewClient(config *consul.Config) (*Client, error) {
uri, err := url.Parse(config.ServerUrl)
if err != nil {
return nil, fmt.Errorf("invalid server url: %s", config.ServerUrl)
Expand All @@ -120,21 +95,21 @@ func (f *DefaultClientFactory) NewClient(config *consul.Config) (*Client, error)
}

type consulService struct {
DataCenter string
Tag string
ServiceName string
}

func (reg *Consul) Start(c registrytype.RegistryConfig) error {
config := c.(*consul.Config)

client, err := reg.clientFactory.NewClient(config)
client, err := reg.NewClient(config)
if err != nil {
return err
}

reg.client = client

services, err := reg.fetchAllServices(reg.client)
services, err := reg.FetchAllServices(reg.client)

if err != nil {
return err
Expand Down Expand Up @@ -193,7 +168,7 @@ func (reg *Consul) Reload(c registrytype.RegistryConfig) error {
return nil
}

func (reg *Consul) fetchAllServices(client *Client) (map[consulService]bool, error) {
func (reg *Consul) FetchAllServices(client *Client) (map[consulService]bool, error) {
q := &consulapi.QueryOptions{}
q.Datacenter = client.DataCenter
q.Namespace = client.NameSpace
Expand All @@ -205,10 +180,10 @@ func (reg *Consul) fetchAllServices(client *Client) (map[consulService]bool, err
return nil, err
}
serviceMap := make(map[consulService]bool)
for serviceName, dataCenters := range services {
for _, dc := range dataCenters {
for serviceName, tags := range services {
for _, tag := range tags {
service := consulService{
DataCenter: dc,
Tag: tag,
ServiceName: serviceName,
}
serviceMap[service] = true
Expand All @@ -229,10 +204,10 @@ func (reg *Consul) unsubscribe(serviceName string) error {

func (reg *Consul) refresh(services map[string][]string) {
serviceMap := make(map[consulService]bool)
for serviceName, dataCenters := range services {
for _, dc := range dataCenters {
for serviceName, tags := range services {
for _, tag := range tags {
service := consulService{
DataCenter: dc,
Tag: tag,
ServiceName: serviceName,
}
serviceMap[service] = true
Expand Down
Loading

0 comments on commit d7de52d

Please sign in to comment.