Skip to content

Commit

Permalink
add feature and poller to handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Phillips committed Mar 28, 2017
1 parent 00603c9 commit 70a24b6
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 41 deletions.
19 changes: 12 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ const (
DefaultAgentId = "-poller-"
)

type Feature struct {
Name string `json:"name"`
Disabled bool `json:"disabled"`
}

type Config struct {
// Addresses
UseSrv bool
Expand All @@ -57,7 +62,7 @@ type Config struct {
// Agent Info
AgentId string
AgentName string
Features []map[string]string
Features []map[string]Feature
Guid string
BundleVersion string
ProcessVersion string
Expand All @@ -84,9 +89,13 @@ type configEntry struct {
Sensitive bool
}

func NewConfig(guid string, useStaging bool) *Config {
func NewConfig(guid string, useStaging bool, features []map[string]Feature) *Config {
cfg := &Config{}
cfg.init()
if features != nil {
cfg.Features = features
} else {
cfg.Features = make([]map[string]Feature, 0)
}
cfg.Guid = guid
cfg.Token = os.Getenv("AGENT_TOKEN")
cfg.AgentId = os.Getenv("AGENT_ID")
Expand All @@ -111,10 +120,6 @@ func NewConfig(guid string, useStaging bool) *Config {
return cfg
}

func (cfg *Config) init() {
cfg.Features = make([]map[string]string, 0)
}

// LoadFromFile populates this Config with the values defined in that file and then calls PostProcess.
func (cfg *Config) LoadFromFile(filepath string) error {
f, err := os.Open(filepath)
Expand Down
38 changes: 19 additions & 19 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type configFields struct {
Addresses []string
AgentId string
AgentName string
Features []map[string]string
Features []map[string]config.Feature
Guid string
BundleVersion string
ProcessVersion string
Expand All @@ -37,7 +37,7 @@ func getConfigFields() configFields {
TimeoutWrite: time.Duration(10 * time.Second),
TimeoutPrepareEnd: config.DefaultTimeoutPrepareEnd,
Token: "",
Features: make([]map[string]string, 0),
Features: make([]map[string]config.Feature, 0),
}
}

Expand Down Expand Up @@ -69,7 +69,7 @@ func TestNewConfig(t *testing.T) {
TimeoutPrepareEnd: config.DefaultTimeoutPrepareEnd,
TimeoutAuth: config.DefaultTimeoutAuth,
Token: "",
Features: make([]map[string]string, 0),
Features: make([]map[string]config.Feature, 0),
},
},
{
Expand All @@ -94,13 +94,13 @@ func TestNewConfig(t *testing.T) {
TimeoutPrepareEnd: config.DefaultTimeoutPrepareEnd,
TimeoutAuth: config.DefaultTimeoutAuth,
Token: "",
Features: make([]map[string]string, 0),
Features: make([]map[string]config.Feature, 0),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := config.NewConfig(tt.guid, tt.useStaging)
got := config.NewConfig(tt.guid, tt.useStaging, nil)
assert.Equal(t, tt.expected, got)
})
}
Expand All @@ -121,13 +121,13 @@ func TestConfig_LoadFromFile(t *testing.T) {
{
name: "No comments config file",
filepath: "testdata/no-comments-config-file.txt",
expected: config.NewConfig("1-2-3", false),
expected: config.NewConfig("1-2-3", false, nil),
expectedErr: false,
},
{
name: "With comments in config file",
filepath: "testdata/with-comments-config-file.txt",
expected: config.NewConfig("1-2-3", false),
expected: config.NewConfig("1-2-3", false, nil),
expectedErr: false,
},
{
Expand All @@ -144,7 +144,7 @@ func TestConfig_LoadFromFile(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

cfg := config.NewConfig("1-2-3", false)
cfg := config.NewConfig("1-2-3", false, nil)
err := cfg.LoadFromFile(tt.filepath)
if tt.expectedErr {
assert.Error(t, err)
Expand All @@ -158,7 +158,7 @@ func TestConfig_LoadFromFile(t *testing.T) {
}

func expectedConfigWithSrvQueries(guid string, useStaging bool, snetRegion string, srvQueries []string) *config.Config {
cfg := config.NewConfig(guid, useStaging)
cfg := config.NewConfig(guid, useStaging, nil)
cfg.SnetRegion = snetRegion
cfg.SrvQueries = srvQueries
return cfg
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestConfig_ParseFields(t *testing.T) {
TimeoutRead: time.Duration(10 * time.Second),
TimeoutWrite: time.Duration(10 * time.Second),
Token: "",
Features: make([]map[string]string, 0),
Features: make([]map[string]config.Feature, 0),
},
expectedErr: false,
},
Expand All @@ -209,7 +209,7 @@ func TestConfig_ParseFields(t *testing.T) {
TimeoutRead: time.Duration(10 * time.Second),
TimeoutWrite: time.Duration(10 * time.Second),
Token: "",
Features: make([]map[string]string, 0),
Features: make([]map[string]config.Feature, 0),
},
expectedErr: true,
},
Expand All @@ -230,7 +230,7 @@ func TestConfig_ParseFields(t *testing.T) {
TimeoutRead: time.Duration(10 * time.Second),
TimeoutWrite: time.Duration(10 * time.Second),
Token: "myawesometoken",
Features: make([]map[string]string, 0),
Features: make([]map[string]config.Feature, 0),
},
expectedErr: false,
},
Expand All @@ -250,7 +250,7 @@ func TestConfig_ParseFields(t *testing.T) {
TimeoutRead: time.Duration(10 * time.Second),
TimeoutWrite: time.Duration(10 * time.Second),
Token: "",
Features: make([]map[string]string, 0),
Features: make([]map[string]config.Feature, 0),
},
expectedErr: true,
},
Expand All @@ -275,7 +275,7 @@ func TestConfig_ParseFields(t *testing.T) {
"127.0.0.1",
"0.0.0.0",
},
Features: make([]map[string]string, 0),
Features: make([]map[string]config.Feature, 0),
},
expectedErr: false,
},
Expand All @@ -295,7 +295,7 @@ func TestConfig_ParseFields(t *testing.T) {
TimeoutRead: time.Duration(10 * time.Second),
TimeoutWrite: time.Duration(10 * time.Second),
Token: "",
Features: make([]map[string]string, 0),
Features: make([]map[string]config.Feature, 0),
},
expectedErr: true,
},
Expand All @@ -316,7 +316,7 @@ func TestConfig_ParseFields(t *testing.T) {
TimeoutRead: time.Duration(10 * time.Second),
TimeoutWrite: time.Duration(10 * time.Second),
Token: "",
Features: make([]map[string]string, 0),
Features: make([]map[string]config.Feature, 0),
},
expectedErr: false,
},
Expand All @@ -336,7 +336,7 @@ func TestConfig_ParseFields(t *testing.T) {
TimeoutRead: time.Duration(10 * time.Second),
TimeoutWrite: time.Duration(10 * time.Second),
Token: "",
Features: make([]map[string]string, 0),
Features: make([]map[string]config.Feature, 0),
SnetRegion: "iad",
},
expectedErr: false,
Expand All @@ -357,7 +357,7 @@ func TestConfig_ParseFields(t *testing.T) {
TimeoutRead: time.Duration(10 * time.Second),
TimeoutWrite: time.Duration(10 * time.Second),
Token: "",
Features: make([]map[string]string, 0),
Features: make([]map[string]config.Feature, 0),
SnetRegion: "",
},
expectedErr: true,
Expand Down Expand Up @@ -416,7 +416,7 @@ func TestConfig_SetPrivateZones(t *testing.T) {
TimeoutRead: time.Duration(10 * time.Second),
TimeoutWrite: time.Duration(10 * time.Second),
Token: "",
Features: make([]map[string]string, 0),
Features: make([]map[string]config.Feature, 0),
ZoneIds: []string{
"zone1",
"zone2",
Expand Down
3 changes: 2 additions & 1 deletion endpoint/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
set "github.com/deckarep/golang-set"
"github.com/fsnotify/fsnotify"
"github.com/racker/rackspace-monitoring-poller/check"
"github.com/racker/rackspace-monitoring-poller/config"
"github.com/racker/rackspace-monitoring-poller/protocol"
protocheck "github.com/racker/rackspace-monitoring-poller/protocol/check"
"github.com/racker/rackspace-monitoring-poller/utils"
Expand Down Expand Up @@ -62,7 +63,7 @@ type agent struct {
name string
processVersion string
bundleVersion string
features []map[string]string
features []map[string]config.Feature
zones []string
prepareBlockSize int

Expand Down
4 changes: 2 additions & 2 deletions poller/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ func TestConnection_Connect(t *testing.T) {

conn := poller.NewConnection(tt.url(), tt.guid, reconciler)
if tt.expectedErr {
err := conn.Connect(tt.ctx, config.NewConfig("1-2-3", false), nil)
err := conn.Connect(tt.ctx, config.NewConfig("1-2-3", false, nil), nil)
assert.Error(t, err)
} else {
assert.NoError(t, conn.Connect(tt.ctx, config.NewConfig("1-2-3", false), &tls.Config{
assert.NoError(t, conn.Connect(tt.ctx, config.NewConfig("1-2-3", false, nil), &tls.Config{
InsecureSkipVerify: true,
ServerName: tt.url(),
RootCAs: nil,
Expand Down
5 changes: 4 additions & 1 deletion poller/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ import (
func Run(configFilePath string, insecure bool) {
guid := uuid.NewV4()
useStaging := config.IsUsingStaging()
features := []map[string]config.Feature{
{"poller": config.Feature{Name: "poller", Disabled: false}},
}

cfg := config.NewConfig(guid.String(), useStaging)
cfg := config.NewConfig(guid.String(), useStaging, features)
if err := cfg.LoadFromFile(configFilePath); err != nil {
utils.Die(err, "Failed to load configuration")
}
Expand Down
8 changes: 4 additions & 4 deletions poller/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func TestEleSession_HandshakeError(t *testing.T) {
defer readsHere.Close()

eventConsumer := newPhasingEventConsumer()
cfg := config.NewConfig("1-2-3", false)
cfg := config.NewConfig("1-2-3", false, nil)
es := poller.NewSession(context.Background(), eleConn, reconciler, cfg)
es.RegisterEventConsumer(eventConsumer)
defer es.Close()
Expand All @@ -246,7 +246,7 @@ func TestEleSession_HandshakeTimeout(t *testing.T) {
eleConn.EXPECT().Close()

eventConsumer := newPhasingEventConsumer()
cfg := config.NewConfig("1-2-3", false)
cfg := config.NewConfig("1-2-3", false, nil)
cfg.TimeoutAuth = 10 * time.Millisecond
es := poller.NewSession(context.Background(), eleConn, reconciler, cfg)
es.RegisterEventConsumer(eventConsumer)
Expand All @@ -266,7 +266,7 @@ func TestEleSession_HandshakeTimeoutStoppedOnSuccess(t *testing.T) {
defer utils.InstallAlternateTimestampFunc(origTimestamper)

eventConsumer := newPhasingEventConsumer()
cfg := config.NewConfig("1-2-3", false)
cfg := config.NewConfig("1-2-3", false, nil)
cfg.TimeoutAuth = 10 * time.Millisecond
es := poller.NewSession(context.Background(), eleConn, reconciler, cfg)
es.RegisterEventConsumer(eventConsumer)
Expand Down Expand Up @@ -566,7 +566,7 @@ func TestEleSession_PollerPrepare(t *testing.T) {
origTimestamper := installDeterministicTimestamper(1000, 2000)
defer utils.InstallAlternateTimestampFunc(origTimestamper)

cfg := config.NewConfig("1-2-3", false)
cfg := config.NewConfig("1-2-3", false, nil)
es := poller.NewSession(context.Background(), eleConn, reconciler, cfg)
defer es.Close()

Expand Down
14 changes: 7 additions & 7 deletions protocol/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ import (
// Handshake

type HandshakeParameters struct {
Token string `json:"token"`
AgentId string `json:"agent_id"`
AgentName string `json:"agent_name"`
ProcessVersion string `json:"process_version"`
BundleVersion string `json:"bundle_version"`
ZoneIds []string `json:"zone_ids"`
Features []map[string]string `json:"features"`
Token string `json:"token"`
AgentId string `json:"agent_id"`
AgentName string `json:"agent_name"`
ProcessVersion string `json:"process_version"`
BundleVersion string `json:"bundle_version"`
ZoneIds []string `json:"zone_ids"`
Features []map[string]config.Feature `json:"features"`
}

type HandshakeRequest struct {
Expand Down

0 comments on commit 70a24b6

Please sign in to comment.