2023-09-14 20:14:55 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
|
|
package templatedpolicy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// golden reads from the golden file returning the contents as a string.
|
|
|
|
func golden(t *testing.T, name string) string {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
golden := filepath.Join("testdata", name+".golden")
|
|
|
|
expected, err := os.ReadFile(golden)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return string(expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFormatTemplatedPolicy(t *testing.T, dirPath string) {
|
|
|
|
type testCase struct {
|
|
|
|
templatedPolicy api.ACLTemplatedPolicyResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := map[string]testCase{
|
|
|
|
"node-templated-policy": {
|
|
|
|
templatedPolicy: api.ACLTemplatedPolicyResponse{
|
|
|
|
TemplateName: api.ACLTemplatedPolicyNodeName,
|
2023-09-15 17:49:22 +00:00
|
|
|
Schema: structs.ACLTemplatedPolicyNodeSchema,
|
2023-09-14 20:14:55 +00:00
|
|
|
Template: structs.ACLTemplatedPolicyNode,
|
2023-11-27 15:34:22 +00:00
|
|
|
Description: structs.ACLTemplatedPolicyNodeDescription,
|
2023-09-14 20:14:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"dns-templated-policy": {
|
|
|
|
templatedPolicy: api.ACLTemplatedPolicyResponse{
|
|
|
|
TemplateName: api.ACLTemplatedPolicyDNSName,
|
2023-09-18 21:10:35 +00:00
|
|
|
Schema: structs.ACLTemplatedPolicyNoRequiredVariablesSchema,
|
2023-09-14 20:14:55 +00:00
|
|
|
Template: structs.ACLTemplatedPolicyDNS,
|
2023-11-27 15:34:22 +00:00
|
|
|
Description: structs.ACLTemplatedPolicyDNSDescription,
|
2023-09-14 20:14:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"service-templated-policy": {
|
|
|
|
templatedPolicy: api.ACLTemplatedPolicyResponse{
|
|
|
|
TemplateName: api.ACLTemplatedPolicyServiceName,
|
2023-09-15 17:49:22 +00:00
|
|
|
Schema: structs.ACLTemplatedPolicyServiceSchema,
|
2023-09-14 20:14:55 +00:00
|
|
|
Template: structs.ACLTemplatedPolicyService,
|
2023-11-27 15:34:22 +00:00
|
|
|
Description: structs.ACLTemplatedPolicyServiceDescription,
|
2023-09-14 20:14:55 +00:00
|
|
|
},
|
|
|
|
},
|
2023-09-20 16:10:55 +00:00
|
|
|
"nomad-server-templated-policy": {
|
|
|
|
templatedPolicy: api.ACLTemplatedPolicyResponse{
|
|
|
|
TemplateName: api.ACLTemplatedPolicyNomadServerName,
|
|
|
|
Schema: structs.ACLTemplatedPolicyNoRequiredVariablesSchema,
|
|
|
|
Template: structs.ACLTemplatedPolicyNomadServer,
|
2023-11-27 15:34:22 +00:00
|
|
|
Description: structs.ACLTemplatedPolicyNomadServerDescription,
|
2023-09-20 16:10:55 +00:00
|
|
|
},
|
|
|
|
},
|
2023-12-06 15:32:12 +00:00
|
|
|
"nomad-client-templated-policy": {
|
|
|
|
templatedPolicy: api.ACLTemplatedPolicyResponse{
|
|
|
|
TemplateName: api.ACLTemplatedPolicyNomadClientName,
|
|
|
|
Schema: structs.ACLTemplatedPolicyNoRequiredVariablesSchema,
|
|
|
|
Template: structs.ACLTemplatedPolicyNomadClient,
|
|
|
|
Description: structs.ACLTemplatedPolicyNomadClientDescription,
|
|
|
|
},
|
|
|
|
},
|
2023-09-14 20:14:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
formatters := map[string]Formatter{
|
|
|
|
"pretty": newPrettyFormatter(false),
|
|
|
|
"pretty-meta": newPrettyFormatter(true),
|
|
|
|
// the JSON formatter ignores the showMeta
|
|
|
|
"json": newJSONFormatter(false),
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tcase := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
for fmtName, formatter := range formatters {
|
|
|
|
t.Run(fmtName, func(t *testing.T) {
|
|
|
|
actual, err := formatter.FormatTemplatedPolicy(tcase.templatedPolicy)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
gName := fmt.Sprintf("%s.%s", name, fmtName)
|
|
|
|
|
|
|
|
expected := golden(t, path.Join(dirPath, gName))
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFormatTemplatedPolicyList(t *testing.T, dirPath string) {
|
|
|
|
// we don't consider the showMeta field for policy list
|
|
|
|
formatters := map[string]Formatter{
|
|
|
|
"pretty": newPrettyFormatter(false),
|
|
|
|
"json": newJSONFormatter(false),
|
|
|
|
}
|
|
|
|
|
|
|
|
policies := map[string]api.ACLTemplatedPolicyResponse{
|
|
|
|
"builtin/node": {
|
|
|
|
TemplateName: api.ACLTemplatedPolicyNodeName,
|
2023-09-15 17:49:22 +00:00
|
|
|
Schema: structs.ACLTemplatedPolicyNodeSchema,
|
2023-09-14 20:14:55 +00:00
|
|
|
Template: structs.ACLTemplatedPolicyNode,
|
2023-11-27 15:34:22 +00:00
|
|
|
Description: structs.ACLTemplatedPolicyNodeDescription,
|
2023-09-14 20:14:55 +00:00
|
|
|
},
|
|
|
|
"builtin/dns": {
|
|
|
|
TemplateName: api.ACLTemplatedPolicyDNSName,
|
2023-09-18 21:10:35 +00:00
|
|
|
Schema: structs.ACLTemplatedPolicyNoRequiredVariablesSchema,
|
2023-09-14 20:14:55 +00:00
|
|
|
Template: structs.ACLTemplatedPolicyDNS,
|
2023-11-27 15:34:22 +00:00
|
|
|
Description: structs.ACLTemplatedPolicyDNSDescription,
|
2023-09-14 20:14:55 +00:00
|
|
|
},
|
|
|
|
"builtin/service": {
|
|
|
|
TemplateName: api.ACLTemplatedPolicyServiceName,
|
2023-09-15 17:49:22 +00:00
|
|
|
Schema: structs.ACLTemplatedPolicyServiceSchema,
|
2023-09-14 20:14:55 +00:00
|
|
|
Template: structs.ACLTemplatedPolicyService,
|
2023-11-27 15:34:22 +00:00
|
|
|
Description: structs.ACLTemplatedPolicyServiceDescription,
|
2023-09-14 20:14:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for fmtName, formatter := range formatters {
|
|
|
|
t.Run(fmtName, func(t *testing.T) {
|
|
|
|
actual, err := formatter.FormatTemplatedPolicyList(policies)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
gName := fmt.Sprintf("list.%s", fmtName)
|
|
|
|
|
|
|
|
expected := golden(t, path.Join(dirPath, gName))
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|