github.com/anycable/anycable-go@v1.5.1/config/presets_test.go (about)

     1  package config
     2  
     3  import (
     4  	"log/slog"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestNoPresets(t *testing.T) {
    13  	config := NewConfig()
    14  
    15  	err := config.LoadPresets(slog.Default())
    16  
    17  	require.NoError(t, err)
    18  
    19  	assert.Equal(t, "localhost", config.Host)
    20  }
    21  
    22  func TestFlyPresets(t *testing.T) {
    23  	cleanupEnv := setEnv(
    24  		"FLY_APP_NAME", "any-test",
    25  		"FLY_REGION", "mag",
    26  		"FLY_ALLOC_ID", "1234",
    27  		"ANYCABLE_FLY_RPC_APP_NAME", "anycable-web",
    28  	)
    29  	defer cleanupEnv()
    30  
    31  	config := NewConfig()
    32  
    33  	config.Port = 8989
    34  
    35  	require.Equal(t, []string{"fly"}, config.Presets())
    36  
    37  	err := config.LoadPresets(slog.Default())
    38  
    39  	require.NoError(t, err)
    40  
    41  	assert.Equal(t, "0.0.0.0", config.Host)
    42  	assert.Equal(t, 8989, config.Port)
    43  	assert.Equal(t, 8989, config.HTTPBroadcast.Port)
    44  	assert.Equal(t, true, config.EmbedNats)
    45  	assert.Equal(t, "nats", config.PubSubAdapter)
    46  	assert.Equal(t, "", config.BrokerAdapter)
    47  	assert.Contains(t, config.EmbeddedNats.Name, "fly-mag-1234-")
    48  	assert.Equal(t, "nats://0.0.0.0:4222", config.EmbeddedNats.ServiceAddr)
    49  	assert.Equal(t, "nats://0.0.0.0:5222", config.EmbeddedNats.ClusterAddr)
    50  	assert.Equal(t, "any-test-mag-cluster", config.EmbeddedNats.ClusterName)
    51  	assert.Equal(t, []string{"nats://mag.any-test.internal:5222"}, config.EmbeddedNats.Routes)
    52  	assert.Equal(t, "dns:///mag.anycable-web.internal:50051", config.RPC.Host)
    53  }
    54  
    55  func TestFlyPresets_When_RedisConfigured(t *testing.T) {
    56  	cleanupEnv := setEnv(
    57  		"FLY_APP_NAME", "any-test",
    58  		"FLY_REGION", "mag",
    59  		"FLY_ALLOC_ID", "1234",
    60  	)
    61  	defer cleanupEnv()
    62  
    63  	config := NewConfig()
    64  	config.Redis.URL = "redis://some.cloud.redis:6379/1"
    65  
    66  	require.Equal(t, []string{"fly"}, config.Presets())
    67  
    68  	err := config.LoadPresets(slog.Default())
    69  
    70  	require.NoError(t, err)
    71  
    72  	assert.Equal(t, "0.0.0.0", config.Host)
    73  	assert.Equal(t, false, config.EmbedNats)
    74  	assert.Equal(t, "http,redis", config.BroadcastAdapter)
    75  	assert.Equal(t, "redis", config.PubSubAdapter)
    76  	assert.Equal(t, "nats://0.0.0.0:4222", config.EmbeddedNats.ServiceAddr)
    77  	assert.Equal(t, "nats://0.0.0.0:5222", config.EmbeddedNats.ClusterAddr)
    78  	assert.Equal(t, "any-test-mag-cluster", config.EmbeddedNats.ClusterName)
    79  	assert.Equal(t, []string{"nats://mag.any-test.internal:5222"}, config.EmbeddedNats.Routes)
    80  	// It stays default
    81  	assert.Equal(t, "localhost:50051", config.RPC.Host)
    82  }
    83  
    84  func TestHerokuPresets(t *testing.T) {
    85  	cleanupEnv := setEnv(
    86  		"HEROKU_DYNO_ID", "web.42",
    87  		"HEROKU_APP_ID", "herr-cable",
    88  		"PORT", "4321",
    89  	)
    90  	defer cleanupEnv()
    91  
    92  	config := NewConfig()
    93  
    94  	require.Equal(t, []string{"heroku"}, config.Presets())
    95  
    96  	err := config.LoadPresets(slog.Default())
    97  
    98  	require.NoError(t, err)
    99  
   100  	assert.Equal(t, "0.0.0.0", config.Host)
   101  	assert.Equal(t, 4321, config.HTTPBroadcast.Port)
   102  }
   103  
   104  func TestBroker(t *testing.T) {
   105  	config := NewConfig()
   106  	config.UserPresets = []string{"broker"}
   107  	config.BroadcastAdapter = "http"
   108  
   109  	require.Equal(t, []string{"broker"}, config.Presets())
   110  
   111  	err := config.LoadPresets(slog.Default())
   112  
   113  	require.NoError(t, err)
   114  
   115  	assert.Equal(t, "memory", config.BrokerAdapter)
   116  	assert.Equal(t, "http", config.BroadcastAdapter)
   117  	assert.Equal(t, "", config.PubSubAdapter)
   118  }
   119  
   120  func TestBrokerWhenRedisConfigured(t *testing.T) {
   121  	config := NewConfig()
   122  	config.UserPresets = []string{"broker"}
   123  	config.Redis.URL = "redis://localhost:6379/1"
   124  
   125  	require.Equal(t, []string{"broker"}, config.Presets())
   126  
   127  	err := config.LoadPresets(slog.Default())
   128  
   129  	require.NoError(t, err)
   130  
   131  	assert.Equal(t, "memory", config.BrokerAdapter)
   132  	assert.Equal(t, "http,redisx,redis", config.BroadcastAdapter)
   133  	assert.Equal(t, "redis", config.PubSubAdapter)
   134  }
   135  
   136  func TestBrokerWhenENATSConfigured(t *testing.T) {
   137  	config := NewConfig()
   138  	config.UserPresets = []string{"broker"}
   139  	config.EmbedNats = true
   140  
   141  	require.Equal(t, []string{"broker"}, config.Presets())
   142  
   143  	err := config.LoadPresets(slog.Default())
   144  
   145  	require.NoError(t, err)
   146  
   147  	assert.Equal(t, "nats", config.BrokerAdapter)
   148  	assert.Equal(t, "http,nats", config.BroadcastAdapter)
   149  	assert.Equal(t, "nats", config.PubSubAdapter)
   150  }
   151  
   152  func TestFlyWithBrokerPresets(t *testing.T) {
   153  	cleanupEnv := setEnv(
   154  		"FLY_APP_NAME", "any-test",
   155  		"FLY_REGION", "mag",
   156  		"FLY_ALLOC_ID", "1234",
   157  	)
   158  	defer cleanupEnv()
   159  
   160  	config := NewConfig()
   161  	config.UserPresets = []string{"fly", "broker"}
   162  	config.Port = 8989
   163  
   164  	require.Equal(t, []string{"fly", "broker"}, config.Presets())
   165  
   166  	err := config.LoadPresets(slog.Default())
   167  
   168  	require.NoError(t, err)
   169  
   170  	assert.Equal(t, "0.0.0.0", config.Host)
   171  	assert.Equal(t, 8989, config.Port)
   172  	assert.Equal(t, 8989, config.HTTPBroadcast.Port)
   173  	assert.Equal(t, true, config.EmbedNats)
   174  	assert.Equal(t, "nats", config.PubSubAdapter)
   175  	assert.Equal(t, "nats", config.BrokerAdapter)
   176  	assert.Equal(t, "http,nats", config.BroadcastAdapter)
   177  	assert.Equal(t, "nats://0.0.0.0:4222", config.EmbeddedNats.ServiceAddr)
   178  	assert.Equal(t, "nats://0.0.0.0:5222", config.EmbeddedNats.ClusterAddr)
   179  	assert.Equal(t, "any-test-mag-cluster", config.EmbeddedNats.ClusterName)
   180  	assert.Equal(t, []string{"nats://mag.any-test.internal:5222"}, config.EmbeddedNats.Routes)
   181  	assert.Equal(t, "localhost:50051", config.RPC.Host)
   182  }
   183  
   184  func TestOverrideSomePresetSettings(t *testing.T) {
   185  	cleanupEnv := setEnv(
   186  		"FLY_APP_NAME", "any-test",
   187  		"FLY_REGION", "mag",
   188  		"FLY_ALLOC_ID", "1234",
   189  	)
   190  	defer cleanupEnv()
   191  
   192  	config := NewConfig()
   193  
   194  	require.Equal(t, []string{"fly"}, config.Presets())
   195  
   196  	config.EmbeddedNats.ServiceAddr = "nats://0.0.0.0:1234"
   197  
   198  	err := config.LoadPresets(slog.Default())
   199  
   200  	require.NoError(t, err)
   201  
   202  	assert.Equal(t, "0.0.0.0", config.Host)
   203  	assert.Equal(t, "nats://0.0.0.0:1234", config.EmbeddedNats.ServiceAddr)
   204  }
   205  
   206  func TestExplicitOverImplicitPresets(t *testing.T) {
   207  	cleanupEnv := setEnv(
   208  		"FLY_APP_NAME", "any-test",
   209  		"FLY_REGION", "mag",
   210  		"FLY_ALLOC_ID", "1234",
   211  	)
   212  	defer cleanupEnv()
   213  
   214  	config := NewConfig()
   215  	config.UserPresets = []string{}
   216  
   217  	assert.Equal(t, []string{}, config.Presets())
   218  }
   219  
   220  func setEnv(pairs ...string) func() {
   221  	keys := []string{}
   222  
   223  	for i := 0; i < len(pairs); i += 2 {
   224  		keys = append(keys, pairs[i])
   225  		os.Setenv(pairs[i], pairs[i+1])
   226  	}
   227  
   228  	return func() {
   229  		unsetEnv(keys...)
   230  	}
   231  }
   232  
   233  func unsetEnv(keys ...string) {
   234  	for _, key := range keys {
   235  		os.Unsetenv(key)
   236  	}
   237  }