feat: add unit tests and fmt

This commit is contained in:
Adrien Raffin-Caboisse 2022-04-16 13:15:04 +02:00
parent db1528bc73
commit 89a1a56328
No known key found for this signature in database
GPG Key ID: 7FB60532DEBEAD6A
2 changed files with 124 additions and 2 deletions

View File

@ -662,13 +662,17 @@ func (machine *Machine) toProto() *v1.Machine {
} }
// getTags will return the tags of the current 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) validTagMap := make(map[string]bool)
invalidTagMap := make(map[string]bool) invalidTagMap := make(map[string]bool)
for _, tag := range machine.HostInfo.RequestTags { for _, tag := range machine.HostInfo.RequestTags {
owners, err := expandTagOwners(aclPolicy, tag, stripEmailDomain) owners, err := expandTagOwners(aclPolicy, tag, stripEmailDomain)
if errors.Is(err, errInvalidTag) { if errors.Is(err, errInvalidTag) {
invalidTags = append(invalidTags, tag) invalidTagMap[tag] = true
} }
var found bool var found bool
for _, owner := range owners { for _, owner := range owners {

View File

@ -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 // nolint
func Test_getFilteredByACLPeers(t *testing.T) { func Test_getFilteredByACLPeers(t *testing.T) {
type args struct { type args struct {