2016-09-01 18:02:19 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2022-09-26 19:14:19 +00:00
|
|
|
"time"
|
2016-09-01 18:02:19 +00:00
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2016-09-01 18:02:19 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2018-06-13 22:33:25 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2016-09-01 18:02:19 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestVaultFingerprint(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2017-07-23 23:21:25 +00:00
|
|
|
tv := testutil.NewTestVault(t)
|
2016-09-01 18:02:19 +00:00
|
|
|
defer tv.Stop()
|
|
|
|
|
2018-09-16 00:48:59 +00:00
|
|
|
fp := NewVaultFingerprint(testlog.HCLogger(t))
|
2016-09-01 18:02:19 +00:00
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
2022-09-26 19:14:19 +00:00
|
|
|
p, period := fp.Periodic()
|
|
|
|
if !p {
|
|
|
|
t.Fatalf("expected fingerprint to be periodic")
|
|
|
|
}
|
|
|
|
if period != (15 * time.Second) {
|
|
|
|
t.Fatalf("expected period to be 15s but found: %s", period)
|
|
|
|
}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
conf := config.DefaultConfig()
|
|
|
|
conf.VaultConfig = tv.Config
|
2016-09-01 18:02:19 +00:00
|
|
|
|
2018-12-01 16:10:39 +00:00
|
|
|
request := &FingerprintRequest{Config: conf, Node: node}
|
|
|
|
var response FingerprintResponse
|
2018-01-26 16:21:07 +00:00
|
|
|
err := fp.Fingerprint(request, &response)
|
2016-09-01 18:02:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to fingerprint: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-01-31 22:03:55 +00:00
|
|
|
if !response.Detected {
|
2018-01-30 17:57:37 +00:00
|
|
|
t.Fatalf("expected response to be applicable")
|
|
|
|
}
|
|
|
|
|
|
|
|
assertNodeAttributeContains(t, response.Attributes, "vault.accessible")
|
|
|
|
assertNodeAttributeContains(t, response.Attributes, "vault.version")
|
|
|
|
assertNodeAttributeContains(t, response.Attributes, "vault.cluster_id")
|
|
|
|
assertNodeAttributeContains(t, response.Attributes, "vault.cluster_name")
|
2022-09-23 18:45:12 +00:00
|
|
|
|
2022-09-26 19:14:19 +00:00
|
|
|
// Period should be longer after initial discovery
|
|
|
|
p, period = fp.Periodic()
|
|
|
|
if !p {
|
|
|
|
t.Fatalf("expected fingerprint to be periodic")
|
|
|
|
}
|
|
|
|
if period < (30*time.Second) || period > (2*time.Minute) {
|
|
|
|
t.Fatalf("expected period to be between 30s and 2m but found: %s", period)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop Vault to simulate it being unavailable
|
2022-09-23 18:45:12 +00:00
|
|
|
tv.Stop()
|
|
|
|
|
|
|
|
err = fp.Fingerprint(request, &response)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to fingerprint: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !response.Detected {
|
|
|
|
t.Fatalf("should still show as detected")
|
|
|
|
}
|
|
|
|
|
|
|
|
assertNodeAttributeContains(t, response.Attributes, "vault.accessible")
|
|
|
|
assertNodeAttributeContains(t, response.Attributes, "vault.version")
|
|
|
|
assertNodeAttributeContains(t, response.Attributes, "vault.cluster_id")
|
|
|
|
assertNodeAttributeContains(t, response.Attributes, "vault.cluster_name")
|
|
|
|
|
2022-09-26 19:14:19 +00:00
|
|
|
// Period should be original once trying to discover Vault is available again
|
|
|
|
p, period = fp.Periodic()
|
|
|
|
if !p {
|
|
|
|
t.Fatalf("expected fingerprint to be periodic")
|
|
|
|
}
|
|
|
|
if period != (15 * time.Second) {
|
|
|
|
t.Fatalf("expected period to be 15s but found: %s", period)
|
|
|
|
}
|
2016-09-01 18:02:19 +00:00
|
|
|
}
|