mirror of
https://github.com/juanfont/headscale.git
synced 2025-08-19 13:48:20 +02:00
move to not trigger test gen checker
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
parent
3ea7ca3709
commit
b4e4b79a71
@ -1,6 +1,13 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import "tailscale.com/util/cmpver"
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"tailscale.com/util/cmpver"
|
||||||
|
)
|
||||||
|
|
||||||
func TailscaleVersionNewerOrEqual(minimum, toCheck string) bool {
|
func TailscaleVersionNewerOrEqual(minimum, toCheck string) bool {
|
||||||
if cmpver.Compare(minimum, toCheck) <= 0 ||
|
if cmpver.Compare(minimum, toCheck) <= 0 ||
|
||||||
@ -11,3 +18,31 @@ func TailscaleVersionNewerOrEqual(minimum, toCheck string) bool {
|
|||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseLoginURLFromCLILogin parses the output of the tailscale up command to extract the login URL.
|
||||||
|
// It returns an error if not exactly one URL is found.
|
||||||
|
func ParseLoginURLFromCLILogin(output string) (*url.URL, error) {
|
||||||
|
lines := strings.Split(output, "\n")
|
||||||
|
var urlStr string
|
||||||
|
|
||||||
|
for _, line := range lines {
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
if strings.HasPrefix(line, "http://") || strings.HasPrefix(line, "https://") {
|
||||||
|
if urlStr != "" {
|
||||||
|
return nil, fmt.Errorf("multiple URLs found: %s and %s", urlStr, line)
|
||||||
|
}
|
||||||
|
urlStr = line
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if urlStr == "" {
|
||||||
|
return nil, errors.New("no URL found")
|
||||||
|
}
|
||||||
|
|
||||||
|
loginURL, err := url.Parse(urlStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse URL: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return loginURL, nil
|
||||||
|
}
|
||||||
|
@ -93,3 +93,88 @@ func TestTailscaleVersionNewerOrEqual(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseLoginURLFromCLILogin(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
output string
|
||||||
|
wantURL string
|
||||||
|
wantErr string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "valid https URL",
|
||||||
|
output: `
|
||||||
|
To authenticate, visit:
|
||||||
|
|
||||||
|
https://headscale.example.com/register/3oYCOZYA2zZmGB4PQ7aHBaMi
|
||||||
|
|
||||||
|
Success.`,
|
||||||
|
wantURL: "https://headscale.example.com/register/3oYCOZYA2zZmGB4PQ7aHBaMi",
|
||||||
|
wantErr: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "valid http URL",
|
||||||
|
output: `
|
||||||
|
To authenticate, visit:
|
||||||
|
|
||||||
|
http://headscale.example.com/register/3oYCOZYA2zZmGB4PQ7aHBaMi
|
||||||
|
|
||||||
|
Success.`,
|
||||||
|
wantURL: "http://headscale.example.com/register/3oYCOZYA2zZmGB4PQ7aHBaMi",
|
||||||
|
wantErr: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "no URL",
|
||||||
|
output: `
|
||||||
|
To authenticate, visit:
|
||||||
|
|
||||||
|
Success.`,
|
||||||
|
wantURL: "",
|
||||||
|
wantErr: "no URL found",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multiple URLs",
|
||||||
|
output: `
|
||||||
|
To authenticate, visit:
|
||||||
|
|
||||||
|
https://headscale.example.com/register/3oYCOZYA2zZmGB4PQ7aHBaMi
|
||||||
|
|
||||||
|
To authenticate, visit:
|
||||||
|
|
||||||
|
http://headscale.example.com/register/dv1l2k5FackOYl-7-V3mSd_E
|
||||||
|
|
||||||
|
Success.`,
|
||||||
|
wantURL: "",
|
||||||
|
wantErr: "multiple URLs found: https://headscale.example.com/register/3oYCOZYA2zZmGB4PQ7aHBaMi and http://headscale.example.com/register/dv1l2k5FackOYl-7-V3mSd_E",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid URL",
|
||||||
|
output: `
|
||||||
|
To authenticate, visit:
|
||||||
|
|
||||||
|
invalid-url
|
||||||
|
|
||||||
|
Success.`,
|
||||||
|
wantURL: "",
|
||||||
|
wantErr: "no URL found",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
gotURL, err := ParseLoginURLFromCLILogin(tt.output)
|
||||||
|
if tt.wantErr != "" {
|
||||||
|
if err == nil || err.Error() != tt.wantErr {
|
||||||
|
t.Errorf("ParseLoginURLFromCLILogin() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("ParseLoginURLFromCLILogin() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
if gotURL.String() != tt.wantURL {
|
||||||
|
t.Errorf("ParseLoginURLFromCLILogin() = %v, want %v", gotURL, tt.wantURL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -503,7 +503,7 @@ func (t *TailscaleInContainer) LoginWithURL(
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
loginURL, err = parseLoginURL(stdout + stderr)
|
loginURL, err = util.ParseLoginURLFromCLILogin(stdout + stderr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -1158,31 +1158,3 @@ func (t *TailscaleInContainer) ReadFile(path string) ([]byte, error) {
|
|||||||
|
|
||||||
return out.Bytes(), nil
|
return out.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseLoginURL parses the output of the tailscale up command to extract the login URL.
|
|
||||||
// It returns an error if not exactly one URL is found.
|
|
||||||
func parseLoginURL(output string) (*url.URL, error) {
|
|
||||||
lines := strings.Split(output, "\n")
|
|
||||||
var urlStr string
|
|
||||||
|
|
||||||
for _, line := range lines {
|
|
||||||
line = strings.TrimSpace(line)
|
|
||||||
if strings.HasPrefix(line, "http://") || strings.HasPrefix(line, "https://") {
|
|
||||||
if urlStr != "" {
|
|
||||||
return nil, fmt.Errorf("multiple URLs found: %s and %s", urlStr, line)
|
|
||||||
}
|
|
||||||
urlStr = line
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if urlStr == "" {
|
|
||||||
return nil, errors.New("no URL found")
|
|
||||||
}
|
|
||||||
|
|
||||||
loginURL, err := url.Parse(urlStr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to parse URL: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return loginURL, nil
|
|
||||||
}
|
|
||||||
|
@ -1,88 +0,0 @@
|
|||||||
package tsic
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestParseLoginURL(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
output string
|
|
||||||
wantURL string
|
|
||||||
wantErr string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "valid https URL",
|
|
||||||
output: `
|
|
||||||
To authenticate, visit:
|
|
||||||
|
|
||||||
https://headscale.example.com/register/3oYCOZYA2zZmGB4PQ7aHBaMi
|
|
||||||
|
|
||||||
Success.`,
|
|
||||||
wantURL: "https://headscale.example.com/register/3oYCOZYA2zZmGB4PQ7aHBaMi",
|
|
||||||
wantErr: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "valid http URL",
|
|
||||||
output: `
|
|
||||||
To authenticate, visit:
|
|
||||||
|
|
||||||
http://headscale.example.com/register/3oYCOZYA2zZmGB4PQ7aHBaMi
|
|
||||||
|
|
||||||
Success.`,
|
|
||||||
wantURL: "http://headscale.example.com/register/3oYCOZYA2zZmGB4PQ7aHBaMi",
|
|
||||||
wantErr: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "no URL",
|
|
||||||
output: `
|
|
||||||
To authenticate, visit:
|
|
||||||
|
|
||||||
Success.`,
|
|
||||||
wantURL: "",
|
|
||||||
wantErr: "no URL found",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "multiple URLs",
|
|
||||||
output: `
|
|
||||||
To authenticate, visit:
|
|
||||||
|
|
||||||
https://headscale.example.com/register/3oYCOZYA2zZmGB4PQ7aHBaMi
|
|
||||||
|
|
||||||
To authenticate, visit:
|
|
||||||
|
|
||||||
http://headscale.example.com/register/dv1l2k5FackOYl-7-V3mSd_E
|
|
||||||
|
|
||||||
Success.`,
|
|
||||||
wantURL: "",
|
|
||||||
wantErr: "multiple URLs found: https://headscale.example.com/register/3oYCOZYA2zZmGB4PQ7aHBaMi and http://headscale.example.com/register/dv1l2k5FackOYl-7-V3mSd_E",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "invalid URL",
|
|
||||||
output: `
|
|
||||||
To authenticate, visit:
|
|
||||||
|
|
||||||
invalid-url
|
|
||||||
|
|
||||||
Success.`,
|
|
||||||
wantURL: "",
|
|
||||||
wantErr: "no URL found",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
gotURL, err := parseLoginURL(tt.output)
|
|
||||||
if tt.wantErr != "" {
|
|
||||||
if err == nil || err.Error() != tt.wantErr {
|
|
||||||
t.Errorf("parseLoginURL() error = %v, wantErr %v", err, tt.wantErr)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("parseLoginURL() error = %v, wantErr %v", err, tt.wantErr)
|
|
||||||
}
|
|
||||||
if gotURL.String() != tt.wantURL {
|
|
||||||
t.Errorf("parseLoginURL() = %v, want %v", gotURL, tt.wantURL)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user