2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-05-13 17:11:27 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2022-06-27 19:51:01 +00:00
|
|
|
"fmt"
|
2022-05-13 17:11:27 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2022-06-14 17:28:10 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2022-05-13 17:11:27 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cb = func(c *Config) {
|
|
|
|
var ns int
|
|
|
|
ns = 0
|
|
|
|
c.LogLevel = "ERROR"
|
|
|
|
c.Server.NumSchedulers = &ns
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestHTTP_Variables(t *testing.T) {
|
2022-06-14 17:28:10 +00:00
|
|
|
ci.Parallel(t)
|
2022-05-13 17:11:27 +00:00
|
|
|
|
|
|
|
httpTest(t, cb, func(s *TestAgent) {
|
2022-06-14 17:28:10 +00:00
|
|
|
// These tests are run against the same running server in order to reduce
|
2023-01-03 17:25:20 +00:00
|
|
|
// the costs of server startup and allow as much parallelization as possible.
|
2022-06-14 17:28:10 +00:00
|
|
|
t.Run("error_badverb_list", func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("LOLWUT", "/v1/vars", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
2022-08-26 18:03:56 +00:00
|
|
|
_, err = s.Server.VariablesListRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.EqualError(t, err, ErrInvalidMethod)
|
|
|
|
})
|
|
|
|
t.Run("error_parse_list", func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/vars?wait=99a", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
2022-08-26 18:03:56 +00:00
|
|
|
_, _ = s.Server.VariablesListRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.Equal(t, http.StatusBadRequest, respW.Code)
|
|
|
|
require.Equal(t, "Invalid wait time", string(respW.Body.Bytes()))
|
|
|
|
})
|
|
|
|
t.Run("error_rpc_list", func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/vars?region=bad", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariablesListRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.EqualError(t, err, "No path to region")
|
|
|
|
require.Nil(t, obj)
|
|
|
|
})
|
|
|
|
t.Run("list", func(t *testing.T) {
|
|
|
|
// Test the empty list case
|
|
|
|
req, err := http.NewRequest("GET", "/v1/vars", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariablesListRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// add vars and test a populated backend
|
2022-08-26 18:03:56 +00:00
|
|
|
svMap := mock.Variables(4, 4)
|
2022-06-14 17:28:10 +00:00
|
|
|
svs := svMap.List()
|
|
|
|
svs[3].Path = svs[0].Path + "/child"
|
|
|
|
for _, sv := range svs {
|
2022-08-25 21:38:15 +00:00
|
|
|
require.NoError(t, rpcWriteSV(s, sv, nil))
|
2022-06-14 17:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err = http.NewRequest("GET", "/v1/vars", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err = s.Server.VariablesListRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Check for the index
|
|
|
|
require.NotZero(t, respW.HeaderMap.Get("X-Nomad-Index"))
|
|
|
|
require.Equal(t, "true", respW.HeaderMap.Get("X-Nomad-KnownLeader"))
|
|
|
|
require.NotZero(t, respW.HeaderMap.Get("X-Nomad-LastContact"))
|
|
|
|
|
2022-08-25 21:38:15 +00:00
|
|
|
// Check the output (the 4 we register )
|
2022-08-26 18:03:56 +00:00
|
|
|
require.Len(t, obj.([]*structs.VariableMetadata), 4)
|
2022-06-14 17:28:10 +00:00
|
|
|
|
|
|
|
// test prefix query
|
|
|
|
req, err = http.NewRequest("GET", "/v1/vars?prefix="+svs[0].Path, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err = s.Server.VariablesListRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-26 18:03:56 +00:00
|
|
|
require.Len(t, obj.([]*structs.VariableMetadata), 2)
|
2022-06-14 17:28:10 +00:00
|
|
|
})
|
|
|
|
rpcResetSV(s)
|
|
|
|
|
|
|
|
t.Run("error_badverb_query", func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("LOLWUT", "/v1/var/does/not/exist", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.EqualError(t, err, ErrInvalidMethod)
|
|
|
|
require.Nil(t, obj)
|
|
|
|
})
|
|
|
|
t.Run("error_parse_query", func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/var/does/not/exist?wait=99a", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
2022-08-26 18:03:56 +00:00
|
|
|
_, _ = s.Server.VariableSpecificRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.Equal(t, http.StatusBadRequest, respW.Code)
|
|
|
|
require.Equal(t, "Invalid wait time", string(respW.Body.Bytes()))
|
|
|
|
})
|
|
|
|
t.Run("error_rpc_query", func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/var/does/not/exist?region=bad", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.EqualError(t, err, "No path to region")
|
|
|
|
require.Nil(t, obj)
|
|
|
|
})
|
|
|
|
t.Run("query_unset_path", func(t *testing.T) {
|
|
|
|
// Make a request for a non-existing variable
|
|
|
|
req, err := http.NewRequest("GET", "/v1/var/", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
|
|
|
require.EqualError(t, err, "missing variable path")
|
2022-06-14 17:28:10 +00:00
|
|
|
require.Nil(t, obj)
|
|
|
|
})
|
|
|
|
t.Run("query_unset_variable", func(t *testing.T) {
|
|
|
|
// Make a request for a non-existing variable
|
|
|
|
req, err := http.NewRequest("GET", "/v1/var/not/real", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
|
|
|
require.EqualError(t, err, "variable not found")
|
2022-06-14 17:28:10 +00:00
|
|
|
require.Nil(t, obj)
|
|
|
|
})
|
|
|
|
t.Run("query", func(t *testing.T) {
|
|
|
|
// Use RPC to make a test variable
|
2022-08-26 18:03:56 +00:00
|
|
|
out := new(structs.VariableDecrypted)
|
|
|
|
sv1 := mock.Variable()
|
2022-08-25 21:38:15 +00:00
|
|
|
require.NoError(t, rpcWriteSV(s, sv1, out))
|
2022-06-14 17:28:10 +00:00
|
|
|
|
|
|
|
// Query a variable
|
|
|
|
req, err := http.NewRequest("GET", "/v1/var/"+sv1.Path, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Check for the index
|
|
|
|
require.NotZero(t, respW.HeaderMap.Get("X-Nomad-Index"))
|
|
|
|
require.Equal(t, "true", respW.HeaderMap.Get("X-Nomad-KnownLeader"))
|
|
|
|
require.NotZero(t, respW.HeaderMap.Get("X-Nomad-LastContact"))
|
|
|
|
|
|
|
|
// Check the output
|
2022-08-26 18:03:56 +00:00
|
|
|
require.Equal(t, out, obj.(*structs.VariableDecrypted))
|
2022-06-14 17:28:10 +00:00
|
|
|
})
|
|
|
|
rpcResetSV(s)
|
2022-05-13 17:11:27 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
sv1 := mock.Variable()
|
2022-06-14 17:28:10 +00:00
|
|
|
t.Run("error_parse_create", func(t *testing.T) {
|
|
|
|
buf := encodeBrokenReq(&sv1)
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/var/"+sv1.Path, buf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.EqualError(t, err, "unexpected EOF")
|
|
|
|
require.Nil(t, obj)
|
|
|
|
})
|
|
|
|
t.Run("error_rpc_create", func(t *testing.T) {
|
|
|
|
buf := encodeReq(sv1)
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/var/does/not/exist?region=bad", buf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.EqualError(t, err, "No path to region")
|
|
|
|
require.Nil(t, obj)
|
|
|
|
})
|
|
|
|
t.Run("create_no_items", func(t *testing.T) {
|
|
|
|
sv2 := sv1.Copy()
|
|
|
|
sv2.Items = nil
|
|
|
|
buf := encodeReq(sv2)
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/var/"+sv1.Path, buf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
|
|
|
require.EqualError(t, err, "variable missing required Items object")
|
2022-06-14 17:28:10 +00:00
|
|
|
require.Nil(t, obj)
|
|
|
|
})
|
|
|
|
t.Run("create", func(t *testing.T) {
|
|
|
|
buf := encodeReq(sv1)
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/var/"+sv1.Path, buf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-25 21:38:15 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Test the returned object and rehydrate to a VariableDecrypted
|
2022-08-25 21:38:15 +00:00
|
|
|
require.NotNil(t, obj)
|
2022-08-26 18:03:56 +00:00
|
|
|
sv1, ok := obj.(*structs.VariableDecrypted)
|
|
|
|
require.True(t, ok, "Unable to convert obj to VariableDecrypted")
|
2022-06-14 17:28:10 +00:00
|
|
|
|
|
|
|
// Check for the index
|
|
|
|
require.NotZero(t, respW.HeaderMap.Get("X-Nomad-Index"))
|
2022-08-25 21:38:15 +00:00
|
|
|
require.Equal(t, fmt.Sprint(sv1.ModifyIndex), respW.HeaderMap.Get("X-Nomad-Index"))
|
2022-06-14 17:28:10 +00:00
|
|
|
|
2022-08-25 21:38:15 +00:00
|
|
|
// Check the variable was put and that the returned item matched the
|
|
|
|
// fetched value
|
2022-06-14 17:28:10 +00:00
|
|
|
out, err := rpcReadSV(s, sv1.Namespace, sv1.Path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, out)
|
|
|
|
require.Equal(t, sv1, out)
|
|
|
|
})
|
|
|
|
rpcResetSV(s)
|
2022-05-13 17:11:27 +00:00
|
|
|
|
2022-06-14 17:28:10 +00:00
|
|
|
t.Run("error_parse_update", func(t *testing.T) {
|
2022-06-27 19:51:01 +00:00
|
|
|
sv1U := sv1.Copy()
|
|
|
|
sv1U.Items["new"] = "new"
|
|
|
|
|
2022-06-14 17:28:10 +00:00
|
|
|
// break the request body
|
|
|
|
badBuf := encodeBrokenReq(&sv1U)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/var/"+sv1.Path, badBuf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.EqualError(t, err, "unexpected EOF")
|
|
|
|
var cErr HTTPCodedError
|
|
|
|
require.ErrorAs(t, err, &cErr)
|
|
|
|
require.Equal(t, http.StatusBadRequest, cErr.Code())
|
|
|
|
require.Nil(t, obj)
|
|
|
|
})
|
|
|
|
t.Run("error_rpc_update", func(t *testing.T) {
|
2022-06-27 19:51:01 +00:00
|
|
|
sv1U := sv1.Copy()
|
|
|
|
sv1U.Items["new"] = "new"
|
|
|
|
|
2022-06-14 17:28:10 +00:00
|
|
|
// test broken rpc error
|
|
|
|
buf := encodeReq(&sv1U)
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/var/"+sv1.Path+"?region=bad", buf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.EqualError(t, err, "No path to region")
|
|
|
|
require.Nil(t, obj)
|
|
|
|
})
|
|
|
|
t.Run("update", func(t *testing.T) {
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := mock.Variable()
|
2022-08-25 21:38:15 +00:00
|
|
|
require.NoError(t, rpcWriteSV(s, sv, sv))
|
2022-06-14 17:28:10 +00:00
|
|
|
|
2022-06-27 19:51:01 +00:00
|
|
|
svU := sv.Copy()
|
|
|
|
svU.Items["new"] = "new"
|
2022-06-14 17:28:10 +00:00
|
|
|
// Make the HTTP request
|
2022-06-27 19:51:01 +00:00
|
|
|
buf := encodeReq(&svU)
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/var/"+sv.Path, buf)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-25 21:38:15 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Test the returned object and rehydrate to a VariableDecrypted
|
2022-08-25 21:38:15 +00:00
|
|
|
require.NotNil(t, obj)
|
2022-08-26 18:03:56 +00:00
|
|
|
out, ok := obj.(*structs.VariableDecrypted)
|
|
|
|
require.True(t, ok, "Unable to convert obj to VariableDecrypted")
|
2022-06-14 17:28:10 +00:00
|
|
|
|
|
|
|
// Check for the index
|
|
|
|
require.NotZero(t, respW.HeaderMap.Get("X-Nomad-Index"))
|
2022-08-25 21:38:15 +00:00
|
|
|
require.Equal(t, fmt.Sprint(out.ModifyIndex), respW.HeaderMap.Get("X-Nomad-Index"))
|
2022-06-14 17:28:10 +00:00
|
|
|
|
2022-06-27 19:51:01 +00:00
|
|
|
{
|
|
|
|
// Check that written varible does not equal the input to rule out input mutation
|
2022-08-26 18:03:56 +00:00
|
|
|
require.NotEqual(t, &svU.VariableMetadata, out.VariableMetadata)
|
2022-06-27 19:51:01 +00:00
|
|
|
|
|
|
|
// Update the input token with the updated metadata so that we
|
|
|
|
// can use a simple equality check
|
2022-08-25 21:38:15 +00:00
|
|
|
svU.ModifyIndex = out.ModifyIndex
|
|
|
|
svU.ModifyTime = out.ModifyTime
|
|
|
|
require.Equal(t, &svU, out)
|
2022-06-27 19:51:01 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("update-cas", func(t *testing.T) {
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := mock.Variable()
|
2022-08-25 21:38:15 +00:00
|
|
|
require.NoError(t, rpcWriteSV(s, sv, sv))
|
2022-06-14 17:28:10 +00:00
|
|
|
|
2022-06-27 19:51:01 +00:00
|
|
|
svU := sv.Copy()
|
|
|
|
svU.Items["new"] = "new"
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
{
|
|
|
|
buf := encodeReq(&svU)
|
2022-08-15 15:19:53 +00:00
|
|
|
req, err := http.NewRequest("PUT", "/v1/var/"+svU.Path+"?cas=1", buf)
|
2022-06-27 19:51:01 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
2022-07-28 15:04:31 +00:00
|
|
|
require.NoError(t, err)
|
2022-06-27 19:51:01 +00:00
|
|
|
require.Equal(t, http.StatusConflict, respW.Result().StatusCode)
|
|
|
|
|
|
|
|
// Evaluate the conflict variable
|
|
|
|
require.NotNil(t, obj)
|
2022-08-26 18:03:56 +00:00
|
|
|
conflict, ok := obj.(*structs.VariableDecrypted)
|
|
|
|
require.True(t, ok, "Expected *structs.VariableDecrypted, got %T", obj)
|
2022-08-25 21:38:15 +00:00
|
|
|
require.Equal(t, conflict, sv)
|
2022-06-27 19:51:01 +00:00
|
|
|
|
|
|
|
// Check for the index
|
|
|
|
require.NotZero(t, respW.HeaderMap.Get("X-Nomad-Index"))
|
|
|
|
}
|
|
|
|
// Check the variable was not updated
|
|
|
|
{
|
|
|
|
out, err := rpcReadSV(s, sv.Namespace, sv.Path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, sv, out)
|
|
|
|
}
|
|
|
|
// Make the HTTP request
|
|
|
|
{
|
|
|
|
buf := encodeReq(&svU)
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/var/"+svU.Path+"?cas="+fmt.Sprint(sv.ModifyIndex), buf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
2022-06-27 19:51:01 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-25 21:38:15 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Test the returned object and rehydrate to a VariableDecrypted
|
2022-08-25 21:38:15 +00:00
|
|
|
require.NotNil(t, obj)
|
2022-08-26 18:03:56 +00:00
|
|
|
sv1, ok := obj.(*structs.VariableDecrypted)
|
|
|
|
require.True(t, ok, "Unable to convert obj to VariableDecrypted")
|
2022-06-27 19:51:01 +00:00
|
|
|
|
|
|
|
// Check for the index
|
|
|
|
require.NotZero(t, respW.HeaderMap.Get("X-Nomad-Index"))
|
2022-08-25 21:38:15 +00:00
|
|
|
require.Equal(t, fmt.Sprint(sv1.ModifyIndex), respW.HeaderMap.Get("X-Nomad-Index"))
|
|
|
|
|
|
|
|
// Check the variable was put and that the returned item matched the
|
|
|
|
// fetched value
|
|
|
|
out, err := rpcReadSV(s, sv.Namespace, sv.Path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, out)
|
|
|
|
require.Equal(t, sv1, out)
|
|
|
|
|
2022-06-27 19:51:01 +00:00
|
|
|
}
|
|
|
|
// Check the variable was created correctly
|
|
|
|
{
|
|
|
|
out, err := rpcReadSV(s, sv.Namespace, sv.Path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, out)
|
|
|
|
|
|
|
|
require.NotEqual(t, sv, out)
|
2022-08-26 18:03:56 +00:00
|
|
|
require.NotEqual(t, svU.VariableMetadata, out.VariableMetadata)
|
2022-06-27 19:51:01 +00:00
|
|
|
|
|
|
|
// Update the input token with the updated metadata so that we
|
|
|
|
// can use a simple equality check
|
|
|
|
svU.CreateIndex, svU.ModifyIndex = out.CreateIndex, out.ModifyIndex
|
|
|
|
svU.CreateTime, svU.ModifyTime = out.CreateTime, out.ModifyTime
|
2022-08-26 18:03:56 +00:00
|
|
|
require.Equal(t, svU.VariableMetadata, out.VariableMetadata)
|
2022-06-27 19:51:01 +00:00
|
|
|
|
|
|
|
// fmt writes sorted output of maps for testability.
|
|
|
|
require.Equal(t, fmt.Sprint(svU.Items), fmt.Sprint(out.Items))
|
|
|
|
}
|
2022-06-14 17:28:10 +00:00
|
|
|
})
|
|
|
|
rpcResetSV(s)
|
|
|
|
|
|
|
|
t.Run("error_rpc_delete", func(t *testing.T) {
|
2022-08-26 18:03:56 +00:00
|
|
|
sv1 := mock.Variable()
|
2022-08-25 21:38:15 +00:00
|
|
|
require.NoError(t, rpcWriteSV(s, sv1, nil))
|
2022-06-14 17:28:10 +00:00
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("DELETE", "/v1/var/"+sv1.Path+"?region=bad", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.EqualError(t, err, "No path to region")
|
|
|
|
require.Nil(t, obj)
|
|
|
|
})
|
2022-06-27 19:51:01 +00:00
|
|
|
t.Run("delete-cas", func(t *testing.T) {
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := mock.Variable()
|
2022-08-25 21:38:15 +00:00
|
|
|
require.NoError(t, rpcWriteSV(s, sv, nil))
|
2022-06-27 19:51:01 +00:00
|
|
|
sv, err := rpcReadSV(s, sv.Namespace, sv.Path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
{
|
2022-08-15 15:19:53 +00:00
|
|
|
req, err := http.NewRequest("DELETE", "/v1/var/"+sv.Path+"?cas=1", nil)
|
2022-06-27 19:51:01 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
2022-06-27 19:51:01 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusConflict, respW.Result().StatusCode)
|
|
|
|
|
|
|
|
// Evaluate the conflict variable
|
|
|
|
require.NotNil(t, obj)
|
2022-08-26 18:03:56 +00:00
|
|
|
conflict, ok := obj.(*structs.VariableDecrypted)
|
|
|
|
require.True(t, ok, "Expected *structs.VariableDecrypted, got %T", obj)
|
2022-10-10 14:28:46 +00:00
|
|
|
require.True(t, sv.Equal(*conflict))
|
2022-06-27 19:51:01 +00:00
|
|
|
|
|
|
|
// Check for the index
|
|
|
|
require.NotZero(t, respW.HeaderMap.Get("X-Nomad-Index"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check variable was not deleted
|
|
|
|
{
|
|
|
|
svChk, err := rpcReadSV(s, sv.Namespace, sv.Path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, svChk)
|
|
|
|
require.Equal(t, sv, svChk)
|
|
|
|
}
|
|
|
|
// Make the HTTP request
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("DELETE", "/v1/var/"+sv.Path+"?cas="+fmt.Sprint(sv.ModifyIndex), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
2022-06-27 19:51:01 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, obj)
|
|
|
|
}
|
|
|
|
// Check variable was deleted
|
|
|
|
{
|
|
|
|
svChk, err := rpcReadSV(s, sv.Namespace, sv.Path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, svChk)
|
|
|
|
}
|
|
|
|
})
|
2022-06-14 17:28:10 +00:00
|
|
|
t.Run("delete", func(t *testing.T) {
|
2022-08-26 18:03:56 +00:00
|
|
|
sv1 := mock.Variable()
|
2022-08-25 21:38:15 +00:00
|
|
|
require.NoError(t, rpcWriteSV(s, sv1, nil))
|
2022-06-14 17:28:10 +00:00
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("DELETE", "/v1/var/"+sv1.Path, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
2022-08-26 18:03:56 +00:00
|
|
|
obj, err := s.Server.VariableSpecificRequest(respW, req)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, obj)
|
|
|
|
|
|
|
|
// Check for the index
|
|
|
|
require.NotZero(t, respW.HeaderMap.Get("X-Nomad-Index"))
|
2022-06-27 19:51:01 +00:00
|
|
|
require.Equal(t, http.StatusNoContent, respW.Result().StatusCode)
|
2022-06-14 17:28:10 +00:00
|
|
|
|
|
|
|
// Check variable was deleted
|
|
|
|
sv, err := rpcReadSV(s, sv1.Namespace, sv1.Path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, sv)
|
|
|
|
})
|
2022-05-13 17:11:27 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-14 17:28:10 +00:00
|
|
|
// encodeBrokenReq is a test helper that damages input JSON in order to create
|
|
|
|
// a parsing error for testing error pathways.
|
2022-05-13 17:11:27 +00:00
|
|
|
func encodeBrokenReq(obj interface{}) io.ReadCloser {
|
|
|
|
// var buf *bytes.Buffer
|
|
|
|
// enc := json.NewEncoder(buf)
|
|
|
|
// enc.Encode(obj)
|
|
|
|
b, _ := json.Marshal(obj)
|
|
|
|
b = b[0 : len(b)-5] // strip newline and final }
|
2023-03-08 15:20:04 +00:00
|
|
|
return io.NopCloser(bytes.NewReader(b))
|
2022-05-13 17:11:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// rpcReadSV lets this test read a variable using the RPC endpoint
|
|
|
|
func rpcReadSV(s *TestAgent, ns, p string) (*structs.VariableDecrypted, error) {
|
|
|
|
checkArgs := structs.VariablesReadRequest{Path: p, QueryOptions: structs.QueryOptions{Namespace: ns, Region: "global"}}
|
|
|
|
var checkResp structs.VariablesReadResponse
|
|
|
|
err := s.Agent.RPC(structs.VariablesReadRPCMethod, &checkArgs, &checkResp)
|
2022-05-13 17:11:27 +00:00
|
|
|
return checkResp.Data, err
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// rpcWriteSV lets this test write a variable using the RPC endpoint
|
|
|
|
func rpcWriteSV(s *TestAgent, sv, out *structs.VariableDecrypted) error {
|
2022-08-15 15:19:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
args := structs.VariablesApplyRequest{
|
|
|
|
Op: structs.VarOpSet,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: sv,
|
2022-05-27 13:33:41 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Namespace: sv.Namespace, Region: "global"},
|
2022-05-13 17:11:27 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
var resp structs.VariablesApplyResponse
|
|
|
|
err := s.Agent.RPC(structs.VariablesApplyRPCMethod, &args, &resp)
|
2022-05-27 13:33:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-25 21:38:15 +00:00
|
|
|
if out != nil {
|
|
|
|
*out = *resp.Output
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
return nil
|
2022-05-13 17:11:27 +00:00
|
|
|
}
|
2022-06-14 17:28:10 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// rpcResetSV lists all the variables for every namespace in the global
|
2022-06-14 17:28:10 +00:00
|
|
|
// region and deletes them using the RPC endpoints
|
|
|
|
func rpcResetSV(s *TestAgent) {
|
2022-08-26 18:03:56 +00:00
|
|
|
var lArgs structs.VariablesListRequest
|
|
|
|
var lResp structs.VariablesListResponse
|
2022-06-14 17:28:10 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
lArgs = structs.VariablesListRequest{
|
2022-06-14 17:28:10 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Namespace: "*",
|
|
|
|
Region: "global",
|
|
|
|
},
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
err := s.Agent.RPC(structs.VariablesListRPCMethod, &lArgs, &lResp)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.NoError(s.T, err)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
dArgs := structs.VariablesApplyRequest{
|
|
|
|
Op: structs.VarOpDelete,
|
|
|
|
Var: &structs.VariableDecrypted{},
|
2022-08-15 15:19:53 +00:00
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Region: "global",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
var dResp structs.VariablesApplyResponse
|
2022-08-15 15:19:53 +00:00
|
|
|
|
2022-06-14 17:28:10 +00:00
|
|
|
for _, v := range lResp.Data {
|
2022-08-15 15:19:53 +00:00
|
|
|
dArgs.Var.Path = v.Path
|
|
|
|
dArgs.Var.Namespace = v.Namespace
|
2022-08-26 18:03:56 +00:00
|
|
|
err := s.Agent.RPC(structs.VariablesApplyRPCMethod, &dArgs, &dResp)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.NoError(s.T, err)
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
err = s.Agent.RPC(structs.VariablesListRPCMethod, &lArgs, &lResp)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.NoError(s.T, err)
|
|
|
|
require.Equal(s.T, 0, len(lResp.Data))
|
|
|
|
}
|