github.com/lingyao2333/mo-zero@v1.4.1/zrpc/resolver/internal/kube/eventhandler_test.go (about)

     1  package kube
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	v1 "k8s.io/api/core/v1"
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  )
    10  
    11  func TestAdd(t *testing.T) {
    12  	var endpoints []string
    13  	h := NewEventHandler(func(change []string) {
    14  		endpoints = change
    15  	})
    16  	h.OnAdd("bad")
    17  	h.OnAdd(&v1.Endpoints{Subsets: []v1.EndpointSubset{
    18  		{
    19  			Addresses: []v1.EndpointAddress{
    20  				{
    21  					IP: "0.0.0.1",
    22  				},
    23  				{
    24  					IP: "0.0.0.2",
    25  				},
    26  				{
    27  					IP: "0.0.0.3",
    28  				},
    29  			},
    30  		},
    31  	}})
    32  	assert.ElementsMatch(t, []string{"0.0.0.1", "0.0.0.2", "0.0.0.3"}, endpoints)
    33  }
    34  
    35  func TestDelete(t *testing.T) {
    36  	var endpoints []string
    37  	h := NewEventHandler(func(change []string) {
    38  		endpoints = change
    39  	})
    40  	h.OnAdd(&v1.Endpoints{Subsets: []v1.EndpointSubset{
    41  		{
    42  			Addresses: []v1.EndpointAddress{
    43  				{
    44  					IP: "0.0.0.1",
    45  				},
    46  				{
    47  					IP: "0.0.0.2",
    48  				},
    49  				{
    50  					IP: "0.0.0.3",
    51  				},
    52  			},
    53  		},
    54  	}})
    55  	h.OnDelete("bad")
    56  	h.OnDelete(&v1.Endpoints{Subsets: []v1.EndpointSubset{
    57  		{
    58  			Addresses: []v1.EndpointAddress{
    59  				{
    60  					IP: "0.0.0.1",
    61  				},
    62  				{
    63  					IP: "0.0.0.2",
    64  				},
    65  			},
    66  		},
    67  	}})
    68  	assert.ElementsMatch(t, []string{"0.0.0.3"}, endpoints)
    69  }
    70  
    71  func TestUpdate(t *testing.T) {
    72  	var endpoints []string
    73  	h := NewEventHandler(func(change []string) {
    74  		endpoints = change
    75  	})
    76  	h.OnUpdate(&v1.Endpoints{
    77  		Subsets: []v1.EndpointSubset{
    78  			{
    79  				Addresses: []v1.EndpointAddress{
    80  					{
    81  						IP: "0.0.0.1",
    82  					},
    83  					{
    84  						IP: "0.0.0.2",
    85  					},
    86  				},
    87  			},
    88  		},
    89  		ObjectMeta: metav1.ObjectMeta{
    90  			ResourceVersion: "1",
    91  		},
    92  	}, &v1.Endpoints{
    93  		Subsets: []v1.EndpointSubset{
    94  			{
    95  				Addresses: []v1.EndpointAddress{
    96  					{
    97  						IP: "0.0.0.1",
    98  					},
    99  					{
   100  						IP: "0.0.0.2",
   101  					},
   102  					{
   103  						IP: "0.0.0.3",
   104  					},
   105  				},
   106  			},
   107  		},
   108  		ObjectMeta: metav1.ObjectMeta{
   109  			ResourceVersion: "2",
   110  		},
   111  	})
   112  	assert.ElementsMatch(t, []string{"0.0.0.1", "0.0.0.2", "0.0.0.3"}, endpoints)
   113  }
   114  
   115  func TestUpdateNoChange(t *testing.T) {
   116  	h := NewEventHandler(func(change []string) {
   117  		assert.Fail(t, "should not called")
   118  	})
   119  	h.OnUpdate(&v1.Endpoints{
   120  		Subsets: []v1.EndpointSubset{
   121  			{
   122  				Addresses: []v1.EndpointAddress{
   123  					{
   124  						IP: "0.0.0.1",
   125  					},
   126  					{
   127  						IP: "0.0.0.2",
   128  					},
   129  				},
   130  			},
   131  		},
   132  		ObjectMeta: metav1.ObjectMeta{
   133  			ResourceVersion: "1",
   134  		},
   135  	}, &v1.Endpoints{
   136  		Subsets: []v1.EndpointSubset{
   137  			{
   138  				Addresses: []v1.EndpointAddress{
   139  					{
   140  						IP: "0.0.0.1",
   141  					},
   142  					{
   143  						IP: "0.0.0.2",
   144  					},
   145  				},
   146  			},
   147  		},
   148  		ObjectMeta: metav1.ObjectMeta{
   149  			ResourceVersion: "1",
   150  		},
   151  	})
   152  }
   153  
   154  func TestUpdateChangeWithDifferentVersion(t *testing.T) {
   155  	var endpoints []string
   156  	h := NewEventHandler(func(change []string) {
   157  		endpoints = change
   158  	})
   159  	h.OnAdd(&v1.Endpoints{Subsets: []v1.EndpointSubset{
   160  		{
   161  			Addresses: []v1.EndpointAddress{
   162  				{
   163  					IP: "0.0.0.1",
   164  				},
   165  				{
   166  					IP: "0.0.0.3",
   167  				},
   168  			},
   169  		},
   170  	}})
   171  	h.OnUpdate(&v1.Endpoints{
   172  		Subsets: []v1.EndpointSubset{
   173  			{
   174  				Addresses: []v1.EndpointAddress{
   175  					{
   176  						IP: "0.0.0.1",
   177  					},
   178  					{
   179  						IP: "0.0.0.3",
   180  					},
   181  				},
   182  			},
   183  		},
   184  		ObjectMeta: metav1.ObjectMeta{
   185  			ResourceVersion: "1",
   186  		},
   187  	}, &v1.Endpoints{
   188  		Subsets: []v1.EndpointSubset{
   189  			{
   190  				Addresses: []v1.EndpointAddress{
   191  					{
   192  						IP: "0.0.0.1",
   193  					},
   194  					{
   195  						IP: "0.0.0.2",
   196  					},
   197  				},
   198  			},
   199  		},
   200  		ObjectMeta: metav1.ObjectMeta{
   201  			ResourceVersion: "2",
   202  		},
   203  	})
   204  	assert.ElementsMatch(t, []string{"0.0.0.1", "0.0.0.2"}, endpoints)
   205  }
   206  
   207  func TestUpdateNoChangeWithDifferentVersion(t *testing.T) {
   208  	var endpoints []string
   209  	h := NewEventHandler(func(change []string) {
   210  		endpoints = change
   211  	})
   212  	h.OnAdd(&v1.Endpoints{Subsets: []v1.EndpointSubset{
   213  		{
   214  			Addresses: []v1.EndpointAddress{
   215  				{
   216  					IP: "0.0.0.1",
   217  				},
   218  				{
   219  					IP: "0.0.0.2",
   220  				},
   221  			},
   222  		},
   223  	}})
   224  	h.OnUpdate("bad", &v1.Endpoints{Subsets: []v1.EndpointSubset{
   225  		{
   226  			Addresses: []v1.EndpointAddress{
   227  				{
   228  					IP: "0.0.0.1",
   229  				},
   230  			},
   231  		},
   232  	}})
   233  	h.OnUpdate(&v1.Endpoints{Subsets: []v1.EndpointSubset{
   234  		{
   235  			Addresses: []v1.EndpointAddress{
   236  				{
   237  					IP: "0.0.0.1",
   238  				},
   239  			},
   240  		},
   241  	}}, "bad")
   242  	h.OnUpdate(&v1.Endpoints{
   243  		Subsets: []v1.EndpointSubset{
   244  			{
   245  				Addresses: []v1.EndpointAddress{
   246  					{
   247  						IP: "0.0.0.1",
   248  					},
   249  					{
   250  						IP: "0.0.0.2",
   251  					},
   252  				},
   253  			},
   254  		},
   255  		ObjectMeta: metav1.ObjectMeta{
   256  			ResourceVersion: "1",
   257  		},
   258  	}, &v1.Endpoints{
   259  		Subsets: []v1.EndpointSubset{
   260  			{
   261  				Addresses: []v1.EndpointAddress{
   262  					{
   263  						IP: "0.0.0.1",
   264  					},
   265  					{
   266  						IP: "0.0.0.2",
   267  					},
   268  				},
   269  			},
   270  		},
   271  		ObjectMeta: metav1.ObjectMeta{
   272  			ResourceVersion: "2",
   273  		},
   274  	})
   275  	assert.ElementsMatch(t, []string{"0.0.0.1", "0.0.0.2"}, endpoints)
   276  }