-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #828 from steinliber/feat-ut-mock-doanwload-test
feat: mock download test logic in pluginmanager
- Loading branch information
Showing
2 changed files
with
97 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,81 @@ | ||
package pluginmanager | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/go-resty/resty/v2" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestDownload(t *testing.T) { | ||
os.Remove(filepath.Join(".", "argocdapp_0.0.1-rc1.so")) | ||
|
||
c := NewDownloadClient() | ||
err := c.download(".", "argocdapp_0.0.1-rc1.so", "0.0.1-ut-do-not-delete") | ||
if err != nil { | ||
t.Fatal("downloaded error") | ||
var _ = Describe("DownloadClient", Ordered, func() { | ||
// mock download success func | ||
mockPlugSuccessGetter := func(reqClient *resty.Client, url, plugName string) error { | ||
return nil | ||
} | ||
// mock download failed func | ||
mockPlugNotFoundGetter := func(reqClient *resty.Client, url, plugName string) error { | ||
return fmt.Errorf("downloading plugin %s from %s status code %d", plugName, url, 404) | ||
} | ||
var ( | ||
tempDir string | ||
) | ||
|
||
const ( | ||
validPlugName = "argocdapp_0.0.1-rc1.so" | ||
notExistPlugName = "argocdapp_not_exist.so" | ||
version = "0.0.1-ut-do-not-delete" | ||
) | ||
|
||
BeforeAll(func() { | ||
tempDir = GinkgoT().TempDir() | ||
|
||
}) | ||
|
||
Describe("download method failed", func() { | ||
var testTable = []struct { | ||
downloadFunc func(reqClient *resty.Client, url, plugName string) error | ||
plugName string | ||
expectedErrorMsg string | ||
describeMsg string | ||
}{ | ||
{ | ||
downloadFunc: mockPlugSuccessGetter, plugName: notExistPlugName, expectedErrorMsg: "no such file or directory", | ||
describeMsg: "should return file not exist if plugin not normal download", | ||
}, | ||
{ | ||
downloadFunc: mockPlugNotFoundGetter, plugName: validPlugName, expectedErrorMsg: "404", | ||
describeMsg: "should return 404 if plugin not exist", | ||
}, | ||
} | ||
|
||
os.Remove(filepath.Join(".", "argocdapp_0.0.1-rc1.so")) | ||
} | ||
for _, testcase := range testTable { | ||
It(testcase.describeMsg, func() { | ||
c := NewDownloadClient() | ||
c.pluginGetter = testcase.downloadFunc | ||
err := c.download(tempDir, testcase.plugName, version) | ||
Expect(err).Error().Should(HaveOccurred()) | ||
Expect(err.Error()).Should(ContainSubstring(testcase.expectedErrorMsg)) | ||
}) | ||
} | ||
}) | ||
|
||
func TestDownloadNotFound(t *testing.T) { | ||
c := NewDownloadClient() | ||
err := c.download(".", "doesntexist", "0.0.1-ut-do-not-delete") | ||
// Since the right granted to public users on aws does not include listing bucket | ||
// AWS returns 403 instead of 404 when acquiring an object where bucket does not exist: there is no list right. | ||
assert.Contains(t, err.Error(), "403") | ||
} | ||
Describe("download method success", func() { | ||
It("should reanme file if download success", func() { | ||
tmpFilePath := filepath.Join(tempDir, fmt.Sprintf("%s.tmp", validPlugName)) | ||
f, err := os.Create(tmpFilePath) | ||
defer os.Remove(tmpFilePath) | ||
defer f.Close() | ||
Expect(err).NotTo(HaveOccurred()) | ||
c := NewDownloadClient() | ||
c.pluginGetter = mockPlugSuccessGetter | ||
err = c.download(tempDir, validPlugName, version) | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
renamedFilePath := filepath.Join(tempDir, validPlugName) | ||
_, err = os.Stat(renamedFilePath) | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
}) | ||
}) | ||
}) |