2022-07-06 19:29:46 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api/internal/testutil"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestVariables_SimpleCRUD(t *testing.T) {
|
2022-07-06 19:29:46 +00:00
|
|
|
testutil.Parallel(t)
|
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
nsv := c.Variables()
|
|
|
|
sv1 := NewVariable("my/first/variable")
|
2022-07-06 19:29:46 +00:00
|
|
|
sv1.Namespace = "default"
|
|
|
|
sv1.Items["k1"] = "v1"
|
|
|
|
sv1.Items["k2"] = "v2"
|
|
|
|
|
|
|
|
sv2 := sv1.Copy()
|
|
|
|
sv2.Path = "other/variable/b"
|
|
|
|
sv2.Items["k1"] = "otherv1"
|
|
|
|
sv2.Items["k2"] = "otherv2"
|
|
|
|
|
|
|
|
t.Run("1 fail create when no items", func(t *testing.T) {
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
_, _, err := nsv.Create(&Variable{Path: "bad/var"}, nil)
|
2022-07-06 19:29:46 +00:00
|
|
|
require.Error(t, err)
|
2022-08-26 18:03:56 +00:00
|
|
|
require.EqualError(t, err, "Unexpected response code: 400 (variable missing required Items object)")
|
2022-07-06 19:29:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("2 create sv1", func(t *testing.T) {
|
|
|
|
|
2022-08-25 21:38:15 +00:00
|
|
|
get, _, err := nsv.Create(sv1, nil)
|
2022-07-06 19:29:46 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, get)
|
2022-08-25 21:38:15 +00:00
|
|
|
require.NotZero(t, get.CreateIndex)
|
|
|
|
require.NotZero(t, get.CreateTime)
|
|
|
|
require.NotZero(t, get.ModifyIndex)
|
|
|
|
require.NotZero(t, get.ModifyTime)
|
2022-07-06 19:29:46 +00:00
|
|
|
require.Equal(t, sv1.Items, get.Items)
|
|
|
|
*sv1 = *get
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("2 create sv2", func(t *testing.T) {
|
|
|
|
|
2022-08-25 21:38:15 +00:00
|
|
|
var err error
|
|
|
|
sv2, _, err = nsv.Create(sv2, nil)
|
2022-07-06 19:29:46 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2022-08-25 21:38:15 +00:00
|
|
|
// TODO: Need to prevent no-op modifications from happening server-side
|
|
|
|
// t.Run("3 update sv1 no change", func(t *testing.T) {
|
2022-07-06 19:29:46 +00:00
|
|
|
|
2022-08-25 21:38:15 +00:00
|
|
|
// get, _, err := nsv.Update(sv1, nil)
|
|
|
|
// require.NoError(t, err)
|
|
|
|
// require.NotNil(t, get)
|
|
|
|
// require.Equal(t, sv1.ModifyIndex, get.ModifyIndex, "ModifyIndex should not change")
|
|
|
|
// require.Equal(t, sv1.Items, get.Items)
|
|
|
|
// *sv1 = *get
|
|
|
|
// })
|
2022-07-06 19:29:46 +00:00
|
|
|
|
|
|
|
t.Run("4 update sv1", func(t *testing.T) {
|
|
|
|
|
|
|
|
sv1.Items["new-hotness"] = "yeah!"
|
2022-08-25 21:38:15 +00:00
|
|
|
get, _, err := nsv.Update(sv1, nil)
|
|
|
|
require.NoError(t, err)
|
2022-07-06 19:29:46 +00:00
|
|
|
require.NotNil(t, get)
|
|
|
|
require.NotEqual(t, sv1.ModifyIndex, get.ModifyIndex, "ModifyIndex should change")
|
|
|
|
require.Equal(t, sv1.Items, get.Items)
|
|
|
|
*sv1 = *get
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("5 list vars", func(t *testing.T) {
|
|
|
|
|
|
|
|
l, _, err := nsv.List(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, l, 2)
|
2022-08-26 18:03:56 +00:00
|
|
|
require.ElementsMatch(t, []*VariableMetadata{sv1.Metadata(), sv2.Metadata()}, l)
|
2022-07-06 19:29:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("5a list vars opts", func(t *testing.T) {
|
|
|
|
|
|
|
|
// Since there are two vars in the backend, we should
|
|
|
|
// get a NextToken with a page size of 1
|
|
|
|
l, qm, err := nsv.List(&QueryOptions{PerPage: 1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, l, 1)
|
|
|
|
require.Equal(t, sv1.Metadata(), l[0])
|
|
|
|
require.NotNil(t, qm.NextToken)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("5b prefixlist", func(t *testing.T) {
|
|
|
|
|
|
|
|
l, _, err := nsv.PrefixList("my", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, l, 1)
|
|
|
|
require.Equal(t, sv1.Metadata(), l[0])
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("6 delete sv1", func(t *testing.T) {
|
|
|
|
|
|
|
|
_, err := nsv.Delete(sv1.Path, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, _, err = nsv.Read(sv1.Path, nil)
|
|
|
|
require.EqualError(t, err, ErrVariableNotFound)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("7 list vars after delete", func(t *testing.T) {
|
|
|
|
|
|
|
|
l, _, err := nsv.List(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, l)
|
|
|
|
require.Len(t, l, 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestVariables_CRUDWithCAS(t *testing.T) {
|
2022-07-06 19:29:46 +00:00
|
|
|
testutil.Parallel(t)
|
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
nsv := c.Variables()
|
|
|
|
sv1 := &Variable{
|
2022-07-06 19:29:46 +00:00
|
|
|
Path: "cas/variable/a",
|
|
|
|
Items: map[string]string{
|
|
|
|
"key1": "value1",
|
|
|
|
"key2": "value2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create sv1: should pass without issue
|
2022-08-25 21:38:15 +00:00
|
|
|
get, _, err := nsv.Create(sv1, nil)
|
2022-07-06 19:29:46 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, get)
|
2022-08-25 21:38:15 +00:00
|
|
|
require.NotZero(t, get.CreateIndex)
|
|
|
|
require.NotZero(t, get.CreateTime)
|
|
|
|
require.NotZero(t, get.ModifyIndex)
|
|
|
|
require.NotZero(t, get.ModifyTime)
|
2022-07-06 19:29:46 +00:00
|
|
|
require.Equal(t, sv1.Items, get.Items)
|
|
|
|
|
|
|
|
// Update sv1 with CAS:
|
|
|
|
|
|
|
|
// - perform out of band upsert
|
|
|
|
oobUpdate := sv1.Copy()
|
|
|
|
oobUpdate.Items["new-hotness"] = "yeah!"
|
2022-08-25 21:38:15 +00:00
|
|
|
nowVal, _, err := nsv.Update(oobUpdate, nil)
|
2022-07-06 19:29:46 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// - try to do an update with sv1's old state; should fail
|
2022-08-25 21:38:15 +00:00
|
|
|
_, _, err = nsv.CheckedUpdate(sv1, nil)
|
2022-07-06 19:29:46 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// - expect the error to be an ErrCASConflict, so we can cast
|
|
|
|
// to it and retrieve the Conflict value
|
|
|
|
var conflictErr ErrCASConflict
|
|
|
|
require.ErrorAs(t, err, &conflictErr)
|
|
|
|
require.Equal(t, nowVal, conflictErr.Conflict)
|
|
|
|
|
|
|
|
// Delete CAS: try to delete sv1 at old ModifyIndex; should
|
|
|
|
// return an ErrCASConflict. Check Conflict.
|
|
|
|
_, err = nsv.CheckedDelete(sv1.Path, sv1.ModifyIndex, nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.ErrorAs(t, err, &conflictErr)
|
|
|
|
require.Equal(t, nowVal, conflictErr.Conflict)
|
|
|
|
|
|
|
|
// Delete CAS: delete at the current index; should succeed.
|
2022-08-25 21:38:15 +00:00
|
|
|
_, err = nsv.CheckedDelete(sv1.Path, nowVal.ModifyIndex, nil)
|
2022-07-06 19:29:46 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestVariables_Read(t *testing.T) {
|
2022-07-06 19:29:46 +00:00
|
|
|
testutil.Parallel(t)
|
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
nsv := c.Variables()
|
2022-07-06 19:29:46 +00:00
|
|
|
tID := fmt.Sprint(time.Now().UTC().UnixNano())
|
2022-08-26 18:03:56 +00:00
|
|
|
sv1 := Variable{
|
2022-07-06 19:29:46 +00:00
|
|
|
Namespace: "default",
|
|
|
|
Path: tID + "/sv1",
|
|
|
|
Items: map[string]string{
|
|
|
|
"kv1": "val1",
|
|
|
|
"kv2": "val2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
writeTestVariable(t, c, &sv1)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
path string
|
|
|
|
expectedError string
|
|
|
|
checkValue bool
|
2022-08-26 18:03:56 +00:00
|
|
|
expectedValue *Variable
|
2022-07-06 19:29:46 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "not found",
|
|
|
|
path: tID + "/not/found",
|
|
|
|
expectedError: ErrVariableNotFound,
|
|
|
|
checkValue: true,
|
|
|
|
expectedValue: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "found",
|
|
|
|
path: sv1.Path,
|
|
|
|
checkValue: true,
|
|
|
|
expectedValue: &sv1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
get, _, err := nsv.Read(tc.path, nil)
|
|
|
|
if tc.expectedError != "" {
|
|
|
|
require.EqualError(t, err, tc.expectedError)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tc.checkValue {
|
|
|
|
if tc.expectedValue != nil {
|
|
|
|
require.NotNil(t, get)
|
|
|
|
require.Equal(t, tc.expectedValue, get)
|
|
|
|
} else {
|
|
|
|
require.Nil(t, get)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func writeTestVariable(t *testing.T, c *Client, sv *Variable) {
|
2022-08-25 21:38:15 +00:00
|
|
|
_, err := c.write("/v1/var/"+sv.Path, sv, sv, nil)
|
2022-07-06 19:29:46 +00:00
|
|
|
require.NoError(t, err, "Error writing test variable")
|
|
|
|
require.NoError(t, err, "Error writing test variable")
|
2022-08-25 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestVariable_CreateReturnsContent(t *testing.T) {
|
2022-08-25 21:38:15 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
nsv := c.Variables()
|
|
|
|
sv1 := NewVariable("my/first/variable")
|
2022-08-25 21:38:15 +00:00
|
|
|
sv1.Namespace = "default"
|
|
|
|
sv1.Items["k1"] = "v1"
|
|
|
|
sv1.Items["k2"] = "v2"
|
|
|
|
|
|
|
|
sv1n, _, err := nsv.Create(sv1, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, sv1n)
|
|
|
|
require.Equal(t, sv1.Items, sv1n.Items)
|
2022-07-06 19:29:46 +00:00
|
|
|
}
|