mirror of
https://github.com/juanfont/headscale.git
synced 2024-12-28 10:47:50 +00:00
feat: add unit tests and fmt
This commit is contained in:
parent
db1528bc73
commit
89a1a56328
@ -662,13 +662,17 @@ func (machine *Machine) toProto() *v1.Machine {
|
||||
}
|
||||
|
||||
// getTags will return the tags of the current machine
|
||||
func getTags(aclPolicy ACLPolicy, machine Machine, stripEmailDomain bool) (validTags []string, invalidTags []string) {
|
||||
func getTags(
|
||||
aclPolicy ACLPolicy,
|
||||
machine Machine,
|
||||
stripEmailDomain bool,
|
||||
) (validTags []string, invalidTags []string) {
|
||||
validTagMap := make(map[string]bool)
|
||||
invalidTagMap := make(map[string]bool)
|
||||
for _, tag := range machine.HostInfo.RequestTags {
|
||||
owners, err := expandTagOwners(aclPolicy, tag, stripEmailDomain)
|
||||
if errors.Is(err, errInvalidTag) {
|
||||
invalidTags = append(invalidTags, tag)
|
||||
invalidTagMap[tag] = true
|
||||
}
|
||||
var found bool
|
||||
for _, owner := range owners {
|
||||
|
118
machine_test.go
118
machine_test.go
@ -277,6 +277,124 @@ func (s *Suite) TestSerdeAddressStrignSlice(c *check.C) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_getTags(t *testing.T) {
|
||||
type args struct {
|
||||
aclPolicy ACLPolicy
|
||||
machine Machine
|
||||
stripEmailDomain bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantInvalid []string
|
||||
wantValid []string
|
||||
}{
|
||||
{
|
||||
name: "valid tag one machine",
|
||||
args: args{
|
||||
aclPolicy: ACLPolicy{
|
||||
TagOwners: TagOwners{
|
||||
"tag:valid": []string{"joe"},
|
||||
},
|
||||
},
|
||||
machine: Machine{
|
||||
Namespace: Namespace{
|
||||
Name: "joe",
|
||||
},
|
||||
HostInfo: HostInfo{
|
||||
RequestTags: []string{"tag:valid"},
|
||||
},
|
||||
},
|
||||
stripEmailDomain: false,
|
||||
},
|
||||
wantValid: []string{"tag:valid"},
|
||||
wantInvalid: nil,
|
||||
},
|
||||
{
|
||||
name: "invalid tag and valid tag one machine",
|
||||
args: args{
|
||||
aclPolicy: ACLPolicy{
|
||||
TagOwners: TagOwners{
|
||||
"tag:valid": []string{"joe"},
|
||||
},
|
||||
},
|
||||
machine: Machine{
|
||||
Namespace: Namespace{
|
||||
Name: "joe",
|
||||
},
|
||||
HostInfo: HostInfo{
|
||||
RequestTags: []string{"tag:valid", "tag:invalid"},
|
||||
},
|
||||
},
|
||||
stripEmailDomain: false,
|
||||
},
|
||||
wantValid: []string{"tag:valid"},
|
||||
wantInvalid: []string{"tag:invalid"},
|
||||
},
|
||||
{
|
||||
name: "multiple invalid and identical tags, should return only one invalid tag",
|
||||
args: args{
|
||||
aclPolicy: ACLPolicy{
|
||||
TagOwners: TagOwners{
|
||||
"tag:valid": []string{"joe"},
|
||||
},
|
||||
},
|
||||
machine: Machine{
|
||||
Namespace: Namespace{
|
||||
Name: "joe",
|
||||
},
|
||||
HostInfo: HostInfo{
|
||||
RequestTags: []string{
|
||||
"tag:invalid",
|
||||
"tag:valid",
|
||||
"tag:invalid",
|
||||
},
|
||||
},
|
||||
},
|
||||
stripEmailDomain: false,
|
||||
},
|
||||
wantValid: []string{"tag:valid"},
|
||||
wantInvalid: []string{"tag:invalid"},
|
||||
},
|
||||
{
|
||||
name: "only invalid tags",
|
||||
args: args{
|
||||
aclPolicy: ACLPolicy{
|
||||
TagOwners: TagOwners{
|
||||
"tag:valid": []string{"joe"},
|
||||
},
|
||||
},
|
||||
machine: Machine{
|
||||
Namespace: Namespace{
|
||||
Name: "joe",
|
||||
},
|
||||
HostInfo: HostInfo{
|
||||
RequestTags: []string{"tag:invalid", "very-invalid"},
|
||||
},
|
||||
},
|
||||
stripEmailDomain: false,
|
||||
},
|
||||
wantValid: nil,
|
||||
wantInvalid: []string{"tag:invalid", "very-invalid"},
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
gotValid, gotInvalid := getTags(
|
||||
test.args.aclPolicy,
|
||||
test.args.machine,
|
||||
test.args.stripEmailDomain,
|
||||
)
|
||||
if !reflect.DeepEqual(gotValid, test.wantValid) {
|
||||
t.Errorf("getTags() = %v, want %v", gotValid, test.wantValid)
|
||||
}
|
||||
if !reflect.DeepEqual(gotInvalid, test.wantInvalid) {
|
||||
t.Errorf("getTags() = %v, want %v", gotInvalid, test.wantInvalid)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// nolint
|
||||
func Test_getFilteredByACLPeers(t *testing.T) {
|
||||
type args struct {
|
||||
|
Loading…
x
Reference in New Issue
Block a user