Skip to content

Commit

Permalink
daemon: add mock dns proxy to tests
Browse files Browse the repository at this point in the history
This change allows for daemon integration tests to run with mock DNS
proxy

Signed-off-by: Maciej Kwiek <maciej@isovalent.com>
  • Loading branch information
nebril committed Sep 7, 2021
1 parent 838982c commit 0ee8a1c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
4 changes: 4 additions & 0 deletions daemon/cmd/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package cmd

import (
"context"
fqdnproxy "github.com/cilium/cilium/pkg/fqdn/proxy"
"github.com/cilium/cilium/pkg/proxy"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -73,6 +75,8 @@ func setupTestDirectories() {
}

func TestMain(m *testing.M) {
proxy.DefaultDNSProxy = fqdnproxy.MockFQDNProxy{}

// Set up all configuration options which are global to the entire test
// run.
option.Config.Populate()
Expand Down
3 changes: 1 addition & 2 deletions daemon/cmd/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,6 @@ func (d *Daemon) QueueEndpointBuild(ctx context.Context, epID uint64) (func(), e
}

func (d *Daemon) GetDNSRules(epID uint16) restore.DNSRules {
// Many unit tests do not initialize the DNS proxy
if proxy.DefaultDNSProxy == nil {
return nil
}
Expand All @@ -1084,9 +1083,9 @@ func (d *Daemon) GetDNSRules(epID uint16) restore.DNSRules {
}

func (d *Daemon) RemoveRestoredDNSRules(epID uint16) {
// Many unit tests do not initialize the DNS proxy
if proxy.DefaultDNSProxy == nil {
return
}

proxy.DefaultDNSProxy.RemoveRestoredRules(epID)
}
10 changes: 5 additions & 5 deletions pkg/fqdn/dnsproxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,11 +633,11 @@ func (s *DNSProxyTestSuite) TestFullPathDependence(c *C) {
Re: restore.RuleRegex{Regexp: s.proxy.allowed[epID1][54][cachedWildcardSelector]},
}},
}
restored1 := s.proxy.GetRules(uint16(epID1)).Sort()
restored1, _ := s.proxy.GetRules(uint16(epID1)).Sort()
c.Assert(restored1, checker.DeepEquals, expected1)

expected2 := restore.DNSRules{}
restored2 := s.proxy.GetRules(uint16(epID2)).Sort()
restored2, _ := s.proxy.GetRules(uint16(epID2)).Sort()
c.Assert(restored2, checker.DeepEquals, expected2)

expected3 := restore.DNSRules{
Expand All @@ -652,7 +652,7 @@ func (s *DNSProxyTestSuite) TestFullPathDependence(c *C) {
Re: restore.RuleRegex{Regexp: s.proxy.allowed[epID3][53][cachedDstID4Selector]},
}}.Sort(),
}
restored3 := s.proxy.GetRules(uint16(epID3)).Sort()
restored3, _ := s.proxy.GetRules(uint16(epID3)).Sort()
c.Assert(restored3, checker.DeepEquals, expected3)

// Test with limited set of allowed IPs
Expand All @@ -671,7 +671,7 @@ func (s *DNSProxyTestSuite) TestFullPathDependence(c *C) {
Re: restore.RuleRegex{Regexp: s.proxy.allowed[epID1][54][cachedWildcardSelector]},
}},
}
restored1b := s.proxy.GetRules(uint16(epID1)).Sort()
restored1b, _ := s.proxy.GetRules(uint16(epID1)).Sort()
c.Assert(restored1b, checker.DeepEquals, expected1b)

// unlimited again
Expand Down Expand Up @@ -934,7 +934,7 @@ func (s *DNSProxyTestSuite) TestRestoredEndpoint(c *C) {
c.Assert(response.Answer[0].String(), Equals, "cilium.io.\t60\tIN\tA\t1.1.1.1", Commentf("Proxy returned incorrect RRs"))

// Get restored rules
restored := s.proxy.GetRules(uint16(epID1)).Sort()
restored, _ := s.proxy.GetRules(uint16(epID1)).Sort()

// remove rules
err = s.proxy.UpdateAllowed(epID1, dstPort, nil)
Expand Down
26 changes: 26 additions & 0 deletions pkg/fqdn/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,29 @@ type FQDNProxy interface {
SetRejectReply(string)
RestoreRules(op *endpoint.Endpoint)
}

type MockFQDNProxy struct{}

func (m MockFQDNProxy) GetRules(u uint16) (restore.DNSRules, error) {
return nil, nil
}

func (m MockFQDNProxy) RemoveRestoredRules(u uint16) {
return
}

func (m MockFQDNProxy) UpdateAllowed(endpointID uint64, destPort uint16, newRules policy.L7DataMap) error {
return nil
}

func (m MockFQDNProxy) GetBindPort() uint16 {
return 0
}

func (m MockFQDNProxy) SetRejectReply(s string) {
return
}

func (m MockFQDNProxy) RestoreRules(op *endpoint.Endpoint) {
return
}

0 comments on commit 0ee8a1c

Please sign in to comment.