github.com/fafucoder/cilium@v1.6.11/pkg/endpointmanager/manager_test.go (about)

     1  // Copyright 2018-2019 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 endpointmanager
    18  
    19  import (
    20  	"context"
    21  	"sync"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/cilium/cilium/common/addressing"
    26  	"github.com/cilium/cilium/pkg/checker"
    27  	"github.com/cilium/cilium/pkg/completion"
    28  	"github.com/cilium/cilium/pkg/datapath"
    29  	"github.com/cilium/cilium/pkg/endpoint"
    30  	endpointid "github.com/cilium/cilium/pkg/endpoint/id"
    31  	"github.com/cilium/cilium/pkg/endpoint/regeneration"
    32  	"github.com/cilium/cilium/pkg/identity/cache"
    33  	"github.com/cilium/cilium/pkg/lock"
    34  	monitorAPI "github.com/cilium/cilium/pkg/monitor/api"
    35  	"github.com/cilium/cilium/pkg/option"
    36  	"github.com/cilium/cilium/pkg/policy"
    37  	"github.com/cilium/cilium/pkg/revert"
    38  
    39  	. "gopkg.in/check.v1"
    40  )
    41  
    42  // Hook up gocheck into the "go test" runner.
    43  func Test(t *testing.T) { TestingT(t) }
    44  
    45  type EndpointManagerSuite struct {
    46  	repo *policy.Repository
    47  }
    48  
    49  var _ = Suite(&EndpointManagerSuite{})
    50  
    51  func (s *EndpointManagerSuite) SetUpSuite(c *C) {
    52  	s.repo = policy.NewPolicyRepository()
    53  }
    54  
    55  func (s *EndpointManagerSuite) GetPolicyRepository() *policy.Repository {
    56  	return s.repo
    57  }
    58  
    59  func (s *EndpointManagerSuite) UpdateProxyRedirect(e regeneration.EndpointUpdater, l4 *policy.L4Filter, wg *completion.WaitGroup) (uint16, error, revert.FinalizeFunc, revert.RevertFunc) {
    60  	return 0, nil, nil, nil
    61  }
    62  
    63  func (s *EndpointManagerSuite) RemoveProxyRedirect(e regeneration.EndpointInfoSource, id string, wg *completion.WaitGroup) (error, revert.FinalizeFunc, revert.RevertFunc) {
    64  	return nil, nil, nil
    65  }
    66  
    67  func (s *EndpointManagerSuite) UpdateNetworkPolicy(e regeneration.EndpointUpdater, policy *policy.L4Policy,
    68  	proxyWaitGroup *completion.WaitGroup) (error, revert.RevertFunc) {
    69  	return nil, nil
    70  }
    71  
    72  func (s *EndpointManagerSuite) RemoveNetworkPolicy(e regeneration.EndpointInfoSource) {}
    73  
    74  func (s *EndpointManagerSuite) QueueEndpointBuild(ctx context.Context, epID uint64) (func(), error) {
    75  	return nil, nil
    76  }
    77  
    78  func (s *EndpointManagerSuite) RemoveFromEndpointQueue(epID uint64) {}
    79  
    80  func (s *EndpointManagerSuite) GetCompilationLock() *lock.RWMutex {
    81  	return nil
    82  }
    83  
    84  func (s *EndpointManagerSuite) SendNotification(typ monitorAPI.AgentNotification, text string) error {
    85  	return nil
    86  }
    87  
    88  func (s *EndpointManagerSuite) Datapath() datapath.Datapath {
    89  	return nil
    90  }
    91  
    92  func (s *EndpointManagerSuite) GetNodeSuffix() string {
    93  	return ""
    94  }
    95  
    96  func (s *EndpointManagerSuite) UpdateIdentities(added, deleted cache.IdentityCache) {}
    97  
    98  type DummyRuleCacheOwner struct{}
    99  
   100  func (d *DummyRuleCacheOwner) ClearPolicyConsumers(id uint16) *sync.WaitGroup {
   101  	return &sync.WaitGroup{}
   102  }
   103  
   104  func (s *EndpointManagerSuite) TestLookup(c *C) {
   105  	ep := endpoint.NewEndpointWithState(s, 10, endpoint.StateReady)
   106  	ep.UpdateLogger(nil)
   107  	type args struct {
   108  		id string
   109  	}
   110  	type want struct {
   111  		ep       *endpoint.Endpoint
   112  		err      error
   113  		errCheck Checker
   114  	}
   115  	tests := []struct {
   116  		name        string
   117  		setupArgs   func() args
   118  		setupWant   func() want
   119  		preTestRun  func()
   120  		postTestRun func()
   121  	}{
   122  		{
   123  			name:       "endpoint does not exist",
   124  			preTestRun: func() {},
   125  			setupArgs: func() args {
   126  				return args{
   127  					"1234",
   128  				}
   129  			},
   130  			setupWant: func() want {
   131  				return want{
   132  					ep:       nil,
   133  					err:      nil,
   134  					errCheck: Equals,
   135  				}
   136  			},
   137  			postTestRun: func() {},
   138  		},
   139  		{
   140  			name: "endpoint by cilium local ID",
   141  			preTestRun: func() {
   142  				ep.ID = 1234
   143  				Insert(ep)
   144  			},
   145  			setupArgs: func() args {
   146  				return args{
   147  					endpointid.NewCiliumID(1234),
   148  				}
   149  			},
   150  			setupWant: func() want {
   151  				return want{
   152  					ep:       ep,
   153  					err:      nil,
   154  					errCheck: Equals,
   155  				}
   156  			},
   157  			postTestRun: func() {
   158  				WaitEndpointRemoved(ep)
   159  				ep.ID = 0
   160  			},
   161  		},
   162  		{
   163  			name: "endpoint by cilium global ID",
   164  			preTestRun: func() {
   165  				ep.ID = 1234
   166  				Insert(ep)
   167  			},
   168  			setupArgs: func() args {
   169  				return args{
   170  					endpointid.NewID(endpointid.CiliumGlobalIdPrefix, "1234"),
   171  				}
   172  			},
   173  			setupWant: func() want {
   174  				return want{
   175  					err:      ErrUnsupportedID,
   176  					errCheck: Equals,
   177  				}
   178  			},
   179  			postTestRun: func() {
   180  				WaitEndpointRemoved(ep)
   181  				ep.ID = 0
   182  			},
   183  		},
   184  		{
   185  			name: "endpoint by container ID",
   186  			preTestRun: func() {
   187  				ep.ContainerID = "1234"
   188  				Insert(ep)
   189  			},
   190  			setupArgs: func() args {
   191  				return args{
   192  					endpointid.NewID(endpointid.ContainerIdPrefix, "1234"),
   193  				}
   194  			},
   195  			setupWant: func() want {
   196  				return want{
   197  					ep:       ep,
   198  					err:      nil,
   199  					errCheck: Equals,
   200  				}
   201  			},
   202  			postTestRun: func() {
   203  				WaitEndpointRemoved(ep)
   204  				ep.ContainerID = ""
   205  			},
   206  		},
   207  		{
   208  			name: "endpoint by docker endpoint ID",
   209  			preTestRun: func() {
   210  				ep.DockerEndpointID = "1234"
   211  				Insert(ep)
   212  			},
   213  			setupArgs: func() args {
   214  				return args{
   215  					endpointid.NewID(endpointid.DockerEndpointPrefix, "1234"),
   216  				}
   217  			},
   218  			setupWant: func() want {
   219  				return want{
   220  					ep:       ep,
   221  					err:      nil,
   222  					errCheck: Equals,
   223  				}
   224  			},
   225  			postTestRun: func() {
   226  				WaitEndpointRemoved(ep)
   227  				ep.DockerEndpointID = ""
   228  			},
   229  		},
   230  		{
   231  			name: "endpoint by container name",
   232  			preTestRun: func() {
   233  				ep.ContainerName = "foo"
   234  				Insert(ep)
   235  			},
   236  			setupArgs: func() args {
   237  				return args{
   238  					endpointid.NewID(endpointid.ContainerNamePrefix, "foo"),
   239  				}
   240  			},
   241  			setupWant: func() want {
   242  				return want{
   243  					ep:       ep,
   244  					err:      nil,
   245  					errCheck: Equals,
   246  				}
   247  			},
   248  			postTestRun: func() {
   249  				WaitEndpointRemoved(ep)
   250  				ep.ContainerName = ""
   251  			},
   252  		},
   253  		{
   254  			name: "endpoint by pod name",
   255  			preTestRun: func() {
   256  				ep.SetK8sNamespace("default")
   257  				ep.SetK8sPodName("foo")
   258  				Insert(ep)
   259  			},
   260  			setupArgs: func() args {
   261  				return args{
   262  					endpointid.NewID(endpointid.PodNamePrefix, "default/foo"),
   263  				}
   264  			},
   265  			setupWant: func() want {
   266  				return want{
   267  					ep:       ep,
   268  					err:      nil,
   269  					errCheck: Equals,
   270  				}
   271  			},
   272  			postTestRun: func() {
   273  				WaitEndpointRemoved(ep)
   274  				ep.SetK8sPodName("")
   275  			},
   276  		},
   277  		{
   278  			name: "endpoint by ipv4",
   279  			preTestRun: func() {
   280  				ipv4, err := addressing.NewCiliumIPv4("127.0.0.1")
   281  				ep.IPv4 = ipv4
   282  				c.Assert(err, IsNil)
   283  				Insert(ep)
   284  			},
   285  			setupArgs: func() args {
   286  				return args{
   287  					endpointid.NewID(endpointid.IPv4Prefix, "127.0.0.1"),
   288  				}
   289  			},
   290  			setupWant: func() want {
   291  				return want{
   292  					ep:       ep,
   293  					err:      nil,
   294  					errCheck: Equals,
   295  				}
   296  			},
   297  			postTestRun: func() {
   298  				WaitEndpointRemoved(ep)
   299  				ep.IPv4 = nil
   300  			},
   301  		},
   302  		{
   303  			name: "invalid ID",
   304  			preTestRun: func() {
   305  			},
   306  			setupArgs: func() args {
   307  				return args{
   308  					endpointid.NewID("foo", "bar"),
   309  				}
   310  			},
   311  			setupWant: func() want {
   312  				return want{
   313  					err:      nil,
   314  					errCheck: Not(Equals),
   315  				}
   316  			},
   317  			postTestRun: func() {
   318  			},
   319  		},
   320  		{
   321  			name: "invalid cilium ID",
   322  			preTestRun: func() {
   323  			},
   324  			setupArgs: func() args {
   325  				return args{
   326  					endpointid.NewID(endpointid.CiliumLocalIdPrefix, "bar"),
   327  				}
   328  			},
   329  			setupWant: func() want {
   330  				return want{
   331  					err:      nil,
   332  					errCheck: Not(Equals),
   333  				}
   334  			},
   335  			postTestRun: func() {
   336  			},
   337  		},
   338  	}
   339  	for _, tt := range tests {
   340  		tt.preTestRun()
   341  		args := tt.setupArgs()
   342  		want := tt.setupWant()
   343  		got, err := Lookup(args.id)
   344  		c.Assert(err, want.errCheck, want.err, Commentf("Test Name: %s", tt.name))
   345  		c.Assert(got, checker.DeepEquals, want.ep, Commentf("Test Name: %s", tt.name))
   346  		tt.postTestRun()
   347  	}
   348  }
   349  
   350  func (s *EndpointManagerSuite) TestLookupCiliumID(c *C) {
   351  	ep := endpoint.NewEndpointWithState(s, 2, endpoint.StateReady)
   352  	ep.UpdateLogger(nil)
   353  	type args struct {
   354  		id uint16
   355  	}
   356  	type want struct {
   357  		ep       *endpoint.Endpoint
   358  		err      error
   359  		errCheck Checker
   360  	}
   361  	tests := []struct {
   362  		name        string
   363  		setupArgs   func() args
   364  		setupWant   func() want
   365  		preTestRun  func()
   366  		postTestRun func()
   367  	}{
   368  		{
   369  			name: "existing cilium ID",
   370  			preTestRun: func() {
   371  				ep.ID = 1
   372  				Insert(ep)
   373  			},
   374  			setupArgs: func() args {
   375  				return args{
   376  					1,
   377  				}
   378  			},
   379  			setupWant: func() want {
   380  				return want{
   381  					ep: ep,
   382  				}
   383  			},
   384  			postTestRun: func() {
   385  				WaitEndpointRemoved(ep)
   386  				ep.ID = 0
   387  			},
   388  		},
   389  		{
   390  			name: "non-existing cilium ID",
   391  			preTestRun: func() {
   392  			},
   393  			setupArgs: func() args {
   394  				return args{
   395  					1,
   396  				}
   397  			},
   398  			setupWant: func() want {
   399  				return want{
   400  					ep: nil,
   401  				}
   402  			},
   403  			postTestRun: func() {
   404  			},
   405  		},
   406  	}
   407  	for _, tt := range tests {
   408  		tt.preTestRun()
   409  		args := tt.setupArgs()
   410  		want := tt.setupWant()
   411  		got := LookupCiliumID(args.id)
   412  		c.Assert(got, checker.DeepEquals, want.ep, Commentf("Test Name: %s", tt.name))
   413  		tt.postTestRun()
   414  	}
   415  }
   416  
   417  func (s *EndpointManagerSuite) TestLookupContainerID(c *C) {
   418  	ep := endpoint.NewEndpointWithState(s, 3, endpoint.StateReady)
   419  	ep.UpdateLogger(nil)
   420  	type args struct {
   421  		id string
   422  	}
   423  	type want struct {
   424  		ep       *endpoint.Endpoint
   425  		err      error
   426  		errCheck Checker
   427  	}
   428  	tests := []struct {
   429  		name        string
   430  		setupArgs   func() args
   431  		setupWant   func() want
   432  		preTestRun  func()
   433  		postTestRun func()
   434  	}{
   435  		{
   436  			name: "existing container ID",
   437  			preTestRun: func() {
   438  				ep.SetContainerID("foo")
   439  				Insert(ep)
   440  			},
   441  			setupArgs: func() args {
   442  				return args{
   443  					"foo",
   444  				}
   445  			},
   446  			setupWant: func() want {
   447  				return want{
   448  					ep: ep,
   449  				}
   450  			},
   451  			postTestRun: func() {
   452  				WaitEndpointRemoved(ep)
   453  				ep.SetContainerID("")
   454  			},
   455  		},
   456  		{
   457  			name: "non-existing container ID",
   458  			preTestRun: func() {
   459  			},
   460  			setupArgs: func() args {
   461  				return args{
   462  					"foo",
   463  				}
   464  			},
   465  			setupWant: func() want {
   466  				return want{
   467  					ep: nil,
   468  				}
   469  			},
   470  			postTestRun: func() {
   471  			},
   472  		},
   473  	}
   474  	for _, tt := range tests {
   475  		tt.preTestRun()
   476  		args := tt.setupArgs()
   477  		want := tt.setupWant()
   478  		got := LookupContainerID(args.id)
   479  		c.Assert(got, checker.DeepEquals, want.ep, Commentf("Test Name: %s", tt.name))
   480  		tt.postTestRun()
   481  	}
   482  }
   483  
   484  func (s *EndpointManagerSuite) TestLookupIPv4(c *C) {
   485  	ep := endpoint.NewEndpointWithState(s, 4, endpoint.StateReady)
   486  	ep.UpdateLogger(nil)
   487  	type args struct {
   488  		ip string
   489  	}
   490  	type want struct {
   491  		ep       *endpoint.Endpoint
   492  		err      error
   493  		errCheck Checker
   494  	}
   495  	tests := []struct {
   496  		name        string
   497  		setupArgs   func() args
   498  		setupWant   func() want
   499  		preTestRun  func()
   500  		postTestRun func()
   501  	}{
   502  		{
   503  			name: "existing LookupIPv4",
   504  			preTestRun: func() {
   505  				ip, err := addressing.NewCiliumIPv4("127.0.0.1")
   506  				c.Assert(err, IsNil)
   507  				ep.IPv4 = ip
   508  				Insert(ep)
   509  			},
   510  			setupArgs: func() args {
   511  				return args{
   512  					"127.0.0.1",
   513  				}
   514  			},
   515  			setupWant: func() want {
   516  				return want{
   517  					ep: ep,
   518  				}
   519  			},
   520  			postTestRun: func() {
   521  				WaitEndpointRemoved(ep)
   522  				ep.IPv4 = nil
   523  			},
   524  		},
   525  		{
   526  			name: "non-existing LookupIPv4",
   527  			preTestRun: func() {
   528  			},
   529  			setupArgs: func() args {
   530  				return args{
   531  					"127.0.0.1",
   532  				}
   533  			},
   534  			setupWant: func() want {
   535  				return want{
   536  					ep: nil,
   537  				}
   538  			},
   539  			postTestRun: func() {
   540  			},
   541  		},
   542  	}
   543  	for _, tt := range tests {
   544  		tt.preTestRun()
   545  		args := tt.setupArgs()
   546  		want := tt.setupWant()
   547  		got := LookupIPv4(args.ip)
   548  		c.Assert(got, checker.DeepEquals, want.ep, Commentf("Test Name: %s", tt.name))
   549  		tt.postTestRun()
   550  	}
   551  }
   552  
   553  func (s *EndpointManagerSuite) TestLookupPodName(c *C) {
   554  	ep := endpoint.NewEndpointWithState(s, 5, endpoint.StateReady)
   555  	ep.UpdateLogger(nil)
   556  	type args struct {
   557  		podName string
   558  	}
   559  	type want struct {
   560  		ep       *endpoint.Endpoint
   561  		err      error
   562  		errCheck Checker
   563  	}
   564  	tests := []struct {
   565  		name        string
   566  		setupArgs   func() args
   567  		setupWant   func() want
   568  		preTestRun  func()
   569  		postTestRun func()
   570  	}{
   571  		{
   572  			name: "existing PodName",
   573  			preTestRun: func() {
   574  				ep.SetK8sNamespace("default")
   575  				ep.SetK8sPodName("foo")
   576  				Insert(ep)
   577  			},
   578  			setupArgs: func() args {
   579  				return args{
   580  					"default/foo",
   581  				}
   582  			},
   583  			setupWant: func() want {
   584  				return want{
   585  					ep: ep,
   586  				}
   587  			},
   588  			postTestRun: func() {
   589  				WaitEndpointRemoved(ep)
   590  				ep.IPv4 = nil
   591  			},
   592  		},
   593  		{
   594  			name: "non-existing PodName",
   595  			preTestRun: func() {
   596  			},
   597  			setupArgs: func() args {
   598  				return args{
   599  					"default/foo",
   600  				}
   601  			},
   602  			setupWant: func() want {
   603  				return want{
   604  					ep: nil,
   605  				}
   606  			},
   607  			postTestRun: func() {
   608  			},
   609  		},
   610  	}
   611  	for _, tt := range tests {
   612  		tt.preTestRun()
   613  		args := tt.setupArgs()
   614  		want := tt.setupWant()
   615  		got := LookupPodName(args.podName)
   616  		c.Assert(got, checker.DeepEquals, want.ep, Commentf("Test Name: %s", tt.name))
   617  		tt.postTestRun()
   618  	}
   619  }
   620  
   621  func (s *EndpointManagerSuite) TestUpdateReferences(c *C) {
   622  	ep := endpoint.NewEndpointWithState(s, 6, endpoint.StateReady)
   623  	ep.UpdateLogger(nil)
   624  	type args struct {
   625  		ep *endpoint.Endpoint
   626  	}
   627  	type want struct {
   628  		ep       *endpoint.Endpoint
   629  		err      error
   630  		errCheck Checker
   631  	}
   632  	tests := []struct {
   633  		name        string
   634  		setupArgs   func() args
   635  		setupWant   func() want
   636  		preTestRun  func()
   637  		postTestRun func()
   638  	}{
   639  		{
   640  			name: "Updating all references",
   641  			preTestRun: func() {
   642  				ep.ID = 1
   643  				Insert(ep)
   644  			},
   645  			setupArgs: func() args {
   646  				// Update endpoint before running test
   647  				ep.SetK8sNamespace("default")
   648  				ep.SetK8sPodName("foo")
   649  				ep.SetContainerID("container")
   650  				ep.SetDockerEndpointID("dockerendpointID")
   651  				ip, err := addressing.NewCiliumIPv4("127.0.0.1")
   652  				c.Assert(err, IsNil)
   653  				ep.IPv4 = ip
   654  				ep.SetContainerName("containername")
   655  				return args{
   656  					ep: ep,
   657  				}
   658  			},
   659  			setupWant: func() want {
   660  				return want{
   661  					ep: ep,
   662  				}
   663  			},
   664  			postTestRun: func() {
   665  				WaitEndpointRemoved(ep)
   666  				ep.SetK8sNamespace("")
   667  				ep.SetK8sPodName("")
   668  				ep.SetContainerID("")
   669  				ep.SetDockerEndpointID("")
   670  				ep.IPv4 = nil
   671  				ep.SetContainerName("")
   672  			},
   673  		},
   674  	}
   675  	for _, tt := range tests {
   676  		tt.preTestRun()
   677  		args := tt.setupArgs()
   678  		want := tt.setupWant()
   679  		UpdateReferences(args.ep)
   680  
   681  		ep = LookupContainerID(want.ep.GetContainerID())
   682  		c.Assert(ep, checker.DeepEquals, want.ep, Commentf("Test Name: %s", tt.name))
   683  
   684  		ep = lookupDockerEndpoint(want.ep.DockerEndpointID)
   685  		c.Assert(ep, checker.DeepEquals, want.ep, Commentf("Test Name: %s", tt.name))
   686  
   687  		ep = LookupIPv4(want.ep.IPv4.String())
   688  		c.Assert(ep, checker.DeepEquals, want.ep, Commentf("Test Name: %s", tt.name))
   689  
   690  		ep = lookupDockerContainerName(want.ep.ContainerName)
   691  		c.Assert(ep, checker.DeepEquals, want.ep, Commentf("Test Name: %s", tt.name))
   692  
   693  		want.ep.UnconditionalRLock()
   694  		ep = LookupPodName(want.ep.GetK8sNamespaceAndPodNameLocked())
   695  		want.ep.RUnlock()
   696  		c.Assert(ep, checker.DeepEquals, want.ep, Commentf("Test Name: %s", tt.name))
   697  		tt.postTestRun()
   698  	}
   699  }
   700  
   701  func (s *EndpointManagerSuite) TestRemove(c *C) {
   702  	ep := endpoint.NewEndpointWithState(s, 7, endpoint.StateReady)
   703  	ep.UpdateLogger(nil)
   704  	type args struct {
   705  	}
   706  	type want struct {
   707  	}
   708  	tests := []struct {
   709  		name        string
   710  		setupArgs   func() args
   711  		setupWant   func() want
   712  		preTestRun  func()
   713  		postTestRun func()
   714  	}{
   715  		{
   716  			name: "Updating all references",
   717  			preTestRun: func() {
   718  				ep.ID = 1
   719  				Insert(ep)
   720  			},
   721  			setupArgs: func() args {
   722  				return args{}
   723  			},
   724  			setupWant: func() want {
   725  				return want{}
   726  			},
   727  			postTestRun: func() {
   728  			},
   729  		},
   730  	}
   731  	for _, tt := range tests {
   732  		tt.preTestRun()
   733  
   734  		RemoveAll()
   735  		c.Assert(len(endpoints), Equals, 0, Commentf("Test Name: %s", tt.name))
   736  		c.Assert(len(endpointsAux), Equals, 0, Commentf("Test Name: %s", tt.name))
   737  		tt.postTestRun()
   738  	}
   739  }
   740  
   741  func (s *EndpointManagerSuite) TestHasGlobalCT(c *C) {
   742  	ep := endpoint.NewEndpointWithState(s, 1, endpoint.StateReady)
   743  	ep.UpdateLogger(nil)
   744  	type args struct {
   745  		ep *endpoint.Endpoint
   746  	}
   747  	type want struct {
   748  		result bool
   749  	}
   750  	tests := []struct {
   751  		name        string
   752  		setupArgs   func() args
   753  		setupWant   func() want
   754  		preTestRun  func()
   755  		postTestRun func()
   756  	}{
   757  		{
   758  			name: "Endpoint with Conntrack global",
   759  			preTestRun: func() {
   760  				ep.ID = 1
   761  				ep.Options = option.NewIntOptions(&endpoint.EndpointMutableOptionLibrary)
   762  				Insert(ep)
   763  			},
   764  			setupWant: func() want {
   765  				return want{
   766  					result: true,
   767  				}
   768  			},
   769  			postTestRun: func() {
   770  				WaitEndpointRemoved(ep)
   771  				ep.ID = 0
   772  				ep.Options = nil
   773  			},
   774  		},
   775  		{
   776  			name: "Endpoint with Conntrack local",
   777  			preTestRun: func() {
   778  				ep.ID = 1
   779  				ep.Options = option.NewIntOptions(&endpoint.EndpointMutableOptionLibrary)
   780  				ep.Options.SetIfUnset(option.ConntrackLocal, option.OptionEnabled)
   781  				Insert(ep)
   782  			},
   783  			setupWant: func() want {
   784  				return want{
   785  					result: false,
   786  				}
   787  			},
   788  			postTestRun: func() {
   789  				WaitEndpointRemoved(ep)
   790  				ep.ID = 0
   791  				ep.Options = nil
   792  			},
   793  		},
   794  	}
   795  	for _, tt := range tests {
   796  		tt.preTestRun()
   797  		want := tt.setupWant()
   798  		got := HasGlobalCT()
   799  		c.Assert(got, checker.DeepEquals, want.result, Commentf("Test Name: %s", tt.name))
   800  		tt.postTestRun()
   801  	}
   802  }
   803  
   804  func (s *EndpointManagerSuite) TestWaitForEndpointsAtPolicyRev(c *C) {
   805  	ep := endpoint.NewEndpointWithState(s, 1, endpoint.StateReady)
   806  	ep.UpdateLogger(nil)
   807  	type args struct {
   808  		ctx    context.Context
   809  		rev    uint64
   810  		cancel context.CancelFunc
   811  	}
   812  	type want struct {
   813  		err      error
   814  		errCheck Checker
   815  	}
   816  	tests := []struct {
   817  		name        string
   818  		setupArgs   func() args
   819  		setupWant   func() want
   820  		preTestRun  func()
   821  		postTestRun func()
   822  	}{
   823  		{
   824  			name: "Endpoint with revision already set",
   825  			preTestRun: func() {
   826  				ep.ID = 1
   827  				ep.SetPolicyRevision(5)
   828  				Insert(ep)
   829  			},
   830  			setupArgs: func() args {
   831  				return args{
   832  					ctx: context.Background(),
   833  					rev: 5,
   834  				}
   835  			},
   836  			setupWant: func() want {
   837  				return want{
   838  					err:      nil,
   839  					errCheck: Equals,
   840  				}
   841  			},
   842  			postTestRun: func() {
   843  				WaitEndpointRemoved(ep)
   844  				ep = endpoint.NewEndpointWithState(s, 1, endpoint.StateReady)
   845  			},
   846  		},
   847  		{
   848  			name: "Context already timed out",
   849  			preTestRun: func() {
   850  				ep.ID = 1
   851  				ep.SetPolicyRevision(5)
   852  				Insert(ep)
   853  			},
   854  			setupArgs: func() args {
   855  				ctx, cancel := context.WithTimeout(context.Background(), 0)
   856  				return args{
   857  					ctx:    ctx,
   858  					rev:    5,
   859  					cancel: cancel,
   860  				}
   861  			},
   862  			setupWant: func() want {
   863  				return want{
   864  					err:      nil,
   865  					errCheck: Not(Equals),
   866  				}
   867  			},
   868  			postTestRun: func() {
   869  				WaitEndpointRemoved(ep)
   870  				ep = endpoint.NewEndpointWithState(s, 1, endpoint.StateReady)
   871  			},
   872  		},
   873  		{
   874  			name: "Revision is will never be set to the waiting revision",
   875  			preTestRun: func() {
   876  				ep.ID = 1
   877  				ep.SetPolicyRevision(4)
   878  				Insert(ep)
   879  			},
   880  			setupArgs: func() args {
   881  				ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
   882  				return args{
   883  					ctx:    ctx,
   884  					rev:    5,
   885  					cancel: cancel,
   886  				}
   887  			},
   888  			setupWant: func() want {
   889  				return want{
   890  					err:      nil,
   891  					errCheck: Not(Equals),
   892  				}
   893  			},
   894  			postTestRun: func() {
   895  				WaitEndpointRemoved(ep)
   896  				ep = endpoint.NewEndpointWithState(s, 1, endpoint.StateReady)
   897  			},
   898  		},
   899  	}
   900  	for _, tt := range tests {
   901  		tt.preTestRun()
   902  		args := tt.setupArgs()
   903  		want := tt.setupWant()
   904  		got := WaitForEndpointsAtPolicyRev(args.ctx, args.rev)
   905  		c.Assert(got, want.errCheck, want.err, Commentf("Test Name: %s", tt.name))
   906  		if args.cancel != nil {
   907  			args.cancel()
   908  		}
   909  		tt.postTestRun()
   910  	}
   911  }