Skip to content

Refactor to move files out of /pkg and fix import cycle #6

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

Merged
merged 1 commit into from
Mar 28, 2019
Merged
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
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func initializeConfiguration(useRegistry bool, useProfile string) (*Configuratio
}

if useRegistry {
registryConfig := registry.Config{
registryConfig := types.Config{
Host: conf.Registry.Host,
Port: conf.Registry.Port,
Type: conf.Registry.Type,
Expand All @@ -49,7 +49,7 @@ func initializeConfiguration(useRegistry bool, useProfile string) (*Configuratio
Stem: internal.ConfigRegistryStem,
}

registryClient, err = factory.NewRegistryClient(registryConfig)
registryClient, err = registry.NewRegistryClient(registryConfig)
if err != nil {
return fmt.Errorf("connection to Registry could not be made: %v", err.Error())
}
Expand Down Expand Up @@ -123,11 +123,12 @@ func listenForConfigChanges() {
This code snippet shows how to get dependent service endpoint information and check status of the dependent service.
```
...
if registry.Client != nil {
endpoint, err = registry.Client.GetServiceEndpoint(params.ServiceKey)
if e.RegistryClient != nil {
endpoint, err = (*e.RegistryClient).GetServiceEndpoint(params.ServiceKey)
...
url := fmt.Sprintf("http://%s:%v%s", endpoint.Address, endpoint.Port, params.Path)
...
if registry.Client.IsServiceAvailable(params.ServiceKey) {
if (*e.RegistryClient).IsServiceAvailable(params.ServiceKey) {
...
}
}
Expand Down
10 changes: 5 additions & 5 deletions internal/pkg/consul/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package consul

import (
"fmt"
"github.com/edgexfoundry/go-mod-registry"
"github.com/edgexfoundry/go-mod-registry/pkg/types"
consulapi "github.com/hashicorp/consul/api"
"github.com/mitchellh/consulstructure"
"github.com/pelletier/go-toml"
Expand Down Expand Up @@ -46,7 +46,7 @@ type consulClient struct {
}

// Create new Consul Client. Service details are optional, not needed just for configuration, but required if registering
func NewConsulClient(registryConfig registry.Config,) (*consulClient, error) {
func NewConsulClient(registryConfig types.Config,) (*consulClient, error) {

client := consulClient{
serviceKey: registryConfig.ServiceKey,
Expand Down Expand Up @@ -280,13 +280,13 @@ func (client *consulClient) PutConfigurationValue(name string, value []byte) err
}

// Gets the service endpoint information for the target ID from Consul
func (client *consulClient) GetServiceEndpoint(serviceID string) (registry.ServiceEndpoint, error) {
func (client *consulClient) GetServiceEndpoint(serviceID string) (types.ServiceEndpoint, error) {
services, err := client.consulClient.Agent().Services()
if err != nil {
return registry.ServiceEndpoint{}, err
return types.ServiceEndpoint{}, err
}

endpoint := registry.ServiceEndpoint{}
endpoint := types.ServiceEndpoint{}
if service, ok := services[serviceID]; ok {
endpoint.Port = service.Port
endpoint.ServiceId = serviceID
Expand Down
119 changes: 60 additions & 59 deletions internal/pkg/consul/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package consul

import (
"github.com/edgexfoundry/go-mod-registry"
"log"
"net/http"
"net/http/httptest"
Expand All @@ -31,6 +30,8 @@ import (
"github.com/hashicorp/consul/api"
"github.com/pelletier/go-toml"
"github.com/stretchr/testify/assert"

"github.com/edgexfoundry/go-mod-registry/pkg/types"
)

const (
Expand All @@ -55,7 +56,7 @@ type LoggingInfo struct {

type MyConfig struct {
Logging LoggingInfo
Service registry.ServiceEndpoint
Service types.ServiceEndpoint
Port int
Host string
LogLevel string
Expand Down Expand Up @@ -116,7 +117,7 @@ func TestHasConfigurationTrue(t *testing.T) {
}

// Now push a value so the configuration will exist
client.PutConfigurationValue("Dummy", []byte("Value"))
_ = client.PutConfigurationValue("Dummy", []byte("Value"))

actual, err := client.HasConfiguration()
if !assert.NoError(t, err) {
Expand Down Expand Up @@ -164,7 +165,7 @@ func TestRegisterWithPingCallback(t *testing.T) {
receivedPing = true

writer.Header().Set("Content-Type", "text/plain")
writer.Write([]byte("pong"))
_,_ = writer.Write([]byte("pong"))

doneChan <- true
}
Expand All @@ -178,13 +179,13 @@ func TestRegisterWithPingCallback(t *testing.T) {

client := makeConsulClient(t, serverPort, true)
// Make sure service is not already registered.
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
client.consulClient.Agent().CheckDeregister(client.serviceKey)
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)

// Try to clean-up after test
defer func(client *consulClient) {
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
client.consulClient.Agent().CheckDeregister(client.serviceKey)
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)
}(client)

// Register the service endpoint and health check callback
Expand All @@ -203,22 +204,22 @@ func TestRegisterWithPingCallback(t *testing.T) {
}

func TestGetServiceEndpoint(t *testing.T) {
expectedNotFoundEndpoint := registry.ServiceEndpoint{}
expectedFoundEndpoint := registry.ServiceEndpoint{
expectedNotFoundEndpoint := types.ServiceEndpoint{}
expectedFoundEndpoint := types.ServiceEndpoint{
ServiceId: serviceName,
Host: serviceHost,
Port: defaultServicePort,
}

client := makeConsulClient(t, defaultServicePort, true)
// Make sure service is not already registered.
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
client.consulClient.Agent().CheckDeregister(client.serviceKey)
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)

// Try to clean-up after test
defer func(client *consulClient) {
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
client.consulClient.Agent().CheckDeregister(client.serviceKey)
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)
}(client)

// Test for endpoint not found
Expand Down Expand Up @@ -251,8 +252,8 @@ func TestIsServiceAvailableNotRegistered(t *testing.T) {
client := makeConsulClient(t, defaultServicePort, true)

// Make sure service is not already registered.
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
client.consulClient.Agent().CheckDeregister(client.serviceKey)
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)

actual := client.IsServiceAvailable(client.serviceKey)
if !assert.Error(t, actual, "expected error") {
Expand All @@ -267,13 +268,13 @@ func TestIsServiceAvailableNotHealthy(t *testing.T) {
client := makeConsulClient(t, defaultServicePort, true)

// Make sure service is not already registered.
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
client.consulClient.Agent().CheckDeregister(client.serviceKey)
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)

// Try to clean-up after test
defer func(client *consulClient) {
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
client.consulClient.Agent().CheckDeregister(client.serviceKey)
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)
}(client)

// Register the service endpoint, without test service to respond to health check
Expand Down Expand Up @@ -303,7 +304,7 @@ func TestIsServiceAvailableHealthy(t *testing.T) {
switch request.Method {
case "GET":
writer.Header().Set("Content-Type", "text/plain")
writer.Write([]byte("pong"))
_,_ = writer.Write([]byte("pong"))

doneChan <- true
}
Expand All @@ -317,13 +318,13 @@ func TestIsServiceAvailableHealthy(t *testing.T) {

client := makeConsulClient(t, serverPort, true)
// Make sure service is not already registered.
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
client.consulClient.Agent().CheckDeregister(client.serviceKey)
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)

// Try to clean-up after test
defer func(client *consulClient) {
client.consulClient.Agent().ServiceDeregister(client.serviceKey)
client.consulClient.Agent().CheckDeregister(client.serviceKey)
_ = client.consulClient.Agent().ServiceDeregister(client.serviceKey)
_ = client.consulClient.Agent().CheckDeregister(client.serviceKey)
}(client)

// Register the service endpoint
Expand Down Expand Up @@ -366,7 +367,7 @@ func TestConfigurationValueExists(t *testing.T) {
if !assert.NoError(t, err) {
t.Fatal()
}
if !assert.Equal(t, expected, actual) {
if !assert.False(t, actual) {
t.Fatal()
}

Expand Down Expand Up @@ -423,7 +424,7 @@ func TestPutConfigurationValue(t *testing.T) {
client := makeConsulClient(t, defaultServicePort, true)

//clean up the the key, if it exists
client.consulClient.KV().Delete(expectedFullKey, nil)
_,_ = client.consulClient.KV().Delete(expectedFullKey, nil)

err := client.PutConfigurationValue(key, expected)
assert.NoError(t, err)
Expand All @@ -448,7 +449,7 @@ func TestGetConfiguration(t *testing.T) {
EnableRemote: true,
File: "NONE",
},
Service: registry.ServiceEndpoint{
Service: types.ServiceEndpoint{
ServiceId: "Dummy",
Host: "10.6.7.8",
Port: 8080,
Expand All @@ -460,14 +461,14 @@ func TestGetConfiguration(t *testing.T) {

client := makeConsulClient(t, defaultServicePort, true)

client.PutConfigurationValue("Logging/EnableRemote", []byte(strconv.FormatBool(expected.Logging.EnableRemote)))
client.PutConfigurationValue("Logging/File", []byte(expected.Logging.File))
client.PutConfigurationValue("Service/ServiceId", []byte(expected.Service.ServiceId))
client.PutConfigurationValue("Service/Host", []byte(expected.Service.Host))
client.PutConfigurationValue("Service/Port", []byte(strconv.Itoa(expected.Service.Port)))
client.PutConfigurationValue("Port", []byte(strconv.Itoa(expected.Port)))
client.PutConfigurationValue("Host", []byte(expected.Host))
client.PutConfigurationValue("LogLevel", []byte(expected.LogLevel))
_ = client.PutConfigurationValue("Logging/EnableRemote", []byte(strconv.FormatBool(expected.Logging.EnableRemote)))
_ = client.PutConfigurationValue("Logging/File", []byte(expected.Logging.File))
_ = client.PutConfigurationValue("Service/ServiceId", []byte(expected.Service.ServiceId))
_ = client.PutConfigurationValue("Service/Host", []byte(expected.Service.Host))
_ = client.PutConfigurationValue("Service/Port", []byte(strconv.Itoa(expected.Service.Port)))
_ = client.PutConfigurationValue("Port", []byte(strconv.Itoa(expected.Port)))
_ = client.PutConfigurationValue("Host", []byte(expected.Host))
_ = client.PutConfigurationValue("LogLevel", []byte(expected.LogLevel))

result, err := client.GetConfiguration(&MyConfig{})

Expand Down Expand Up @@ -497,7 +498,7 @@ func TestPutConfiguration(t *testing.T) {
EnableRemote: true,
File: "NONE",
},
Service: registry.ServiceEndpoint{
Service: types.ServiceEndpoint{
ServiceId: "Dummy",
Host: "10.6.7.8",
Port: 8080,
Expand All @@ -511,11 +512,11 @@ func TestPutConfiguration(t *testing.T) {
client := makeConsulClient(t, defaultServicePort, true)

// Make sure the tree of values doesn't exist.
client.consulClient.KV().DeleteTree(consulBasePath, nil)
_ , _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)

defer func() {
// Clean up
client.consulClient.KV().DeleteTree(consulBasePath, nil)
_ , _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)
}()

err := client.PutConfiguration(expected, true)
Expand All @@ -538,7 +539,7 @@ func TestPutConfiguration(t *testing.T) {
assert.True(t,configValueSet("LogLevel", client))
}

func configValueSet(key string, client registry.Client) bool {
func configValueSet(key string, client *consulClient) bool {
exists, _ := client.ConfigurationValueExists(key)
return exists
}
Expand All @@ -547,11 +548,11 @@ func TestPutConfigurationTomlNoPreviousValues(t *testing.T) {
client := makeConsulClient(t, defaultServicePort, true)

// Make sure the tree of values doesn't exist.
client.consulClient.KV().DeleteTree(consulBasePath, nil)
_ , _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)

defer func() {
// Clean up
client.consulClient.KV().DeleteTree(consulBasePath, nil)
_ , _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)
}()

configMap := createKeyValueMap()
Expand Down Expand Up @@ -582,11 +583,11 @@ func TestPutConfigurationTomlWithoutOverWrite(t *testing.T) {
client := makeConsulClient(t, defaultServicePort, true)

// Make sure the tree of values doesn't exist.
client.consulClient.KV().DeleteTree(consulBasePath, nil)
_ , _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)

defer func() {
// Clean up
client.consulClient.KV().DeleteTree(consulBasePath, nil)
_ , _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)
}()

configMap := createKeyValueMap()
Expand Down Expand Up @@ -629,10 +630,10 @@ func TestPutConfigurationTomlOverWrite(t *testing.T) {
client := makeConsulClient(t, defaultServicePort, true)

// Make sure the tree of values doesn't exist.
client.consulClient.KV().DeleteTree(consulBasePath, nil)
_, _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)
// Clean up after unit test
defer func() {
client.consulClient.KV().DeleteTree(consulBasePath, nil)
_, _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)
}()

configMap := createKeyValueMap()
Expand Down Expand Up @@ -676,7 +677,7 @@ func TestWatchForChanges(t *testing.T) {
EnableRemote: true,
File: "NONE",
},
Service: registry.ServiceEndpoint{
Service: types.ServiceEndpoint{
ServiceId: "Dummy",
Host: "10.6.7.8",
Port: 8080,
Expand All @@ -691,20 +692,20 @@ func TestWatchForChanges(t *testing.T) {
client := makeConsulClient(t, defaultServicePort, false)

// Make sure the tree of values doesn't exist.
client.consulClient.KV().DeleteTree(consulBasePath, nil)
_, _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)
// Clean up after unit test
defer func() {
client.consulClient.KV().DeleteTree(consulBasePath, nil)
_, _ = client.consulClient.KV().DeleteTree(consulBasePath, nil)
}()

client.PutConfigurationValue("Logging/EnableRemote", []byte(strconv.FormatBool(expectedConfig.Logging.EnableRemote)))
client.PutConfigurationValue("Logging/File", []byte(expectedConfig.Logging.File))
client.PutConfigurationValue("Service/ServiceId", []byte(expectedConfig.Service.ServiceId))
client.PutConfigurationValue("Service/Host", []byte(expectedConfig.Service.Host))
client.PutConfigurationValue("Service/Port", []byte(strconv.Itoa(expectedConfig.Service.Port)))
client.PutConfigurationValue("Port", []byte(strconv.Itoa(expectedConfig.Port)))
client.PutConfigurationValue("Host", []byte(expectedConfig.Host))
client.PutConfigurationValue("LogLevel", []byte(expectedConfig.LogLevel))
_ = client.PutConfigurationValue("Logging/EnableRemote", []byte(strconv.FormatBool(expectedConfig.Logging.EnableRemote)))
_ = client.PutConfigurationValue("Logging/File", []byte(expectedConfig.Logging.File))
_ = client.PutConfigurationValue("Service/ServiceId", []byte(expectedConfig.Service.ServiceId))
_ = client.PutConfigurationValue("Service/Host", []byte(expectedConfig.Service.Host))
_ = client.PutConfigurationValue("Service/Port", []byte(strconv.Itoa(expectedConfig.Service.Port)))
_ = client.PutConfigurationValue("Port", []byte(strconv.Itoa(expectedConfig.Port)))
_ = client.PutConfigurationValue("Host", []byte(expectedConfig.Host))
_ = client.PutConfigurationValue("LogLevel", []byte(expectedConfig.LogLevel))

updateChannel := make(chan interface{})
errorChannel := make(chan error)
Expand All @@ -728,7 +729,7 @@ func TestWatchForChanges(t *testing.T) {
}

// Make a change to logging
client.PutConfigurationValue("Logging/File", []byte(expectedChange))
_ = client.PutConfigurationValue("Logging/File", []byte(expectedChange))

pass--
continue
Expand All @@ -745,7 +746,7 @@ func TestWatchForChanges(t *testing.T) {
}

func makeConsulClient(t *testing.T, servicePort int, setServiceInfo bool) *consulClient {
registryConfig := registry.Config{
registryConfig := types.Config{
Host: testHost,
Port: port,
Stem: "edgex/core/1.0/",
Expand Down
Loading