This repository was archived by the owner on Jun 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathcompatibility_test.go
193 lines (167 loc) · 5.7 KB
/
compatibility_test.go
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package e2e
import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
"github.com/opencontainers/go-digest"
"gotest.tools/assert"
"k8s.io/apimachinery/pkg/util/wait"
"gotest.tools/fs"
)
const (
pollTimeout = 30 * time.Second
)
func loadAndTagImage(info dindSwarmAndRegistryInfo, tmpDir *fs.Dir, tag string, url string) error {
err := downloadImageTarball(tmpDir.Join("image.tar"), url)
if err != nil {
return err
}
digest := ""
combined := info.dockerCmd("load", "-q", "-i", tmpDir.Join("image.tar"))
for _, line := range strings.Split(combined, "\n") {
if strings.Contains(line, "sha256:") {
digest = strings.Split(line, "sha256:")[1]
}
}
if digest == "" {
return errors.New("Image digest not found in docker load's stdout")
}
digest = strings.Trim(digest, " \r\n")
info.dockerCmd("tag", digest, tag)
return nil
}
func downloadImageTarball(filepath string, url string) error {
client := http.Client{Timeout: time.Minute * 1}
res, err := client.Get(url)
if err != nil {
return err
}
defer res.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, res.Body)
return err
}
func TestBackwardsCompatibilityV1(t *testing.T) {
runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
appName := "app-e2e"
data, err := ioutil.ReadFile(filepath.Join("testdata", "compatibility", "bundle-v0.9.0.json"))
assert.NilError(t, err)
// update bundle
dg := digest.SHA256.FromBytes(data)
bundleDir := filepath.Join(info.configDir, "app", "bundles", "contents", dg.Algorithm().String(), dg.Encoded())
assert.NilError(t, os.MkdirAll(bundleDir, os.FileMode(0777)))
assert.NilError(t, ioutil.WriteFile(filepath.Join(bundleDir, "bundle.json"), data, os.FileMode(0644)))
metadata := filepath.Join(info.configDir, "app", "bundles", "repositories.json")
json := fmt.Sprintf("{\"Repositories\":{\"app-e2e\":{\"app-e2e:v0.9.0\":\"%s\"}}}", dg.String())
assert.NilError(t, ioutil.WriteFile(metadata, []byte(json), 0777))
// load images build with an old Docker App version
assert.NilError(t, loadAndTagImage(info, info.tmpDir, "app-e2e:0.1.0-invoc", "https://github.com/docker/app-e2e/raw/master/images/v0.9.0/app-e2e-invoc.tar"))
assert.NilError(t, loadAndTagImage(info, info.tmpDir, "app-e2e/backend", "https://github.com/docker/app-e2e/raw/master/images/v0.9.0/backend.tar"))
assert.NilError(t, loadAndTagImage(info, info.tmpDir, "app-e2e/frontend", "https://github.com/docker/app-e2e/raw/master/images/v0.9.0/frontend.tar"))
// list images
output := info.dockerCmd("app", "image", "ls")
checkContains(t, output, []string{appName})
// inspect bundle
output = info.dockerCmd("app", "image", "inspect", "app-e2e:v0.9.0", "--pretty")
checkContains(t, output,
[]string{
`name:\s+app-e2e`,
`backend\s+1\s+app-e2e/backend`,
`frontend\s+1\s+8080\s+app-e2e/frontend`,
`ports.frontend\s+8080`,
})
// render bundle
output = info.dockerCmd("app", "image", "render", "app-e2e:v0.9.0")
checkContains(t, output,
[]string{
"image: app-e2e/frontend",
"image: app-e2e/backend",
"published: 8080",
"target: 80",
})
// Install app
output = info.dockerCmd("app", "run", "app-e2e:v0.9.0", "--name", appName)
checkContains(t, output,
[]string{
fmt.Sprintf("Creating service %s_backend", appName),
fmt.Sprintf("Creating service %s_frontend", appName),
fmt.Sprintf("Creating network %s_default", appName),
})
// Status check -- poll app list
checkStatus := func(lastAction string) {
err = wait.Poll(2*time.Second, pollTimeout, func() (bool, error) {
output = info.dockerCmd("app", "ls")
expectedLines := []string{
`RUNNING APP\s+APP NAME\s+SERVICES\s+LAST ACTION\s+RESULT\s+CREATED\s+MODIFIED\s+REFERENCE`,
fmt.Sprintf(`%s\s+%s \(0.1.0\)\s+2/2\s+%s\s+success\s+.+second[s]?\sago\s+.+second[s]?\sago\s+`, appName, appName, lastAction),
}
matches := true
for _, expected := range expectedLines {
exp := regexp.MustCompile(expected)
matches = matches && exp.MatchString(output)
}
return matches, nil
})
assert.NilError(t, err)
}
queryService := func(port string) {
err = wait.Poll(2*time.Second, pollTimeout, func() (bool, error) {
// Check the frontend service responds
url := `http://localhost:` + port
output = info.execCmd("/usr/bin/wget", "-O", "-", url)
return strings.Contains(output, `Hi there, I love Docker!`), nil
})
output = ""
if err != nil {
output = info.dockerCmd("stack", "ps", appName)
}
assert.NilError(t, err, output)
}
// Check status on install
checkStatus("install")
// query deployed service
queryService("8080")
// Inspect app
output = info.dockerCmd("app", "inspect", appName, "--pretty")
checkContains(t, output,
[]string{
"Running App:",
fmt.Sprintf("Name: %s", appName),
"Result: success",
`ports.frontend: "8080"`,
})
// Update the application, changing the port
output = info.dockerCmd("app", "update", appName, "--set", "ports.frontend=8081")
checkContains(t, output,
[]string{
fmt.Sprintf("Updating service %s_backend", appName),
fmt.Sprintf("Updating service %s_frontend", appName),
})
// check status on upgrade
checkStatus("upgrade")
// Check the frontend service responds on the new port
queryService("8081")
// Uninstall the application
output = info.dockerCmd("app", "rm", appName)
checkContains(t, output,
[]string{
fmt.Sprintf("Removing service %s_backend", appName),
fmt.Sprintf("Removing service %s_frontend", appName),
fmt.Sprintf("Removing network %s_default", appName),
})
})
}