63 lines
1.9 KiB
Go
63 lines
1.9 KiB
Go
package syncjob
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"linkport/internal/model"
|
|
"linkport/internal/store"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestCatalogOnlyDeviceSyncAndFirstSyncConflict(t *testing.T) {
|
|
object := &objectFixture{}
|
|
server := httptest.NewServer(http.HandlerFunc(object.serve))
|
|
defer server.Close()
|
|
a, ea := testDevice(t, server.URL, 1)
|
|
b, eb := testDevice(t, server.URL, 2)
|
|
c, ec := testDevice(t, server.URL, 3)
|
|
set := func(s *store.Store, project, environment string) {
|
|
t.Helper()
|
|
current, err := s.ScopeCatalog()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = s.SaveScopeCatalog(model.ScopeCatalog{Projects: []string{project}, Environments: []string{environment}}, current.Revision); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
set(a, "远端闲置项目", "开发")
|
|
runSync(t, ea, "pushed")
|
|
// A genuinely empty device can pull the catalog without a conflict.
|
|
runSync(t, ec, "pulled")
|
|
first, _ := a.ScopeCatalog()
|
|
pulled, _ := c.ScopeCatalog()
|
|
if !reflect.DeepEqual(first.ScopeCatalog, pulled.ScopeCatalog) {
|
|
t.Fatal("empty device did not receive catalog")
|
|
}
|
|
set(b, "本地闲置项目", "验收")
|
|
before, _ := b.ScopeCatalog()
|
|
if _, err := eb.Run(context.Background(), "test", Request{}); !errors.Is(err, ErrConflict) {
|
|
t.Fatalf("catalog-only local configuration overwritten: %v", err)
|
|
}
|
|
after, _ := b.ScopeCatalog()
|
|
if !reflect.DeepEqual(before.ScopeCatalog, after.ScopeCatalog) {
|
|
t.Fatal("conflict modified local choices")
|
|
}
|
|
state, err := b.WebDAVSyncState()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result, err := eb.Run(context.Background(), "test", Request{Resolution: "local", LocalHash: state.LocalHash, RemoteHash: state.RemoteHash})
|
|
if err != nil || result.Action != "pushed" {
|
|
t.Fatal("cannot resolve catalog-only conflict", err)
|
|
}
|
|
runSync(t, ea, "pulled")
|
|
after, _ = a.ScopeCatalog()
|
|
if !reflect.DeepEqual(before.ScopeCatalog, after.ScopeCatalog) {
|
|
t.Fatal("catalog-only changes did not synchronize")
|
|
}
|
|
}
|