Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 5 | package main_test |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "fmt" |
| 10 | "io/ioutil" |
| 11 | "net/http" |
| 12 | "os" |
| 13 | "os/exec" |
| 14 | "strings" |
| 15 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 16 | "v.io/v23/naming" |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 17 | "v.io/x/ref/test/testutil" |
| 18 | "v.io/x/ref/test/v23tests" |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | //go:generate v23 test generate |
| 22 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 23 | func checkFileType(i *v23tests.T, file, typeString string) { |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 24 | var catOut bytes.Buffer |
| 25 | catCmd := exec.Command("cat", file+".__info") |
| 26 | catCmd.Stdout = &catOut |
| 27 | catCmd.Stderr = &catOut |
| 28 | if err := catCmd.Run(); err != nil { |
| 29 | i.Fatalf("%q failed: %v\n%v", strings.Join(catCmd.Args, " "), err, catOut.String()) |
| 30 | } |
| 31 | if got, want := strings.TrimSpace(catOut.String()), typeString; got != want { |
| 32 | i.Fatalf("unexpect file type: got %v, want %v", got, want) |
| 33 | } |
| 34 | } |
| 35 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 36 | func readFileOrDie(i *v23tests.T, path string) []byte { |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 37 | result, err := ioutil.ReadFile(path) |
| 38 | if err != nil { |
| 39 | i.Fatalf("ReadFile(%q) failed: %v", path, err) |
| 40 | } |
| 41 | return result |
| 42 | } |
| 43 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 44 | func compareFiles(i *v23tests.T, f1, f2 string) { |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 45 | if !bytes.Equal(readFileOrDie(i, f1), readFileOrDie(i, f2)) { |
| 46 | i.Fatalf("the contents of %s and %s differ when they should not", f1, f2) |
| 47 | } |
| 48 | } |
| 49 | |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 50 | func deleteFile(i *v23tests.T, clientBin *v23tests.Binary, name, suffix string) { |
| 51 | clientBin.Start("delete", naming.Join(name, suffix)).WaitOrDie(os.Stdout, os.Stderr) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 54 | func downloadFile(i *v23tests.T, clientBin *v23tests.Binary, expectError bool, name, path, suffix string) { |
| 55 | args := []string{"download", naming.Join(name, suffix), path} |
| 56 | err := clientBin.Start(args...).Wait(os.Stdout, os.Stderr) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 57 | if expectError && err == nil { |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 58 | i.Fatalf("%s %v: did not fail when it should", clientBin.Path(), args) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 59 | } |
| 60 | if !expectError && err != nil { |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 61 | i.Fatalf("%s %v: failed: %v", clientBin.Path(), args, err) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 65 | func downloadURL(i *v23tests.T, path, rootURL, suffix string) { |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 66 | url := fmt.Sprintf("http://%v/%v", rootURL, suffix) |
| 67 | resp, err := http.Get(url) |
| 68 | if err != nil { |
| 69 | i.Fatalf("Get(%q) failed: %v", url, err) |
| 70 | } |
| 71 | output, err := ioutil.ReadAll(resp.Body) |
| 72 | resp.Body.Close() |
| 73 | if err != nil { |
| 74 | i.Fatalf("ReadAll() failed: %v", err) |
| 75 | } |
| 76 | if err = ioutil.WriteFile(path, output, 0600); err != nil { |
| 77 | i.Fatalf("WriteFile() failed: %v", err) |
| 78 | } |
| 79 | } |
| 80 | |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 81 | func rootURL(i *v23tests.T, clientBin *v23tests.Binary, name string) string { |
| 82 | return strings.TrimSpace(clientBin.Start("url", name).Output()) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 85 | func uploadFile(i *v23tests.T, clientBin *v23tests.Binary, name, path, suffix string) { |
| 86 | clientBin.Start("upload", naming.Join(name, suffix), path).WaitOrDie(os.Stdout, os.Stderr) |
| 87 | } |
| 88 | |
| 89 | func binaryWithCredentials(i *v23tests.T, extension, pkgpath string) *v23tests.Binary { |
| 90 | creds, err := i.Shell().NewChildCredentials(extension) |
| 91 | if err != nil { |
| 92 | i.Fatalf("NewCustomCredentials (for %q) failed: %v", pkgpath, err) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 93 | } |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 94 | b := i.BuildV23Pkg(pkgpath) |
| 95 | return b.WithStartOpts(b.StartOpts().WithCustomCredentials(creds)) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Cosmos Nicolaou | 01007a0 | 2015-02-11 15:38:38 -0800 | [diff] [blame] | 98 | func V23TestBinaryRepositoryIntegration(i *v23tests.T) { |
Asim Shankar | f32d24d | 2015-04-01 16:34:26 -0700 | [diff] [blame] | 99 | v23tests.RunRootMT(i, "--v23.tcp.address=127.0.0.1:0") |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 100 | |
| 101 | // Build the required binaries. |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 102 | // The client must run as a "delegate" of the server in order to pass |
| 103 | // the default authorization checks on the server. |
| 104 | var ( |
Todd Wang | 1f7a6c6 | 2015-04-03 17:05:09 -0700 | [diff] [blame] | 105 | binaryRepoBin = binaryWithCredentials(i, "binaryd", "v.io/x/ref/services/binary/binaryd") |
| 106 | clientBin = binaryWithCredentials(i, "binaryd/client", "v.io/x/ref/services/binary/binary") |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 107 | ) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 108 | |
| 109 | // Start the build server. |
| 110 | binaryRepoName := "test-binary-repository" |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 111 | binaryRepoBin.Start( |
| 112 | "-name="+binaryRepoName, |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 113 | "-http=127.0.0.1:0", |
Asim Shankar | f32d24d | 2015-04-01 16:34:26 -0700 | [diff] [blame] | 114 | "-v23.tcp.address=127.0.0.1:0") |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 115 | |
| 116 | // Upload a random binary file. |
Cosmos Nicolaou | 93dd88b | 2015-02-19 15:10:53 -0800 | [diff] [blame] | 117 | binFile := i.NewTempFile() |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 118 | if _, err := binFile.Write(testutil.RandomBytes(16 * 1000 * 1000)); err != nil { |
| 119 | i.Fatalf("Write() failed: %v", err) |
| 120 | } |
| 121 | binSuffix := "test-binary" |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 122 | uploadFile(i, clientBin, binaryRepoName, binFile.Name(), binSuffix) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 123 | |
| 124 | // Upload a compressed version of the binary file. |
| 125 | tarFile := binFile.Name() + ".tar.gz" |
| 126 | var tarOut bytes.Buffer |
| 127 | tarCmd := exec.Command("tar", "zcvf", tarFile, binFile.Name()) |
| 128 | tarCmd.Stdout = &tarOut |
| 129 | tarCmd.Stderr = &tarOut |
| 130 | if err := tarCmd.Run(); err != nil { |
| 131 | i.Fatalf("%q failed: %v\n%v", strings.Join(tarCmd.Args, " "), err, tarOut.String()) |
| 132 | } |
| 133 | defer os.Remove(tarFile) |
| 134 | tarSuffix := "test-compressed-file" |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 135 | uploadFile(i, clientBin, binaryRepoName, tarFile, tarSuffix) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 136 | |
| 137 | // Download the binary file and check that it matches the |
| 138 | // original one and that it has the right file type. |
| 139 | downloadedBinFile := binFile.Name() + "-downloaded" |
| 140 | defer os.Remove(downloadedBinFile) |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 141 | downloadFile(i, clientBin, false, binaryRepoName, downloadedBinFile, binSuffix) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 142 | compareFiles(i, binFile.Name(), downloadedBinFile) |
| 143 | checkFileType(i, downloadedBinFile, `{"Type":"application/octet-stream","Encoding":""}`) |
| 144 | |
| 145 | // Download the compressed version of the binary file and |
| 146 | // check that it matches the original one and that it has the |
| 147 | // right file type. |
| 148 | downloadedTarFile := binFile.Name() + "-downloaded.tar.gz" |
| 149 | defer os.Remove(downloadedTarFile) |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 150 | downloadFile(i, clientBin, false, binaryRepoName, downloadedTarFile, tarSuffix) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 151 | compareFiles(i, tarFile, downloadedTarFile) |
| 152 | checkFileType(i, downloadedTarFile, `{"Type":"application/x-tar","Encoding":"gzip"}`) |
| 153 | |
| 154 | // Fetch the root URL of the HTTP server used by the binary |
| 155 | // repository to serve URLs. |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 156 | root := rootURL(i, clientBin, binaryRepoName) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 157 | |
| 158 | // Download the binary file using the HTTP protocol and check |
| 159 | // that it matches the original one. |
| 160 | downloadedBinFileURL := binFile.Name() + "-downloaded-url" |
| 161 | defer os.Remove(downloadedBinFileURL) |
| 162 | downloadURL(i, downloadedBinFileURL, root, binSuffix) |
| 163 | compareFiles(i, downloadedBinFile, downloadedBinFileURL) |
| 164 | |
| 165 | // Download the compressed version of the binary file using |
| 166 | // the HTTP protocol and check that it matches the original |
| 167 | // one. |
| 168 | downloadedTarFileURL := binFile.Name() + "-downloaded-url.tar.gz" |
| 169 | defer os.Remove(downloadedTarFileURL) |
| 170 | downloadURL(i, downloadedTarFileURL, root, tarSuffix) |
| 171 | compareFiles(i, downloadedTarFile, downloadedTarFileURL) |
| 172 | |
| 173 | // Delete the files. |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 174 | deleteFile(i, clientBin, binaryRepoName, binSuffix) |
| 175 | deleteFile(i, clientBin, binaryRepoName, tarSuffix) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 176 | |
| 177 | // Check the files no longer exist. |
Asim Shankar | 6cc759d | 2015-03-14 03:31:44 -0700 | [diff] [blame] | 178 | downloadFile(i, clientBin, true, binaryRepoName, downloadedBinFile, binSuffix) |
| 179 | downloadFile(i, clientBin, true, binaryRepoName, downloadedTarFile, tarSuffix) |
Cosmos Nicolaou | 82d00d8 | 2015-02-10 21:31:00 -0800 | [diff] [blame] | 180 | } |