This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added consul wrapper * rename test function * tmp. added verbose flag * - hiding error removed from the constructor - added interface for update TTL - added dummy consul client * removed verbose flag from test runner * update change log
- Loading branch information
Showing
10 changed files
with
230 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
app: | ||
opt1: opt1 | ||
opt2: 2 | ||
app2: | ||
opt1: 2 | ||
opt2: opt2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package consul | ||
|
||
import ( | ||
"github.com/hashicorp/consul/api" | ||
) | ||
|
||
// TTLUpdater is an interface to update the TTL of service | ||
type TTLUpdater interface { | ||
UpdateTTL(checkID, status, output string) error | ||
} | ||
|
||
// Client provides a client to functions of Consul API | ||
// for one serivce | ||
type Client struct { | ||
clientAPI *api.Client | ||
srvID string | ||
} | ||
|
||
// NewDefaultClient is the most default constructor for Consul agent for one serivce. | ||
// It initializes HTTP client for a local agent and register service on it. | ||
// srvName sets for ID, Name and CheckID of service. | ||
func NewDefaultClient(srvName, localIP string, svcPort int, checkTTL string) (*Client, error) { | ||
var err error | ||
|
||
client := &Client{srvID: srvName} | ||
|
||
client.clientAPI, err = api.NewClient(api.DefaultConfig()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
srvRegInfo := &api.AgentServiceRegistration{ | ||
ID: srvName, | ||
Name: srvName, | ||
Address: localIP, | ||
Port: svcPort, | ||
Check: &api.AgentServiceCheck{ | ||
CheckID: srvName, | ||
TTL: checkTTL, | ||
}, | ||
} | ||
|
||
if err = client.Register(srvRegInfo); err != nil { | ||
return nil, err | ||
} | ||
|
||
return client, nil | ||
} | ||
|
||
// Agent returns a handle to the agent endpoints | ||
func (c *Client) Agent() *api.Agent { | ||
return c.clientAPI.Agent() | ||
} | ||
|
||
// IsReachable check whether we can reach the agent | ||
func (c *Client) IsReachable() bool { | ||
_, err := c.clientAPI.Agent().Self() | ||
|
||
return err == nil | ||
} | ||
|
||
// Register is used to register a new service with given ID | ||
func (c *Client) Register(srvInfo *api.AgentServiceRegistration) error { | ||
c.srvID = srvInfo.ID | ||
return c.clientAPI.Agent().ServiceRegister(srvInfo) | ||
} | ||
|
||
// Deregister is used to deregister a service | ||
func (c *Client) Deregister() error { | ||
return c.clientAPI.Agent().ServiceDeregister(c.srvID) | ||
} | ||
|
||
// PassingTTL is used to update the TTL of a default check | ||
// with status 'passing' | ||
func (c *Client) PassingTTL(output string) error { | ||
return c.UpdateTTL(c.srvID, api.HealthPassing, output) | ||
} | ||
|
||
// CriticalTTL is used to update the TTL of a default check | ||
// with status 'critical' | ||
func (c *Client) CriticalTTL(output string) error { | ||
return c.UpdateTTL(c.srvID, api.HealthCritical, output) | ||
} | ||
|
||
// WarningTTL is used to update the TTL of a default check | ||
// with status 'warning' | ||
func (c *Client) WarningTTL(output string) error { | ||
return c.UpdateTTL(c.srvID, api.HealthWarning, output) | ||
} | ||
|
||
// UpdateTTL is used to update the TTL of a check | ||
func (c *Client) UpdateTTL(checkID, status, output string) error { | ||
return c.clientAPI.Agent().UpdateTTL(checkID, output, status) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package consul | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/consul/testutil" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConsulWrapper(t *testing.T) { | ||
srv1, err := testutil.NewTestServerConfig(func(c *testutil.TestServerConfig) { | ||
c.Ports.HTTP = 8500 | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
serviceName := "testService" | ||
client, err := NewDefaultClient(serviceName, `127.0.0.1`, 8080, "10m") | ||
assert.NoError(t, err) | ||
assert.Implements(t, (*TTLUpdater)(nil), client) | ||
|
||
consulAgent := client.Agent() | ||
|
||
assert.NoError(t, client.PassingTTL(testutil.HealthPassing)) | ||
|
||
status, info, err := consulAgent.AgentHealthServiceByID(serviceName) | ||
assert.NoError(t, err) | ||
assert.Equal(t, testutil.HealthPassing, info.Checks[0].Output) | ||
assert.Equal(t, testutil.HealthPassing, status) | ||
|
||
assert.NoError(t, client.WarningTTL(testutil.HealthWarning)) | ||
|
||
status, info, err = consulAgent.AgentHealthServiceByID(serviceName) | ||
assert.NoError(t, err) | ||
assert.Equal(t, testutil.HealthWarning, info.Checks[0].Output) | ||
assert.Equal(t, testutil.HealthWarning, status) | ||
|
||
assert.NoError(t, client.CriticalTTL(testutil.HealthCritical)) | ||
|
||
status, info, err = consulAgent.AgentHealthServiceByID(serviceName) | ||
assert.NoError(t, err) | ||
assert.Equal(t, testutil.HealthCritical, info.Checks[0].Output) | ||
assert.Equal(t, testutil.HealthCritical, status) | ||
|
||
assert.NoError(t, client.UpdateTTL(serviceName, testutil.HealthPassing, testutil.HealthPassing)) | ||
|
||
status, info, err = consulAgent.AgentHealthServiceByID(serviceName) | ||
assert.NoError(t, err) | ||
assert.Equal(t, testutil.HealthPassing, info.Checks[0].Output) | ||
assert.Equal(t, testutil.HealthPassing, status) | ||
|
||
assert.NoError(t, client.Deregister()) | ||
|
||
status, _, _ = consulAgent.AgentHealthServiceByID(serviceName) | ||
assert.Equal(t, testutil.HealthCritical, status) | ||
|
||
srv1.Stop() | ||
|
||
assert.False(t, client.IsReachable()) | ||
|
||
client, err = NewDefaultClient(serviceName, `127.0.0.1`, 8080, "10m") | ||
assert.Error(t, err) | ||
assert.Nil(t, client) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package consul | ||
|
||
// Dummy is struct for implements TTLUpdater interface | ||
type Dummy struct{} | ||
|
||
// NewDummyClient return initialized struct Dummy | ||
func NewDummyClient() *Dummy { | ||
return &Dummy{} | ||
} | ||
|
||
// UpdateTTL implements TTLUpdater interface. | ||
// Return always nil. | ||
func (c *Dummy) UpdateTTL(checkID, status, output string) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package consul | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDummyConsulWrapper(t *testing.T) { | ||
client := NewDummyClient() | ||
assert.Implements(t, (*TTLUpdater)(nil), client) | ||
|
||
assert.NoError(t, client.UpdateTTL("", "", "")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters