Skip to content

Commit

Permalink
add applicable boolean to fingerprint response
Browse files Browse the repository at this point in the history
public fields and remove getter functions
  • Loading branch information
chelseakomlo committed Jan 31, 2018
1 parent e8aaa93 commit a9447ad
Show file tree
Hide file tree
Showing 45 changed files with 329 additions and 206 deletions.
25 changes: 15 additions & 10 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,9 +951,6 @@ func (c *Client) fingerprint() error {
return err
}

// Apply the finerprint to our list so that we can log it later
appliedFingerprints = append(appliedFingerprints, name)

request := &cstructs.FingerprintRequest{Config: c.config, Node: c.config.Node}
var response cstructs.FingerprintResponse
c.configLock.Lock()
Expand All @@ -963,6 +960,11 @@ func (c *Client) fingerprint() error {
return err
}

// log the fingerprinters which have been applied
if response.Applicable {
appliedFingerprints = append(appliedFingerprints, name)
}

// add the diff found from each fingerprinter
c.updateNodeFromFingerprint(&response)

Expand Down Expand Up @@ -1029,10 +1031,6 @@ func (c *Client) setupDrivers() error {
continue
}

// Add this driver to our list of available drivers so that we can log it
// later
availDrivers = append(availDrivers, name)

d, err := driver.NewDriver(name, driverCtx)
if err != nil {
return err
Expand All @@ -1046,6 +1044,11 @@ func (c *Client) setupDrivers() error {
}
c.configLock.Unlock()

// log the fingerprinters which have been applied
if response.Applicable {
availDrivers = append(availDrivers, name)
}

c.updateNodeFromFingerprint(&response)

p, period := d.Periodic()
Expand All @@ -1068,7 +1071,7 @@ func (c *Client) setupDrivers() error {
func (c *Client) updateNodeFromFingerprint(response *cstructs.FingerprintResponse) {
c.configLock.Lock()
defer c.configLock.Unlock()
for name, val := range response.GetAttributes() {
for name, val := range response.Attributes {
if val == "" {
delete(c.config.Node.Attributes, name)
} else {
Expand All @@ -1078,15 +1081,17 @@ func (c *Client) updateNodeFromFingerprint(response *cstructs.FingerprintRespons

// update node links and resources from the diff created from
// fingerprinting
for name, val := range response.GetLinks() {
for name, val := range response.Links {
if val == "" {
delete(c.config.Node.Links, name)
} else {
c.config.Node.Links[name] = val
}
}

c.config.Node.Resources.Merge(response.GetResources())
if response.Resources != nil {
c.config.Node.Resources.Merge(response.Resources)
}
}

// retryIntv calculates a retry interval value given the base
Expand Down
1 change: 1 addition & 0 deletions client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ func (d *DockerDriver) Fingerprint(req *cstructs.FingerprintRequest, resp *cstru

resp.AddAttribute(dockerDriverAttr, "1")
resp.AddAttribute("driver.docker.version", env.Get("Version"))
resp.Applicable = true

privileged := d.config.ReadBoolDefault(dockerPrivilegedConfigOption, false)
if privileged {
Expand Down
19 changes: 17 additions & 2 deletions client/driver/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,20 @@ func TestDockerDriver_Fingerprint(t *testing.T) {
t.Fatalf("err: %v", err)
}

attributes := response.GetAttributes()
attributes := response.Attributes
if testutil.DockerIsConnected(t) && attributes["driver.docker"] == "" {
t.Fatalf("Fingerprinter should detect when docker is available")
}

if attributes["driver.docker"] != "1" {
t.Log("Docker daemon not available. The remainder of the docker tests will be skipped.")
} else {

// if docker is available, make sure that the response is tagged as
// applicable
if !response.Applicable {
t.Fatalf("expected response to be applicable")
}
}

t.Logf("Found docker version %s", attributes["driver.docker.version"])
Expand Down Expand Up @@ -225,7 +232,15 @@ func TestDockerDriver_Fingerprint_Bridge(t *testing.T) {
t.Fatalf("error fingerprinting docker: %v", err)
}

attributes := response.GetAttributes()
if !response.Applicable {
t.Fatalf("expected response to be applicable")
}

attributes := response.Attributes
if attributes == nil {
t.Fatalf("expected attributes to be set")
}

if attributes["driver.docker"] == "" {
t.Fatalf("expected Docker to be enabled but false was returned")
}
Expand Down
1 change: 1 addition & 0 deletions client/driver/exec_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ import (

func (d *ExecDriver) Fingerprint(req *cstructs.FingerprintRequest, resp *cstructs.FingerprintResponse) error {
d.fingerprintSuccess = helper.BoolToPtr(false)
resp.Applicable = true
return nil
}
1 change: 1 addition & 0 deletions client/driver/exec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ func (d *ExecDriver) Fingerprint(req *cstructs.FingerprintRequest, resp *cstruct
}
resp.AddAttribute(execDriverAttr, "1")
d.fingerprintSuccess = helper.BoolToPtr(true)
resp.Applicable = true
return nil
}
7 changes: 6 additions & 1 deletion client/driver/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ func TestExecDriver_Fingerprint(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
if response.GetAttributes()["driver.exec"] == "" {

if !response.Applicable {
t.Fatalf("expected response to be applicable")
}

if response.Attributes == nil || response.Attributes["driver.exec"] == "" {
t.Fatalf("missing driver")
}
}
Expand Down
1 change: 1 addition & 0 deletions client/driver/java.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func (d *JavaDriver) Fingerprint(req *cstructs.FingerprintRequest, resp *cstruct
resp.AddAttribute("driver.java.runtime", info[1])
resp.AddAttribute("driver.java.vm", info[2])
d.fingerprintSuccess = helper.BoolToPtr(true)
resp.Applicable = true

return nil
}
Expand Down
9 changes: 6 additions & 3 deletions client/driver/java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,19 @@ func TestJavaDriver_Fingerprint(t *testing.T) {
t.Fatalf("err: %v", err)
}

attributes := response.GetAttributes()
if attributes["driver.java"] != "1" && javaLocated() {
if !response.Applicable {
t.Fatalf("expected response to be applicable")
}

if response.Attributes["driver.java"] != "1" && javaLocated() {
if v, ok := osJavaDriverSupport[runtime.GOOS]; v && ok {
t.Fatalf("missing java driver")
} else {
t.Skipf("missing java driver, no OS support")
}
}
for _, key := range []string{"driver.java.version", "driver.java.runtime", "driver.java.vm"} {
if attributes[key] == "" {
if response.Attributes[key] == "" {
t.Fatalf("missing driver key (%s)", key)
}
}
Expand Down
1 change: 1 addition & 0 deletions client/driver/lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func (d *LxcDriver) Fingerprint(req *cstructs.FingerprintRequest, resp *cstructs
}
resp.AddAttribute("driver.lxc.version", version)
resp.AddAttribute("driver.lxc", "1")
resp.Applicable = true

// Advertise if this node supports lxc volumes
if d.config.ReadBoolDefault(lxcVolumesConfigOption, lxcVolumesConfigDefault) {
Expand Down
7 changes: 6 additions & 1 deletion client/driver/lxc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ func TestLxcDriver_Fingerprint(t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
if response.GetAttributes()["driver.lxc"] == "" {

if !response.Applicable {
t.Fatalf("expected response to be applicable")
}

if response.Attributes["driver.lxc"] == "" {
t.Fatalf("missing driver")
}
}
Expand Down
1 change: 1 addition & 0 deletions client/driver/mock_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func (m *MockDriver) Fingerprint(req *cstructs.FingerprintRequest, resp *cstruct
resp.RemoveAttribute("driver.mock_driver")
default:
resp.AddAttribute("driver.mock_driver", "1")
resp.Applicable = true
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions client/driver/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func (d *QemuDriver) Fingerprint(req *cstructs.FingerprintRequest, resp *cstruct

resp.AddAttribute(qemuDriverAttr, "1")
resp.AddAttribute(qemuDriverVersionAttr, currentQemuVersion)
resp.Applicable = true

return nil
}
Expand Down
11 changes: 9 additions & 2 deletions client/driver/qemu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ func TestQemuDriver_Fingerprint(t *testing.T) {
t.Fatalf("err: %v", err)
}

attributes := response.GetAttributes()
if !response.Applicable {
t.Fatalf("expected response to be applicable")
}

attributes := response.Attributes
if attributes == nil {
t.Fatalf("attributes should not be nil")
}

if attributes[qemuDriverAttr] == "" {
t.Fatalf("Missing Qemu driver")
Expand Down Expand Up @@ -176,7 +183,7 @@ func TestQemuDriver_GracefulShutdown(t *testing.T) {
t.Fatalf("err: %v", err)
}

for name, value := range response.GetAttributes() {
for name, value := range response.Attributes {
ctx.DriverCtx.node.Attributes[name] = value
}

Expand Down
1 change: 1 addition & 0 deletions client/driver/raw_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (d *RawExecDriver) Fingerprint(req *cstructs.FingerprintRequest, resp *cstr
if enabled || req.Config.DevMode {
d.logger.Printf("[WARN] driver.raw_exec: raw exec is enabled. Only enable if needed")
resp.AddAttribute(rawExecDriverAttr, "1")
resp.Applicable = true
return nil
}

Expand Down
8 changes: 6 additions & 2 deletions client/driver/raw_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestRawExecDriver_Fingerprint(t *testing.T) {
t.Fatalf("err: %v", err)
}

if response.GetAttributes()["driver.raw_exec"] != "" {
if response.Attributes["driver.raw_exec"] != "" {
t.Fatalf("driver incorrectly enabled")
}

Expand All @@ -53,7 +53,11 @@ func TestRawExecDriver_Fingerprint(t *testing.T) {
t.Fatalf("err: %v", err)
}

if response.GetAttributes()["driver.raw_exec"] != "1" {
if !response.Applicable {
t.Fatalf("expected response to be applicable")
}

if response.Attributes["driver.raw_exec"] != "1" {
t.Fatalf("driver not enabled")
}
}
Expand Down
1 change: 1 addition & 0 deletions client/driver/rkt.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ func (d *RktDriver) Fingerprint(req *cstructs.FingerprintRequest, resp *cstructs
resp.AddAttribute(rktDriverAttr, "1")
resp.AddAttribute("driver.rkt.version", rktMatches[1])
resp.AddAttribute("driver.rkt.appc.version", appcMatches[1])
resp.Applicable = true

// Advertise if this node supports rkt volumes
if d.config.ReadBoolDefault(rktVolumesConfigOption, rktVolumesConfigDefault) {
Expand Down
9 changes: 8 additions & 1 deletion client/driver/rkt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ func TestRktDriver_Fingerprint(t *testing.T) {
t.Fatalf("err: %v", err)
}

attributes := response.GetAttributes()
if !response.Applicable {
t.Fatalf("expected response to be applicable")
}

attributes := response.Attributes
if attributes == nil {
t.Fatalf("expected attributes to not equal nil")
}
if attributes["driver.rkt"] != "1" {
t.Fatalf("Missing Rkt driver")
}
Expand Down
1 change: 1 addition & 0 deletions client/fingerprint/arch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ func NewArchFingerprint(logger *log.Logger) Fingerprint {

func (f *ArchFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp *cstructs.FingerprintResponse) error {
resp.AddAttribute("cpu.arch", runtime.GOARCH)
resp.Applicable = true
return nil
}
6 changes: 5 additions & 1 deletion client/fingerprint/arch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ func TestArchFingerprint(t *testing.T) {
t.Fatalf("err: %v", err)
}

assertNodeAttributeContains(t, response.GetAttributes(), "cpu.arch")
if !response.Applicable {
t.Fatalf("expected response to be applicable")
}

assertNodeAttributeContains(t, response.Attributes, "cpu.arch")
}
1 change: 1 addition & 0 deletions client/fingerprint/cgroup_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (f *CGroupFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp *
}

resp.AddAttribute("unique.cgroup.mountpoint", mount)
resp.Applicable = true

if f.lastState == cgroupUnavailable {
f.logger.Printf("[INFO] fingerprint.cgroups: cgroups are available")
Expand Down
8 changes: 4 additions & 4 deletions client/fingerprint/cgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestCGroupFingerprint(t *testing.T) {
t.Fatalf("expected an error")
}

if a, _ := response.GetAttributes()["unique.cgroup.mountpoint"]; a != "" {
if a, _ := response.Attributes["unique.cgroup.mountpoint"]; a != "" {
t.Fatalf("unexpected attribute found, %s", a)
}
}
Expand All @@ -80,7 +80,7 @@ func TestCGroupFingerprint(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error, %s", err)
}
if a, ok := response.GetAttributes()["unique.cgroup.mountpoint"]; !ok {
if a, ok := response.Attributes["unique.cgroup.mountpoint"]; !ok {
t.Fatalf("unable to find attribute: %s", a)
}
}
Expand All @@ -102,7 +102,7 @@ func TestCGroupFingerprint(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error, %s", err)
}
if a, _ := response.GetAttributes()["unique.cgroup.mountpoint"]; a != "" {
if a, _ := response.Attributes["unique.cgroup.mountpoint"]; a != "" {
t.Fatalf("unexpected attribute found, %s", a)
}
}
Expand All @@ -123,7 +123,7 @@ func TestCGroupFingerprint(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error, %s", err)
}
if a, _ := response.GetAttributes()["unique.cgroup.mountpoint"]; a == "" {
if a, _ := response.Attributes["unique.cgroup.mountpoint"]; a == "" {
t.Fatalf("expected attribute to be found, %s", a)
}
}
Expand Down
10 changes: 5 additions & 5 deletions client/fingerprint/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ func (f *ConsulFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp *
f.logger.Printf("[WARN] fingerprint.consul: unable to fingerprint consul.datacenter")
}

attributes := resp.GetAttributes()
if attributes["consul.datacenter"] != "" || attributes["unique.consul.name"] != "" {
resp.AddLink("consul", fmt.Sprintf("%s.%s",
attributes["consul.datacenter"],
attributes["unique.consul.name"]))
if dc, ok := resp.Attributes["consul.datacenter"]; ok {
if name, ok2 := resp.Attributes["unique.consul.name"]; ok2 {
resp.AddLink("consul", fmt.Sprintf("%s.%s", dc, name))
}
} else {
f.logger.Printf("[WARN] fingerprint.consul: malformed Consul response prevented linking")
}
Expand All @@ -99,6 +98,7 @@ func (f *ConsulFingerprint) Fingerprint(req *cstructs.FingerprintRequest, resp *
f.logger.Printf("[INFO] fingerprint.consul: consul agent is available")
}
f.lastState = consulAvailable
resp.Applicable = true
return nil
}

Expand Down
Loading

0 comments on commit a9447ad

Please sign in to comment.