github.com/hashicorp/nomad/api@v0.0.0-20240306165712-3193ac204f65/node_meta_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package api
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/hashicorp/nomad/api/internal/testutil"
    11  	"github.com/shoenig/test/must"
    12  )
    13  
    14  func TestNodeMeta_Apply(t *testing.T) {
    15  	testutil.Parallel(t)
    16  
    17  	cb := func(c *testutil.TestServerConfig) {
    18  		c.DevMode = true
    19  	}
    20  	c, s := makeClient(t, nil, cb)
    21  	defer s.Stop()
    22  
    23  	nodeID := oneNodeFromNodeList(t, c.Nodes()).ID
    24  
    25  	node, _, err := c.Nodes().Info(nodeID, nil)
    26  	must.NoError(t, err)
    27  	must.Greater(t, 1, len(node.Meta),
    28  		must.Sprintf("expected more Node.Meta by default: %#v", node.Meta))
    29  
    30  	metaResp, err := c.Nodes().Meta().Read(node.ID, nil)
    31  	must.NoError(t, err)
    32  	must.MapEq(t, node.Meta, metaResp.Meta)
    33  	must.MapEq(t, node.Meta, metaResp.Static)
    34  	must.MapEmpty(t, metaResp.Dynamic)
    35  
    36  	staticKey := ""
    37  	for staticKey = range node.Meta {
    38  		break
    39  	}
    40  
    41  	req := &NodeMetaApplyRequest{
    42  		NodeID: node.ID,
    43  		Meta: map[string]*string{
    44  			staticKey: nil,
    45  			"foo":     pointerOf("bar"),
    46  		},
    47  	}
    48  
    49  	metaResp, err = c.Nodes().Meta().Apply(req, nil)
    50  	must.NoError(t, err)
    51  	must.MapEq(t, req.Meta, metaResp.Dynamic)
    52  	must.MapEq(t, node.Meta, metaResp.Static)
    53  	must.MapNotContainsKey(t, metaResp.Meta, staticKey)
    54  	must.Eq(t, "bar", metaResp.Meta["foo"])
    55  
    56  	// Wait up to 10s (plus buffer) for node to re-register
    57  	deadline := time.Now().Add(11 * time.Second)
    58  	found := false
    59  	for !found && time.Now().Before(deadline) {
    60  		node, _, err = c.Nodes().Info(node.ID, nil)
    61  		must.NoError(t, err)
    62  		found = node.Meta["foo"] == "bar"
    63  		time.Sleep(100 * time.Millisecond)
    64  	}
    65  	must.True(t, found)
    66  }