|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "regexp" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | + |
| 16 | + "gotest.tools/assert" |
| 17 | + "k8s.io/apimachinery/pkg/util/wait" |
| 18 | + |
| 19 | + "gotest.tools/fs" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + pollTimeout = 30 * time.Second |
| 24 | +) |
| 25 | + |
| 26 | +func loadAndTagImage(info dindSwarmAndRegistryInfo, tmpDir *fs.Dir, tag string, url string) error { |
| 27 | + err := downloadImageTarball(tmpDir.Join("image.tar"), url) |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + |
| 32 | + digest := "" |
| 33 | + combined := info.dockerCmd("load", "-q", "-i", tmpDir.Join("image.tar")) |
| 34 | + for _, line := range strings.Split(combined, "\n") { |
| 35 | + if strings.Contains(line, "sha256:") { |
| 36 | + digest = strings.Split(line, "sha256:")[1] |
| 37 | + } |
| 38 | + } |
| 39 | + if digest == "" { |
| 40 | + return errors.New("Image digest not found in docker load's stdout") |
| 41 | + } |
| 42 | + |
| 43 | + digest = strings.Trim(digest, " \r\n") |
| 44 | + info.dockerCmd("tag", digest, tag) |
| 45 | + |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +func downloadImageTarball(filepath string, url string) error { |
| 50 | + client := http.Client{Timeout: time.Minute * 1} |
| 51 | + res, err := client.Get(url) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + defer res.Body.Close() |
| 56 | + |
| 57 | + // Create the file |
| 58 | + out, err := os.Create(filepath) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + defer out.Close() |
| 63 | + // Write the body to file |
| 64 | + _, err = io.Copy(out, res.Body) |
| 65 | + return err |
| 66 | +} |
| 67 | + |
| 68 | +func TestBackwardsCompatibilityV1(t *testing.T) { |
| 69 | + runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) { |
| 70 | + appName := "app-e2e" |
| 71 | + |
| 72 | + data, err := ioutil.ReadFile(filepath.Join("testdata", "compatibility", "bundle-v0.9.0.json")) |
| 73 | + assert.NilError(t, err) |
| 74 | + // update bundle |
| 75 | + bundleDir := filepath.Join(info.configDir, "app", "bundles", "docker.io", "library", "app-e2e", "_tags", "v0.9.0") |
| 76 | + assert.NilError(t, os.MkdirAll(bundleDir, os.FileMode(0777))) |
| 77 | + assert.NilError(t, ioutil.WriteFile(filepath.Join(bundleDir, "bundle.json"), data, os.FileMode(0644))) |
| 78 | + |
| 79 | + // load images build with an old Docker App version |
| 80 | + 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")) |
| 81 | + assert.NilError(t, loadAndTagImage(info, info.tmpDir, "app-e2e/backend", "https://github.com/docker/app-e2e/raw/master/images/v0.9.0/backend.tar")) |
| 82 | + assert.NilError(t, loadAndTagImage(info, info.tmpDir, "app-e2e/frontend", "https://github.com/docker/app-e2e/raw/master/images/v0.9.0/frontend.tar")) |
| 83 | + |
| 84 | + // list images |
| 85 | + output := info.dockerCmd("app", "image", "ls") |
| 86 | + checkContains(t, output, []string{appName}) |
| 87 | + // inspect bundle |
| 88 | + output = info.dockerCmd("app", "image", "inspect", "app-e2e:v0.9.0", "--pretty") |
| 89 | + checkContains(t, output, |
| 90 | + []string{ |
| 91 | + `name:\s+app-e2e`, |
| 92 | + `backend\s+1\s+app-e2e/backend`, |
| 93 | + `frontend\s+1\s+8080\s+app-e2e/frontend`, |
| 94 | + `ports.frontend\s+8080`, |
| 95 | + }) |
| 96 | + |
| 97 | + // render bundle |
| 98 | + output = info.dockerCmd("app", "image", "render", "app-e2e:v0.9.0") |
| 99 | + checkContains(t, output, |
| 100 | + []string{ |
| 101 | + "image: app-e2e/frontend", |
| 102 | + "image: app-e2e/backend", |
| 103 | + "published: 8080", |
| 104 | + "target: 80", |
| 105 | + }) |
| 106 | + |
| 107 | + // Install app |
| 108 | + output = info.dockerCmd("app", "run", "app-e2e:v0.9.0", "--name", appName) |
| 109 | + checkContains(t, output, |
| 110 | + []string{ |
| 111 | + fmt.Sprintf("Creating service %s_backend", appName), |
| 112 | + fmt.Sprintf("Creating service %s_frontend", appName), |
| 113 | + fmt.Sprintf("Creating network %s_default", appName), |
| 114 | + }) |
| 115 | + |
| 116 | + // Status check -- poll app list |
| 117 | + checkStatus := func(lastAction string) { |
| 118 | + err = wait.Poll(2*time.Second, pollTimeout, func() (bool, error) { |
| 119 | + output = info.dockerCmd("app", "ls") |
| 120 | + expectedLines := []string{ |
| 121 | + `RUNNING APP\s+APP NAME\s+SERVICES\s+LAST ACTION\s+RESULT\s+CREATED\s+MODIFIED\s+REFERENCE`, |
| 122 | + 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), |
| 123 | + } |
| 124 | + matches := true |
| 125 | + for _, expected := range expectedLines { |
| 126 | + exp := regexp.MustCompile(expected) |
| 127 | + matches = matches && exp.MatchString(output) |
| 128 | + } |
| 129 | + return matches, nil |
| 130 | + }) |
| 131 | + assert.NilError(t, err) |
| 132 | + } |
| 133 | + |
| 134 | + queryService := func(port string) { |
| 135 | + err = wait.Poll(2*time.Second, pollTimeout, func() (bool, error) { |
| 136 | + // Check the frontend service responds |
| 137 | + url := `http://localhost:` + port |
| 138 | + output = info.execCmd("/usr/bin/wget", "-O", "-", url) |
| 139 | + return strings.Contains(output, `Hi there, I love Docker!`), nil |
| 140 | + }) |
| 141 | + assert.NilError(t, err) |
| 142 | + } |
| 143 | + |
| 144 | + // Check status on install |
| 145 | + checkStatus("install") |
| 146 | + |
| 147 | + // query deployed service |
| 148 | + queryService("8080") |
| 149 | + |
| 150 | + // Inspect app |
| 151 | + output = info.dockerCmd("app", "inspect", appName, "--pretty") |
| 152 | + checkContains(t, output, |
| 153 | + []string{ |
| 154 | + "Running App:", |
| 155 | + fmt.Sprintf("Name: %s", appName), |
| 156 | + "Result: success", |
| 157 | + `ports.frontend: "8080"`, |
| 158 | + }) |
| 159 | + |
| 160 | + // Update the application, changing the port |
| 161 | + output = info.dockerCmd("app", "update", appName, "--set", "ports.frontend=8081") |
| 162 | + checkContains(t, output, |
| 163 | + []string{ |
| 164 | + fmt.Sprintf("Updating service %s_backend", appName), |
| 165 | + fmt.Sprintf("Updating service %s_frontend", appName), |
| 166 | + }) |
| 167 | + |
| 168 | + // check status on upgrade |
| 169 | + checkStatus("upgrade") |
| 170 | + |
| 171 | + // Check the frontend service responds on the new port |
| 172 | + queryService("8081") |
| 173 | + |
| 174 | + // Uninstall the application |
| 175 | + output = info.dockerCmd("app", "rm", appName) |
| 176 | + checkContains(t, output, |
| 177 | + []string{ |
| 178 | + fmt.Sprintf("Removing service %s_backend", appName), |
| 179 | + fmt.Sprintf("Removing service %s_frontend", appName), |
| 180 | + fmt.Sprintf("Removing network %s_default", appName), |
| 181 | + }) |
| 182 | + }) |
| 183 | +} |
0 commit comments