mirror of
https://github.com/jsiebens/ionscale.git
synced 2026-03-31 15:07:49 +01:00
chore: use require and asserts
This commit is contained in:
@@ -56,11 +56,11 @@ func TestNodeWithSameHostname(t *testing.T) {
|
||||
|
||||
tsNode := s.NewTailscaleNode("test")
|
||||
|
||||
_ = tsNode.Up(authKey)
|
||||
tsNode.Up(authKey)
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
tc := s.NewTailscaleNode("test")
|
||||
_ = tc.Up(authKey)
|
||||
tc.Up(authKey)
|
||||
}
|
||||
|
||||
machines := make(map[string]bool)
|
||||
|
||||
+10
-22
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/ory/dockertest/v3"
|
||||
"github.com/stretchr/testify/require"
|
||||
"strings"
|
||||
"tailscale.com/ipn/ipnstate"
|
||||
"testing"
|
||||
@@ -12,7 +13,7 @@ import (
|
||||
|
||||
type TailscaleNode interface {
|
||||
Hostname() string
|
||||
Up(authkey string) ipnstate.Status
|
||||
Up(authkey string)
|
||||
IPv4() string
|
||||
IPv6() string
|
||||
Ping(target string)
|
||||
@@ -30,9 +31,9 @@ func (t *tailscaleNode) Hostname() string {
|
||||
return t.hostname
|
||||
}
|
||||
|
||||
func (t *tailscaleNode) Up(authkey string) ipnstate.Status {
|
||||
func (t *tailscaleNode) Up(authkey string) {
|
||||
t.mustExecTailscaleCmd("up", "--login-server", t.loginServer, "--authkey", authkey)
|
||||
return t.waitForReady()
|
||||
t.waitForReady()
|
||||
}
|
||||
|
||||
func (t *tailscaleNode) IPv4() string {
|
||||
@@ -61,9 +62,7 @@ func (t *tailscaleNode) waitForReady() ipnstate.Status {
|
||||
|
||||
return fmt.Errorf("not connected")
|
||||
})
|
||||
if err != nil {
|
||||
t.t.Fatal(err)
|
||||
}
|
||||
require.NoError(t.t, err)
|
||||
return status
|
||||
}
|
||||
|
||||
@@ -85,9 +84,7 @@ func (t *tailscaleNode) WaitForPeers(expected int) {
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.t.Fatal(err)
|
||||
}
|
||||
require.NoError(t.t, err)
|
||||
}
|
||||
|
||||
func (t *tailscaleNode) WaitFor(check func(status *ipnstate.Status) bool) {
|
||||
@@ -108,20 +105,13 @@ func (t *tailscaleNode) WaitFor(check func(status *ipnstate.Status) bool) {
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.t.Fatal(err)
|
||||
}
|
||||
require.NoError(t.t, err)
|
||||
}
|
||||
|
||||
func (t *tailscaleNode) Ping(target string) {
|
||||
result, err := t.execTailscaleCmd("ping", "--timeout=1s", "--c=10", "--until-direct=true", target)
|
||||
if err != nil {
|
||||
t.t.Fatal(err)
|
||||
}
|
||||
|
||||
if !strings.Contains(result, "pong") && !strings.Contains(result, "is local") {
|
||||
t.t.Fatal("ping failed")
|
||||
}
|
||||
require.NoError(t.t, err)
|
||||
require.True(t.t, strings.Contains(result, "pong") || strings.Contains(result, "is local"), "ping failed")
|
||||
}
|
||||
|
||||
func (t *tailscaleNode) execTailscaleCmd(cmd ...string) (string, error) {
|
||||
@@ -132,9 +122,7 @@ func (t *tailscaleNode) execTailscaleCmd(cmd ...string) (string, error) {
|
||||
func (t *tailscaleNode) mustExecTailscaleCmd(cmd ...string) string {
|
||||
i := append([]string{"/app/tailscale", "--socket=/tmp/tailscaled.sock"}, cmd...)
|
||||
s, err := execCmd(t.resource, i...)
|
||||
if err != nil {
|
||||
t.t.Fatal(err)
|
||||
}
|
||||
require.NoError(t.t, err)
|
||||
return s
|
||||
}
|
||||
|
||||
|
||||
+15
-39
@@ -9,6 +9,7 @@ import (
|
||||
ionscaleconnect "github.com/jsiebens/ionscale/pkg/gen/ionscale/v1/ionscalev1connect"
|
||||
"github.com/ory/dockertest/v3"
|
||||
"github.com/ory/dockertest/v3/docker"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/types/known/durationpb"
|
||||
"io"
|
||||
"log"
|
||||
@@ -49,33 +50,25 @@ type scenario struct {
|
||||
|
||||
func (s *scenario) CreateTailnet(name string) *api.Tailnet {
|
||||
createTailnetResponse, err := s.client.CreateTailnet(context.Background(), connect.NewRequest(&api.CreateTailnetRequest{Name: name}))
|
||||
if err != nil {
|
||||
s.t.Fatal(err)
|
||||
}
|
||||
require.NoError(s.t, err)
|
||||
return createTailnetResponse.Msg.GetTailnet()
|
||||
}
|
||||
|
||||
func (s *scenario) CreateAuthKey(tailnetID uint64, ephemeral bool) string {
|
||||
key, err := s.client.CreateAuthKey(context.Background(), connect.NewRequest(&api.CreateAuthKeyRequest{TailnetId: tailnetID, Ephemeral: ephemeral, Tags: []string{"tag:test"}, Expiry: durationpb.New(60 * time.Minute)}))
|
||||
if err != nil {
|
||||
s.t.Fatal(err)
|
||||
}
|
||||
require.NoError(s.t, err)
|
||||
return key.Msg.Value
|
||||
}
|
||||
|
||||
func (s *scenario) ListMachines(tailnetID uint64) []*api.Machine {
|
||||
machines, err := s.client.ListMachines(context.Background(), connect.NewRequest(&api.ListMachinesRequest{TailnetId: tailnetID}))
|
||||
if err != nil {
|
||||
s.t.Fatal(err)
|
||||
}
|
||||
require.NoError(s.t, err)
|
||||
return machines.Msg.Machines
|
||||
}
|
||||
|
||||
func (s *scenario) SetAclPolicy(tailnetID uint64, policy *api.ACLPolicy) {
|
||||
_, err := s.client.SetACLPolicy(context.Background(), connect.NewRequest(&api.SetACLPolicyRequest{TailnetId: tailnetID, Policy: policy}))
|
||||
if err != nil {
|
||||
s.t.Fatal(err)
|
||||
}
|
||||
require.NoError(s.t, err)
|
||||
}
|
||||
|
||||
func (s *scenario) NewTailscaleNode(hostname string) TailscaleNode {
|
||||
@@ -93,14 +86,10 @@ func (s *scenario) NewTailscaleNode(hostname string) TailscaleNode {
|
||||
tailscaleOptions,
|
||||
restartPolicy,
|
||||
)
|
||||
if err != nil {
|
||||
s.t.Fatal(err)
|
||||
}
|
||||
require.NoError(s.t, err)
|
||||
|
||||
err = s.pool.Retry(portCheck(resource.GetPort("1055/tcp")))
|
||||
if err != nil {
|
||||
s.t.Fatal(err)
|
||||
}
|
||||
require.NoError(s.t, err)
|
||||
|
||||
s.resources = append(s.resources, resource)
|
||||
|
||||
@@ -143,19 +132,14 @@ func Run(t *testing.T, f func(s Scenario)) {
|
||||
s.network = nil
|
||||
}()
|
||||
|
||||
if s.pool, err = dockertest.NewPool(""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.pool, err = dockertest.NewPool("")
|
||||
require.NoError(t, err)
|
||||
|
||||
s.network, err = pool.CreateNetwork("ionscale-test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(s.t, err)
|
||||
|
||||
currentPath, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(s.t, err)
|
||||
|
||||
ionscale := &dockertest.RunOptions{
|
||||
Hostname: "ionscale",
|
||||
@@ -169,26 +153,18 @@ func Run(t *testing.T, f func(s Scenario)) {
|
||||
}
|
||||
|
||||
s.ionscale, err = pool.RunWithOptions(ionscale, restartPolicy)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(s.t, err)
|
||||
|
||||
port := s.ionscale.GetPort("8080/tcp")
|
||||
|
||||
err = pool.Retry(httpCheck(port, "/key"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(s.t, err)
|
||||
|
||||
auth, err := ionscaleclt.LoadClientAuth("804ecd57365342254ce6647da5c249e85c10a0e51e74856bfdf292a2136b4249")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(s.t, err)
|
||||
|
||||
s.client, err = ionscaleclt.NewClient(auth, fmt.Sprintf("http://localhost:%s", port), true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(s.t, err)
|
||||
|
||||
f(s)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user