github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/store/redis/node_test.go (about)

     1  package redis
     2  
     3  import (
     4  	"context"
     5  	"path/filepath"
     6  	"time"
     7  
     8  	"github.com/projecteru2/core/types"
     9  )
    10  
    11  func (s *RediaronTestSuite) TestAddNode() {
    12  	ctx := context.Background()
    13  	podname := "testpod"
    14  	_, err := s.rediaron.AddPod(ctx, podname, "test")
    15  	s.NoError(err)
    16  	_, err = s.rediaron.AddPod(ctx, "numapod", "test")
    17  	s.NoError(err)
    18  	s.rediaron.config.Scheduler.ShareBase = 100
    19  	labels := map[string]string{"test": "1"}
    20  
    21  	// with tls
    22  	ca := `-----BEGIN CERTIFICATE-----
    23  MIIC7TCCAdWgAwIBAgIJAM8uLRZf9jttMA0GCSqGSIb3DQEBCwUAMA0xCzAJBgNV
    24  BAYTAkNOMB4XDTE4MDYxODA5MTkwNloXDTI4MDYxNTA5MTkwNlowDTELMAkGA1UE
    25  BhMCQ04wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDWMezGaddx5dmf
    26  i3p28ZqBk2JHhAxlKqvKTIfaH3D/44roZD0kXgLNRYp3FxV4TUK4A2jxvOxfF/UZ
    27  /fLjX66zsi/nKX0Uv3i+tk4MbU3H+A0N2YsE7Nx5ldDSw1gUe2LoA687Fn0iZHED
    28  wWdUhkev3291wK2DY0BnSfTUFejhaUbDPgbTIaQwzxFCN26QpO5EBBFQPfdp9ilg
    29  rfhYgoQ016BWM3Nuor3yeX9WPZdCdCq0/zM+Pn62A6EUBnC0oLskz5Sbdq3kAio2
    30  FmQLEu2ZGV9BzfIPFLfI/K9y7kH+OvbaFlZxjqsYVqjpIwmeCINYcQGP6JVrPvcQ
    31  5X4u0oHVAgMBAAGjUDBOMB0GA1UdDgQWBBRElDLtPaaMarh8xKkHsmHZmug6sDAf
    32  BgNVHSMEGDAWgBRElDLtPaaMarh8xKkHsmHZmug6sDAMBgNVHRMEBTADAQH/MA0G
    33  CSqGSIb3DQEBCwUAA4IBAQBGREhJnNwgZIPio/Dof1JUWSmD1uamNMNQSbluWs+5
    34  fQrxGPMzh0maEfah21R2fXcrqXDH9qa7lGGPWc1wrn/pqTPQYYZyeCiEOZAHYMM7
    35  orSraF6M9pwLn/MB5O9onTg1RkEABpw6K0YsKShTW/rM0o5JMGD/fnX4Vdr/vsTi
    36  190TboLkQwFd+16x1C6/YVIBx2wM8b+shm5sZeXHQNDD4Cp8iQOPKwxot1vnefTl
    37  +ksb5sqiWIx88LBRouXI65ORPHSejZrr3iVowu9EvtwaxQ8E9QXfb1eks0WWzPkX
    38  lgCr4uFZk7z7nRQVUfLSkNYNGKY8P62xLtjigp4rYsLB
    39  -----END CERTIFICATE-----`
    40  	cert := `-----BEGIN CERTIFICATE-----
    41  MIICmjCCAYICCQDeEJuzNfHgXzANBgkqhkiG9w0BAQsFADANMQswCQYDVQQGEwJD
    42  TjAeFw0xODA2MTgwOTE5MDZaFw0yODA2MTUwOTE5MDZaMBExDzANBgNVBAMMBmNs
    43  aWVudDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAPftUgiEEXkQK2Mj
    44  3PNTNZsV6K8ouKyUlVAA/7w102VsP27oSbzjTc1ad9Hyfc9PxYe1l7sbTgKqlC9S
    45  p5C1wTswtnbcVwdIUMtizupr9MCoBLXpEFTpNS0FxGKyltJTGOppUvlN9VKbrV4B
    46  rUIGpTClbvfAc8iLymYn05sRw+JMBD5rStv/KnZPYeIYliMaS147fXGQDhgQ4gdx
    47  6cguL22hfvWGov3Sot1FsANStVqYAEvd6Bk0KgZi2eupRigduboUth1VzRN4TNSi
    48  Rzk2Vy6CBhMyYRj0UogJD9WfI//qej1/IOFA9hiOY6+WflFiaqCi4TbJd2D9IDof
    49  hGE7bdsCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAKgW1Co/H34XrXS+ms+9X9xKJ
    50  sv8HCrNUrsKF3/srwkNmLySJYqAaIcUmXKCzslYOF5mPi3XCcKNVWdH8FYwQxgJJ
    51  mK9a460/0z9Znf0J1cfFELR4BTvFEp0s2HY38W5yojiJqo6zQIztD1hqONHJPeF8
    52  jvPFvH2WREjVLvF8Y8U+qWcRhZRoPex2a6rr/33kgx1+cuNC9opBD0pIa5+HpnqW
    53  M+P7/vNqjwaYS5HMKPs9bqrtYLKAIVSWa4Do0kDd2sX8/ngMPsxTJzZEqbXBnEJt
    54  YsRamLe0Yu66g0ZhRzHpUxOio9+LyT1vs/hRGEtoEXZfZUF39+4J4C6xmNcE4A==
    55  -----END CERTIFICATE-----`
    56  	certkey := `-----BEGIN PRIVATE KEY-----
    57  MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQD37VIIhBF5ECtj
    58  I9zzUzWbFeivKLislJVQAP+8NdNlbD9u6Em8403NWnfR8n3PT8WHtZe7G04CqpQv
    59  UqeQtcE7MLZ23FcHSFDLYs7qa/TAqAS16RBU6TUtBcRispbSUxjqaVL5TfVSm61e
    60  Aa1CBqUwpW73wHPIi8pmJ9ObEcPiTAQ+a0rb/yp2T2HiGJYjGkteO31xkA4YEOIH
    61  cenILi9toX71hqL90qLdRbADUrVamABL3egZNCoGYtnrqUYoHbm6FLYdVc0TeEzU
    62  okc5NlcuggYTMmEY9FKICQ/VnyP/6no9fyDhQPYYjmOvln5RYmqgouE2yXdg/SA6
    63  H4RhO23bAgMBAAECggEAY4jkoUScWzUxpgi04P9sCwo9s2yuz6KLW2Y7RY16hEJ0
    64  KQua5vl+t831QtWOytck33n5I4YvyIRBH8qYOVGu9Rt2dbu6ONNAlJbjqVuUFHCg
    65  C4Q5KU3DKoMhN9qpEGGKJDoKtMomjnavoIkdzN8sHJ6eMVsTYNU2edLNcnksYkIG
    66  Oi+LZFaYqI3Gn5GfWznJi12CIcNjutipWHB2Q01r/THEUUDAm6f/vUonVbJKn0GQ
    67  dHUNl52Z3Gg6i+R1zz/ilm0a6wbVhz0aKTyhGOru+z9dZzxktl51JUws42zbTp7e
    68  xU8x+kA4Nvec228a5wcpS1uiO1ogme48grEK2NhqgQKBgQD+VD5WiGVGUuGRgsQ6
    69  dpO+IcG5LZC0lrxGR3zY0fvtPyqqDmnmA80jefZUy5THsJ0FMg6lKdfIIO+W2nkk
    70  +jG4RAv/AQiM2QT9jQMDpF6eJxSQYm4zcGhtrIy0i79upZM0sPoBxzNQVUWpRxPT
    71  YJWwiTaPFc0KaQ40ZbU8s1xDwQKBgQD5jk8wFoSLSN/nTVY3m23SJBejGBxyUtP/
    72  mIPBmSdRxbdB2MoTTg+zRiQj7McUu8E/SW6RB4ScQWBRjbgin0lTVYUn25xlrXDB
    73  sFh/AWq4m98O5ju/fZGDRSheDkMUxyC0DFSin91sF7iBZqv/HJBITncTghhrLXpQ
    74  umltothomwKBgQDxInysXMvw3jpCVYKpj63K0oSzhzExF83QsIz9ojJDIeXYsKvV
    75  SvtfzI4ynYclwh1ORMS/8ilF9XxUQjYkSheEBvh8wcUSjdz+bYlTFbAkMRd9QeYM
    76  XWKVwcjykaFiThiBF98ienT7kK3orpxsiKHEbIRPK7NpUGwIX/pzX/d1wQKBgQDF
    77  /GtCwXqibkyE20xdjYhRQaUnFYfsA16B12Qggfs52tyK9w1Kx5GZLzqY7c772gF0
    78  zjNUCFzjAtMBoKfHgAvSe3TKrGamHDXq1JdBG8SpdbA/x9T7FQoO1R0zkakSoPCH
    79  J4k2BBLNIPyWXPhzyxuE4guChKIO1ePGjD38Z0e9pQKBgFHI//1VkRSZu49jrGOw
    80  Dl0aR0+ZbHq5hv5feDdpeKxMxKkKnCu1cl47gKAyFet5nvK7htBUk9aIph8zDBZj
    81  cmag5uyTcIogsd5GyOg06jDD1aCqz3FbTX1KQLeSFQUCTT+m100rohrc2rW5m4Al
    82  RdCPRPt513WozkJZZAjUSP2U
    83  -----END PRIVATE KEY-----`
    84  	nodename3 := "nodename3"
    85  	endpoint3 := "tcp://path"
    86  	s.rediaron.config.CertPath = "/tmp"
    87  	node3, err := s.rediaron.doAddNode(ctx, nodename3, endpoint3, podname, ca, cert, certkey, labels, false)
    88  	s.NoError(err)
    89  	_, err = s.rediaron.makeClient(ctx, node3)
    90  	s.Error(err)
    91  	// failed by get key
    92  	node3.Name = "nokey"
    93  	_, err = s.rediaron.makeClient(ctx, node3)
    94  	s.Error(err)
    95  }
    96  
    97  func (s *RediaronTestSuite) TestRemoveNode() {
    98  	ctx := context.Background()
    99  	node, err := s.rediaron.doAddNode(ctx, "test", "mock://", "testpod", "", "", "", nil, false)
   100  	s.NoError(err)
   101  	s.Equal(node.Name, "test")
   102  	s.NoError(s.rediaron.RemoveNode(ctx, nil))
   103  	s.NoError(s.rediaron.RemoveNode(ctx, node))
   104  }
   105  
   106  func (s *RediaronTestSuite) TestGetNode() {
   107  	ctx := context.Background()
   108  	node, err := s.rediaron.doAddNode(ctx, "test", "mock://", "testpod", "", "", "", nil, false)
   109  	s.NoError(err)
   110  	s.Equal(node.Name, "test")
   111  	_, err = s.rediaron.GetNode(ctx, "wtf")
   112  	s.Error(err)
   113  	n, err := s.rediaron.GetNode(ctx, "test")
   114  	s.NoError(err)
   115  	s.Equal(node.Name, n.Name)
   116  }
   117  
   118  func (s *RediaronTestSuite) TestGetNodesByPod() {
   119  	ctx := context.Background()
   120  	node, err := s.rediaron.doAddNode(ctx, "test", "mock://", "testpod", "", "", "", map[string]string{"x": "y"}, false)
   121  	s.NoError(err)
   122  	s.Equal(node.Name, "test")
   123  	ns, err := s.rediaron.GetNodesByPod(ctx, &types.NodeFilter{Podname: "wtf", All: false})
   124  	s.NoError(err)
   125  	s.Empty(ns)
   126  	ns, err = s.rediaron.GetNodesByPod(ctx, &types.NodeFilter{Podname: "testpod", All: true})
   127  	s.NoError(err)
   128  	s.NotEmpty(ns)
   129  	_, err = s.rediaron.AddPod(ctx, "testpod", "")
   130  	s.NoError(err)
   131  	ns, err = s.rediaron.GetNodesByPod(ctx, &types.NodeFilter{All: false})
   132  	s.NoError(err)
   133  	s.Len(ns, 1) // because mock forced to up, so here is 1
   134  	ns, err = s.rediaron.GetNodesByPod(ctx, &types.NodeFilter{All: true})
   135  	s.NoError(err)
   136  	s.NotEmpty(ns)
   137  }
   138  
   139  func (s *RediaronTestSuite) TestUpdateNode() {
   140  	ctx := context.Background()
   141  	node, err := s.rediaron.doAddNode(ctx, "test", "mock://", "testpod", "", "", "", map[string]string{"x": "y"}, false)
   142  	s.NoError(err)
   143  	s.Equal(node.Name, "test")
   144  	fakeNode := &types.Node{
   145  		NodeMeta: types.NodeMeta{
   146  			Name:     "nil",
   147  			Podname:  "wtf",
   148  			Endpoint: "mock://hh",
   149  			Ca:       "hh",
   150  			Cert:     "hh",
   151  			Key:      "hh",
   152  		},
   153  	}
   154  	s.NoError(s.rediaron.UpdateNodes(ctx, fakeNode))
   155  	s.NoError(s.rediaron.UpdateNodes(ctx, node))
   156  }
   157  
   158  func (s *RediaronTestSuite) TestUpdateNodeResource() {
   159  	ctx := context.Background()
   160  	node, err := s.rediaron.doAddNode(ctx, "test", "mock://", "testpod", "", "", "", map[string]string{"x": "y"}, false)
   161  	s.NoError(err)
   162  	s.Equal(node.Name, "test")
   163  }
   164  
   165  func (s *RediaronTestSuite) TestExtractNodename() {
   166  	s.Equal(extractNodename("/nodestatus/testname"), "testname")
   167  }
   168  
   169  func (s *RediaronTestSuite) TestSetNodeStatus() {
   170  	node := &types.Node{
   171  		NodeMeta: types.NodeMeta{
   172  			Name:     "testname",
   173  			Endpoint: "ep",
   174  			Podname:  "testpod",
   175  		},
   176  	}
   177  	s.NoError(s.rediaron.SetNodeStatus(context.Background(), node, 1))
   178  	key := filepath.Join(nodeStatusPrefix, node.Name)
   179  
   180  	// not expired yet
   181  	_, err := s.rediaron.GetOne(context.Background(), key)
   182  	s.NoError(err)
   183  	// expired
   184  	time.Sleep(2 * time.Second)
   185  	// fastforward
   186  	s.rediserver.FastForward(2 * time.Second)
   187  	_, err = s.rediaron.GetOne(context.Background(), key)
   188  	s.Error(err)
   189  }
   190  
   191  func (s *RediaronTestSuite) TestGetNodeStatus() {
   192  	node := &types.Node{
   193  		NodeMeta: types.NodeMeta{
   194  			Name:     "testname",
   195  			Endpoint: "ep",
   196  			Podname:  "testpod",
   197  		},
   198  	}
   199  	s.NoError(s.rediaron.SetNodeStatus(context.Background(), node, 1))
   200  
   201  	// not expired yet
   202  	ns, err := s.rediaron.GetNodeStatus(context.Background(), node.Name)
   203  	s.NoError(err)
   204  	s.Equal(ns.Nodename, node.Name)
   205  	s.True(ns.Alive)
   206  	// expired
   207  	time.Sleep(2 * time.Second)
   208  	// fastforward
   209  	s.rediserver.FastForward(2 * time.Second)
   210  	ns1, err := s.rediaron.GetNodeStatus(context.Background(), node.Name)
   211  	s.Error(err)
   212  	s.Nil(ns1)
   213  }
   214  
   215  func (s *RediaronTestSuite) TestNodeStatusStream() {
   216  	node := &types.Node{
   217  		NodeMeta: types.NodeMeta{
   218  			Name:     "testname",
   219  			Endpoint: "ep",
   220  			Podname:  "testpod",
   221  		},
   222  	}
   223  
   224  	go func() {
   225  		ctx, cancel := context.WithTimeout(context.Background(), 1000*time.Millisecond)
   226  		defer cancel()
   227  		for {
   228  			select {
   229  			case <-ctx.Done():
   230  				return
   231  			default:
   232  			}
   233  			time.Sleep(500 * time.Millisecond)
   234  			s.NoError(s.rediaron.SetNodeStatus(context.Background(), node, 1))
   235  			// manually trigger
   236  			triggerMockedKeyspaceNotification(s.rediaron.cli, filepath.Join(nodeStatusPrefix, node.Name), actionSet)
   237  		}
   238  	}()
   239  
   240  	ctx, cancel := context.WithCancel(context.Background())
   241  	ch := s.rediaron.NodeStatusStream(ctx)
   242  	go func() {
   243  		time.Sleep(1500 * time.Millisecond)
   244  		// manually trigger
   245  		triggerMockedKeyspaceNotification(s.rediaron.cli, filepath.Join(nodeStatusPrefix, node.Name), actionExpired)
   246  		time.Sleep(500 * time.Millisecond)
   247  		cancel()
   248  	}()
   249  
   250  	statuses := []*types.NodeStatus{}
   251  	for m := range ch {
   252  		statuses = append(statuses, m)
   253  	}
   254  	for _, m := range statuses[:len(statuses)-1] {
   255  		s.True(m.Alive)
   256  	}
   257  	s.False(statuses[len(statuses)-1].Alive)
   258  }