-
Notifications
You must be signed in to change notification settings - Fork 87
/
main_test.go
115 lines (95 loc) · 3.36 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"fmt"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
// Expect to download 2 assets:
// - health-checker_linux_386
// - health-checker_linux_amd64
const SAMPLE_RELEASE_ASSET_REGEX = "health-checker_linux_[a-z0-9]+"
func TestDownloadReleaseAssets(t *testing.T) {
tmpDir := mkTempDir(t)
logger := GetProjectLogger()
testInst := GitHubInstance{
BaseUrl: "github.com",
ApiUrl: "api.github.com",
}
githubRepo, err := ParseUrlIntoGitHubRepo(SAMPLE_RELEASE_ASSET_GITHUB_REPO_URL, "", testInst)
if err != nil {
t.Fatalf("Failed to parse sample release asset GitHub URL into Fetch GitHubRepo struct: %s", err)
}
assetPaths, fetchErr := downloadReleaseAssets(logger, SAMPLE_RELEASE_ASSET_REGEX, tmpDir, githubRepo, SAMPLE_RELEASE_ASSET_VERSION, false)
if fetchErr != nil {
t.Fatalf("Failed to download release asset: %s", fetchErr)
}
if len(assetPaths) != 2 {
t.Fatalf("Expected to download 2 assets, not %d", len(assetPaths))
}
for _, assetPath := range assetPaths {
if _, err := os.Stat(assetPath); os.IsNotExist(err) {
t.Fatalf("Downloaded file should exist at %s", assetPath)
} else {
fmt.Printf("Verified the downloaded asset exists at %s\n", assetPath)
}
}
}
func TestDownloadReleaseAssetsWithRegexCharacters(t *testing.T) {
tmpDir := mkTempDir(t)
logger := GetProjectLogger()
testInst := GitHubInstance{
BaseUrl: "github.com",
ApiUrl: "api.github.com",
}
const githubRepoUrl = "https://github.com/gruntwork-io/fetch-test-public"
const releaseAsset = "hello+world.txt"
const assetVersion = "v0.0.4"
githubRepo, err := ParseUrlIntoGitHubRepo(githubRepoUrl, "", testInst)
if err != nil {
t.Fatalf("Failed to parse sample release asset GitHub URL into Fetch GitHubRepo struct: %s", err)
}
assetPaths, fetchErr := downloadReleaseAssets(logger, releaseAsset, tmpDir, githubRepo, assetVersion, false)
if fetchErr != nil {
t.Fatalf("Failed to download release asset: %s", fetchErr)
}
if len(assetPaths) != 1 {
t.Fatalf("Expected to download 1 assets, not %d", len(assetPaths))
}
assetPath := assetPaths[0]
if _, err := os.Stat(assetPath); os.IsNotExist(err) {
t.Fatalf("Downloaded file should exist at %s", assetPath)
} else {
fmt.Printf("Verified the downloaded asset exists at %s\n", assetPath)
}
}
func TestInvalidReleaseAssetsRegex(t *testing.T) {
tmpDir := mkTempDir(t)
logger := GetProjectLogger()
testInst := GitHubInstance{
BaseUrl: "github.com",
ApiUrl: "api.github.com",
}
githubRepo, err := ParseUrlIntoGitHubRepo(SAMPLE_RELEASE_ASSET_GITHUB_REPO_URL, "", testInst)
if err != nil {
t.Fatalf("Failed to parse sample release asset GitHub URL into Fetch GitHubRepo struct: %s", err)
}
_, fetchErr := downloadReleaseAssets(logger, "*", tmpDir, githubRepo, SAMPLE_RELEASE_ASSET_VERSION, false)
if fetchErr == nil {
t.Fatalf("Expected error for invalid regex")
}
}
func TestInvalidReleaseAssetTag(t *testing.T) {
tmpDir := mkTempDir(t)
logger := GetProjectLogger()
testInst := GitHubInstance{
BaseUrl: "github.com",
ApiUrl: "api.github.com",
}
githubRepo, err := ParseUrlIntoGitHubRepo(SAMPLE_RELEASE_ASSET_GITHUB_REPO_URL, "", testInst)
if err != nil {
t.Fatalf("Failed to parse sample release asset GitHub URL into Fetch GitHubRepo struct: %s", err)
}
_, fetchErr := downloadReleaseAssets(logger, SAMPLE_RELEASE_ASSET_REGEX, tmpDir, githubRepo, "6.6.6", false)
assert.Error(t, fetchErr)
}