config_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package orchestration
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/require"
  7. "github.com/cloudflare/cloudflared/config"
  8. "github.com/cloudflare/cloudflared/ingress"
  9. )
  10. // TestNewLocalConfig_MarshalJSON tests that we are able to converte a compiled and validated config back
  11. // into an "unvalidated" format which is compatible with Remote Managed configurations.
  12. func TestNewLocalConfig_MarshalJSON(t *testing.T) {
  13. rawConfig := []byte(`
  14. {
  15. "originRequest": {
  16. "connectTimeout": 160,
  17. "httpHostHeader": "default"
  18. },
  19. "ingress": [
  20. {
  21. "hostname": "tun.example.com",
  22. "service": "https://localhost:8000"
  23. },
  24. {
  25. "hostname": "*",
  26. "service": "https://localhost:8001",
  27. "originRequest": {
  28. "connectTimeout": 121,
  29. "tlsTimeout": 2,
  30. "noHappyEyeballs": false,
  31. "tcpKeepAlive": 2,
  32. "keepAliveConnections": 2,
  33. "keepAliveTimeout": 2,
  34. "httpHostHeader": "def",
  35. "originServerName": "b2",
  36. "caPool": "/tmp/path1",
  37. "noTLSVerify": false,
  38. "disableChunkedEncoding": false,
  39. "bastionMode": false,
  40. "proxyAddress": "interface",
  41. "proxyPort": 200,
  42. "proxyType": "",
  43. "ipRules": [
  44. {
  45. "prefix": "10.0.0.0/16",
  46. "ports": [3000, 3030],
  47. "allow": false
  48. },
  49. {
  50. "prefix": "192.16.0.0/24",
  51. "ports": [5000, 5050],
  52. "allow": true
  53. }
  54. ]
  55. }
  56. }
  57. ],
  58. "warp-routing": {
  59. "connectTimeout": 1
  60. }
  61. }
  62. `)
  63. var expectedConfig ingress.RemoteConfig
  64. err := json.Unmarshal(rawConfig, &expectedConfig)
  65. require.NoError(t, err)
  66. c := &newLocalConfig{
  67. RemoteConfig: expectedConfig,
  68. ConfigurationFlags: nil,
  69. }
  70. jsonSerde, err := json.Marshal(c)
  71. require.NoError(t, err)
  72. var remoteConfig ingress.RemoteConfig
  73. err = json.Unmarshal(jsonSerde, &remoteConfig)
  74. require.NoError(t, err)
  75. require.Equal(t, remoteConfig.WarpRouting, ingress.WarpRoutingConfig{
  76. ConnectTimeout: config.CustomDuration{
  77. Duration: time.Second,
  78. },
  79. TCPKeepAlive: config.CustomDuration{
  80. Duration: 30 * time.Second, // default value is 30 seconds
  81. },
  82. })
  83. require.Equal(t, remoteConfig.Ingress.Rules, expectedConfig.Ingress.Rules)
  84. }