Browse code

Aux functions for build testing

Docker-DCO-1.1-Signed-off-by: Alexandr Morozov <lk4d4math@gmail.com> (github: LK4D4)

LK4D4 authored on 2014/05/31 21:42:49
Showing 1 changed files
... ...
@@ -2,7 +2,12 @@ package main
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"io/ioutil"
6
+	"net/http"
7
+	"net/http/httptest"
8
+	"os"
5 9
 	"os/exec"
10
+	"path"
6 11
 	"strconv"
7 12
 	"strings"
8 13
 	"testing"
... ...
@@ -97,3 +102,120 @@ func getContainerCount() (int, error) {
97 97
 	}
98 98
 	return 0, fmt.Errorf("couldn't find the Container count in the output")
99 99
 }
100
+
101
+type FakeContext struct {
102
+	Dir string
103
+}
104
+
105
+func (f *FakeContext) Add(file, content string) error {
106
+	filepath := path.Join(f.Dir, file)
107
+	dirpath := path.Dir(filepath)
108
+	if dirpath != "." {
109
+		if err := os.MkdirAll(dirpath, 0755); err != nil {
110
+			return err
111
+		}
112
+	}
113
+	return ioutil.WriteFile(filepath, []byte(content), 0644)
114
+}
115
+
116
+func (f *FakeContext) Delete(file string) error {
117
+	filepath := path.Join(f.Dir, file)
118
+	return os.RemoveAll(filepath)
119
+}
120
+
121
+func (f *FakeContext) Close() error {
122
+	return os.RemoveAll(f.Dir)
123
+}
124
+
125
+func fakeContext(dockerfile string, files map[string]string) (*FakeContext, error) {
126
+	tmp, err := ioutil.TempDir("", "fake-context")
127
+	if err != nil {
128
+		return nil, err
129
+	}
130
+	ctx := &FakeContext{tmp}
131
+	for file, content := range files {
132
+		if err := ctx.Add(file, content); err != nil {
133
+			ctx.Close()
134
+			return nil, err
135
+		}
136
+	}
137
+	if err := ctx.Add("Dockerfile", dockerfile); err != nil {
138
+		ctx.Close()
139
+		return nil, err
140
+	}
141
+	return ctx, nil
142
+}
143
+
144
+type FakeStorage struct {
145
+	*FakeContext
146
+	*httptest.Server
147
+}
148
+
149
+func (f *FakeStorage) Close() error {
150
+	f.Server.Close()
151
+	return f.FakeContext.Close()
152
+}
153
+
154
+func fakeStorage(files map[string]string) (*FakeStorage, error) {
155
+	tmp, err := ioutil.TempDir("", "fake-storage")
156
+	if err != nil {
157
+		return nil, err
158
+	}
159
+	ctx := &FakeContext{tmp}
160
+	for file, content := range files {
161
+		if err := ctx.Add(file, content); err != nil {
162
+			ctx.Close()
163
+			return nil, err
164
+		}
165
+	}
166
+	handler := http.FileServer(http.Dir(ctx.Dir))
167
+	server := httptest.NewServer(handler)
168
+	return &FakeStorage{
169
+		FakeContext: ctx,
170
+		Server:      server,
171
+	}, nil
172
+}
173
+
174
+func inspectField(name, field string) (string, error) {
175
+	format := fmt.Sprintf("{{.%s}}", field)
176
+	inspectCmd := exec.Command(dockerBinary, "inspect", "-f", format, name)
177
+	out, exitCode, err := runCommandWithOutput(inspectCmd)
178
+	if err != nil || exitCode != 0 {
179
+		return "", fmt.Errorf("failed to inspect %s: %s", name, out)
180
+	}
181
+	return strings.TrimSpace(out), nil
182
+}
183
+
184
+func getIDByName(name string) (string, error) {
185
+	return inspectField(name, "Id")
186
+}
187
+
188
+func buildImage(name, dockerfile string, useCache bool) (string, error) {
189
+	args := []string{"build", "-t", name}
190
+	if !useCache {
191
+		args = append(args, "--no-cache")
192
+	}
193
+	args = append(args, "-")
194
+	buildCmd := exec.Command(dockerBinary, args...)
195
+	buildCmd.Stdin = strings.NewReader(dockerfile)
196
+	out, exitCode, err := runCommandWithOutput(buildCmd)
197
+	if err != nil || exitCode != 0 {
198
+		return "", fmt.Errorf("failed to build the image: %s", out)
199
+	}
200
+	return getIDByName(name)
201
+}
202
+
203
+func buildImageFromContext(name string, ctx *FakeContext, useCache bool) (string, error) {
204
+	args := []string{"build", "-t", name}
205
+	if !useCache {
206
+		args = append(args, "--no-cache")
207
+	}
208
+	args = append(args, ".")
209
+	buildCmd := exec.Command(dockerBinary, args...)
210
+	buildCmd.Dir = ctx.Dir
211
+	out, exitCode, err := runCommandWithOutput(buildCmd)
212
+	if err != nil || exitCode != 0 {
213
+		return "", fmt.Errorf("failed to build the image: %s", out)
214
+	}
215
+	return getIDByName(name)
216
+}