github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/fingerprint/vault_test.go (about)

     1  package fingerprint
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/hashicorp/nomad/ci"
     8  	"github.com/hashicorp/nomad/client/config"
     9  	"github.com/hashicorp/nomad/helper/testlog"
    10  	"github.com/hashicorp/nomad/nomad/structs"
    11  	"github.com/hashicorp/nomad/testutil"
    12  )
    13  
    14  func TestVaultFingerprint(t *testing.T) {
    15  	ci.Parallel(t)
    16  
    17  	tv := testutil.NewTestVault(t)
    18  	defer tv.Stop()
    19  
    20  	fp := NewVaultFingerprint(testlog.HCLogger(t))
    21  	node := &structs.Node{
    22  		Attributes: make(map[string]string),
    23  	}
    24  
    25  	p, period := fp.Periodic()
    26  	if !p {
    27  		t.Fatalf("expected fingerprint to be periodic")
    28  	}
    29  	if period != (15 * time.Second) {
    30  		t.Fatalf("expected period to be 15s but found: %s", period)
    31  	}
    32  
    33  	conf := config.DefaultConfig()
    34  	conf.VaultConfig = tv.Config
    35  
    36  	request := &FingerprintRequest{Config: conf, Node: node}
    37  	var response FingerprintResponse
    38  	err := fp.Fingerprint(request, &response)
    39  	if err != nil {
    40  		t.Fatalf("Failed to fingerprint: %s", err)
    41  	}
    42  
    43  	if !response.Detected {
    44  		t.Fatalf("expected response to be applicable")
    45  	}
    46  
    47  	assertNodeAttributeContains(t, response.Attributes, "vault.accessible")
    48  	assertNodeAttributeContains(t, response.Attributes, "vault.version")
    49  	assertNodeAttributeContains(t, response.Attributes, "vault.cluster_id")
    50  	assertNodeAttributeContains(t, response.Attributes, "vault.cluster_name")
    51  
    52  	// Period should be longer after initial discovery
    53  	p, period = fp.Periodic()
    54  	if !p {
    55  		t.Fatalf("expected fingerprint to be periodic")
    56  	}
    57  	if period < (30*time.Second) || period > (2*time.Minute) {
    58  		t.Fatalf("expected period to be between 30s and 2m but found: %s", period)
    59  	}
    60  
    61  	// Stop Vault to simulate it being unavailable
    62  	tv.Stop()
    63  
    64  	err = fp.Fingerprint(request, &response)
    65  	if err != nil {
    66  		t.Fatalf("Failed to fingerprint: %s", err)
    67  	}
    68  
    69  	if !response.Detected {
    70  		t.Fatalf("should still show as detected")
    71  	}
    72  
    73  	assertNodeAttributeContains(t, response.Attributes, "vault.accessible")
    74  	assertNodeAttributeContains(t, response.Attributes, "vault.version")
    75  	assertNodeAttributeContains(t, response.Attributes, "vault.cluster_id")
    76  	assertNodeAttributeContains(t, response.Attributes, "vault.cluster_name")
    77  
    78  	// Period should be original once trying to discover Vault is available again
    79  	p, period = fp.Periodic()
    80  	if !p {
    81  		t.Fatalf("expected fingerprint to be periodic")
    82  	}
    83  	if period != (15 * time.Second) {
    84  		t.Fatalf("expected period to be 15s but found: %s", period)
    85  	}
    86  }