Browse code

Unit test for builder/remote.go

Signed-off-by: Tomasz Kopczynski <tomek@kopczynski.net.pl>

Tomasz Kopczynski authored on 2016/05/01 19:58:39
Showing 1 changed files
... ...
@@ -2,8 +2,17 @@ package builder
2 2
 
3 3
 import (
4 4
 	"bytes"
5
+	"io"
5 6
 	"io/ioutil"
7
+	"net/http"
8
+	"net/http/httptest"
9
+	"net/url"
10
+	"os"
11
+	"path/filepath"
6 12
 	"testing"
13
+
14
+	"github.com/docker/docker/pkg/archive"
15
+	"github.com/docker/docker/pkg/httputils"
7 16
 )
8 17
 
9 18
 var textPlainDockerfile = "FROM busybox"
... ...
@@ -144,3 +153,69 @@ func TestInspectResponseEmptyContentType(t *testing.T) {
144 144
 		t.Fatalf("Corrupted response body %s", body)
145 145
 	}
146 146
 }
147
+
148
+func TestMakeRemoteContext(t *testing.T) {
149
+	contextDir, err := ioutil.TempDir("", "builder-remote-test")
150
+
151
+	if err != nil {
152
+		t.Fatalf("Error with creating temporary directory: %s", err)
153
+	}
154
+
155
+	defer os.RemoveAll(contextDir)
156
+
157
+	testFilename := filepath.Join(contextDir, DefaultDockerfileName)
158
+	err = ioutil.WriteFile(testFilename, []byte(textPlainDockerfile), 0777)
159
+
160
+	if err != nil {
161
+		t.Fatalf("Error when writing file (%s) contents: %s", testFilename, err)
162
+	}
163
+
164
+	mux := http.NewServeMux()
165
+	server := httptest.NewServer(mux)
166
+	serverURL, _ := url.Parse(server.URL)
167
+
168
+	serverURL.Path = "/" + DefaultDockerfileName
169
+	remoteURL := serverURL.String()
170
+
171
+	mux.Handle("/", http.FileServer(http.Dir(contextDir)))
172
+
173
+	remoteContext, err := MakeRemoteContext(remoteURL, map[string]func(io.ReadCloser) (io.ReadCloser, error){
174
+		httputils.MimeTypes.TextPlain: func(rc io.ReadCloser) (io.ReadCloser, error) {
175
+			dockerfile, err := ioutil.ReadAll(rc)
176
+			if err != nil {
177
+				return nil, err
178
+			}
179
+			return archive.Generate(DefaultDockerfileName, string(dockerfile))
180
+		},
181
+	})
182
+
183
+	if err != nil {
184
+		t.Fatalf("Error when executing DetectContextFromRemoteURL: %s", err)
185
+	}
186
+
187
+	if remoteContext == nil {
188
+		t.Fatalf("Remote context should not be nil")
189
+	}
190
+
191
+	tarSumCtx, ok := remoteContext.(*tarSumContext)
192
+
193
+	if !ok {
194
+		t.Fatalf("Cast error, remote context should be casted to tarSumContext")
195
+	}
196
+
197
+	fileInfoSums := tarSumCtx.sums
198
+
199
+	if fileInfoSums.Len() != 1 {
200
+		t.Fatalf("Size of file info sums should be 1, got: %d", fileInfoSums.Len())
201
+	}
202
+
203
+	fileInfo := fileInfoSums.GetFile(DefaultDockerfileName)
204
+
205
+	if fileInfo == nil {
206
+		t.Fatalf("There should be file named %s in fileInfoSums", DefaultDockerfileName)
207
+	}
208
+
209
+	if fileInfo.Pos() != 0 {
210
+		t.Fatalf("File %s should have position 0, got %d", DefaultDockerfileName, fileInfo.Pos())
211
+	}
212
+}