Use new machine types in tests

This commit is contained in:
Kristoffer Dalby 2022-03-01 16:34:35 +00:00
parent 6477e6a583
commit 49cd761bf6
4 changed files with 87 additions and 75 deletions

View file

@ -6,7 +6,6 @@ import (
"testing" "testing"
"gopkg.in/check.v1" "gopkg.in/check.v1"
"gorm.io/datatypes"
"inet.af/netaddr" "inet.af/netaddr"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
) )
@ -108,9 +107,12 @@ func (s *Suite) TestValidExpandTagOwnersInUsers(c *check.C) {
_, err = app.GetMachine("user1", "testmachine") _, err = app.GetMachine("user1", "testmachine")
c.Assert(err, check.NotNil) c.Assert(err, check.NotNil)
hostInfo := []byte( hostInfo := tailcfg.Hostinfo{
"{\"OS\":\"centos\",\"Hostname\":\"testmachine\",\"RequestTags\":[\"tag:test\"]}", OS: "centos",
) Hostname: "testmachine",
RequestTags: []string{"tag:test"},
}
machine := Machine{ machine := Machine{
ID: 0, ID: 0,
MachineKey: "foo", MachineKey: "foo",
@ -122,7 +124,7 @@ func (s *Suite) TestValidExpandTagOwnersInUsers(c *check.C) {
Registered: true, Registered: true,
RegisterMethod: RegisterMethodAuthKey, RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID), AuthKeyID: uint(pak.ID),
HostInfo: datatypes.JSON(hostInfo), HostInfo: HostInfo(hostInfo),
} }
app.db.Save(&machine) app.db.Save(&machine)
@ -152,9 +154,12 @@ func (s *Suite) TestValidExpandTagOwnersInPorts(c *check.C) {
_, err = app.GetMachine("user1", "testmachine") _, err = app.GetMachine("user1", "testmachine")
c.Assert(err, check.NotNil) c.Assert(err, check.NotNil)
hostInfo := []byte( hostInfo := tailcfg.Hostinfo{
"{\"OS\":\"centos\",\"Hostname\":\"testmachine\",\"RequestTags\":[\"tag:test\"]}", OS: "centos",
) Hostname: "testmachine",
RequestTags: []string{"tag:test"},
}
machine := Machine{ machine := Machine{
ID: 1, ID: 1,
MachineKey: "12345", MachineKey: "12345",
@ -166,7 +171,7 @@ func (s *Suite) TestValidExpandTagOwnersInPorts(c *check.C) {
Registered: true, Registered: true,
RegisterMethod: RegisterMethodAuthKey, RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID), AuthKeyID: uint(pak.ID),
HostInfo: datatypes.JSON(hostInfo), HostInfo: HostInfo(hostInfo),
} }
app.db.Save(&machine) app.db.Save(&machine)
@ -196,9 +201,12 @@ func (s *Suite) TestInvalidTagValidNamespace(c *check.C) {
_, err = app.GetMachine("user1", "testmachine") _, err = app.GetMachine("user1", "testmachine")
c.Assert(err, check.NotNil) c.Assert(err, check.NotNil)
hostInfo := []byte( hostInfo := tailcfg.Hostinfo{
"{\"OS\":\"centos\",\"Hostname\":\"testmachine\",\"RequestTags\":[\"tag:foo\"]}", OS: "centos",
) Hostname: "testmachine",
RequestTags: []string{"tag:foo"},
}
machine := Machine{ machine := Machine{
ID: 1, ID: 1,
MachineKey: "12345", MachineKey: "12345",
@ -210,7 +218,7 @@ func (s *Suite) TestInvalidTagValidNamespace(c *check.C) {
Registered: true, Registered: true,
RegisterMethod: RegisterMethodAuthKey, RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID), AuthKeyID: uint(pak.ID),
HostInfo: datatypes.JSON(hostInfo), HostInfo: HostInfo(hostInfo),
} }
app.db.Save(&machine) app.db.Save(&machine)
@ -239,9 +247,12 @@ func (s *Suite) TestValidTagInvalidNamespace(c *check.C) {
_, err = app.GetMachine("user1", "webserver") _, err = app.GetMachine("user1", "webserver")
c.Assert(err, check.NotNil) c.Assert(err, check.NotNil)
hostInfo := []byte( hostInfo := tailcfg.Hostinfo{
"{\"OS\":\"centos\",\"Hostname\":\"webserver\",\"RequestTags\":[\"tag:webapp\"]}", OS: "centos",
) Hostname: "webserver",
RequestTags: []string{"tag:webapp"},
}
machine := Machine{ machine := Machine{
ID: 1, ID: 1,
MachineKey: "12345", MachineKey: "12345",
@ -253,11 +264,14 @@ func (s *Suite) TestValidTagInvalidNamespace(c *check.C) {
Registered: true, Registered: true,
RegisterMethod: RegisterMethodAuthKey, RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID), AuthKeyID: uint(pak.ID),
HostInfo: datatypes.JSON(hostInfo), HostInfo: HostInfo(hostInfo),
} }
app.db.Save(&machine) app.db.Save(&machine)
_, err = app.GetMachine("user1", "user") _, err = app.GetMachine("user1", "user")
hostInfo = []byte("{\"OS\":\"debian\",\"Hostname\":\"user\"}") hostInfo2 := tailcfg.Hostinfo{
OS: "debian",
Hostname: "Hostname",
}
c.Assert(err, check.NotNil) c.Assert(err, check.NotNil)
machine = Machine{ machine = Machine{
ID: 2, ID: 2,
@ -270,7 +284,7 @@ func (s *Suite) TestValidTagInvalidNamespace(c *check.C) {
Registered: true, Registered: true,
RegisterMethod: RegisterMethodAuthKey, RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID), AuthKeyID: uint(pak.ID),
HostInfo: datatypes.JSON(hostInfo), HostInfo: HostInfo(hostInfo2),
} }
app.db.Save(&machine) app.db.Save(&machine)
@ -872,18 +886,22 @@ func Test_expandAlias(t *testing.T) {
netaddr.MustParseIP("100.64.0.1"), netaddr.MustParseIP("100.64.0.1"),
}, },
Namespace: Namespace{Name: "joe"}, Namespace: Namespace{Name: "joe"},
HostInfo: []byte( HostInfo: HostInfo{
"{\"OS\":\"centos\",\"Hostname\":\"foo\",\"RequestTags\":[\"tag:hr-webserver\"]}", OS: "centos",
), Hostname: "foo",
RequestTags: []string{"tag:hr-webserver"},
},
}, },
{ {
IPAddresses: MachineAddresses{ IPAddresses: MachineAddresses{
netaddr.MustParseIP("100.64.0.2"), netaddr.MustParseIP("100.64.0.2"),
}, },
Namespace: Namespace{Name: "joe"}, Namespace: Namespace{Name: "joe"},
HostInfo: []byte( HostInfo: HostInfo{
"{\"OS\":\"centos\",\"Hostname\":\"foo\",\"RequestTags\":[\"tag:hr-webserver\"]}", OS: "centos",
), Hostname: "foo",
RequestTags: []string{"tag:hr-webserver"},
},
}, },
{ {
IPAddresses: MachineAddresses{ IPAddresses: MachineAddresses{
@ -955,18 +973,22 @@ func Test_expandAlias(t *testing.T) {
netaddr.MustParseIP("100.64.0.1"), netaddr.MustParseIP("100.64.0.1"),
}, },
Namespace: Namespace{Name: "joe"}, Namespace: Namespace{Name: "joe"},
HostInfo: []byte( HostInfo: HostInfo{
"{\"OS\":\"centos\",\"Hostname\":\"foo\",\"RequestTags\":[\"tag:accountant-webserver\"]}", OS: "centos",
), Hostname: "foo",
RequestTags: []string{"tag:accountant-webserver"},
},
}, },
{ {
IPAddresses: MachineAddresses{ IPAddresses: MachineAddresses{
netaddr.MustParseIP("100.64.0.2"), netaddr.MustParseIP("100.64.0.2"),
}, },
Namespace: Namespace{Name: "joe"}, Namespace: Namespace{Name: "joe"},
HostInfo: []byte( HostInfo: HostInfo{
"{\"OS\":\"centos\",\"Hostname\":\"foo\",\"RequestTags\":[\"tag:accountant-webserver\"]}", OS: "centos",
), Hostname: "foo",
RequestTags: []string{"tag:accountant-webserver"},
},
}, },
{ {
IPAddresses: MachineAddresses{ IPAddresses: MachineAddresses{
@ -1032,18 +1054,22 @@ func Test_excludeCorrectlyTaggedNodes(t *testing.T) {
netaddr.MustParseIP("100.64.0.1"), netaddr.MustParseIP("100.64.0.1"),
}, },
Namespace: Namespace{Name: "joe"}, Namespace: Namespace{Name: "joe"},
HostInfo: []byte( HostInfo: HostInfo{
"{\"OS\":\"centos\",\"Hostname\":\"foo\",\"RequestTags\":[\"tag:accountant-webserver\"]}", OS: "centos",
), Hostname: "foo",
RequestTags: []string{"tag:accountant-webserver"},
},
}, },
{ {
IPAddresses: MachineAddresses{ IPAddresses: MachineAddresses{
netaddr.MustParseIP("100.64.0.2"), netaddr.MustParseIP("100.64.0.2"),
}, },
Namespace: Namespace{Name: "joe"}, Namespace: Namespace{Name: "joe"},
HostInfo: []byte( HostInfo: HostInfo{
"{\"OS\":\"centos\",\"Hostname\":\"foo\",\"RequestTags\":[\"tag:accountant-webserver\"]}", OS: "centos",
), Hostname: "foo",
RequestTags: []string{"tag:accountant-webserver"},
},
}, },
{ {
IPAddresses: MachineAddresses{ IPAddresses: MachineAddresses{
@ -1074,18 +1100,22 @@ func Test_excludeCorrectlyTaggedNodes(t *testing.T) {
netaddr.MustParseIP("100.64.0.1"), netaddr.MustParseIP("100.64.0.1"),
}, },
Namespace: Namespace{Name: "joe"}, Namespace: Namespace{Name: "joe"},
HostInfo: []byte( HostInfo: HostInfo{
"{\"OS\":\"centos\",\"Hostname\":\"hr-web1\",\"RequestTags\":[\"tag:hr-webserver\"]}", OS: "centos",
), Hostname: "hr-web1",
RequestTags: []string{"tag:hr-webserver"},
},
}, },
{ {
IPAddresses: MachineAddresses{ IPAddresses: MachineAddresses{
netaddr.MustParseIP("100.64.0.2"), netaddr.MustParseIP("100.64.0.2"),
}, },
Namespace: Namespace{Name: "joe"}, Namespace: Namespace{Name: "joe"},
HostInfo: []byte( HostInfo: HostInfo{
"{\"OS\":\"centos\",\"Hostname\":\"hr-web2\",\"RequestTags\":[\"tag:hr-webserver\"]}", OS: "centos",
), Hostname: "hr-web2",
RequestTags: []string{"tag:hr-webserver"},
},
}, },
{ {
IPAddresses: MachineAddresses{ IPAddresses: MachineAddresses{
@ -1102,18 +1132,22 @@ func Test_excludeCorrectlyTaggedNodes(t *testing.T) {
netaddr.MustParseIP("100.64.0.1"), netaddr.MustParseIP("100.64.0.1"),
}, },
Namespace: Namespace{Name: "joe"}, Namespace: Namespace{Name: "joe"},
HostInfo: []byte( HostInfo: HostInfo{
"{\"OS\":\"centos\",\"Hostname\":\"hr-web1\",\"RequestTags\":[\"tag:hr-webserver\"]}", OS: "centos",
), Hostname: "hr-web1",
RequestTags: []string{"tag:hr-webserver"},
},
}, },
{ {
IPAddresses: MachineAddresses{ IPAddresses: MachineAddresses{
netaddr.MustParseIP("100.64.0.2"), netaddr.MustParseIP("100.64.0.2"),
}, },
Namespace: Namespace{Name: "joe"}, Namespace: Namespace{Name: "joe"},
HostInfo: []byte( HostInfo: HostInfo{
"{\"OS\":\"centos\",\"Hostname\":\"hr-web2\",\"RequestTags\":[\"tag:hr-webserver\"]}", OS: "centos",
), Hostname: "hr-web2",
RequestTags: []string{"tag:hr-webserver"},
},
}, },
{ {
IPAddresses: MachineAddresses{ IPAddresses: MachineAddresses{

View file

@ -35,7 +35,4 @@ func (s *Suite) TestRegisterMachine(c *check.C) {
) )
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
c.Assert(machineAfterRegistering.Registered, check.Equals, true) c.Assert(machineAfterRegistering.Registered, check.Equals, true)
_, err = machineAfterRegistering.GetHostInfo()
c.Assert(err, check.IsNil)
} }

View file

@ -35,10 +35,7 @@ func (s *Suite) TestGetMachine(c *check.C) {
} }
app.db.Save(machine) app.db.Save(machine)
machineFromDB, err := app.GetMachine("test", "testmachine") _, err = app.GetMachine("test", "testmachine")
c.Assert(err, check.IsNil)
_, err = machineFromDB.GetHostInfo()
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
} }
@ -65,10 +62,7 @@ func (s *Suite) TestGetMachineByID(c *check.C) {
} }
app.db.Save(&machine) app.db.Save(&machine)
machineByID, err := app.GetMachineByID(0) _, err = app.GetMachineByID(0)
c.Assert(err, check.IsNil)
_, err = machineByID.GetHostInfo()
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
} }
@ -146,9 +140,6 @@ func (s *Suite) TestListPeers(c *check.C) {
machine0ByID, err := app.GetMachineByID(0) machine0ByID, err := app.GetMachineByID(0)
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
_, err = machine0ByID.GetHostInfo()
c.Assert(err, check.IsNil)
peersOfMachine0, err := app.ListPeers(machine0ByID) peersOfMachine0, err := app.ListPeers(machine0ByID)
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
@ -219,9 +210,6 @@ func (s *Suite) TestGetACLFilteredPeers(c *check.C) {
c.Logf("Machine(%v), namespace: %v", testMachine.Name, testMachine.Namespace) c.Logf("Machine(%v), namespace: %v", testMachine.Name, testMachine.Namespace)
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
_, err = testMachine.GetHostInfo()
c.Assert(err, check.IsNil)
machines, err := app.ListMachines() machines, err := app.ListMachines()
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)

View file

@ -1,10 +1,7 @@
package headscale package headscale
import ( import (
"encoding/json"
"gopkg.in/check.v1" "gopkg.in/check.v1"
"gorm.io/datatypes"
"inet.af/netaddr" "inet.af/netaddr"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
) )
@ -25,8 +22,6 @@ func (s *Suite) TestGetRoutes(c *check.C) {
hostInfo := tailcfg.Hostinfo{ hostInfo := tailcfg.Hostinfo{
RoutableIPs: []netaddr.IPPrefix{route}, RoutableIPs: []netaddr.IPPrefix{route},
} }
hostinfo, err := json.Marshal(hostInfo)
c.Assert(err, check.IsNil)
machine := Machine{ machine := Machine{
ID: 0, ID: 0,
@ -38,7 +33,7 @@ func (s *Suite) TestGetRoutes(c *check.C) {
Registered: true, Registered: true,
RegisterMethod: RegisterMethodAuthKey, RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID), AuthKeyID: uint(pak.ID),
HostInfo: datatypes.JSON(hostinfo), HostInfo: HostInfo(hostInfo),
} }
app.db.Save(&machine) app.db.Save(&machine)
@ -79,8 +74,6 @@ func (s *Suite) TestGetEnableRoutes(c *check.C) {
hostInfo := tailcfg.Hostinfo{ hostInfo := tailcfg.Hostinfo{
RoutableIPs: []netaddr.IPPrefix{route, route2}, RoutableIPs: []netaddr.IPPrefix{route, route2},
} }
hostinfo, err := json.Marshal(hostInfo)
c.Assert(err, check.IsNil)
machine := Machine{ machine := Machine{
ID: 0, ID: 0,
@ -92,7 +85,7 @@ func (s *Suite) TestGetEnableRoutes(c *check.C) {
Registered: true, Registered: true,
RegisterMethod: RegisterMethodAuthKey, RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID), AuthKeyID: uint(pak.ID),
HostInfo: datatypes.JSON(hostinfo), HostInfo: HostInfo(hostInfo),
} }
app.db.Save(&machine) app.db.Save(&machine)