github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/registry/mdns/mdns_test.go (about)

     1  package mdns
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/volts-dev/volts/registry"
     9  )
    10  
    11  func TestMDNS(t *testing.T) {
    12  	// skip test in travis because of sendto: operation not permitted error
    13  	if travis := os.Getenv("TRAVIS"); travis == "true" {
    14  		t.Skip()
    15  	}
    16  
    17  	testData := []*registry.Service{
    18  		{
    19  			Name:    "test1",
    20  			Version: "1.0.1",
    21  			Nodes: []*registry.Node{
    22  				{
    23  					Id:      "test1-1",
    24  					Address: "10.0.0.1:10001",
    25  					Metadata: map[string]string{
    26  						"foo": "bar",
    27  					},
    28  				},
    29  			},
    30  		},
    31  		{
    32  			Name:    "test2",
    33  			Version: "1.0.2",
    34  			Nodes: []*registry.Node{
    35  				{
    36  					Id:      "test2-1",
    37  					Address: "10.0.0.2:10002",
    38  					Metadata: map[string]string{
    39  						"foo2": "bar2",
    40  					},
    41  				},
    42  			},
    43  		},
    44  		{
    45  			Name:    "test3",
    46  			Version: "1.0.3",
    47  			Nodes: []*registry.Node{
    48  				{
    49  					Id:      "test3-1",
    50  					Address: "10.0.0.3:10003",
    51  					Metadata: map[string]string{
    52  						"foo3": "bar3",
    53  					},
    54  				},
    55  			},
    56  		},
    57  	}
    58  
    59  	travis := os.Getenv("TRAVIS")
    60  
    61  	var opts []registry.Option
    62  
    63  	if travis == "true" {
    64  		opts = append(opts, registry.Timeout(time.Millisecond*100))
    65  	}
    66  
    67  	// new registry
    68  	r := New(opts...)
    69  
    70  	for _, service := range testData {
    71  		// register service
    72  		if err := r.Register(service); err != nil {
    73  			t.Fatal(err)
    74  		}
    75  
    76  		// get registered service
    77  		s, err := r.GetService(service.Name)
    78  		if err != nil {
    79  			t.Fatal(err)
    80  		}
    81  
    82  		if len(s) != 1 {
    83  			t.Fatalf("Expected one result for %s got %d", service.Name, len(s))
    84  
    85  		}
    86  
    87  		if s[0].Name != service.Name {
    88  			t.Fatalf("Expected name %s got %s", service.Name, s[0].Name)
    89  		}
    90  
    91  		if s[0].Version != service.Version {
    92  			t.Fatalf("Expected version %s got %s", service.Version, s[0].Version)
    93  		}
    94  
    95  		if len(s[0].Nodes) != 1 {
    96  			t.Fatalf("Expected 1 node, got %d", len(s[0].Nodes))
    97  		}
    98  
    99  		node := s[0].Nodes[0]
   100  
   101  		if node.Id != service.Nodes[0].Id {
   102  			t.Fatalf("Expected node id %s got %s", service.Nodes[0].Id, node.Id)
   103  		}
   104  
   105  		if node.Address != service.Nodes[0].Address {
   106  			t.Fatalf("Expected node address %s got %s", service.Nodes[0].Address, node.Address)
   107  		}
   108  	}
   109  
   110  	services, err := r.ListServices()
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  
   115  	for _, service := range testData {
   116  		var seen bool
   117  		for _, s := range services {
   118  			if s.Name == service.Name {
   119  				seen = true
   120  				break
   121  			}
   122  		}
   123  		if !seen {
   124  			t.Fatalf("Expected service %s got nothing", service.Name)
   125  		}
   126  
   127  		// deregister
   128  		if err := r.Deregister(service); err != nil {
   129  			t.Fatal(err)
   130  		}
   131  
   132  		time.Sleep(time.Millisecond * 5)
   133  
   134  		// check its gone
   135  		s, _ := r.GetService(service.Name)
   136  		if len(s) > 0 {
   137  			t.Fatalf("Expected nothing got %+v", s[0])
   138  		}
   139  	}
   140  
   141  }
   142  
   143  func TestEncoding(t *testing.T) {
   144  	testData := []*mdnsTxt{
   145  		{
   146  			Version: "1.0.0",
   147  			Metadata: map[string]string{
   148  				"foo": "bar",
   149  			},
   150  			Endpoints: []*registry.Endpoint{
   151  				{
   152  					Name: "endpoint1",
   153  					Request: &registry.Value{
   154  						Name: "request",
   155  						Type: "request",
   156  					},
   157  					Response: &registry.Value{
   158  						Name: "response",
   159  						Type: "response",
   160  					},
   161  					Metadata: map[string]string{
   162  						"foo1": "bar1",
   163  					},
   164  				},
   165  			},
   166  		},
   167  	}
   168  
   169  	for _, d := range testData {
   170  		encoded, err := encode(d)
   171  		if err != nil {
   172  			t.Fatal(err)
   173  		}
   174  
   175  		for _, txt := range encoded {
   176  			if len(txt) > 255 {
   177  				t.Fatalf("One of parts for txt is %d characters", len(txt))
   178  			}
   179  		}
   180  
   181  		decoded, err := decode(encoded)
   182  		if err != nil {
   183  			t.Fatal(err)
   184  		}
   185  
   186  		if decoded.Version != d.Version {
   187  			t.Fatalf("Expected version %s got %s", d.Version, decoded.Version)
   188  		}
   189  
   190  		if len(decoded.Endpoints) != len(d.Endpoints) {
   191  			t.Fatalf("Expected %d endpoints, got %d", len(d.Endpoints), len(decoded.Endpoints))
   192  		}
   193  
   194  		for k, v := range d.Metadata {
   195  			if val := decoded.Metadata[k]; val != v {
   196  				t.Fatalf("Expected %s=%s got %s=%s", k, v, k, val)
   197  			}
   198  		}
   199  	}
   200  
   201  }
   202  
   203  func TestWatcher(t *testing.T) {
   204  	if travis := os.Getenv("TRAVIS"); travis == "true" {
   205  		t.Skip()
   206  	}
   207  
   208  	testData := []*registry.Service{
   209  		{
   210  			Name:    "test1",
   211  			Version: "1.0.1",
   212  			Nodes: []*registry.Node{
   213  				{
   214  					Id:      "test1-1",
   215  					Address: "10.0.0.1:10001",
   216  					Metadata: map[string]string{
   217  						"foo": "bar",
   218  					},
   219  				},
   220  			},
   221  		},
   222  		{
   223  			Name:    "test2",
   224  			Version: "1.0.2",
   225  			Nodes: []*registry.Node{
   226  				{
   227  					Id:      "test2-1",
   228  					Address: "10.0.0.2:10002",
   229  					Metadata: map[string]string{
   230  						"foo2": "bar2",
   231  					},
   232  				},
   233  			},
   234  		},
   235  		{
   236  			Name:    "test3",
   237  			Version: "1.0.3",
   238  			Nodes: []*registry.Node{
   239  				{
   240  					Id:      "test3-1",
   241  					Address: "10.0.0.3:10003",
   242  					Metadata: map[string]string{
   243  						"foo3": "bar3",
   244  					},
   245  				},
   246  			},
   247  		},
   248  	}
   249  
   250  	testFn := func(service, s *registry.Service) {
   251  		if s == nil {
   252  			t.Fatalf("Expected one result for %s got nil", service.Name)
   253  
   254  		}
   255  
   256  		if s.Name != service.Name {
   257  			t.Fatalf("Expected name %s got %s", service.Name, s.Name)
   258  		}
   259  
   260  		if s.Version != service.Version {
   261  			t.Fatalf("Expected version %s got %s", service.Version, s.Version)
   262  		}
   263  
   264  		if len(s.Nodes) != 1 {
   265  			t.Fatalf("Expected 1 node, got %d", len(s.Nodes))
   266  		}
   267  
   268  		node := s.Nodes[0]
   269  
   270  		if node.Id != service.Nodes[0].Id {
   271  			t.Fatalf("Expected node id %s got %s", service.Nodes[0].Id, node.Id)
   272  		}
   273  
   274  		if node.Address != service.Nodes[0].Address {
   275  			t.Fatalf("Expected node address %s got %s", service.Nodes[0].Address, node.Address)
   276  		}
   277  	}
   278  
   279  	travis := os.Getenv("TRAVIS")
   280  
   281  	var opts []registry.Option
   282  
   283  	if travis == "true" {
   284  		opts = append(opts, registry.Timeout(time.Millisecond*100))
   285  	}
   286  
   287  	// new registry
   288  	r := New(opts...)
   289  
   290  	w, err := r.Watcher()
   291  	if err != nil {
   292  		t.Fatal(err)
   293  	}
   294  	defer w.Stop()
   295  
   296  	for _, service := range testData {
   297  		// register service
   298  		if err := r.Register(service); err != nil {
   299  			t.Fatal(err)
   300  		}
   301  
   302  		for {
   303  			res, err := w.Next()
   304  			if err != nil {
   305  				t.Fatal(err)
   306  			}
   307  
   308  			if res.Service.Name != service.Name {
   309  				continue
   310  			}
   311  
   312  			if res.Action != "create" {
   313  				t.Fatalf("Expected create event got %s for %s", res.Action, res.Service.Name)
   314  			}
   315  
   316  			testFn(service, res.Service)
   317  			break
   318  		}
   319  
   320  		// deregister
   321  		if err := r.Deregister(service); err != nil {
   322  			t.Fatal(err)
   323  		}
   324  
   325  		for {
   326  			res, err := w.Next()
   327  			if err != nil {
   328  				t.Fatal(err)
   329  			}
   330  
   331  			if res.Service.Name != service.Name {
   332  				continue
   333  			}
   334  
   335  			if res.Action != "delete" {
   336  				continue
   337  			}
   338  
   339  			testFn(service, res.Service)
   340  			break
   341  		}
   342  	}
   343  }