mirror of
https://github.com/status-im/consul.git
synced 2025-01-09 13:26:07 +00:00
3c4413cbed
A Node Identity is very similar to a service identity. Its main targeted use is to allow creating tokens for use by Consul agents that will grant the necessary permissions for all the typical agent operations (node registration, coordinate updates, anti-entropy). Half of this commit is for golden file based tests of the acl token and role cli output. Another big updates was to refactor many of the tests in agent/consul/acl_endpoint_test.go to use the same style of tests and the same helpers. Besides being less boiler plate in the tests it also uses a common way of starting a test server with ACLs that should operate without any warnings regarding deprecated non-uuid master tokens etc.
196 lines
4.8 KiB
Go
196 lines
4.8 KiB
Go
package role
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"path"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// update allows golden files to be updated based on the current output.
|
|
var update = flag.Bool("update", false, "update golden files")
|
|
|
|
// golden reads and optionally writes the expected data to the golden file,
|
|
// returning the contents as a string.
|
|
func golden(t *testing.T, name, got string) string {
|
|
t.Helper()
|
|
|
|
golden := filepath.Join("testdata", name+".golden")
|
|
if *update && got != "" {
|
|
err := ioutil.WriteFile(golden, []byte(got), 0644)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
expected, err := ioutil.ReadFile(golden)
|
|
require.NoError(t, err)
|
|
|
|
return string(expected)
|
|
}
|
|
|
|
func TestFormatRole(t *testing.T) {
|
|
type testCase struct {
|
|
role api.ACLRole
|
|
overrideGoldenName string
|
|
}
|
|
|
|
cases := map[string]testCase{
|
|
"basic": {
|
|
role: api.ACLRole{
|
|
ID: "bd6c9fb0-2d1a-4b96-acaf-669f5d7e7852",
|
|
Name: "basic",
|
|
Description: "test role",
|
|
Hash: []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'},
|
|
CreateIndex: 42,
|
|
ModifyIndex: 100,
|
|
},
|
|
},
|
|
"complex": {
|
|
role: api.ACLRole{
|
|
ID: "c29c4ee4-bca6-474e-be37-7d9606f9582a",
|
|
Name: "complex",
|
|
Namespace: "foo",
|
|
Description: "test role complex",
|
|
Hash: []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'},
|
|
CreateIndex: 5,
|
|
ModifyIndex: 10,
|
|
Policies: []*api.ACLLink{
|
|
&api.ACLLink{
|
|
ID: "beb04680-815b-4d7c-9e33-3d707c24672c",
|
|
Name: "hobbiton",
|
|
},
|
|
&api.ACLLink{
|
|
ID: "18788457-584c-4812-80d3-23d403148a90",
|
|
Name: "bywater",
|
|
},
|
|
},
|
|
ServiceIdentities: []*api.ACLServiceIdentity{
|
|
&api.ACLServiceIdentity{
|
|
ServiceName: "gardener",
|
|
Datacenters: []string{"middleearth-northwest"},
|
|
},
|
|
},
|
|
NodeIdentities: []*api.ACLNodeIdentity{
|
|
&api.ACLNodeIdentity{
|
|
NodeName: "bagend",
|
|
Datacenter: "middleearth-northwest",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
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.FormatRole(&tcase.role)
|
|
require.NoError(t, err)
|
|
|
|
gName := fmt.Sprintf("%s.%s", name, fmtName)
|
|
if tcase.overrideGoldenName != "" {
|
|
gName = tcase.overrideGoldenName
|
|
}
|
|
|
|
expected := golden(t, path.Join("FormatRole", gName), actual)
|
|
require.Equal(t, expected, actual)
|
|
})
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFormatTokenList(t *testing.T) {
|
|
type testCase struct {
|
|
roles []*api.ACLRole
|
|
overrideGoldenName string
|
|
}
|
|
|
|
cases := map[string]testCase{
|
|
"basic": {
|
|
roles: []*api.ACLRole{
|
|
&api.ACLRole{
|
|
ID: "bd6c9fb0-2d1a-4b96-acaf-669f5d7e7852",
|
|
Name: "basic",
|
|
Description: "test role",
|
|
Hash: []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'},
|
|
CreateIndex: 42,
|
|
ModifyIndex: 100,
|
|
},
|
|
},
|
|
},
|
|
"complex": {
|
|
roles: []*api.ACLRole{
|
|
&api.ACLRole{
|
|
ID: "c29c4ee4-bca6-474e-be37-7d9606f9582a",
|
|
Name: "complex",
|
|
Namespace: "foo",
|
|
Description: "test role complex",
|
|
Hash: []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'},
|
|
CreateIndex: 5,
|
|
ModifyIndex: 10,
|
|
Policies: []*api.ACLLink{
|
|
&api.ACLLink{
|
|
ID: "beb04680-815b-4d7c-9e33-3d707c24672c",
|
|
Name: "hobbiton",
|
|
},
|
|
&api.ACLLink{
|
|
ID: "18788457-584c-4812-80d3-23d403148a90",
|
|
Name: "bywater",
|
|
},
|
|
},
|
|
ServiceIdentities: []*api.ACLServiceIdentity{
|
|
&api.ACLServiceIdentity{
|
|
ServiceName: "gardener",
|
|
Datacenters: []string{"middleearth-northwest"},
|
|
},
|
|
},
|
|
NodeIdentities: []*api.ACLNodeIdentity{
|
|
&api.ACLNodeIdentity{
|
|
NodeName: "bagend",
|
|
Datacenter: "middleearth-northwest",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
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.FormatRoleList(tcase.roles)
|
|
require.NoError(t, err)
|
|
|
|
gName := fmt.Sprintf("%s.%s", name, fmtName)
|
|
if tcase.overrideGoldenName != "" {
|
|
gName = tcase.overrideGoldenName
|
|
}
|
|
|
|
expected := golden(t, path.Join("FormatRoleList", gName), actual)
|
|
require.Equal(t, expected, actual)
|
|
})
|
|
}
|
|
})
|
|
}
|
|
}
|