2021-05-24 05:27:07 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2022-09-21 05:47:02 +00:00
|
|
|
"context"
|
2021-05-24 05:27:07 +00:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2022-09-21 05:47:02 +00:00
|
|
|
"time"
|
2021-06-15 21:11:35 +00:00
|
|
|
|
2022-10-05 09:33:59 +00:00
|
|
|
gittypes "github.com/portainer/portainer/api/git/types"
|
2021-06-15 21:11:35 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-05-24 05:27:07 +00:00
|
|
|
)
|
|
|
|
|
2022-09-21 05:47:02 +00:00
|
|
|
const (
|
|
|
|
privateGitRepoURL string = "https://github.com/portainer/private-test-repository.git"
|
|
|
|
)
|
|
|
|
|
2021-05-24 05:27:07 +00:00
|
|
|
func TestService_ClonePrivateRepository_GitHub(t *testing.T) {
|
|
|
|
ensureIntegrationTest(t)
|
|
|
|
|
2021-08-17 01:12:07 +00:00
|
|
|
accessToken := getRequiredValue(t, "GITHUB_PAT")
|
2021-05-24 05:27:07 +00:00
|
|
|
username := getRequiredValue(t, "GITHUB_USERNAME")
|
2022-09-21 05:47:02 +00:00
|
|
|
service := newService(context.TODO(), 0, 0)
|
2021-05-24 05:27:07 +00:00
|
|
|
|
2022-09-14 05:59:47 +00:00
|
|
|
dst := t.TempDir()
|
2021-05-24 05:27:07 +00:00
|
|
|
|
2022-09-21 05:47:02 +00:00
|
|
|
repositoryUrl := privateGitRepoURL
|
2023-04-03 06:19:17 +00:00
|
|
|
err := service.CloneRepository(dst, repositoryUrl, "refs/heads/main", username, accessToken, false)
|
2021-05-24 05:27:07 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.FileExists(t, filepath.Join(dst, "README.md"))
|
|
|
|
}
|
2021-08-17 01:12:07 +00:00
|
|
|
|
|
|
|
func TestService_LatestCommitID_GitHub(t *testing.T) {
|
|
|
|
ensureIntegrationTest(t)
|
|
|
|
|
|
|
|
accessToken := getRequiredValue(t, "GITHUB_PAT")
|
|
|
|
username := getRequiredValue(t, "GITHUB_USERNAME")
|
2022-09-21 05:47:02 +00:00
|
|
|
service := newService(context.TODO(), 0, 0)
|
2021-08-17 01:12:07 +00:00
|
|
|
|
2022-09-21 05:47:02 +00:00
|
|
|
repositoryUrl := privateGitRepoURL
|
2023-04-03 06:19:17 +00:00
|
|
|
id, err := service.LatestCommitID(repositoryUrl, "refs/heads/main", username, accessToken, false)
|
2021-08-17 01:12:07 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, id, "cannot guarantee commit id, but it should be not empty")
|
|
|
|
}
|
2022-09-21 05:47:02 +00:00
|
|
|
|
|
|
|
func TestService_ListRefs_GitHub(t *testing.T) {
|
|
|
|
ensureIntegrationTest(t)
|
|
|
|
|
|
|
|
accessToken := getRequiredValue(t, "GITHUB_PAT")
|
|
|
|
username := getRequiredValue(t, "GITHUB_USERNAME")
|
|
|
|
service := newService(context.TODO(), 0, 0)
|
|
|
|
|
|
|
|
repositoryUrl := privateGitRepoURL
|
2023-04-03 06:19:17 +00:00
|
|
|
refs, err := service.ListRefs(repositoryUrl, username, accessToken, false, false)
|
2022-09-21 05:47:02 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.GreaterOrEqual(t, len(refs), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_ListRefs_Github_Concurrently(t *testing.T) {
|
|
|
|
ensureIntegrationTest(t)
|
|
|
|
|
|
|
|
accessToken := getRequiredValue(t, "GITHUB_PAT")
|
|
|
|
username := getRequiredValue(t, "GITHUB_USERNAME")
|
2023-03-13 16:18:28 +00:00
|
|
|
service := newService(context.TODO(), repositoryCacheSize, 200*time.Millisecond)
|
2022-09-21 05:47:02 +00:00
|
|
|
|
|
|
|
repositoryUrl := privateGitRepoURL
|
2023-04-03 06:19:17 +00:00
|
|
|
go service.ListRefs(repositoryUrl, username, accessToken, false, false)
|
|
|
|
service.ListRefs(repositoryUrl, username, accessToken, false, false)
|
2022-09-21 05:47:02 +00:00
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_ListFiles_GitHub(t *testing.T) {
|
|
|
|
ensureIntegrationTest(t)
|
|
|
|
|
|
|
|
type expectResult struct {
|
|
|
|
shouldFail bool
|
|
|
|
err error
|
|
|
|
matchedCount int
|
|
|
|
}
|
|
|
|
service := newService(context.TODO(), 0, 0)
|
|
|
|
accessToken := getRequiredValue(t, "GITHUB_PAT")
|
|
|
|
username := getRequiredValue(t, "GITHUB_USERNAME")
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args fetchOption
|
|
|
|
extensions []string
|
|
|
|
expect expectResult
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "list tree with real repository and head ref but incorrect credential",
|
|
|
|
args: fetchOption{
|
|
|
|
baseOption: baseOption{
|
|
|
|
repositoryUrl: privateGitRepoURL,
|
|
|
|
username: "test-username",
|
|
|
|
password: "test-token",
|
|
|
|
},
|
|
|
|
referenceName: "refs/heads/main",
|
|
|
|
},
|
|
|
|
extensions: []string{},
|
|
|
|
expect: expectResult{
|
|
|
|
shouldFail: true,
|
2022-10-05 09:33:59 +00:00
|
|
|
err: gittypes.ErrAuthenticationFailure,
|
2022-09-21 05:47:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "list tree with real repository and head ref but no credential",
|
|
|
|
args: fetchOption{
|
|
|
|
baseOption: baseOption{
|
|
|
|
repositoryUrl: privateGitRepoURL + "fake",
|
|
|
|
username: "",
|
|
|
|
password: "",
|
|
|
|
},
|
|
|
|
referenceName: "refs/heads/main",
|
|
|
|
},
|
|
|
|
extensions: []string{},
|
|
|
|
expect: expectResult{
|
|
|
|
shouldFail: true,
|
2022-10-05 09:33:59 +00:00
|
|
|
err: gittypes.ErrAuthenticationFailure,
|
2022-09-21 05:47:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "list tree with real repository and head ref",
|
|
|
|
args: fetchOption{
|
|
|
|
baseOption: baseOption{
|
|
|
|
repositoryUrl: privateGitRepoURL,
|
|
|
|
username: username,
|
|
|
|
password: accessToken,
|
|
|
|
},
|
|
|
|
referenceName: "refs/heads/main",
|
|
|
|
},
|
|
|
|
extensions: []string{},
|
|
|
|
expect: expectResult{
|
|
|
|
err: nil,
|
|
|
|
matchedCount: 15,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "list tree with real repository and head ref and existing file extension",
|
|
|
|
args: fetchOption{
|
|
|
|
baseOption: baseOption{
|
|
|
|
repositoryUrl: privateGitRepoURL,
|
|
|
|
username: username,
|
|
|
|
password: accessToken,
|
|
|
|
},
|
|
|
|
referenceName: "refs/heads/main",
|
|
|
|
},
|
|
|
|
extensions: []string{"yml"},
|
|
|
|
expect: expectResult{
|
|
|
|
err: nil,
|
|
|
|
matchedCount: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "list tree with real repository and head ref and non-existing file extension",
|
|
|
|
args: fetchOption{
|
|
|
|
baseOption: baseOption{
|
|
|
|
repositoryUrl: privateGitRepoURL,
|
|
|
|
username: username,
|
|
|
|
password: accessToken,
|
|
|
|
},
|
|
|
|
referenceName: "refs/heads/main",
|
|
|
|
},
|
|
|
|
extensions: []string{"hcl"},
|
|
|
|
expect: expectResult{
|
|
|
|
err: nil,
|
|
|
|
matchedCount: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "list tree with real repository but non-existing ref",
|
|
|
|
args: fetchOption{
|
|
|
|
baseOption: baseOption{
|
|
|
|
repositoryUrl: privateGitRepoURL,
|
|
|
|
username: username,
|
|
|
|
password: accessToken,
|
|
|
|
},
|
|
|
|
referenceName: "refs/fake/feature",
|
|
|
|
},
|
|
|
|
extensions: []string{},
|
|
|
|
expect: expectResult{
|
|
|
|
shouldFail: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "list tree with fake repository ",
|
|
|
|
args: fetchOption{
|
|
|
|
baseOption: baseOption{
|
|
|
|
repositoryUrl: privateGitRepoURL + "fake",
|
|
|
|
username: username,
|
|
|
|
password: accessToken,
|
|
|
|
},
|
|
|
|
referenceName: "refs/fake/feature",
|
|
|
|
},
|
|
|
|
extensions: []string{},
|
|
|
|
expect: expectResult{
|
|
|
|
shouldFail: true,
|
2022-10-05 09:33:59 +00:00
|
|
|
err: gittypes.ErrIncorrectRepositoryURL,
|
2022-09-21 05:47:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-04-03 06:19:17 +00:00
|
|
|
paths, err := service.ListFiles(tt.args.repositoryUrl, tt.args.referenceName, tt.args.username, tt.args.password, false, tt.extensions, false)
|
2022-09-21 05:47:02 +00:00
|
|
|
if tt.expect.shouldFail {
|
|
|
|
assert.Error(t, err)
|
|
|
|
if tt.expect.err != nil {
|
|
|
|
assert.Equal(t, tt.expect.err, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if tt.expect.matchedCount > 0 {
|
|
|
|
assert.Greater(t, len(paths), 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_ListFiles_Github_Concurrently(t *testing.T) {
|
|
|
|
ensureIntegrationTest(t)
|
|
|
|
|
|
|
|
repositoryUrl := privateGitRepoURL
|
|
|
|
accessToken := getRequiredValue(t, "GITHUB_PAT")
|
|
|
|
username := getRequiredValue(t, "GITHUB_USERNAME")
|
2023-03-13 16:18:28 +00:00
|
|
|
service := newService(context.TODO(), repositoryCacheSize, 200*time.Millisecond)
|
2022-09-21 05:47:02 +00:00
|
|
|
|
2023-04-03 06:19:17 +00:00
|
|
|
go service.ListFiles(repositoryUrl, "refs/heads/main", username, accessToken, false, []string{}, false)
|
|
|
|
service.ListFiles(repositoryUrl, "refs/heads/main", username, accessToken, false, []string{}, false)
|
2022-09-21 05:47:02 +00:00
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_purgeCache_Github(t *testing.T) {
|
|
|
|
ensureIntegrationTest(t)
|
|
|
|
|
|
|
|
repositoryUrl := privateGitRepoURL
|
|
|
|
accessToken := getRequiredValue(t, "GITHUB_PAT")
|
|
|
|
username := getRequiredValue(t, "GITHUB_USERNAME")
|
|
|
|
service := NewService(context.TODO())
|
|
|
|
|
2023-04-03 06:19:17 +00:00
|
|
|
service.ListRefs(repositoryUrl, username, accessToken, false, false)
|
|
|
|
service.ListFiles(repositoryUrl, "refs/heads/main", username, accessToken, false, []string{}, false)
|
2022-09-21 05:47:02 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 1, service.repoRefCache.Len())
|
|
|
|
assert.Equal(t, 1, service.repoFileCache.Len())
|
|
|
|
|
|
|
|
service.purgeCache()
|
|
|
|
assert.Equal(t, 0, service.repoRefCache.Len())
|
|
|
|
assert.Equal(t, 0, service.repoFileCache.Len())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_purgeCacheByTTL_Github(t *testing.T) {
|
|
|
|
ensureIntegrationTest(t)
|
|
|
|
|
|
|
|
timeout := 100 * time.Millisecond
|
|
|
|
repositoryUrl := privateGitRepoURL
|
|
|
|
accessToken := getRequiredValue(t, "GITHUB_PAT")
|
|
|
|
username := getRequiredValue(t, "GITHUB_USERNAME")
|
|
|
|
// 40*timeout is designed for giving enough time for ListRefs and ListFiles to cache the result
|
|
|
|
service := newService(context.TODO(), 2, 40*timeout)
|
|
|
|
|
2023-04-03 06:19:17 +00:00
|
|
|
service.ListRefs(repositoryUrl, username, accessToken, false, false)
|
|
|
|
service.ListFiles(repositoryUrl, "refs/heads/main", username, accessToken, false, []string{}, false)
|
2022-09-21 05:47:02 +00:00
|
|
|
assert.Equal(t, 1, service.repoRefCache.Len())
|
|
|
|
assert.Equal(t, 1, service.repoFileCache.Len())
|
|
|
|
|
|
|
|
// 40*timeout is designed for giving enough time for TTL being activated
|
|
|
|
time.Sleep(40 * timeout)
|
|
|
|
assert.Equal(t, 0, service.repoRefCache.Len())
|
|
|
|
assert.Equal(t, 0, service.repoFileCache.Len())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_canStopCacheCleanTimer_whenContextDone(t *testing.T) {
|
|
|
|
timeout := 10 * time.Millisecond
|
2022-10-17 16:57:41 +00:00
|
|
|
deadlineCtx, cancel := context.WithDeadline(context.TODO(), time.Now().Add(10*timeout))
|
|
|
|
defer cancel()
|
2022-09-21 05:47:02 +00:00
|
|
|
|
|
|
|
service := NewService(deadlineCtx)
|
|
|
|
assert.False(t, service.timerHasStopped(), "timer should not be stopped")
|
|
|
|
|
|
|
|
<-time.After(20 * timeout)
|
|
|
|
|
|
|
|
assert.True(t, service.timerHasStopped(), "timer should be stopped")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_HardRefresh_ListRefs_GitHub(t *testing.T) {
|
|
|
|
ensureIntegrationTest(t)
|
|
|
|
|
|
|
|
accessToken := getRequiredValue(t, "GITHUB_PAT")
|
|
|
|
username := getRequiredValue(t, "GITHUB_USERNAME")
|
|
|
|
service := newService(context.TODO(), 2, 0)
|
|
|
|
|
|
|
|
repositoryUrl := privateGitRepoURL
|
2023-04-03 06:19:17 +00:00
|
|
|
refs, err := service.ListRefs(repositoryUrl, username, accessToken, false, false)
|
2022-09-21 05:47:02 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.GreaterOrEqual(t, len(refs), 1)
|
|
|
|
assert.Equal(t, 1, service.repoRefCache.Len())
|
|
|
|
|
2023-04-03 06:19:17 +00:00
|
|
|
_, err = service.ListRefs(repositoryUrl, username, "fake-token", false, false)
|
2022-09-21 05:47:02 +00:00
|
|
|
assert.Error(t, err)
|
2022-10-11 05:31:21 +00:00
|
|
|
assert.Equal(t, 1, service.repoRefCache.Len())
|
2022-09-21 05:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_HardRefresh_ListRefs_And_RemoveAllCaches_GitHub(t *testing.T) {
|
|
|
|
ensureIntegrationTest(t)
|
|
|
|
|
|
|
|
accessToken := getRequiredValue(t, "GITHUB_PAT")
|
|
|
|
username := getRequiredValue(t, "GITHUB_USERNAME")
|
|
|
|
service := newService(context.TODO(), 2, 0)
|
|
|
|
|
|
|
|
repositoryUrl := privateGitRepoURL
|
2023-04-03 06:19:17 +00:00
|
|
|
refs, err := service.ListRefs(repositoryUrl, username, accessToken, false, false)
|
2022-09-21 05:47:02 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.GreaterOrEqual(t, len(refs), 1)
|
|
|
|
assert.Equal(t, 1, service.repoRefCache.Len())
|
|
|
|
|
2023-04-03 06:19:17 +00:00
|
|
|
files, err := service.ListFiles(repositoryUrl, "refs/heads/main", username, accessToken, false, []string{}, false)
|
2022-09-21 05:47:02 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.GreaterOrEqual(t, len(files), 1)
|
|
|
|
assert.Equal(t, 1, service.repoFileCache.Len())
|
|
|
|
|
2023-04-03 06:19:17 +00:00
|
|
|
files, err = service.ListFiles(repositoryUrl, "refs/heads/test", username, accessToken, false, []string{}, false)
|
2022-09-21 05:47:02 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.GreaterOrEqual(t, len(files), 1)
|
|
|
|
assert.Equal(t, 2, service.repoFileCache.Len())
|
|
|
|
|
2023-04-03 06:19:17 +00:00
|
|
|
_, err = service.ListRefs(repositoryUrl, username, "fake-token", false, false)
|
2022-09-21 05:47:02 +00:00
|
|
|
assert.Error(t, err)
|
2022-10-11 05:31:21 +00:00
|
|
|
assert.Equal(t, 1, service.repoRefCache.Len())
|
2022-09-21 05:47:02 +00:00
|
|
|
|
2023-04-03 06:19:17 +00:00
|
|
|
_, err = service.ListRefs(repositoryUrl, username, "fake-token", true, false)
|
2022-10-11 05:31:21 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, 1, service.repoRefCache.Len())
|
2022-09-21 05:47:02 +00:00
|
|
|
// The relevant file caches should be removed too
|
|
|
|
assert.Equal(t, 0, service.repoFileCache.Len())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_HardRefresh_ListFiles_GitHub(t *testing.T) {
|
|
|
|
ensureIntegrationTest(t)
|
|
|
|
|
|
|
|
service := newService(context.TODO(), 2, 0)
|
|
|
|
accessToken := getRequiredValue(t, "GITHUB_PAT")
|
|
|
|
username := getRequiredValue(t, "GITHUB_USERNAME")
|
|
|
|
repositoryUrl := privateGitRepoURL
|
2023-04-03 06:19:17 +00:00
|
|
|
files, err := service.ListFiles(repositoryUrl, "refs/heads/main", username, accessToken, false, []string{}, false)
|
2022-09-21 05:47:02 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.GreaterOrEqual(t, len(files), 1)
|
|
|
|
assert.Equal(t, 1, service.repoFileCache.Len())
|
|
|
|
|
2023-04-03 06:19:17 +00:00
|
|
|
_, err = service.ListFiles(repositoryUrl, "refs/heads/main", username, "fake-token", true, []string{}, false)
|
2022-09-21 05:47:02 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, 0, service.repoFileCache.Len())
|
|
|
|
}
|