Skip to content

Commit

Permalink
Adds unit test for fqdn.go
Browse files Browse the repository at this point in the history
Signed-off-by: Harshitha U R <harshithaur1611@gmail.com>
  • Loading branch information
urharshitha committed Jan 25, 2023
1 parent 4de4392 commit 85b6c83
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions pkg/agent/controller/networkpolicy/fqdn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package networkpolicy

import (
"context"
"net"
"testing"
"time"

Expand Down Expand Up @@ -305,3 +306,85 @@ func TestLookupIPFallback(t *testing.T) {
err := f.lookupIP(ctx, "www.google.com")
require.NoError(t, err, "Error when resolving name")
}

func TestString(t *testing.T) {
tests := []struct {
name string
selectorItem *fqdnSelectorItem
matchName string
matchRegex string
expectedOutput string
}{
{
name: "matching the regex",
selectorItem: &fqdnSelectorItem{
matchRegex: "^.*antrea[.]io$",
},
expectedOutput: "matchRegex:^.*antrea[.]io$",
},
{
name: "matching the name",
selectorItem: &fqdnSelectorItem{
matchName: "test.antrea.io",
},
expectedOutput: "matchName:test.antrea.io",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
gotOutput := tc.selectorItem.String()
assert.Equal(t, tc.expectedOutput, gotOutput)
})
}
}

func TestGetIPsForFQDNSelectors(t *testing.T) {
selectorItem := fqdnSelectorItem{
matchName: "test.antrea.io",
}
tests := []struct {
name string
fqdns []string
existingSelectorItemToFQDN map[fqdnSelectorItem]sets.String
existingDNSCache map[string]dnsMeta
expectedMatchedIPs []net.IP
}{
{
name: "matched ip found",
fqdns: []string{"test.antrea.io"},
existingSelectorItemToFQDN: map[fqdnSelectorItem]sets.String{
selectorItem: sets.NewString("test.antrea.io"),
},
existingDNSCache: map[string]dnsMeta{
"test.antrea.io": {
responseIPs: map[string]net.IP{
"IP": net.ParseIP("127.0.0.1"),
},
},
},
expectedMatchedIPs: []net.IP{net.ParseIP("127.0.0.1")},
},
{
name: "no matched ip",
fqdns: []string{"^.*antrea[.]io$"},
expectedMatchedIPs: nil,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
f, _ := newMockFQDNController(t, controller, nil)
if tc.existingSelectorItemToFQDN != nil {
f.selectorItemToFQDN = tc.existingSelectorItemToFQDN
}
if tc.existingDNSCache != nil {
f.dnsEntryCache = tc.existingDNSCache
}
gotOutput := f.getIPsForFQDNSelectors(tc.fqdns)
assert.Equal(t, tc.expectedMatchedIPs, gotOutput)
})
}
}

0 comments on commit 85b6c83

Please sign in to comment.