|
| 1 | +package sync |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/loft-sh/devspace/helper/remote" |
| 12 | + "github.com/loft-sh/devspace/helper/server" |
| 13 | + "github.com/loft-sh/devspace/pkg/devspace/config/versions/latest" |
| 14 | + "github.com/loft-sh/devspace/pkg/util/log" |
| 15 | +) |
| 16 | + |
| 17 | +func TestInitialSyncRemotePreference(t *testing.T) { |
| 18 | + for _, testCase := range []struct { |
| 19 | + strategy latest.InitialSyncStrategy |
| 20 | + want string |
| 21 | + }{ |
| 22 | + {latest.InitialSyncStrategyMirrorRemote, "remote"}, |
| 23 | + {latest.InitialSyncStrategyPreferRemote, "remote"}, |
| 24 | + {latest.InitialSyncStrategyPreferNewest, "local"}, |
| 25 | + {latest.InitialSyncStrategyMirrorLocal, "local"}, |
| 26 | + {latest.InitialSyncStrategyPreferLocal, "local"}, |
| 27 | + {latest.InitialSyncStrategyKeepAll, "local"}, |
| 28 | + {latest.InitialSyncStrategyDisabled, "local"}, |
| 29 | + } { |
| 30 | + t.Run(string(testCase.strategy), func(t *testing.T) { |
| 31 | + local, remotePath := t.TempDir(), t.TempDir() |
| 32 | + localFile := filepath.Join(local, "conflict.txt") |
| 33 | + remoteFile := filepath.Join(remotePath, "conflict.txt") |
| 34 | + writeFile := func(filename, content string, modified time.Time) { |
| 35 | + t.Helper() |
| 36 | + if err := os.WriteFile(filename, []byte(content), 0644); err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + if err := os.Chtimes(filename, modified, modified); err != nil { |
| 40 | + t.Fatal(err) |
| 41 | + } |
| 42 | + } |
| 43 | + writeFile(localFile, "local", time.Unix(2000000000, 0)) |
| 44 | + writeFile(remoteFile, "remote", time.Unix(1700000000, 0)) |
| 45 | + |
| 46 | + client, err := NewSync(context.Background(), local, Options{ |
| 47 | + InitialSync: testCase.strategy, |
| 48 | + UpstreamDisabled: true, |
| 49 | + Log: log.Discard, |
| 50 | + }) |
| 51 | + if err != nil { |
| 52 | + t.Fatal(err) |
| 53 | + } |
| 54 | + t.Cleanup(func() { client.Stop(nil) }) |
| 55 | + // Download-only sync still marks the upstream initial-sync state complete. |
| 56 | + client.upstream = &upstream{} |
| 57 | + |
| 58 | + clientConn, serverConn := net.Pipe() |
| 59 | + t.Cleanup(func() { |
| 60 | + _ = clientConn.Close() |
| 61 | + _ = serverConn.Close() |
| 62 | + }) |
| 63 | + go func() { |
| 64 | + if err := server.StartDownstreamServer(serverConn, serverConn, &server.DownstreamOptions{ |
| 65 | + RemotePath: remotePath, |
| 66 | + Polling: true, |
| 67 | + }); err != nil { |
| 68 | + panic(err) |
| 69 | + } |
| 70 | + }() |
| 71 | + if err := client.InitDownstream(clientConn, clientConn); err != nil { |
| 72 | + t.Fatal(err) |
| 73 | + } |
| 74 | + uploadDone := make(chan struct{}) |
| 75 | + if err := client.initialSync(uploadDone, nil); err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + <-uploadDone |
| 79 | + content, err := os.ReadFile(localFile) |
| 80 | + if err != nil { |
| 81 | + t.Fatal(err) |
| 82 | + } |
| 83 | + if string(content) != testCase.want { |
| 84 | + t.Errorf("initial sync: got %q, want %q", content, testCase.want) |
| 85 | + } |
| 86 | + |
| 87 | + // Later downloads must continue to protect a newer, pending local edit. |
| 88 | + writeFile(localFile, "pending local edit", time.Unix(2000000000, 0)) |
| 89 | + writeFile(remoteFile, "later remote edit", time.Unix(1700000001, 0)) |
| 90 | + if err := client.downstream.applyChanges([]*remote.Change{{ |
| 91 | + ChangeType: remote.ChangeType_CHANGE, |
| 92 | + Path: "/conflict.txt", |
| 93 | + }}, false); err != nil { |
| 94 | + t.Fatal(err) |
| 95 | + } |
| 96 | + content, err = os.ReadFile(localFile) |
| 97 | + if err != nil { |
| 98 | + t.Fatal(err) |
| 99 | + } |
| 100 | + if string(content) != "pending local edit" { |
| 101 | + t.Errorf("normal sync overwrote a newer local edit: %q", content) |
| 102 | + } |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
0 commit comments