github.com/hernad/nomad@v1.6.112/nomad/structs/variables_test.go (about)

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