github.com/fafucoder/cilium@v1.6.11/proxylib/npds/client_test.go (about)

     1  // Copyright 2018 Authors of Cilium
     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  // +build !privileged_tests
    16  
    17  package npds
    18  
    19  import (
    20  	"context"
    21  	"path/filepath"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/cilium/cilium/pkg/completion"
    26  	"github.com/cilium/cilium/pkg/envoy"
    27  	"github.com/cilium/cilium/proxylib/test"
    28  
    29  	"github.com/cilium/proxy/go/cilium/api"
    30  	envoy_api_v2 "github.com/cilium/proxy/go/envoy/api/v2"
    31  	log "github.com/sirupsen/logrus"
    32  	. "gopkg.in/check.v1"
    33  )
    34  
    35  // Hook up gocheck into the "go test" runner.
    36  func Test(t *testing.T) {
    37  	TestingT(t)
    38  }
    39  
    40  type ClientSuite struct {
    41  	acks  int
    42  	nacks int
    43  }
    44  
    45  var _ = Suite(&ClientSuite{})
    46  
    47  const (
    48  	TestTimeout      = 10 * time.Second
    49  	CacheUpdateDelay = 250 * time.Millisecond
    50  )
    51  
    52  var resources = []*cilium.NetworkPolicy{
    53  	{Name: "resource0"},
    54  	{Name: "resource1"},
    55  	{Name: "resource2"},
    56  }
    57  
    58  // UpsertNetworkPolicy must only be used for testing!
    59  func (cs *ClientSuite) UpsertNetworkPolicy(c *C, s *envoy.XDSServer, p *cilium.NetworkPolicy) {
    60  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    61  	defer cancel()
    62  	wg := completion.NewWaitGroup(ctx)
    63  
    64  	callback := func(err error) {
    65  		if err == nil {
    66  			log.Debug("ACK Callback called")
    67  			cs.acks++
    68  		} else {
    69  			log.Debug("NACK Callback called")
    70  			cs.nacks++
    71  		}
    72  	}
    73  
    74  	s.NetworkPolicyMutator.Upsert(envoy.NetworkPolicyTypeURL, p.Name, p, []string{"127.0.0.1"}, wg, callback)
    75  }
    76  
    77  type updater struct{}
    78  
    79  func (u *updater) PolicyUpdate(resp *envoy_api_v2.DiscoveryResponse) error {
    80  	log.Debugf("Received policy update: %v", *resp)
    81  	return nil
    82  }
    83  
    84  func (s *ClientSuite) TestRequestAllResources(c *C) {
    85  	var updater *updater
    86  	xdsPath := filepath.Join(test.Tmpdir, "xds.sock")
    87  	client1 := NewClient(xdsPath, "sidecar~127.0.0.1~v0.default~default.svc.cluster.local", updater)
    88  	if client1 == nil {
    89  		c.Error("NewClient() failed")
    90  	}
    91  
    92  	// Start another client, which will never connect
    93  	xdsPath2 := filepath.Join(test.Tmpdir, "xds.sock2")
    94  	client2 := NewClient(xdsPath2, "sidecar~127.0.0.2~v0.default~default.svc.cluster.local", updater)
    95  	if client2 == nil {
    96  		c.Error("NewClient() failed")
    97  	}
    98  
    99  	// Some wait before server is made available
   100  	time.Sleep(500 * time.Millisecond)
   101  	xdsServer := envoy.StartXDSServer(test.Tmpdir)
   102  	time.Sleep(500 * time.Millisecond)
   103  
   104  	// Create version 1 with resource 0.
   105  	s.UpsertNetworkPolicy(c, xdsServer, resources[0])
   106  
   107  	time.Sleep(DialDelay * BackOffLimit)
   108  	c.Assert(s.acks, Equals, 1)
   109  	c.Assert(s.nacks, Equals, 0)
   110  }