2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-06-14 17:28:10 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/ci"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestStructs_VariableDecrypted_Copy(t *testing.T) {
|
2022-06-14 17:28:10 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
n := time.Now()
|
2022-08-26 18:03:56 +00:00
|
|
|
a := VariableMetadata{
|
2022-06-14 17:28:10 +00:00
|
|
|
Namespace: "a",
|
|
|
|
Path: "a/b/c",
|
|
|
|
CreateIndex: 1,
|
2022-06-27 19:51:01 +00:00
|
|
|
CreateTime: n.UnixNano(),
|
2022-06-14 17:28:10 +00:00
|
|
|
ModifyIndex: 2,
|
2022-06-27 19:51:01 +00:00
|
|
|
ModifyTime: n.Add(48 * time.Hour).UnixNano(),
|
2022-06-14 17:28:10 +00:00
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := VariableDecrypted{
|
|
|
|
VariableMetadata: a,
|
|
|
|
Items: VariableItems{
|
2022-06-14 17:28:10 +00:00
|
|
|
"foo": "bar",
|
|
|
|
"k1": "v1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
sv2 := sv.Copy()
|
2022-10-10 14:28:46 +00:00
|
|
|
require.True(t, sv.Equal(sv2), "sv and sv2 should be equal")
|
2022-06-14 17:28:10 +00:00
|
|
|
sv2.Items["new"] = "new"
|
2022-10-10 14:28:46 +00:00
|
|
|
require.False(t, sv.Equal(sv2), "sv and sv2 should not be equal")
|
2022-06-14 17:28:10 +00:00
|
|
|
}
|
2022-07-19 20:17:34 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestStructs_VariableDecrypted_Validate(t *testing.T) {
|
2022-07-19 20:17:34 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := VariableDecrypted{
|
|
|
|
VariableMetadata: VariableMetadata{Namespace: "a"},
|
|
|
|
Items: VariableItems{"foo": "bar"},
|
2022-07-19 20:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
path string
|
|
|
|
ok bool
|
|
|
|
}{
|
|
|
|
{path: ""},
|
|
|
|
{path: "nomad"},
|
|
|
|
{path: "nomad/other"},
|
|
|
|
{path: "a/b/c", ok: true},
|
|
|
|
{path: "nomad/jobs", ok: true},
|
|
|
|
{path: "nomadjobs", ok: true},
|
|
|
|
{path: "nomad/jobs/whatever", ok: true},
|
2022-09-12 20:37:33 +00:00
|
|
|
{path: "example/_-~/whatever", ok: true},
|
|
|
|
{path: "example/@whatever"},
|
|
|
|
{path: "example/what.ever"},
|
2023-02-02 15:37:40 +00:00
|
|
|
{path: "nomad/job-templates"},
|
|
|
|
{path: "nomad/job-templates/whatever", ok: true},
|
2022-07-19 20:17:34 +00:00
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
sv.Path = tc.path
|
|
|
|
err := sv.Validate()
|
|
|
|
if tc.ok {
|
|
|
|
require.NoError(t, err, "should not get error for: %s", tc.path)
|
|
|
|
} else {
|
|
|
|
require.Error(t, err, "should get error for: %s", tc.path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|