mirror of
https://github.com/jsiebens/ionscale.git
synced 2026-03-31 15:07:49 +01:00
feat: add command to set name of a machine
This commit is contained in:
@@ -85,6 +85,23 @@ func (s *Scenario) ExpireMachines(tailnetID uint64) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scenario) FindMachine(tailnetID uint64, name string) (uint64, error) {
|
||||
machines := s.ListMachines(tailnetID)
|
||||
|
||||
for _, m := range machines {
|
||||
if m.Name == name {
|
||||
return m.Id, nil
|
||||
}
|
||||
}
|
||||
return 0, fmt.Errorf("machine %s not found", name)
|
||||
}
|
||||
|
||||
func (s *Scenario) SetMachineName(machineID uint64, useOSHostname bool, name string) error {
|
||||
req := &api.SetMachineNameRequest{MachineId: machineID, UseOsHostname: useOSHostname, Name: name}
|
||||
_, err := s.ionscaleClient.SetMachineName(context.Background(), connect.NewRequest(req))
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Scenario) SetACLPolicy(tailnetID uint64, policy *ionscaleclt.ACLPolicy) {
|
||||
_, err := s.ionscaleClient.SetACLPolicy(context.Background(), connect.NewRequest(&api.SetACLPolicyRequest{TailnetId: tailnetID, Policy: policy.Marshal()}))
|
||||
require.NoError(s.t, err)
|
||||
@@ -136,6 +153,10 @@ type TailscaleNodeConfig struct {
|
||||
|
||||
type TailscaleNodeOpt = func(*TailscaleNodeConfig)
|
||||
|
||||
func RandomName() string {
|
||||
return petname.Generate(3, "-")
|
||||
}
|
||||
|
||||
func WithName(name string) TailscaleNodeOpt {
|
||||
return func(config *TailscaleNodeConfig) {
|
||||
config.Hostname = name
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"github.com/jsiebens/ionscale/tests/sc"
|
||||
"github.com/jsiebens/ionscale/tests/tsn"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewHostnameShouldPropagateToPeersWhenSet(t *testing.T) {
|
||||
sc.Run(t, func(s *sc.Scenario) {
|
||||
tailnet := s.CreateTailnet()
|
||||
key := s.CreateAuthKey(tailnet.Id, true)
|
||||
|
||||
initialName := sc.RandomName()
|
||||
|
||||
nodeA := s.NewTailscaleNode()
|
||||
nodeB := s.NewTailscaleNode(sc.WithName(initialName))
|
||||
|
||||
require.NoError(t, nodeA.Up(key))
|
||||
require.NoError(t, nodeB.Up(key))
|
||||
|
||||
require.NoError(t, nodeA.WaitFor(tsn.HasPeer(initialName)))
|
||||
|
||||
newName := sc.RandomName()
|
||||
|
||||
require.NoError(t, nodeB.SetHostname(newName))
|
||||
|
||||
require.NoError(t, nodeA.WaitFor(tsn.HasPeer(newName)))
|
||||
})
|
||||
}
|
||||
|
||||
func TestSetHostname(t *testing.T) {
|
||||
sc.Run(t, func(s *sc.Scenario) {
|
||||
tailnet := s.CreateTailnet()
|
||||
key := s.CreateAuthKey(tailnet.Id, true)
|
||||
|
||||
initialName := sc.RandomName()
|
||||
|
||||
nodeA := s.NewTailscaleNode()
|
||||
nodeB := s.NewTailscaleNode(sc.WithName(initialName))
|
||||
|
||||
require.NoError(t, nodeA.Up(key))
|
||||
require.NoError(t, nodeB.Up(key))
|
||||
|
||||
require.NoError(t, nodeA.WaitFor(tsn.HasPeer(initialName)))
|
||||
|
||||
mid, err := s.FindMachine(tailnet.Id, initialName)
|
||||
require.NoError(t, err)
|
||||
|
||||
newName := sc.RandomName()
|
||||
|
||||
require.NoError(t, s.SetMachineName(mid, false, newName))
|
||||
require.NoError(t, nodeA.WaitFor(tsn.HasPeer(newName)))
|
||||
|
||||
require.NoError(t, s.SetMachineName(mid, true, ""))
|
||||
require.NoError(t, nodeA.WaitFor(tsn.HasPeer(initialName)))
|
||||
})
|
||||
}
|
||||
|
||||
func TestSetHostnameWhenNameAlreadyInUse(t *testing.T) {
|
||||
sc.Run(t, func(s *sc.Scenario) {
|
||||
tailnet := s.CreateTailnet()
|
||||
key := s.CreateAuthKey(tailnet.Id, true)
|
||||
|
||||
nodeA := s.NewTailscaleNode(sc.WithName("node-a"))
|
||||
nodeB := s.NewTailscaleNode(sc.WithName("node-b"))
|
||||
|
||||
require.NoError(t, nodeA.Up(key))
|
||||
require.NoError(t, nodeB.Up(key))
|
||||
|
||||
require.NoError(t, nodeA.WaitFor(tsn.PeerCount(1)))
|
||||
require.NoError(t, nodeB.WaitFor(tsn.PeerCount(1)))
|
||||
|
||||
mida, err := s.FindMachine(tailnet.Id, "node-a")
|
||||
require.NoError(t, err)
|
||||
|
||||
midb, err := s.FindMachine(tailnet.Id, "node-b")
|
||||
require.NoError(t, err)
|
||||
|
||||
newName := sc.RandomName()
|
||||
|
||||
require.NoError(t, s.SetMachineName(mida, false, newName))
|
||||
require.NoError(t, nodeB.WaitFor(tsn.HasPeer(newName)))
|
||||
|
||||
err = s.SetMachineName(midb, false, newName)
|
||||
require.ErrorContains(t, err, "machine name already in use")
|
||||
})
|
||||
}
|
||||
@@ -77,6 +77,17 @@ func HasExpiredPeer(name string) Condition {
|
||||
}
|
||||
}
|
||||
|
||||
func HasPeer(name string) Condition {
|
||||
return func(status *ipnstate.Status) bool {
|
||||
for _, peer := range status.Peer {
|
||||
if strings.HasPrefix(peer.DNSName, name) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func HasCapability(capability tailcfg.NodeCapability) Condition {
|
||||
return func(status *ipnstate.Status) bool {
|
||||
self := status.Self
|
||||
|
||||
@@ -148,6 +148,14 @@ func (t *TailscaleNode) Ping(target string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TailscaleNode) SetHostname(hostname string) error {
|
||||
_, _, err := t.execTailscaleCmd("set", "--hostname", hostname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TailscaleNode) NetCheck() (*netcheck.Report, error) {
|
||||
result, _, err := t.execTailscaleCmd("netcheck", "--format=json")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user