Skip to content

Commit 860f7f7

Browse files
nan-yugoogle-oss-prow[bot]
authored andcommitted
test: add a test to confirm the git-sync not pulling latest issue (#1146)
`git-sync` v4.1.0 couldn't pull the latest commit from a branch when the branch has multiple remotes and the branch in different remotes are out of sync. This commit adds a test to confirm the behavior. A subsequent commit will update git-sync to v4.2.0 to fix this issue.
1 parent 14a0540 commit 860f7f7

File tree

2 files changed

+90
-5
lines changed

2 files changed

+90
-5
lines changed

e2e/nomostest/git-server.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"k8s.io/apimachinery/pkg/types"
2424
"k8s.io/apimachinery/pkg/util/intstr"
2525
"kpt.dev/configsync/e2e"
26+
"kpt.dev/configsync/e2e/nomostest/gitproviders"
2627
"kpt.dev/configsync/e2e/nomostest/testing"
2728
"kpt.dev/configsync/pkg/core"
2829
"kpt.dev/configsync/pkg/kinds"
@@ -32,7 +33,8 @@ import (
3233

3334
const testGitNamespace = "config-management-system-test"
3435
const testGitServer = "test-git-server"
35-
const testGitServerImage = testing.TestInfraArtifactRegistry + "/git-server:v1.0.0"
36+
37+
const testGitServerImage = testing.TestInfraArtifactRegistry + "/git-server:v1.0.0-68df304c"
3638
const testGitHTTPServer = "http-git-server"
3739
const testGitHTTPServerImage = testing.TestInfraArtifactRegistry + "/http-git-server:v1.0.0-b3b4984cd"
3840

@@ -208,13 +210,19 @@ func InitGitRepos(nt *NT, repos ...types.NamespacedName) {
208210
podName := pod.Name
209211

210212
for _, repo := range repos {
211-
nt.MustKubectl("exec", "-n", testGitNamespace, podName, "-c", testGitServer, "--",
212-
"git", "init", "--bare", "--shared", fmt.Sprintf("/git-server/repos/%s/%s", repo.Namespace, repo.Name))
213+
nt.MustKubectl("exec", "-n", testGitNamespace, podName,
214+
"-c", testGitServer, "--",
215+
"git", "init", "--bare", "--shared",
216+
// Use `main` as the initial branch so that `HEAD` can reference refs/heads/main
217+
"--initial-branch="+gitproviders.MainBranch,
218+
fmt.Sprintf("/git-server/repos/%s/%s", repo.Namespace, repo.Name))
213219
// We set receive.denyNonFastforwards to allow force pushes for legacy test support (bats). In the future we may
214220
// need this support for testing GKE clusters since we will likely be re-using the cluster in that case.
215221
// Alternatively, we could also run "rm -rf /git-server/repos/*" to clear out the state of the git server and
216222
// re-initialize.
217-
nt.MustKubectl("exec", "-n", testGitNamespace, podName, "-c", testGitServer, "--",
218-
"git", "-C", fmt.Sprintf("/git-server/repos/%s", repo), "config", "receive.denyNonFastforwards", "false")
223+
nt.MustKubectl("exec", "-n", testGitNamespace, podName,
224+
"-c", testGitServer, "--",
225+
"git", "-C", fmt.Sprintf("/git-server/repos/%s", repo),
226+
"config", "receive.denyNonFastforwards", "false")
219227
}
220228
}

e2e/testcases/git_sync_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package e2e
16+
17+
import (
18+
"testing"
19+
20+
corev1 "k8s.io/api/core/v1"
21+
"k8s.io/apimachinery/pkg/types"
22+
"kpt.dev/configsync/e2e/nomostest"
23+
"kpt.dev/configsync/e2e/nomostest/gitproviders"
24+
nomostesting "kpt.dev/configsync/e2e/nomostest/testing"
25+
"kpt.dev/configsync/pkg/api/configmanagement"
26+
"kpt.dev/configsync/pkg/api/configsync"
27+
"kpt.dev/configsync/pkg/testing/fake"
28+
)
29+
30+
func TestMultipleRemoteBranchesOutOfSync(t *testing.T) {
31+
nt := nomostest.New(t, nomostesting.ACMController)
32+
33+
rs := fake.RootSyncObjectV1Beta1(configsync.RootSyncName)
34+
if err := nt.KubeClient.Get(configsync.RootSyncName, configmanagement.ControllerNamespace, rs); err != nil {
35+
nt.T.Fatal(err)
36+
}
37+
initialSyncedCommit := rs.Status.LastSyncedCommit
38+
39+
nt.T.Log("Create an extra remote tracking branch")
40+
nt.Must(nt.RootRepos[configsync.RootSyncName].Push("HEAD:refs/remotes/upstream/main"))
41+
nt.T.Cleanup(func() {
42+
// Delete the remote tracking branch in the end so other subsequent tests
43+
// can pull from the latest commit, instead of the HEAD of the remote.
44+
nt.Must(nt.RootRepos[configsync.RootSyncName].Push(":refs/remotes/upstream/main"))
45+
})
46+
47+
nt.T.Logf("Update the remote main branch by adding a test namespace")
48+
nt.Must(nt.RootRepos[configsync.RootSyncName].Add("acme/namespaces/hello/ns.yaml", fake.NamespaceObject("hello")))
49+
nt.Must(nt.RootRepos[configsync.RootSyncName].CommitAndPush("add Namespace"))
50+
51+
nt.T.Logf("Mitigation: set spec.git.branch to HEAD to pull the latest commit")
52+
nomostest.SetGitBranch(nt, configsync.RootSyncName, "HEAD")
53+
// WatchForAllSyncs validates RootSync's lastSyncedCommit is updated to the
54+
// local HEAD with the DefaultRootSha1Fn function.
55+
if err := nt.WatchForAllSyncs(); err != nil {
56+
nt.T.Fatal(err)
57+
}
58+
if err := nt.Validate("hello", "", &corev1.Namespace{}); err != nil {
59+
nt.T.Fatal(err)
60+
}
61+
62+
// Apply the mitigation first to validate Config Sync couldn't pull the latest commit.
63+
nt.T.Logf("Verify the issue exist with the default branch and revision")
64+
nomostest.SetGitBranch(nt, configsync.RootSyncName, gitproviders.MainBranch)
65+
if err := nt.WatchForAllSyncs(nomostest.WithRootSha1Func(
66+
// DefaultRootSha1Fn returns the hash with `git rev-parse HEAD`, which is
67+
// different from `git ls-remote ...`
68+
// So, overwrite the root hash with the initial lastSyncedCommit.
69+
func(_ *nomostest.NT, _ types.NamespacedName) (string, error) {
70+
return initialSyncedCommit, nil
71+
})); err != nil {
72+
nt.T.Fatal(err)
73+
}
74+
if err := nt.ValidateNotFound("hello", "", &corev1.Namespace{}); err != nil {
75+
nt.T.Fatal(err)
76+
}
77+
}

0 commit comments

Comments
 (0)