consul/command/services/config_test.go

106 lines
2.1 KiB
Go
Raw Normal View History

package services
2018-09-28 06:52:17 +00:00
import (
"testing"
"github.com/hashicorp/consul/agent/config"
"github.com/hashicorp/consul/agent/structs"
2018-09-28 06:52:17 +00:00
"github.com/hashicorp/consul/api"
"github.com/stretchr/testify/require"
)
// This test ensures that dev mode doesn't register services by default.
// We depend on this behavior for ServiesFromFiles so we want to fail
// tests if that ever changes.
func TestDevModeHasNoServices(t *testing.T) {
t.Parallel()
require := require.New(t)
devMode := true
b, err := config.NewBuilder(config.Flags{
DevMode: &devMode,
})
require.NoError(err)
cfg, err := b.BuildAndValidate()
require.NoError(err)
require.Empty(cfg.Services)
}
func TestStructsToAgentService(t *testing.T) {
t.Parallel()
2018-09-28 06:52:17 +00:00
cases := []struct {
Name string
Input *structs.ServiceDefinition
2018-09-28 06:52:17 +00:00
Output *api.AgentServiceRegistration
}{
{
"Basic service with port",
&structs.ServiceDefinition{
Name: "web",
2018-09-28 06:52:17 +00:00
Tags: []string{"leader"},
Port: 1234,
2018-09-28 06:52:17 +00:00
},
&api.AgentServiceRegistration{
Name: "web",
Tags: []string{"leader"},
Port: 1234,
},
},
{
"Service with a check",
&structs.ServiceDefinition{
Name: "web",
Check: structs.CheckType{
Name: "ping",
},
},
&api.AgentServiceRegistration{
Name: "web",
Check: &api.AgentServiceCheck{
Name: "ping",
},
},
},
{
"Service with checks",
&structs.ServiceDefinition{
Name: "web",
Checks: structs.CheckTypes{
&structs.CheckType{
Name: "ping",
},
&structs.CheckType{
Name: "pong",
},
},
},
&api.AgentServiceRegistration{
Name: "web",
Checks: api.AgentServiceChecks{
&api.AgentServiceCheck{
Name: "ping",
},
&api.AgentServiceCheck{
Name: "pong",
},
},
},
},
2018-09-28 06:52:17 +00:00
}
for _, tc := range cases {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
2018-09-28 06:52:17 +00:00
require := require.New(t)
actual, err := serviceToAgentService(tc.Input)
2018-09-28 06:52:17 +00:00
require.NoError(err)
require.Equal(tc.Output, actual)
})
}
}
func intPtr(v int) *int { return &v }
func strPtr(v string) *string { return &v }