Migrate taildrop test to v2

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
Kristoffer Dalby 2022-10-23 14:13:22 +02:00
parent ae189c03ac
commit 018b1d68f2
No known key found for this signature in database
3 changed files with 189 additions and 39 deletions

View file

@ -34,6 +34,10 @@ type TailscaleInContainer struct {
pool *dockertest.Pool
container *dockertest.Resource
network *dockertest.Network
// "cache"
ips []netip.Addr
fqdn string
}
func New(
@ -104,6 +108,33 @@ func (t *TailscaleInContainer) Version() string {
return t.version
}
func (t *TailscaleInContainer) Execute(
command []string,
) (string, error) {
log.Println("command", command)
log.Printf("running command for %s\n", t.hostname)
stdout, stderr, err := dockertestutil.ExecuteCommand(
t.container,
command,
[]string{},
)
if err != nil {
log.Printf("command stderr: %s\n", stderr)
if strings.Contains(stderr, "NeedsLogin") {
return "", errTailscaleNotLoggedIn
}
return "", err
}
if stdout != "" {
log.Printf("command stdout: %s\n", stdout)
}
return stdout, nil
}
func (t *TailscaleInContainer) Up(
loginServer, authKey string,
) error {
@ -118,30 +149,19 @@ func (t *TailscaleInContainer) Up(
t.hostname,
}
log.Println("Join command:", command)
log.Printf("Running join command for %s\n", t.hostname)
stdout, stderr, err := dockertestutil.ExecuteCommand(
t.container,
command,
[]string{},
)
if err != nil {
log.Printf("tailscale join stderr: %s\n", stderr)
return err
if _, err := t.Execute(command); err != nil {
return fmt.Errorf("failed to join tailscale client: %w", err)
}
if stdout != "" {
log.Printf("tailscale join stdout: %s\n", stdout)
}
log.Printf("%s joined\n", t.hostname)
return nil
}
// TODO(kradalby): Make cached/lazy.
func (t *TailscaleInContainer) IPs() ([]netip.Addr, error) {
if t.ips != nil && len(t.ips) != 0 {
return t.ips, nil
}
ips := make([]netip.Addr, 0)
command := []string{
@ -149,19 +169,9 @@ func (t *TailscaleInContainer) IPs() ([]netip.Addr, error) {
"ip",
}
result, stderr, err := dockertestutil.ExecuteCommand(
t.container,
command,
[]string{},
)
result, err := t.Execute(command)
if err != nil {
log.Printf("failed commands stderr: %s\n", stderr)
if strings.Contains(stderr, "NeedsLogin") {
return []netip.Addr{}, errTailscaleNotLoggedIn
}
return []netip.Addr{}, err
return []netip.Addr{}, fmt.Errorf("failed to join tailscale client: %w", err)
}
for _, address := range strings.Split(result, "\n") {
@ -186,11 +196,7 @@ func (t *TailscaleInContainer) Status() (*ipnstate.Status, error) {
"--json",
}
result, _, err := dockertestutil.ExecuteCommand(
t.container,
command,
[]string{},
)
result, err := t.Execute(command)
if err != nil {
return nil, fmt.Errorf("failed to execute tailscale status command: %w", err)
}
@ -205,6 +211,10 @@ func (t *TailscaleInContainer) Status() (*ipnstate.Status, error) {
}
func (t *TailscaleInContainer) FQDN() (string, error) {
if t.fqdn != "" {
return t.fqdn, nil
}
status, err := t.Status()
if err != nil {
return "", fmt.Errorf("failed to get FQDN: %w", err)
@ -239,11 +249,7 @@ func (t *TailscaleInContainer) Ping(hostnameOrIP string) error {
hostnameOrIP,
}
result, _, err := dockertestutil.ExecuteCommand(
t.container,
command,
[]string{},
)
result, err := t.Execute(command)
if err != nil {
log.Printf(
"failed to run ping command from %s to %s, err: %s",