|
| 1 | +/* |
| 2 | + Copyright 2025 The Tekton Authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +
|
| 16 | +*/ |
| 17 | + |
| 18 | +package git |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "encoding/base64" |
| 23 | + "os/exec" |
| 24 | + "reflect" |
| 25 | + "testing" |
| 26 | +) |
| 27 | + |
| 28 | +func TestClone(t *testing.T) { |
| 29 | + type testCase struct { |
| 30 | + url string |
| 31 | + username string |
| 32 | + password string |
| 33 | + expectErr string |
| 34 | + } |
| 35 | + |
| 36 | + testCases := map[string]testCase{ |
| 37 | + "normal usage": {url: "https://github.com/tektoncd/pipeline"}, |
| 38 | + "normal usage with .git": {url: "https://github.com/tektoncd/pipeline.git"}, |
| 39 | + "private repository": {url: "https://github.com/tektoncd/not-a-repository.git"}, |
| 40 | + "with crendentials": {url: "https://github.com/tektoncd/not-a-repository.git", username: "fake", password: "fake"}, |
| 41 | + } |
| 42 | + |
| 43 | + for name, test := range testCases { |
| 44 | + t.Run(name, func(t *testing.T) { |
| 45 | + executions := []*exec.Cmd{} |
| 46 | + |
| 47 | + executor := func(ctx context.Context, name string, args ...string) *exec.Cmd { |
| 48 | + args = append([]string{name}, args...) |
| 49 | + // Run the command as `echo` args to avoid side effects |
| 50 | + cmd := exec.CommandContext(ctx, "echo", args...) |
| 51 | + executions = append(executions, cmd) |
| 52 | + return cmd |
| 53 | + } |
| 54 | + |
| 55 | + mockCmdRemote := remote{url: test.url, username: test.username, password: test.password, cmdExecutor: executor} |
| 56 | + repo, cleanup, err := mockCmdRemote.clone(context.Background()) |
| 57 | + defer cleanup() |
| 58 | + if test.expectErr != "" { |
| 59 | + if err.Error() != test.expectErr { |
| 60 | + t.Fatalf("Expected error %q but got %q", test.expectErr, err) |
| 61 | + } |
| 62 | + } else { |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("Error cloning repository %q: %v", test.url, err) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + expectedEnv := []string{"GIT_TERMINAL_PROMPT=false"} |
| 69 | + expectedCmd := []string{"git", "-C", repo.directory} |
| 70 | + if test.username != "" { |
| 71 | + token := base64.URLEncoding.EncodeToString([]byte(test.username + ":" + test.password)) |
| 72 | + expectedCmd = append(expectedCmd, "--config-env", "http.extraHeader=GIT_AUTH_HEADER") |
| 73 | + expectedEnv = append(expectedEnv, "GIT_AUTH_HEADER=Authorization=Basic "+token) |
| 74 | + } |
| 75 | + expectedCmd = append(expectedCmd, "clone", test.url, repo.directory, "--depth=1", "--no-checkout") |
| 76 | + |
| 77 | + if len(executions) != 1 { |
| 78 | + t.Fatalf("Expected 1 command execution during cloning, got %d: %v", len(executions), executions) |
| 79 | + } |
| 80 | + |
| 81 | + cmd := executions[0] |
| 82 | + // Remove the `echo` prefix |
| 83 | + cmdParts := cmd.Args[1:] |
| 84 | + if !reflect.DeepEqual(cmdParts, expectedCmd) { |
| 85 | + t.Fatalf("Expected clone command to be %v but got %v", expectedCmd, cmdParts) |
| 86 | + } |
| 87 | + if !reflect.DeepEqual(cmd.Env, expectedEnv) { |
| 88 | + t.Fatalf("Expected clone command env vars to be %v but got %v", expectedEnv, cmd.Env) |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestCheckout(t *testing.T) { |
| 95 | + repoPath, revisions := createTestRepo( |
| 96 | + t, |
| 97 | + []commitForRepo{ |
| 98 | + { |
| 99 | + Filename: "README.md", |
| 100 | + Content: "some content", |
| 101 | + Branch: "non-main", |
| 102 | + Tag: "1.0.0", |
| 103 | + }, |
| 104 | + { |
| 105 | + Filename: "otherfile.yaml", |
| 106 | + Content: "some data", |
| 107 | + Branch: "to-be-deleted", |
| 108 | + }, |
| 109 | + }, |
| 110 | + ) |
| 111 | + gitCmd := getGitCmd(t, repoPath) |
| 112 | + if err := gitCmd("checkout", "main").Run(); err != nil { |
| 113 | + t.Fatalf("cloud not checkout main branch after repo initialization: %v", err) |
| 114 | + } |
| 115 | + if err := gitCmd("branch", "-D", "to-be-deleted").Run(); err != nil { |
| 116 | + t.Fatalf("coun't delete branch to orphan commit: %v", err) |
| 117 | + } |
| 118 | + |
| 119 | + ctx := context.Background() |
| 120 | + |
| 121 | + type testCase struct { |
| 122 | + revision string |
| 123 | + expectedRevision string |
| 124 | + expectErr string |
| 125 | + } |
| 126 | + testCases := map[string]testCase{ |
| 127 | + "revision is branch": {revision: "non-main", expectedRevision: revisions[0]}, |
| 128 | + "revision is tag": {revision: "1.0.0", expectedRevision: revisions[0]}, |
| 129 | + "revision is sha": {revision: revisions[0], expectedRevision: revisions[0]}, |
| 130 | + "revision is unreachable sha": {revision: revisions[1], expectedRevision: revisions[1]}, |
| 131 | + "non-existent revision": {revision: "fake-revision", expectErr: "git fetch error: fatal: couldn't find remote ref fake-revision: exit status 128"}, |
| 132 | + } |
| 133 | + |
| 134 | + for name, test := range testCases { |
| 135 | + t.Run(name, func(t *testing.T) { |
| 136 | + repo, cleanup, err := remote{url: repoPath}.clone(ctx) |
| 137 | + defer cleanup() |
| 138 | + |
| 139 | + if err != nil { |
| 140 | + t.Fatalf("Error cloning repository %v", err) |
| 141 | + } |
| 142 | + |
| 143 | + err = repo.checkout(ctx, test.revision) |
| 144 | + if test.expectErr != "" { |
| 145 | + if err == nil { |
| 146 | + t.Fatal("Expected error checking out revision but got none") |
| 147 | + } else if err.Error() != test.expectErr { |
| 148 | + t.Fatalf("Expected error %q but got %q", test.expectErr, err) |
| 149 | + } |
| 150 | + return |
| 151 | + } else if err != nil { |
| 152 | + t.Fatalf("Error checking out revision: %v", err) |
| 153 | + } |
| 154 | + |
| 155 | + revision, err := repo.currentRevision(ctx) |
| 156 | + if err != nil { |
| 157 | + t.Fatal(err) |
| 158 | + } |
| 159 | + if revision != test.expectedRevision { |
| 160 | + t.Fatalf("Expected revision to be %q but got %q", test.expectedRevision, revision) |
| 161 | + } |
| 162 | + }) |
| 163 | + } |
| 164 | +} |
0 commit comments