hub.fastgit.org/hashicorp/consul.git@v1.4.5/command/services/config_test.go (about)

     1  package services
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/consul/agent/config"
     7  	"github.com/hashicorp/consul/agent/structs"
     8  	"github.com/hashicorp/consul/api"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  // This test ensures that dev mode doesn't register services by default.
    13  // We depend on this behavior for ServiesFromFiles so we want to fail
    14  // tests if that ever changes.
    15  func TestDevModeHasNoServices(t *testing.T) {
    16  	t.Parallel()
    17  	require := require.New(t)
    18  
    19  	devMode := true
    20  	b, err := config.NewBuilder(config.Flags{
    21  		DevMode: &devMode,
    22  	})
    23  	require.NoError(err)
    24  
    25  	cfg, err := b.BuildAndValidate()
    26  	require.NoError(err)
    27  	require.Empty(cfg.Services)
    28  }
    29  
    30  func TestStructsToAgentService(t *testing.T) {
    31  	t.Parallel()
    32  
    33  	cases := []struct {
    34  		Name   string
    35  		Input  *structs.ServiceDefinition
    36  		Output *api.AgentServiceRegistration
    37  	}{
    38  		{
    39  			"Basic service with port",
    40  			&structs.ServiceDefinition{
    41  				Name: "web",
    42  				Tags: []string{"leader"},
    43  				Port: 1234,
    44  			},
    45  			&api.AgentServiceRegistration{
    46  				Name: "web",
    47  				Tags: []string{"leader"},
    48  				Port: 1234,
    49  			},
    50  		},
    51  		{
    52  			"Service with a check",
    53  			&structs.ServiceDefinition{
    54  				Name: "web",
    55  				Check: structs.CheckType{
    56  					Name: "ping",
    57  				},
    58  			},
    59  			&api.AgentServiceRegistration{
    60  				Name: "web",
    61  				Check: &api.AgentServiceCheck{
    62  					Name: "ping",
    63  				},
    64  			},
    65  		},
    66  		{
    67  			"Service with checks",
    68  			&structs.ServiceDefinition{
    69  				Name: "web",
    70  				Checks: structs.CheckTypes{
    71  					&structs.CheckType{
    72  						Name: "ping",
    73  					},
    74  					&structs.CheckType{
    75  						Name: "pong",
    76  					},
    77  				},
    78  			},
    79  			&api.AgentServiceRegistration{
    80  				Name: "web",
    81  				Checks: api.AgentServiceChecks{
    82  					&api.AgentServiceCheck{
    83  						Name: "ping",
    84  					},
    85  					&api.AgentServiceCheck{
    86  						Name: "pong",
    87  					},
    88  				},
    89  			},
    90  		},
    91  		{
    92  			"Proxy service",
    93  			&structs.ServiceDefinition{
    94  				Name: "web-proxy",
    95  				Kind: structs.ServiceKindConnectProxy,
    96  				Tags: []string{"leader"},
    97  				Port: 1234,
    98  				Proxy: &structs.ConnectProxyConfig{
    99  					DestinationServiceID:   "web1",
   100  					DestinationServiceName: "web",
   101  					LocalServiceAddress:    "127.0.0.1",
   102  					LocalServicePort:       8181,
   103  					Upstreams:              structs.TestUpstreams(t),
   104  					Config: map[string]interface{}{
   105  						"foo": "bar",
   106  					},
   107  				},
   108  			},
   109  			&api.AgentServiceRegistration{
   110  				Name: "web-proxy",
   111  				Tags: []string{"leader"},
   112  				Port: 1234,
   113  				Kind: api.ServiceKindConnectProxy,
   114  				Proxy: &api.AgentServiceConnectProxyConfig{
   115  					DestinationServiceID:   "web1",
   116  					DestinationServiceName: "web",
   117  					LocalServiceAddress:    "127.0.0.1",
   118  					LocalServicePort:       8181,
   119  					Upstreams:              structs.TestUpstreams(t).ToAPI(),
   120  					Config: map[string]interface{}{
   121  						"foo": "bar",
   122  					},
   123  				},
   124  			},
   125  		},
   126  	}
   127  
   128  	for _, tt := range cases {
   129  		// Capture the loop variable locally otherwise parallel will cause us to run
   130  		// N copies of the last test case but with different names!!
   131  		tc := tt
   132  		t.Run(tc.Name, func(t *testing.T) {
   133  			t.Parallel()
   134  			require := require.New(t)
   135  			actual, err := serviceToAgentService(tc.Input)
   136  			require.NoError(err)
   137  			require.Equal(tc.Output, actual)
   138  		})
   139  	}
   140  }