-
Notifications
You must be signed in to change notification settings - Fork 988
Show specific error messages on errors creating the tunnel. Fixes #972 #1302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
metadaddy
wants to merge
1
commit into
cloudflare:master
Choose a base branch
from
metadaddy:improve-messages-on-create-tunnel-errors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package tunnel | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"github.com/cloudflare/cloudflared/cmd/cloudflared/cliutil" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/urfave/cli/v2" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func makeContext(i int, serverUrl string) *cli.Context { | ||
flagSet := flag.NewFlagSet(fmt.Sprintf("test%d", i), flag.PanicOnError) | ||
flagSet.String("edge-ip-version", "", "") | ||
flagSet.String("protocol", "", "") | ||
flagSet.String("url", "", "") | ||
flagSet.String("quick-service", "", "") | ||
|
||
c := cli.NewContext(cli.NewApp(), flagSet, nil) | ||
_ = c.Set("edge-ip-version", "auto") | ||
_ = c.Set("protocol", "quic") | ||
_ = c.Set("url", "http://localhost:8080") | ||
_ = c.Set("quick-service", serverUrl) | ||
return c | ||
} | ||
|
||
// @noinspection SpellCheckingInspection | ||
func TestQuickTunnel(t *testing.T) { | ||
var tests = []struct { | ||
name string | ||
statusCode int | ||
response string | ||
wantErr bool | ||
expectedErr error | ||
}{ | ||
{ | ||
name: "200 OK response from server, valid response", | ||
statusCode: http.StatusOK, | ||
response: `{"success":true,"result":{"id":"0347c3ea-504b-47bc-8e2c-339961e6ea3e","name":"qt-not-a-real-name","hostname":"not-a-real-hostname.trycloudflare.com","account_tag":"not-an-account-tag","secret":"notreallyasecret"},"errors":[]}`, | ||
}, | ||
{ | ||
name: "200 OK response from server, bad tunnel ID", | ||
statusCode: http.StatusOK, | ||
response: `{"success":true,"result":{"id":"not-a-uuid","name":"qt-not-a-real-name","hostname":"not-a-real-hostname.trycloudflare.com","account_tag":"not-an-account-tag","secret":"notreallyasecret"},"errors":[]}`, | ||
wantErr: true, | ||
expectedErr: errors.New("failed to parse quick Tunnel ID: invalid UUID length: 10"), | ||
}, | ||
{ | ||
name: "200 OK response from server, bad JSON", | ||
statusCode: http.StatusOK, | ||
response: `This is not JSON!`, | ||
wantErr: true, | ||
expectedErr: errors.New("failed to unmarshal quick Tunnel: invalid character 'T' looking for beginning of value"), | ||
}, | ||
{ | ||
name: "429 Too Many Requests response from server", | ||
statusCode: http.StatusTooManyRequests, | ||
response: `error`, | ||
wantErr: true, | ||
expectedErr: errors.New("rate limit exceeded; wait a while and try again"), | ||
}, | ||
{ | ||
name: "400 Bad Request response from server", | ||
statusCode: http.StatusBadRequest, | ||
response: `error`, | ||
wantErr: true, | ||
expectedErr: errors.New("HTTP error 400"), | ||
}, | ||
} | ||
|
||
for i, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// Initialize tunnel subcommand | ||
bInfo := cliutil.GetBuildInfo("", "DEV") | ||
graceShutdownC := make(chan struct{}) | ||
Init(bInfo, graceShutdownC) | ||
|
||
// Create a test HTTP server to act in place of the Cloudflare service | ||
serverReceivedRequest := false | ||
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
// All requests should be to /tunnel | ||
assert.Equal(t, "/tunnel", req.URL.String()) | ||
|
||
serverReceivedRequest = true | ||
|
||
rw.WriteHeader(tt.statusCode) | ||
_, _ = rw.Write([]byte(tt.response)) | ||
})) | ||
defer server.Close() | ||
|
||
if !tt.wantErr { | ||
// Close the shutdown channel now so that the tunnel subcommand doesn't proceed to the quic negotiation | ||
close(graceShutdownC) | ||
} | ||
|
||
err := TunnelCommand(makeContext(i, server.URL)) | ||
|
||
if tt.wantErr { | ||
assert.Equal(t, tt.expectedErr.Error(), err.Error()) | ||
} else { | ||
assert.Nil(t, err) | ||
} | ||
assert.True(t, serverReceivedRequest) | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.