1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Unit test for builder/remote.go

Signed-off-by: Tomasz Kopczynski <tomek@kopczynski.net.pl>
This commit is contained in:
Tomasz Kopczynski 2016-05-01 12:58:39 +02:00
parent e385faa35b
commit e00ad7227e

View file

@ -2,8 +2,17 @@ package builder
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"testing"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/httputils"
)
var textPlainDockerfile = "FROM busybox"
@ -144,3 +153,69 @@ func TestInspectResponseEmptyContentType(t *testing.T) {
t.Fatalf("Corrupted response body %s", body)
}
}
func TestMakeRemoteContext(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-remote-test")
if err != nil {
t.Fatalf("Error with creating temporary directory: %s", err)
}
defer os.RemoveAll(contextDir)
testFilename := filepath.Join(contextDir, DefaultDockerfileName)
err = ioutil.WriteFile(testFilename, []byte(textPlainDockerfile), 0777)
if err != nil {
t.Fatalf("Error when writing file (%s) contents: %s", testFilename, err)
}
mux := http.NewServeMux()
server := httptest.NewServer(mux)
serverURL, _ := url.Parse(server.URL)
serverURL.Path = "/" + DefaultDockerfileName
remoteURL := serverURL.String()
mux.Handle("/", http.FileServer(http.Dir(contextDir)))
remoteContext, err := MakeRemoteContext(remoteURL, map[string]func(io.ReadCloser) (io.ReadCloser, error){
httputils.MimeTypes.TextPlain: func(rc io.ReadCloser) (io.ReadCloser, error) {
dockerfile, err := ioutil.ReadAll(rc)
if err != nil {
return nil, err
}
return archive.Generate(DefaultDockerfileName, string(dockerfile))
},
})
if err != nil {
t.Fatalf("Error when executing DetectContextFromRemoteURL: %s", err)
}
if remoteContext == nil {
t.Fatalf("Remote context should not be nil")
}
tarSumCtx, ok := remoteContext.(*tarSumContext)
if !ok {
t.Fatalf("Cast error, remote context should be casted to tarSumContext")
}
fileInfoSums := tarSumCtx.sums
if fileInfoSums.Len() != 1 {
t.Fatalf("Size of file info sums should be 1, got: %d", fileInfoSums.Len())
}
fileInfo := fileInfoSums.GetFile(DefaultDockerfileName)
if fileInfo == nil {
t.Fatalf("There should be file named %s in fileInfoSums", DefaultDockerfileName)
}
if fileInfo.Pos() != 0 {
t.Fatalf("File %s should have position 0, got %d", DefaultDockerfileName, fileInfo.Pos())
}
}