blob: e0611a6b8609168037e2fdfac051ad1d141280d0 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// 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 Nicolaou82d00d82015-02-10 21:31:00 -08005package main_test
6
7import (
8 "bytes"
9 "fmt"
10 "io/ioutil"
11 "net/http"
12 "os"
13 "os/exec"
14 "strings"
15
Jiri Simsa6ac95222015-02-23 16:11:49 -080016 "v.io/v23/naming"
Cosmos Nicolaou1381f8a2015-03-13 09:40:34 -070017 "v.io/x/ref/test/testutil"
18 "v.io/x/ref/test/v23tests"
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -080019)
20
21//go:generate v23 test generate
22
Cosmos Nicolaou01007a02015-02-11 15:38:38 -080023func checkFileType(i *v23tests.T, file, typeString string) {
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -080024 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 Nicolaou01007a02015-02-11 15:38:38 -080036func readFileOrDie(i *v23tests.T, path string) []byte {
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -080037 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 Nicolaou01007a02015-02-11 15:38:38 -080044func compareFiles(i *v23tests.T, f1, f2 string) {
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -080045 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 Shankar6cc759d2015-03-14 03:31:44 -070050func deleteFile(i *v23tests.T, clientBin *v23tests.Binary, name, suffix string) {
51 clientBin.Start("delete", naming.Join(name, suffix)).WaitOrDie(os.Stdout, os.Stderr)
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -080052}
53
Asim Shankar6cc759d2015-03-14 03:31:44 -070054func 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 Nicolaou82d00d82015-02-10 21:31:00 -080057 if expectError && err == nil {
Asim Shankar6cc759d2015-03-14 03:31:44 -070058 i.Fatalf("%s %v: did not fail when it should", clientBin.Path(), args)
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -080059 }
60 if !expectError && err != nil {
Asim Shankar6cc759d2015-03-14 03:31:44 -070061 i.Fatalf("%s %v: failed: %v", clientBin.Path(), args, err)
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -080062 }
63}
64
Cosmos Nicolaou01007a02015-02-11 15:38:38 -080065func downloadURL(i *v23tests.T, path, rootURL, suffix string) {
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -080066 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 Shankar6cc759d2015-03-14 03:31:44 -070081func rootURL(i *v23tests.T, clientBin *v23tests.Binary, name string) string {
82 return strings.TrimSpace(clientBin.Start("url", name).Output())
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -080083}
84
Asim Shankar6cc759d2015-03-14 03:31:44 -070085func 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
89func 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 Nicolaou82d00d82015-02-10 21:31:00 -080093 }
Asim Shankar6cc759d2015-03-14 03:31:44 -070094 b := i.BuildV23Pkg(pkgpath)
95 return b.WithStartOpts(b.StartOpts().WithCustomCredentials(creds))
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -080096}
97
Cosmos Nicolaou01007a02015-02-11 15:38:38 -080098func V23TestBinaryRepositoryIntegration(i *v23tests.T) {
Asim Shankarf32d24d2015-04-01 16:34:26 -070099 v23tests.RunRootMT(i, "--v23.tcp.address=127.0.0.1:0")
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -0800100
101 // Build the required binaries.
Asim Shankar6cc759d2015-03-14 03:31:44 -0700102 // 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 Wang1f7a6c62015-04-03 17:05:09 -0700105 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 Shankar6cc759d2015-03-14 03:31:44 -0700107 )
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -0800108
109 // Start the build server.
110 binaryRepoName := "test-binary-repository"
Asim Shankar6cc759d2015-03-14 03:31:44 -0700111 binaryRepoBin.Start(
112 "-name="+binaryRepoName,
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -0800113 "-http=127.0.0.1:0",
Asim Shankarf32d24d2015-04-01 16:34:26 -0700114 "-v23.tcp.address=127.0.0.1:0")
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -0800115
116 // Upload a random binary file.
Cosmos Nicolaou93dd88b2015-02-19 15:10:53 -0800117 binFile := i.NewTempFile()
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -0800118 if _, err := binFile.Write(testutil.RandomBytes(16 * 1000 * 1000)); err != nil {
119 i.Fatalf("Write() failed: %v", err)
120 }
121 binSuffix := "test-binary"
Asim Shankar6cc759d2015-03-14 03:31:44 -0700122 uploadFile(i, clientBin, binaryRepoName, binFile.Name(), binSuffix)
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -0800123
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 Shankar6cc759d2015-03-14 03:31:44 -0700135 uploadFile(i, clientBin, binaryRepoName, tarFile, tarSuffix)
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -0800136
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 Shankar6cc759d2015-03-14 03:31:44 -0700141 downloadFile(i, clientBin, false, binaryRepoName, downloadedBinFile, binSuffix)
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -0800142 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 Shankar6cc759d2015-03-14 03:31:44 -0700150 downloadFile(i, clientBin, false, binaryRepoName, downloadedTarFile, tarSuffix)
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -0800151 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 Shankar6cc759d2015-03-14 03:31:44 -0700156 root := rootURL(i, clientBin, binaryRepoName)
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -0800157
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 Shankar6cc759d2015-03-14 03:31:44 -0700174 deleteFile(i, clientBin, binaryRepoName, binSuffix)
175 deleteFile(i, clientBin, binaryRepoName, tarSuffix)
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -0800176
177 // Check the files no longer exist.
Asim Shankar6cc759d2015-03-14 03:31:44 -0700178 downloadFile(i, clientBin, true, binaryRepoName, downloadedBinFile, binSuffix)
179 downloadFile(i, clientBin, true, binaryRepoName, downloadedTarFile, tarSuffix)
Cosmos Nicolaou82d00d82015-02-10 21:31:00 -0800180}