cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
config/configuration_test.go
179lines · modecode
| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | yaml "gopkg.in/yaml.v2" |
| 10 | ) |
| 11 | |
| 12 | func TestConfigFileSettings(t *testing.T) { |
| 13 | var ( |
| 14 | firstIngress = UnvalidatedIngressRule{ |
| 15 | Hostname: "tunnel1.example.com", |
| 16 | Path: "/id", |
| 17 | Service: "https://localhost:8000", |
| 18 | } |
| 19 | secondIngress = UnvalidatedIngressRule{ |
| 20 | Hostname: "*", |
| 21 | Path: "", |
| 22 | Service: "https://localhost:8001", |
| 23 | } |
| 24 | warpRouting = WarpRoutingConfig{ |
| 25 | Enabled: true, |
| 26 | } |
| 27 | ) |
| 28 | rawYAML := ` |
| 29 | tunnel: config-file-test |
| 30 | originRequest: |
| 31 | ipRules: |
| 32 | - prefix: "10.0.0.0/8" |
| 33 | ports: |
| 34 | - 80 |
| 35 | - 8080 |
| 36 | allow: false |
| 37 | - prefix: "fc00::/7" |
| 38 | ports: |
| 39 | - 443 |
| 40 | - 4443 |
| 41 | allow: true |
| 42 | ingress: |
| 43 | - hostname: tunnel1.example.com |
| 44 | path: /id |
| 45 | service: https://localhost:8000 |
| 46 | - hostname: "*" |
| 47 | service: https://localhost:8001 |
| 48 | warp-routing: |
| 49 | enabled: true |
| 50 | retries: 5 |
| 51 | grace-period: 30s |
| 52 | percentage: 3.14 |
| 53 | hostname: example.com |
| 54 | tag: |
| 55 | - test |
| 56 | - central-1 |
| 57 | counters: |
| 58 | - 123 |
| 59 | - 456 |
| 60 | ` |
| 61 | var config configFileSettings |
| 62 | err := yaml.Unmarshal([]byte(rawYAML), &config) |
| 63 | assert.NoError(t, err) |
| 64 | |
| 65 | assert.Equal(t, "config-file-test", config.TunnelID) |
| 66 | assert.Equal(t, firstIngress, config.Ingress[0]) |
| 67 | assert.Equal(t, secondIngress, config.Ingress[1]) |
| 68 | assert.Equal(t, warpRouting, config.WarpRouting) |
| 69 | privateV4 := "10.0.0.0/8" |
| 70 | privateV6 := "fc00::/7" |
| 71 | ipRules := []IngressIPRule{ |
| 72 | { |
| 73 | Prefix: &privateV4, |
| 74 | Ports: []int{80, 8080}, |
| 75 | Allow: false, |
| 76 | }, |
| 77 | { |
| 78 | Prefix: &privateV6, |
| 79 | Ports: []int{443, 4443}, |
| 80 | Allow: true, |
| 81 | }, |
| 82 | } |
| 83 | assert.Equal(t, ipRules, config.OriginRequest.IPRules) |
| 84 | |
| 85 | retries, err := config.Int("retries") |
| 86 | assert.NoError(t, err) |
| 87 | assert.Equal(t, 5, retries) |
| 88 | |
| 89 | gracePeriod, err := config.Duration("grace-period") |
| 90 | assert.NoError(t, err) |
| 91 | assert.Equal(t, time.Second*30, gracePeriod) |
| 92 | |
| 93 | percentage, err := config.Float64("percentage") |
| 94 | assert.NoError(t, err) |
| 95 | assert.Equal(t, 3.14, percentage) |
| 96 | |
| 97 | hostname, err := config.String("hostname") |
| 98 | assert.NoError(t, err) |
| 99 | assert.Equal(t, "example.com", hostname) |
| 100 | |
| 101 | tags, err := config.StringSlice("tag") |
| 102 | assert.NoError(t, err) |
| 103 | assert.Equal(t, "test", tags[0]) |
| 104 | assert.Equal(t, "central-1", tags[1]) |
| 105 | |
| 106 | counters, err := config.IntSlice("counters") |
| 107 | assert.NoError(t, err) |
| 108 | assert.Equal(t, 123, counters[0]) |
| 109 | assert.Equal(t, 456, counters[1]) |
| 110 | |
| 111 | } |
| 112 | |
| 113 | func TestUnmarshalOriginRequestConfig(t *testing.T) { |
| 114 | raw := []byte(` |
| 115 | { |
| 116 | "connectTimeout": 10000000000, |
| 117 | "tlsTimeout": 30000000000, |
| 118 | "tcpKeepAlive": 30000000000, |
| 119 | "noHappyEyeballs": true, |
| 120 | "keepAliveTimeout": 60000000000, |
| 121 | "keepAliveConnections": 10, |
| 122 | "httpHostHeader": "app.tunnel.com", |
| 123 | "originServerName": "app.tunnel.com", |
| 124 | "caPool": "/etc/capool", |
| 125 | "noTLSVerify": true, |
| 126 | "disableChunkedEncoding": true, |
| 127 | "bastionMode": true, |
| 128 | "proxyAddress": "127.0.0.3", |
| 129 | "proxyPort": 9000, |
| 130 | "proxyType": "socks", |
| 131 | "ipRules": [ |
| 132 | { |
| 133 | "prefix": "10.0.0.0/8", |
| 134 | "ports": [80, 8080], |
| 135 | "allow": false |
| 136 | }, |
| 137 | { |
| 138 | "prefix": "fc00::/7", |
| 139 | "ports": [443, 4443], |
| 140 | "allow": true |
| 141 | } |
| 142 | ] |
| 143 | } |
| 144 | `) |
| 145 | var config OriginRequestConfig |
| 146 | assert.NoError(t, json.Unmarshal(raw, &config)) |
| 147 | assert.Equal(t, time.Second*10, *config.ConnectTimeout) |
| 148 | assert.Equal(t, time.Second*30, *config.TLSTimeout) |
| 149 | assert.Equal(t, time.Second*30, *config.TCPKeepAlive) |
| 150 | assert.Equal(t, true, *config.NoHappyEyeballs) |
| 151 | assert.Equal(t, time.Second*60, *config.KeepAliveTimeout) |
| 152 | assert.Equal(t, 10, *config.KeepAliveConnections) |
| 153 | assert.Equal(t, "app.tunnel.com", *config.HTTPHostHeader) |
| 154 | assert.Equal(t, "app.tunnel.com", *config.OriginServerName) |
| 155 | assert.Equal(t, "/etc/capool", *config.CAPool) |
| 156 | assert.Equal(t, true, *config.NoTLSVerify) |
| 157 | assert.Equal(t, true, *config.DisableChunkedEncoding) |
| 158 | assert.Equal(t, true, *config.BastionMode) |
| 159 | assert.Equal(t, "127.0.0.3", *config.ProxyAddress) |
| 160 | assert.Equal(t, true, *config.NoTLSVerify) |
| 161 | assert.Equal(t, uint(9000), *config.ProxyPort) |
| 162 | assert.Equal(t, "socks", *config.ProxyType) |
| 163 | |
| 164 | privateV4 := "10.0.0.0/8" |
| 165 | privateV6 := "fc00::/7" |
| 166 | ipRules := []IngressIPRule{ |
| 167 | { |
| 168 | Prefix: &privateV4, |
| 169 | Ports: []int{80, 8080}, |
| 170 | Allow: false, |
| 171 | }, |
| 172 | { |
| 173 | Prefix: &privateV6, |
| 174 | Ports: []int{443, 4443}, |
| 175 | Allow: true, |
| 176 | }, |
| 177 | } |
| 178 | assert.Equal(t, ipRules, config.IPRules) |
| 179 | } |
| 180 | |