Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix flaky gitea tests #10354

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions applicationset/services/pull_request/gitea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ func giteaMockHandler(t *testing.T) func(http.ResponseWriter, *http.Request) {
}
}
}

func TestGiteaList(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
giteaMockHandler(t)(w, r)
Expand Down
67 changes: 56 additions & 11 deletions applicationset/services/scm_provider/gitea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"io"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/argoproj/argo-cd/v2/pkg/apis/applicationset/v1alpha1"
)
Expand Down Expand Up @@ -217,6 +219,39 @@ func giteaMockHandler(t *testing.T) func(http.ResponseWriter, *http.Request) {
if err != nil {
t.Fail()
}
case "/api/v1/repos/gitea/go-sdk/contents/README.md?ref=master":
_, err := io.WriteString(w, `{
"name": "README.md",
"path": "README.md",
"sha": "3605625ef3f80dc092167b54e3f55eb0663d729f",
"last_commit_sha": "6b6fdd91ce769bb4641084e15f76554fb841bf27",
"type": "file",
"size": 1673,
"encoding": "base64",
"content": "IyBHaXRlYSBTREsgZm9yIEdvCgpbIVtMaWNlbnNlOiBNSVRdKGh0dHBzOi8vaW1nLnNoaWVsZHMuaW8vYmFkZ2UvTGljZW5zZS1NSVQtYmx1ZS5zdmcpXShodHRwczovL29wZW5zb3VyY2Uub3JnL2xpY2Vu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kay9naXRlYT9zdGF0dXMuc3ZnKV0oaHR0cHM6Ly9nb2RvYy5vcmcvY29kZS5naXRlYS5pby9zZGsvZ2l0ZWEpCgpUaGlzIHByb2plY3QgYWN0cyBhcyBhIGNsaWVudCBTREsgaW1wbGVtZW50YXRpb24gd3JpdHRlbiBpbiBHbyB0byBpbnRlcmFjdCB3aXRoIHRoZSBHaXRlYSBBUEkgaW1wbGVtZW50YXRpb24uIEZvciBmdXJ0aGVyIGluZm9ybWF0aW9ucyB0YWtlIGEgbG9vayBhdCB0aGUgY3VycmVudCBbZG9jdW1lbnRhdGlvbl0oaHR0cHM6Ly9nb2RvYy5vcmcvY29kZS5naXRlYS5pby9zZGsvZ2l0ZWEpLgoKTm90ZTogZnVuY3Rpb24gYXJndW1lbnRzIGFyZSBlc2NhcGVkIGJ5IHRoZSBTREsuCgojIyBVc2UgaXQKCmBgYGdvCmltcG9ydCAiY29kZS5naXRlYS5pby9zZGsvZ2l0ZWEiCmBgYAoKIyMgVmVyc2lvbiBSZXF1aXJlbWVudHMKICogZ28gPj0gMS4xMwogKiBnaXRlYSA+PSAxLjExCgojIyBDb250cmlidXRpbmcKCkZvcmsgLT4gUGF0Y2ggLT4gUHVzaCAtPiBQdWxsIFJlcXVlc3QKCiMjIEF1dGhvcnMKCiogW01haW50YWluZXJzXShodHRwczovL2dpdGh1Yi5jb20vb3Jncy9nby1naXRlYS9wZW9wbGUpCiogW0NvbnRyaWJ1dG9yc10oaHR0cHM6Ly9naXRodWIuY29tL2dvLWdpdGVhL2dvLXNkay9ncmFwaHMvY29udHJpYnV0b3JzKQoKIyMgTGljZW5zZQoKVGhpcyBwcm9qZWN0IGlzIHVuZGVyIHRoZSBNSVQgTGljZW5zZS4gU2VlIHRoZSBbTElDRU5TRV0oTElDRU5TRSkgZmlsZSBmb3IgdGhlIGZ1bGwgbGljZW5zZSB0ZXh0Lgo=",
"target": null,
"url": "https://gitea.com/api/v1/repos/gitea/go-sdk/contents/README.md?ref=master",
"html_url": "https://gitea.com/gitea/go-sdk/src/branch/master/README.md",
"git_url": "https://gitea.com/api/v1/repos/gitea/go-sdk/git/blobs/3605625ef3f80dc092167b54e3f55eb0663d729f",
"download_url": "https://gitea.com/gitea/go-sdk/raw/branch/master/README.md",
"submodule_git_url": null,
"_links": {
"self": "https://gitea.com/api/v1/repos/gitea/go-sdk/contents/README.md?ref=master",
"git": "https://gitea.com/api/v1/repos/gitea/go-sdk/git/blobs/3605625ef3f80dc092167b54e3f55eb0663d729f",
"html": "https://gitea.com/gitea/go-sdk/src/branch/master/README.md"
}
}
`)
require.NoError(t, err)
case "/api/v1/repos/gitea/go-sdk/contents/gitea?ref=master":
response, err := os.ReadFile("testdata/repos_gitea_go-sdk_contents_gitea.json")
crenshaw-dev marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)
_, err = io.WriteString(w, string(response))
require.NoError(t, err)
case "/api/v1/repos/gitea/go-sdk/contents/notathing?ref=master":
w.WriteHeader(404)
_, err := io.WriteString(w, `{"errors":["object does not exist [id: , rel_path: notathing]"],"message":"GetContentsOrList","url":"https://gitea.com/api/swagger"}`)
require.NoError(t, err)
default:
_, err := io.WriteString(w, `[]`)
if err != nil {
Expand Down Expand Up @@ -295,22 +330,32 @@ func TestGiteaListRepos(t *testing.T) {
}

func TestGiteaHasPath(t *testing.T) {
host, _ := NewGiteaProvider(context.Background(), "gitea", "", "https://gitea.com/", false, false)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
giteaMockHandler(t)(w, r)
}))
defer ts.Close()
host, _ := NewGiteaProvider(context.Background(), "gitea", "", ts.URL, false, false)
repo := &Repository{
Organization: "gitea",
Repository: "go-sdk",
Branch: "master",
}
ok, err := host.RepoHasPath(context.Background(), repo, "README.md")
assert.Nil(t, err)
assert.True(t, ok)

// directory
ok, err = host.RepoHasPath(context.Background(), repo, "gitea")
assert.Nil(t, err)
assert.True(t, ok)
t.Run("file exists", func(t *testing.T) {
ok, err := host.RepoHasPath(context.Background(), repo, "README.md")
assert.Nil(t, err)
assert.True(t, ok)
})

t.Run("directory exists", func(t *testing.T) {
ok, err := host.RepoHasPath(context.Background(), repo, "gitea")
assert.Nil(t, err)
assert.True(t, ok)
})

ok, err = host.RepoHasPath(context.Background(), repo, "notathing")
assert.Nil(t, err)
assert.False(t, ok)
t.Run("does not exists", func(t *testing.T) {
ok, err := host.RepoHasPath(context.Background(), repo, "notathing")
assert.Nil(t, err)
assert.False(t, ok)
})
}