github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/nomad/structs/variables_test.go (about)

     1  package structs
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/hashicorp/nomad/ci"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestStructs_VariableDecrypted_Copy(t *testing.T) {
    12  	ci.Parallel(t)
    13  	n := time.Now()
    14  	a := VariableMetadata{
    15  		Namespace:   "a",
    16  		Path:        "a/b/c",
    17  		CreateIndex: 1,
    18  		CreateTime:  n.UnixNano(),
    19  		ModifyIndex: 2,
    20  		ModifyTime:  n.Add(48 * time.Hour).UnixNano(),
    21  	}
    22  	sv := VariableDecrypted{
    23  		VariableMetadata: a,
    24  		Items: VariableItems{
    25  			"foo": "bar",
    26  			"k1":  "v1",
    27  		},
    28  	}
    29  	sv2 := sv.Copy()
    30  	require.True(t, sv.Equal(sv2), "sv and sv2 should be equal")
    31  	sv2.Items["new"] = "new"
    32  	require.False(t, sv.Equal(sv2), "sv and sv2 should not be equal")
    33  }
    34  
    35  func TestStructs_VariableDecrypted_Validate(t *testing.T) {
    36  	ci.Parallel(t)
    37  
    38  	sv := VariableDecrypted{
    39  		VariableMetadata: VariableMetadata{Namespace: "a"},
    40  		Items:            VariableItems{"foo": "bar"},
    41  	}
    42  
    43  	testCases := []struct {
    44  		path string
    45  		ok   bool
    46  	}{
    47  		{path: ""},
    48  		{path: "nomad"},
    49  		{path: "nomad/other"},
    50  		{path: "a/b/c", ok: true},
    51  		{path: "nomad/jobs", ok: true},
    52  		{path: "nomadjobs", ok: true},
    53  		{path: "nomad/jobs/whatever", ok: true},
    54  		{path: "example/_-~/whatever", ok: true},
    55  		{path: "example/@whatever"},
    56  		{path: "example/what.ever"},
    57  	}
    58  	for _, tc := range testCases {
    59  		tc := tc
    60  		sv.Path = tc.path
    61  		err := sv.Validate()
    62  		if tc.ok {
    63  			require.NoError(t, err, "should not get error for: %s", tc.path)
    64  		} else {
    65  			require.Error(t, err, "should get error for: %s", tc.path)
    66  		}
    67  	}
    68  }