Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
bf456f35
Commit
bf456f35
authored
Jun 22, 2015
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
common: download client tests
/cc @cbednarski
parent
42000dda
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
123 additions
and
3 deletions
+123
-3
common/download_test.go
common/download_test.go
+121
-3
common/test-fixtures/root/another.txt
common/test-fixtures/root/another.txt
+1
-0
common/test-fixtures/root/basic.txt
common/test-fixtures/root/basic.txt
+1
-0
No files found.
common/download_test.go
View file @
bf456f35
...
...
@@ -10,7 +10,7 @@ import (
"testing"
)
func
TestDownloadClient
_
VerifyChecksum
(
t
*
testing
.
T
)
{
func
TestDownloadClientVerifyChecksum
(
t
*
testing
.
T
)
{
tf
,
err
:=
ioutil
.
TempFile
(
""
,
"packer"
)
if
err
!=
nil
{
t
.
Fatalf
(
"tempfile error: %s"
,
err
)
...
...
@@ -43,7 +43,125 @@ func TestDownloadClient_VerifyChecksum(t *testing.T) {
}
}
func
TestDownloadClientUsesDefaultUserAgent
(
t
*
testing
.
T
)
{
func
TestDownloadClient_basic
(
t
*
testing
.
T
)
{
tf
,
_
:=
ioutil
.
TempFile
(
""
,
"packer"
)
tf
.
Close
()
os
.
Remove
(
tf
.
Name
())
ts
:=
httptest
.
NewServer
(
http
.
FileServer
(
http
.
Dir
(
"./test-fixtures/root"
)))
defer
ts
.
Close
()
client
:=
NewDownloadClient
(
&
DownloadConfig
{
Url
:
ts
.
URL
+
"/basic.txt"
,
TargetPath
:
tf
.
Name
(),
})
path
,
err
:=
client
.
Get
()
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
raw
,
err
:=
ioutil
.
ReadFile
(
path
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
string
(
raw
)
!=
"hello
\n
"
{
t
.
Fatalf
(
"bad: %s"
,
string
(
raw
))
}
}
func
TestDownloadClient_checksumBad
(
t
*
testing
.
T
)
{
checksum
,
err
:=
hex
.
DecodeString
(
"b2946ac92492d2347c6235b4d2611184"
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
tf
,
_
:=
ioutil
.
TempFile
(
""
,
"packer"
)
tf
.
Close
()
os
.
Remove
(
tf
.
Name
())
ts
:=
httptest
.
NewServer
(
http
.
FileServer
(
http
.
Dir
(
"./test-fixtures/root"
)))
defer
ts
.
Close
()
client
:=
NewDownloadClient
(
&
DownloadConfig
{
Url
:
ts
.
URL
+
"/basic.txt"
,
TargetPath
:
tf
.
Name
(),
Hash
:
HashForType
(
"md5"
),
Checksum
:
checksum
,
})
if
_
,
err
:=
client
.
Get
();
err
==
nil
{
t
.
Fatal
(
"should error"
)
}
}
func
TestDownloadClient_checksumGood
(
t
*
testing
.
T
)
{
checksum
,
err
:=
hex
.
DecodeString
(
"b1946ac92492d2347c6235b4d2611184"
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
tf
,
_
:=
ioutil
.
TempFile
(
""
,
"packer"
)
tf
.
Close
()
os
.
Remove
(
tf
.
Name
())
ts
:=
httptest
.
NewServer
(
http
.
FileServer
(
http
.
Dir
(
"./test-fixtures/root"
)))
defer
ts
.
Close
()
client
:=
NewDownloadClient
(
&
DownloadConfig
{
Url
:
ts
.
URL
+
"/basic.txt"
,
TargetPath
:
tf
.
Name
(),
Hash
:
HashForType
(
"md5"
),
Checksum
:
checksum
,
})
path
,
err
:=
client
.
Get
()
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
raw
,
err
:=
ioutil
.
ReadFile
(
path
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
string
(
raw
)
!=
"hello
\n
"
{
t
.
Fatalf
(
"bad: %s"
,
string
(
raw
))
}
}
func
TestDownloadClient_checksumNoDownload
(
t
*
testing
.
T
)
{
checksum
,
err
:=
hex
.
DecodeString
(
"3740570a423feec44c2a759225a9fcf9"
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
ts
:=
httptest
.
NewServer
(
http
.
FileServer
(
http
.
Dir
(
"./test-fixtures/root"
)))
defer
ts
.
Close
()
client
:=
NewDownloadClient
(
&
DownloadConfig
{
Url
:
ts
.
URL
+
"/basic.txt"
,
TargetPath
:
"./test-fixtures/root/another.txt"
,
Hash
:
HashForType
(
"md5"
),
Checksum
:
checksum
,
})
path
,
err
:=
client
.
Get
()
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
raw
,
err
:=
ioutil
.
ReadFile
(
path
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
// If this says "hello" it means we downloaded it. We faked out
// the downloader above by giving it the checksum for "another", but
// requested the download of "hello"
if
string
(
raw
)
!=
"another
\n
"
{
t
.
Fatalf
(
"bad: %s"
,
string
(
raw
))
}
}
func
TestDownloadClient_usesDefaultUserAgent
(
t
*
testing
.
T
)
{
tf
,
err
:=
ioutil
.
TempFile
(
""
,
"packer"
)
if
err
!=
nil
{
t
.
Fatalf
(
"tempfile error: %s"
,
err
)
...
...
@@ -97,7 +215,7 @@ func TestDownloadClientUsesDefaultUserAgent(t *testing.T) {
}
}
func
TestDownloadClient
S
etsUserAgent
(
t
*
testing
.
T
)
{
func
TestDownloadClient
_s
etsUserAgent
(
t
*
testing
.
T
)
{
tf
,
err
:=
ioutil
.
TempFile
(
""
,
"packer"
)
if
err
!=
nil
{
t
.
Fatalf
(
"tempfile error: %s"
,
err
)
...
...
common/test-fixtures/root/another.txt
0 → 100644
View file @
bf456f35
another
common/test-fixtures/root/basic.txt
0 → 100644
View file @
bf456f35
hello
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment