go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/dnsplugin/vppcalls/vpp2202/dns_test.go (about)

     1  // Copyright (c) 2022 Pantheon.tech
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at:
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package vpp2202_test
    16  
    17  import (
    18  	"net"
    19  	"testing"
    20  
    21  	. "github.com/onsi/gomega"
    22  	govppapi "go.fd.io/govpp/api"
    23  	"go.ligato.io/cn-infra/v2/logging/logrus"
    24  
    25  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/dns"
    26  	"go.ligato.io/vpp-agent/v3/plugins/vpp/dnsplugin/vppcalls"
    27  	"go.ligato.io/vpp-agent/v3/plugins/vpp/dnsplugin/vppcalls/vpp2202"
    28  	"go.ligato.io/vpp-agent/v3/plugins/vpp/vppmock"
    29  )
    30  
    31  var (
    32  	upstreamDNSServerIPv4 = net.ParseIP("8.8.8.8").To4()        // google dns
    33  	upstreamDNSServerIPv6 = net.ParseIP("2001:4860:4860::8888") // google dns
    34  )
    35  
    36  // TestEnableDisableDNS tests all cases for methods EnableDNS and DisableDNS
    37  func TestEnableDisableDNS(t *testing.T) {
    38  	// Prepare different cases
    39  	cases := []struct {
    40  		Name          string
    41  		TestEnable    bool
    42  		FailInVPP     bool
    43  		ExpectFailure bool
    44  		Expected      govppapi.Message
    45  	}{
    46  		{
    47  			Name:       "successful enabling of DNS",
    48  			TestEnable: true,
    49  			Expected: &dns.DNSEnableDisable{
    50  				Enable: 1,
    51  			},
    52  		},
    53  		{
    54  			Name:          "enable failure",
    55  			TestEnable:    true,
    56  			FailInVPP:     true,
    57  			ExpectFailure: true,
    58  		},
    59  		{
    60  			Name:       "successful disabling of DNS",
    61  			TestEnable: false,
    62  			Expected: &dns.DNSEnableDisable{
    63  				Enable: 0,
    64  			},
    65  		},
    66  		{
    67  			Name:          "disable failure",
    68  			TestEnable:    false,
    69  			FailInVPP:     true,
    70  			ExpectFailure: true,
    71  		},
    72  	}
    73  
    74  	// Run all cases
    75  	for _, td := range cases {
    76  		t.Run(td.Name, func(t *testing.T) {
    77  			ctx, vppCalls := setup(t)
    78  			defer teardown(ctx)
    79  			// prepare reply
    80  			if td.FailInVPP {
    81  				ctx.MockVpp.MockReply(&dns.DNSEnableDisableReply{Retval: 1})
    82  			} else {
    83  				ctx.MockVpp.MockReply(&dns.DNSEnableDisableReply{})
    84  			}
    85  
    86  			// make the call
    87  			var err error
    88  			if td.TestEnable {
    89  				err = vppCalls.EnableDNS()
    90  			} else {
    91  				err = vppCalls.DisableDNS()
    92  			}
    93  
    94  			// verify result
    95  			if td.ExpectFailure {
    96  				Expect(err).Should(HaveOccurred())
    97  			} else {
    98  				Expect(err).ShouldNot(HaveOccurred())
    99  				Expect(ctx.MockChannel.Msg).To(Equal(td.Expected))
   100  			}
   101  		})
   102  	}
   103  }
   104  
   105  // TestAddRemoveUpstreamDNSServer tests all cases for methods AddUpstreamDNSServer and RemoveUpstreamDNSServer
   106  func TestAddRemoveUpstreamDNSServer(t *testing.T) {
   107  	// Prepare different cases
   108  	cases := []struct {
   109  		Name          string
   110  		TestAdding    bool
   111  		FailInVPP     bool
   112  		ExpectFailure bool
   113  		Input         net.IP
   114  		Expected      govppapi.Message
   115  	}{
   116  		{
   117  			Name:       "successful adding of IPv4 upstream DNS server",
   118  			TestAdding: true,
   119  			Input:      upstreamDNSServerIPv4,
   120  			Expected: &dns.DNSNameServerAddDel{
   121  				IsIP6:         0,
   122  				IsAdd:         1,
   123  				ServerAddress: upstreamDNSServerIPv4,
   124  			},
   125  		},
   126  		{
   127  			Name:       "successful adding of IPv6 upstream DNS server",
   128  			TestAdding: true,
   129  			Input:      upstreamDNSServerIPv6,
   130  			Expected: &dns.DNSNameServerAddDel{
   131  				IsIP6:         1,
   132  				IsAdd:         1,
   133  				ServerAddress: upstreamDNSServerIPv6,
   134  			},
   135  		},
   136  		{
   137  			Name:       "successful removal of IPv4 upstream DNS server",
   138  			TestAdding: false,
   139  			Input:      upstreamDNSServerIPv4,
   140  			Expected: &dns.DNSNameServerAddDel{
   141  				IsIP6:         0,
   142  				IsAdd:         0,
   143  				ServerAddress: upstreamDNSServerIPv4,
   144  			},
   145  		},
   146  		{
   147  			Name:       "successful removal of IPv6 upstream DNS server",
   148  			TestAdding: false,
   149  			Input:      upstreamDNSServerIPv6,
   150  			Expected: &dns.DNSNameServerAddDel{
   151  				IsIP6:         1,
   152  				IsAdd:         0,
   153  				ServerAddress: upstreamDNSServerIPv6,
   154  			},
   155  		},
   156  		{
   157  			Name:          "failure propagation from VPP",
   158  			TestAdding:    false,
   159  			FailInVPP:     true,
   160  			ExpectFailure: true,
   161  			Input:         upstreamDNSServerIPv4,
   162  		},
   163  		{
   164  			Name:          "bad IP address input",
   165  			TestAdding:    false,
   166  			FailInVPP:     true,
   167  			ExpectFailure: true,
   168  			Input:         nil,
   169  		},
   170  	}
   171  
   172  	// Run all cases
   173  	for _, td := range cases {
   174  		t.Run(td.Name, func(t *testing.T) {
   175  			ctx, vppCalls := setup(t)
   176  			defer teardown(ctx)
   177  			// prepare reply
   178  			if td.FailInVPP {
   179  				ctx.MockVpp.MockReply(&dns.DNSNameServerAddDelReply{Retval: 1})
   180  			} else {
   181  				ctx.MockVpp.MockReply(&dns.DNSNameServerAddDelReply{})
   182  			}
   183  
   184  			// make the call
   185  			var err error
   186  			if td.TestAdding {
   187  				err = vppCalls.AddUpstreamDNSServer(td.Input)
   188  			} else {
   189  				err = vppCalls.DeleteUpstreamDNSServer(td.Input)
   190  			}
   191  
   192  			// verify result
   193  			if td.ExpectFailure {
   194  				Expect(err).Should(HaveOccurred())
   195  			} else {
   196  				Expect(err).ShouldNot(HaveOccurred())
   197  				Expect(ctx.MockChannel.Msg).To(Equal(td.Expected))
   198  			}
   199  		})
   200  	}
   201  }
   202  
   203  func setup(t *testing.T) (*vppmock.TestCtx, vppcalls.DNSVppAPI) {
   204  	ctx := vppmock.SetupTestCtx(t)
   205  	log := logrus.NewLogger("test")
   206  	vppCalls := vpp2202.NewDNSVppHandler(ctx.MockVPPClient, log)
   207  	return ctx, vppCalls
   208  }
   209  
   210  func teardown(ctx *vppmock.TestCtx) {
   211  	ctx.TeardownTestCtx()
   212  }