-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathmain_test.go
More file actions
127 lines (102 loc) · 3.43 KB
/
Copy pathmain_test.go
File metadata and controls
127 lines (102 loc) · 3.43 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"os"
"testing"
"github.com/gruntwork-io/fetch/source"
_ "github.com/gruntwork-io/fetch/source/github"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// 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)
defer os.RemoveAll(tmpDir)
logger := GetProjectLogger()
config := source.Config{
ApiVersion: "v3",
Logger: logger,
}
src, err := source.NewSource(source.TypeGitHub, config)
require.NoError(t, err)
repo, err := src.ParseUrl(SAMPLE_RELEASE_ASSET_GITHUB_REPO_URL, "")
require.NoError(t, err)
assetPaths, fetchErr := downloadReleaseAssetsWithSource(logger, src, SAMPLE_RELEASE_ASSET_REGEX, tmpDir, repo, 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)
defer os.RemoveAll(tmpDir)
logger := GetProjectLogger()
config := source.Config{
ApiVersion: "v3",
Logger: logger,
}
src, err := source.NewSource(source.TypeGitHub, config)
require.NoError(t, err)
const githubRepoUrl = "https://github.com/gruntwork-io/fetch-test-public"
const releaseAsset = "hello+world.txt"
const assetVersion = "v0.0.4"
repo, err := src.ParseUrl(githubRepoUrl, "")
require.NoError(t, err)
assetPaths, fetchErr := downloadReleaseAssetsWithSource(logger, src, releaseAsset, tmpDir, repo, 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)
defer os.RemoveAll(tmpDir)
logger := GetProjectLogger()
config := source.Config{
ApiVersion: "v3",
Logger: logger,
}
src, err := source.NewSource(source.TypeGitHub, config)
require.NoError(t, err)
repo, err := src.ParseUrl(SAMPLE_RELEASE_ASSET_GITHUB_REPO_URL, "")
require.NoError(t, err)
_, fetchErr := downloadReleaseAssetsWithSource(logger, src, "*", tmpDir, repo, SAMPLE_RELEASE_ASSET_VERSION, false)
if fetchErr == nil {
t.Fatalf("Expected error for invalid regex")
}
}
func TestInvalidReleaseAssetTag(t *testing.T) {
tmpDir := mkTempDir(t)
defer os.RemoveAll(tmpDir)
logger := GetProjectLogger()
config := source.Config{
ApiVersion: "v3",
Logger: logger,
}
src, err := source.NewSource(source.TypeGitHub, config)
require.NoError(t, err)
repo, err := src.ParseUrl(SAMPLE_RELEASE_ASSET_GITHUB_REPO_URL, "")
require.NoError(t, err)
_, fetchErr := downloadReleaseAssetsWithSource(logger, src, SAMPLE_RELEASE_ASSET_REGEX, tmpDir, repo, "6.6.6", false)
assert.Error(t, fetchErr)
}