diff --git a/model/model.go b/model/model.go index 8356954f3..27a0bbdf6 100644 --- a/model/model.go +++ b/model/model.go @@ -951,6 +951,9 @@ func (m *Model) clusterConfig(node protocol.NodeID) protocol.ClusterConfigMessag ID: repo, } for _, node := range m.repoNodes[repo] { + // NodeID is a value type, but with an underlying array. Copy it + // so we don't grab aliases to the same array later on in node[:] + node := node // TODO: Set read only bit when relevant cr.Nodes = append(cr.Nodes, protocol.Node{ ID: node[:], diff --git a/model/model_test.go b/model/model_test.go index 0a417fcfe..864ccc1c4 100644 --- a/model/model_test.go +++ b/model/model_test.go @@ -301,3 +301,71 @@ func TestNodeRename(t *testing.T) { t.Errorf("Node name got overwritten") } } + +func TestClusterConfig(t *testing.T) { + cfg := config.New("test", node1) + cfg.Nodes = []config.NodeConfiguration{ + { + NodeID: node1, + }, + { + NodeID: node2, + }, + } + cfg.Repositories = []config.RepositoryConfiguration{ + { + ID: "repo1", + Nodes: []config.RepositoryNodeConfiguration{ + {NodeID: node1}, + {NodeID: node2}, + }, + }, + { + ID: "repo2", + Nodes: []config.RepositoryNodeConfiguration{ + {NodeID: node1}, + {NodeID: node2}, + }, + }, + } + + db, _ := leveldb.Open(storage.NewMemStorage(), nil) + + m := NewModel("/tmp", &cfg, "node", "syncthing", "dev", db) + m.AddRepo(cfg.Repositories[0]) + m.AddRepo(cfg.Repositories[1]) + + cm := m.clusterConfig(node2) + + if l := len(cm.Repositories); l != 2 { + t.Fatalf("Incorrect number of repos %d != 2", l) + } + + r := cm.Repositories[0] + if r.ID != "repo1" { + t.Errorf("Incorrect repo %q != repo1", r.ID) + } + if l := len(r.Nodes); l != 2 { + t.Errorf("Incorrect number of nodes %d != 2", l) + } + if id := r.Nodes[0].ID; bytes.Compare(id, node1[:]) != 0 { + t.Errorf("Incorrect node ID %x != %x", id, node1) + } + if id := r.Nodes[1].ID; bytes.Compare(id, node2[:]) != 0 { + t.Errorf("Incorrect node ID %x != %x", id, node2) + } + + r = cm.Repositories[1] + if r.ID != "repo2" { + t.Errorf("Incorrect repo %q != repo2", r.ID) + } + if l := len(r.Nodes); l != 2 { + t.Errorf("Incorrect number of nodes %d != 2", l) + } + if id := r.Nodes[0].ID; bytes.Compare(id, node1[:]) != 0 { + t.Errorf("Incorrect node ID %x != %x", id, node1) + } + if id := r.Nodes[1].ID; bytes.Compare(id, node2[:]) != 0 { + t.Errorf("Incorrect node ID %x != %x", id, node2) + } +}