2018-10-01 15:27:59 +00:00
|
|
|
package services
|
2018-09-28 06:52:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-10-02 19:45:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/config"
|
2018-10-01 15:05:57 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2018-09-28 06:52:17 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-10-02 19:45:00 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2018-10-01 15:05:57 +00:00
|
|
|
func TestStructsToAgentService(t *testing.T) {
|
2018-10-02 19:45:00 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-09-28 06:52:17 +00:00
|
|
|
cases := []struct {
|
|
|
|
Name string
|
2018-10-01 15:05:57 +00:00
|
|
|
Input *structs.ServiceDefinition
|
2018-09-28 06:52:17 +00:00
|
|
|
Output *api.AgentServiceRegistration
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"Basic service with port",
|
2018-10-01 15:05:57 +00:00
|
|
|
&structs.ServiceDefinition{
|
|
|
|
Name: "web",
|
2018-09-28 06:52:17 +00:00
|
|
|
Tags: []string{"leader"},
|
2018-10-01 15:05:57 +00:00
|
|
|
Port: 1234,
|
2018-09-28 06:52:17 +00:00
|
|
|
},
|
|
|
|
&api.AgentServiceRegistration{
|
|
|
|
Name: "web",
|
|
|
|
Tags: []string{"leader"},
|
|
|
|
Port: 1234,
|
|
|
|
},
|
|
|
|
},
|
2018-10-01 02:17:45 +00:00
|
|
|
{
|
|
|
|
"Service with a check",
|
2018-10-01 15:05:57 +00:00
|
|
|
&structs.ServiceDefinition{
|
|
|
|
Name: "web",
|
|
|
|
Check: structs.CheckType{
|
|
|
|
Name: "ping",
|
2018-10-01 02:17:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&api.AgentServiceRegistration{
|
|
|
|
Name: "web",
|
|
|
|
Check: &api.AgentServiceCheck{
|
|
|
|
Name: "ping",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Service with checks",
|
2018-10-01 15:05:57 +00:00
|
|
|
&structs.ServiceDefinition{
|
|
|
|
Name: "web",
|
|
|
|
Checks: structs.CheckTypes{
|
|
|
|
&structs.CheckType{
|
|
|
|
Name: "ping",
|
2018-10-01 02:17:45 +00:00
|
|
|
},
|
2018-10-01 15:05:57 +00:00
|
|
|
&structs.CheckType{
|
|
|
|
Name: "pong",
|
2018-10-01 02:17:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&api.AgentServiceRegistration{
|
|
|
|
Name: "web",
|
|
|
|
Checks: api.AgentServiceChecks{
|
|
|
|
&api.AgentServiceCheck{
|
|
|
|
Name: "ping",
|
|
|
|
},
|
|
|
|
&api.AgentServiceCheck{
|
|
|
|
Name: "pong",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-10-04 13:08:12 +00:00
|
|
|
{
|
|
|
|
"Proxy service",
|
|
|
|
&structs.ServiceDefinition{
|
|
|
|
Name: "web-proxy",
|
|
|
|
Kind: structs.ServiceKindConnectProxy,
|
|
|
|
Tags: []string{"leader"},
|
|
|
|
Port: 1234,
|
|
|
|
Proxy: &structs.ConnectProxyConfig{
|
|
|
|
DestinationServiceID: "web1",
|
|
|
|
DestinationServiceName: "web",
|
|
|
|
LocalServiceAddress: "127.0.0.1",
|
|
|
|
LocalServicePort: 8181,
|
|
|
|
Upstreams: structs.TestUpstreams(t),
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&api.AgentServiceRegistration{
|
|
|
|
Name: "web-proxy",
|
|
|
|
Tags: []string{"leader"},
|
|
|
|
Port: 1234,
|
|
|
|
Kind: api.ServiceKindConnectProxy,
|
|
|
|
Proxy: &api.AgentServiceConnectProxyConfig{
|
|
|
|
DestinationServiceID: "web1",
|
|
|
|
DestinationServiceName: "web",
|
|
|
|
LocalServiceAddress: "127.0.0.1",
|
|
|
|
LocalServicePort: 8181,
|
|
|
|
Upstreams: structs.TestUpstreams(t).ToAPI(),
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-09-28 06:52:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 13:08:12 +00:00
|
|
|
for _, tt := range cases {
|
|
|
|
// Capture the loop variable locally otherwise parallel will cause us to run
|
|
|
|
// N copies of the last test case but with different names!!
|
|
|
|
tc := tt
|
2018-09-28 06:52:17 +00:00
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
2018-10-02 19:45:00 +00:00
|
|
|
t.Parallel()
|
2018-09-28 06:52:17 +00:00
|
|
|
require := require.New(t)
|
2018-10-01 15:05:57 +00:00
|
|
|
actual, err := serviceToAgentService(tc.Input)
|
2018-09-28 06:52:17 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(tc.Output, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|