From 89a1a56328ed2e8f1e288faa1e4ad4a9c2c6122b Mon Sep 17 00:00:00 2001 From: Adrien Raffin-Caboisse Date: Sat, 16 Apr 2022 13:15:04 +0200 Subject: [PATCH] feat: add unit tests and fmt --- machine.go | 8 +++- machine_test.go | 118 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 2 deletions(-) diff --git a/machine.go b/machine.go index f24b0392..3c0ea77f 100644 --- a/machine.go +++ b/machine.go @@ -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 { diff --git a/machine_test.go b/machine_test.go index a455a0bb..a72dfbf0 100644 --- a/machine_test.go +++ b/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 {