Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions pkg/util/github/branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
)

type newBranchTest struct {
baseTest
BaseTest
baseBranch string
newBranch string
wantErr bool
}

type delBranchTest struct {
baseTest
BaseTest
branch string
wantErr bool
}
Expand All @@ -30,20 +30,20 @@ func TestClient_NewBranch(t *testing.T) {
"url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
}
}`
mux, serverUrl, teardown := setup(t)
mux, serverUrl, teardown := Setup()
defer teardown()

tests := []newBranchTest{
// TODO: Add test cases.
{
baseTest{"base", getClientWithOption(
BaseTest{"base", GetClientWithOption(
t, &Option{Owner: "o", Repo: "r", Org: "or"}, serverUrl,
),
"/repos/or/r/git/ref/heads/b", http.MethodGet, false, "", ""},
"b", "", true,
},
{
baseTest{"base set wrong register url for GetRef api in mock server", getClientWithOption(
BaseTest{"base set wrong register url for GetRef api in mock server", GetClientWithOption(
t, &Option{Owner: "o", Repo: "r"}, serverUrl,
),
"repos", http.MethodGet, false, "", ""},
Expand All @@ -64,13 +64,13 @@ func TestClient_NewBranch(t *testing.T) {
}

func TestClient_DeleteBranch(t *testing.T) {
mux, serverUrl, teardown := setup(t)
mux, serverUrl, teardown := Setup()
defer teardown()

tests := []delBranchTest{
// TODO: Add test cases.
{
baseTest{"base", getClientWithOption(
BaseTest{"base", GetClientWithOption(
t, &Option{Owner: "o", Repo: "r", Org: "or"}, serverUrl,
),
"/repos/or/r/git/ref/heads/b", http.MethodGet, false, "", ""},
Expand Down
12 changes: 6 additions & 6 deletions pkg/util/github/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@ import (
)

type commitTest struct {
baseTest
BaseTest
want *github.RepositoryCommit
wantErr bool
}

func TestClient_GetLastCommit(t *testing.T) {
mux, serverUrl, teardown := setup(t)
mux, serverUrl, teardown := Setup()
defer teardown()
sha := "s"
tests := []commitTest{
// TODO: Add test cases.
{
baseTest{"base 200 ", getClientWithOption(
BaseTest{"base 200 ", GetClientWithOption(
t, &Option{Owner: "o", Repo: "r", Org: "or"}, serverUrl,
),
"/repos/or/r/commits", http.MethodGet, false, "", `[{"sha": "s"}]`},
&github.RepositoryCommit{SHA: &sha}, false,
},
{
baseTest{"base 200 with empty result", getClientWithOption(
BaseTest{"base 200 with empty result", GetClientWithOption(
t, &Option{Owner: "o", Repo: "r"}, serverUrl,
),
"/repos/o/r/commits", http.MethodGet, false, "", `[]`},
nil, true,
},
{
baseTest{"base 404", getClientWithOption(
BaseTest{"base 404", GetClientWithOption(
t, &Option{Owner: "o"}, serverUrl,
),
"/aaa", http.MethodGet, false, "", ""},
Expand All @@ -46,7 +46,7 @@ func TestClient_GetLastCommit(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mux.HandleFunc(tt.registerUrl, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, tt.wantMethod)
DoTestMethod(t, r, tt.wantMethod)
fmt.Fprint(w, tt.respBody)
})
got, err := tt.client.GetLastCommit()
Expand Down
224 changes: 224 additions & 0 deletions pkg/util/github/download_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
package github_test

import (
"fmt"
"net/http"
"os"
"path/filepath"

"github.com/argoproj/gitops-engine/pkg/utils/io"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/devstream-io/devstream/pkg/util/github"
)

var _ = Describe("DownloadAsset", func() {
Context(("do DownloadAsset 200"), func() {

BeforeEach(func() {
mux.HandleFunc("/repos/r/o/releases", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `[{"id":1}]`)
})
})

It("do ListReleases with wrong url ", func() {
ghClient, err := github.NewClientWithOption(&github.Option{
Org: "or",
}, serverURL)
Expect(err).NotTo(HaveOccurred())
Expect(ghClient).NotTo(Equal(nil))
err = ghClient.DownloadAsset("t", "a", "f")
Expect(err).NotTo(Succeed())
})

})

Context(("do Downloaset step 1"), func() {

BeforeEach(func() {
mux.HandleFunc("/repos/devstream-io/dtm-scaffolding-golang/releases", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `[{"id":1, "tag_name":"t", "name":"n", "assets": [{"id":1}]}]`)
})
})

It("do ListReleases with correct url ", func() {
ghClient, err := github.NewClientWithOption(github.OptNotNeedAuth, serverURL)
Expect(err).NotTo(HaveOccurred())
Expect(ghClient).NotTo(Equal(nil))
err = ghClient.DownloadAsset("t", "a", "f")
Expect(err).NotTo(Succeed())
})

})

Context(("do DownloadAsset step 2 "), func() {

BeforeEach(func() {
mux.HandleFunc("/repos/oo/rr/releases", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `[{"id":1, "tag_name":"t", "name":"n", "assets": [{"id":1}]}]`)
})
})

It("do ListReleases with correct url ", func() {
ghClient, err := github.NewClientWithOption(&github.Option{
Owner: "oo",
Repo: "rr",
}, serverURL)
Expect(err).NotTo(HaveOccurred())
Expect(ghClient).NotTo(Equal(nil))
err = ghClient.DownloadAsset("t", "a", "f")
Expect(err).NotTo(Succeed())
})

})

Context(("do DownloadAsset step 2"), func() {

BeforeEach(func() {
mux.HandleFunc("/repos/a/b/releases", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `[{"id":1, "tag_name":"t", "name":"n", "assets":[]}`)
})
})

It("do ListReleases with correct url but empty asset", func() {
ghClient, err := github.NewClientWithOption(&github.Option{
Owner: "a",
Repo: "b",
}, serverURL)
Expect(err).NotTo(HaveOccurred())
Expect(ghClient).NotTo(Equal(nil))
err = ghClient.DownloadAsset("t", "a", "f")
Expect(err).NotTo(Succeed())
})

})

Context(("do DownloadAsset step 3"), func() {

BeforeEach(func() {
mux.HandleFunc("/repos/ooo/rrr/releases", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `[
{
"id":1,
"tag_name":"t",
"name":"n",
"assets": [{"id":1, "name":"a"}]
}]`)
})
})

It("do get download url without browser_download_url", func() {
ghClient, err := github.NewClientWithOption(&github.Option{
Owner: "ooo",
Repo: "rrr",
}, serverURL)
Expect(err).NotTo(HaveOccurred())
Expect(ghClient).NotTo(Equal(nil))
err = ghClient.DownloadAsset("t", "a", "f")
Expect(err).NotTo(Succeed())
})

})
var WorkPath = "./"
Context(("do DownloadAsset step 4"), func() {
BeforeEach(func() {
DeferCleanup(os.RemoveAll, "./"+github.DefaultWorkPath)
mux.HandleFunc("/repos/ow/re/releases", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `[
{
"id":1,
"tag_name":"t",
"name":"n",
"assets": [{"id":1, "name":"a", "browser_download_url":"u"}]
}]`)
})
})

It("do get download url browser_download_url with unsupported proto scheme", func() {
ghClient, err := github.NewClientWithOption(&github.Option{
Owner: "ow",
Repo: "re",
}, serverURL)
Expect(err).NotTo(HaveOccurred())
Expect(ghClient).NotTo(Equal(nil))
err = ghClient.DownloadAsset("t", "a", "f")
Expect(err).NotTo(Succeed())
})

})

Context(("do DownloadAsset step 4"), func() {
var filename = "f"
var downloadUrl = "/download"
BeforeEach(func() {
mux.HandleFunc("/repos/own/rep/releases", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `[
{
"id":1,
"tag_name":"t",
"name":"n",
"assets": [{"id":1, "name":"a", "browser_download_url":"%s"}]
}]`, serverURL+github.BaseURLPath+downloadUrl)
})
mux.HandleFunc(downloadUrl, func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s", "download content")
})
})

It("do get download url browser_download_url with supported proto scheme", func() {
ghClient, err := github.NewClientWithOption(&github.Option{
Owner: "own",
Repo: "rep",
WorkPath: WorkPath,
}, serverURL)
Expect(err).NotTo(HaveOccurred())
Expect(ghClient).NotTo(Equal(nil))
err = ghClient.DownloadAsset("t", "a", filename)
fmt.Println(err)
Expect(err).To(Succeed())
})

AfterEach(func() {
io.DeleteFile(filepath.Join(WorkPath, filename))
})
})
})

var _ = Describe("DownloadLatestCodeAsZipFile", func() {
var owner, repo, org, workPath = "owner", "repo", "org", "./"
Context(("do DownloadLatestCodeAsZipFile"), func() {

It("corrent url ", func() {
ghClient, err := github.NewClientWithOption(&github.Option{
Owner: owner,
Repo: repo,
Org: org,
WorkPath: workPath,
}, serverURL)
Expect(err).NotTo(HaveOccurred())
Expect(ghClient).NotTo(Equal(nil))
err = ghClient.DownloadLatestCodeAsZipFile()
Expect(err).To(Succeed())
})

It("produce an error", func() {
ghClient, err := github.NewClientWithOption(&github.Option{
Owner: owner,
Repo: repo,
Org: org,
WorkPath: "//",
}, serverURL)
Expect(err).NotTo(HaveOccurred())
Expect(ghClient).NotTo(Equal(nil))
err = ghClient.DownloadLatestCodeAsZipFile()
Expect(err).NotTo(Succeed())
})

AfterEach(func() {
DeferCleanup(io.DeleteFile, workPath+github.DefaultLatestCodeZipfileName)
})

})

})
8 changes: 4 additions & 4 deletions pkg/util/github/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import (
)

type fileTest struct {
baseTest
BaseTest
content []byte
filePath string
targetBranch string
wantErr bool
}

func TestClient_CreateFile(t *testing.T) {
mux, serverUrl, teardown := setup(t)
mux, serverUrl, teardown := Setup()
defer teardown()
tests := []fileTest{
// TODO: Add test cases.
{
baseTest{"base ", getClientWithOption(
BaseTest{"base ", GetClientWithOption(
t, &Option{Owner: "o", Repo: "r", Org: "or"}, serverUrl,
),
"/repos/or/r/contents/a", http.MethodPut, true, `{"message":"Initialize the repository","content":"Yw==","branch":"b"}`, ""},
Expand All @@ -30,7 +30,7 @@ func TestClient_CreateFile(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mux.HandleFunc(tt.registerUrl, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, tt.wantMethod)
DoTestMethod(t, r, tt.wantMethod)
fmt.Fprint(w, tt.respBody)
})
if err := tt.client.CreateFile(tt.content, tt.filePath, tt.targetBranch); (err != nil) != tt.wantErr {
Expand Down
17 changes: 17 additions & 0 deletions pkg/util/github/github_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
package github_test

import (
"net/http"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

util_github "github.com/devstream-io/devstream/pkg/util/github"
)

var (
mux *http.ServeMux
serverURL string
teardown func()
)

var _ = BeforeSuite(func() {
mux, serverURL, teardown = util_github.Setup()
})

var _ = AfterSuite(func() {
teardown()
})

func TestPlanmanager(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "GitHub Suite")
Expand Down
Loading