blob: ca0a76f242020a640624ad6cf1602d2c075a494f [file] [log] [blame]
Jiri Simsa51d78fc2014-07-09 18:34:08 -07001package binary
2
3import (
4 "bytes"
5 "io/ioutil"
6 "os"
7 "path/filepath"
Robin Thellende2627892014-11-26 09:34:37 -08008 "reflect"
Jiri Simsa51d78fc2014-07-09 18:34:08 -07009 "testing"
10
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -080011 "veyron.io/veyron/veyron2"
Jiri Simsa519c5072014-09-17 21:37:57 -070012 "veyron.io/veyron/veyron2/naming"
13 "veyron.io/veyron/veyron2/rt"
Robin Thellende2627892014-11-26 09:34:37 -080014 "veyron.io/veyron/veyron2/services/mgmt/repository"
Jiri Simsa519c5072014-09-17 21:37:57 -070015 "veyron.io/veyron/veyron2/vlog"
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070016
17 "veyron.io/veyron/veyron/lib/testutil"
18 "veyron.io/veyron/veyron/profiles"
19 "veyron.io/veyron/veyron/services/mgmt/binary/impl"
Jiri Simsa51d78fc2014-07-09 18:34:08 -070020)
21
22const (
23 veyronPrefix = "veyron_binary_repository"
24)
25
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -080026var runtime veyron2.Runtime
27
Jiri Simsa51d78fc2014-07-09 18:34:08 -070028func init() {
Asim Shankarc920db32014-10-16 19:18:21 -070029 testutil.Init()
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -080030
31 var err error
32 runtime, err = rt.New()
33 if err != nil {
34 panic(err)
35 }
Jiri Simsa51d78fc2014-07-09 18:34:08 -070036}
37
38func setupRepository(t *testing.T) (string, func()) {
39 // Setup the root of the binary repository.
40 root, err := ioutil.TempDir("", veyronPrefix)
41 if err != nil {
42 t.Fatalf("TempDir() failed: %v", err)
43 }
44 path, perm := filepath.Join(root, impl.VersionFile), os.FileMode(0600)
45 if err := ioutil.WriteFile(path, []byte(impl.Version), perm); err != nil {
46 vlog.Fatalf("WriteFile(%v, %v, %v) failed: %v", path, impl.Version, perm, err)
47 }
48 // Setup and start the binary repository server.
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -080049 server, err := runtime.NewServer()
Jiri Simsa51d78fc2014-07-09 18:34:08 -070050 if err != nil {
51 t.Fatalf("NewServer() failed: %v", err)
52 }
53 depth := 2
Bogdan Capritae783dcc2014-11-04 14:16:55 -080054 state, err := impl.NewState(root, depth)
Jiri Simsa51d78fc2014-07-09 18:34:08 -070055 if err != nil {
Bogdan Capritae783dcc2014-11-04 14:16:55 -080056 t.Fatalf("NewState(%v, %v) failed: %v", root, depth, err)
Jiri Simsa51d78fc2014-07-09 18:34:08 -070057 }
Bogdan Capritae783dcc2014-11-04 14:16:55 -080058 dispatcher := impl.NewDispatcher(state, nil)
Cosmos Nicolaouf8d4c2b2014-10-23 22:36:38 -070059 endpoint, err := server.Listen(profiles.LocalListenSpec)
Jiri Simsa51d78fc2014-07-09 18:34:08 -070060 if err != nil {
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070061 t.Fatalf("Listen(%s) failed: %v", profiles.LocalListenSpec, err)
Jiri Simsa51d78fc2014-07-09 18:34:08 -070062 }
63 suffix := ""
Cosmos Nicolaou92dba582014-11-05 17:24:10 -080064 if err := server.ServeDispatcher(suffix, dispatcher); err != nil {
Jiri Simsa51d78fc2014-07-09 18:34:08 -070065 t.Fatalf("Serve(%v, %v) failed: %v", suffix, dispatcher, err)
66 }
David Why Use Two When One Will Do Presottoadf0ca12014-11-13 10:49:01 -080067 von := naming.JoinAddressName(endpoint.String(), "test")
Jiri Simsa51d78fc2014-07-09 18:34:08 -070068 return von, func() {
69 if err := os.Remove(path); err != nil {
70 t.Fatalf("Remove(%v) failed: %v", path, err)
71 }
72 // Check that any directories and files that were created to
73 // represent the binary objects have been garbage collected.
74 if err := os.Remove(root); err != nil {
75 t.Fatalf("Remove(%v) failed: %v", root, err)
76 }
77 // Shutdown the binary repository server.
78 if err := server.Stop(); err != nil {
79 t.Fatalf("Stop() failed: %v", err)
80 }
81 }
82}
83
84// TestBufferAPI tests the binary repository client-side library
85// interface using buffers.
86func TestBufferAPI(t *testing.T) {
87 von, cleanup := setupRepository(t)
88 defer cleanup()
89 data := testutil.RandomBytes(testutil.Rand.Intn(10 << 20))
Robin Thellende2627892014-11-26 09:34:37 -080090 mediaInfo := repository.MediaInfo{Type: "application/octet-stream"}
91 if err := Upload(runtime.NewContext(), von, data, mediaInfo); err != nil {
Jiri Simsa51d78fc2014-07-09 18:34:08 -070092 t.Fatalf("Upload(%v) failed: %v", von, err)
93 }
Robin Thellende2627892014-11-26 09:34:37 -080094 output, outInfo, err := Download(runtime.NewContext(), von)
Jiri Simsa51d78fc2014-07-09 18:34:08 -070095 if err != nil {
96 t.Fatalf("Download(%v) failed: %v", von, err)
97 }
98 if bytes.Compare(data, output) != 0 {
Robin Thellende2627892014-11-26 09:34:37 -080099 t.Errorf("Data mismatch:\nexpected %v %v\ngot %v %v", len(data), data[:100], len(output), output[:100])
Jiri Simsa51d78fc2014-07-09 18:34:08 -0700100 }
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -0800101 if err := Delete(runtime.NewContext(), von); err != nil {
Robin Thellende2627892014-11-26 09:34:37 -0800102 t.Errorf("Delete(%v) failed: %v", von, err)
Jiri Simsa51d78fc2014-07-09 18:34:08 -0700103 }
Robin Thellende2627892014-11-26 09:34:37 -0800104 if _, _, err := Download(runtime.NewContext(), von); err == nil {
105 t.Errorf("Download(%v) did not fail", von)
106 }
107 if !reflect.DeepEqual(mediaInfo, outInfo) {
108 t.Errorf("unexpected media info: expected %v, got %v", mediaInfo, outInfo)
Jiri Simsa51d78fc2014-07-09 18:34:08 -0700109 }
110}
111
112// TestFileAPI tests the binary repository client-side library
113// interface using files.
114func TestFileAPI(t *testing.T) {
115 von, cleanup := setupRepository(t)
116 defer cleanup()
117 // Create up to 10MB of random bytes.
118 data := testutil.RandomBytes(testutil.Rand.Intn(10 << 20))
119 dir, prefix := "", ""
120 src, err := ioutil.TempFile(dir, prefix)
121 if err != nil {
122 t.Fatalf("TempFile(%v, %v) failed: %v", dir, prefix, err)
123 }
124 defer os.Remove(src.Name())
125 defer src.Close()
Robin Thellende2627892014-11-26 09:34:37 -0800126 dstdir, err := ioutil.TempDir(dir, prefix)
Jiri Simsa51d78fc2014-07-09 18:34:08 -0700127 if err != nil {
Robin Thellende2627892014-11-26 09:34:37 -0800128 t.Fatalf("TempDir(%v, %v) failed: %v", dir, prefix, err)
Jiri Simsa51d78fc2014-07-09 18:34:08 -0700129 }
Robin Thellende2627892014-11-26 09:34:37 -0800130 defer os.RemoveAll(dstdir)
131 dst, err := ioutil.TempFile(dstdir, prefix)
132 if err != nil {
133 t.Fatalf("TempFile(%v, %v) failed: %v", dstdir, prefix, err)
134 }
Jiri Simsa51d78fc2014-07-09 18:34:08 -0700135 defer dst.Close()
136 if _, err := src.Write(data); err != nil {
137 t.Fatalf("Write() failed: %v", err)
138 }
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -0800139 if err := UploadFromFile(runtime.NewContext(), von, src.Name()); err != nil {
Jiri Simsa51d78fc2014-07-09 18:34:08 -0700140 t.Fatalf("UploadFromFile(%v, %v) failed: %v", von, src.Name(), err)
141 }
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -0800142 if err := DownloadToFile(runtime.NewContext(), von, dst.Name()); err != nil {
Jiri Simsa51d78fc2014-07-09 18:34:08 -0700143 t.Fatalf("DownloadToFile(%v, %v) failed: %v", von, dst.Name(), err)
144 }
145 output, err := ioutil.ReadFile(dst.Name())
146 if err != nil {
Robin Thellende2627892014-11-26 09:34:37 -0800147 t.Errorf("ReadFile(%v) failed: %v", dst.Name(), err)
Jiri Simsa51d78fc2014-07-09 18:34:08 -0700148 }
149 if bytes.Compare(data, output) != 0 {
Robin Thellende2627892014-11-26 09:34:37 -0800150 t.Errorf("Data mismatch:\nexpected %v %v\ngot %v %v", len(data), data[:100], len(output), output[:100])
151 }
152 jMediaInfo, err := ioutil.ReadFile(dst.Name() + ".__info")
153 if err != nil {
154 t.Errorf("ReadFile(%v) failed: %v", dst.Name()+".__info", err)
155 }
156 if expected := `{"Type":"application/octet-stream","Encoding":""}`; string(jMediaInfo) != expected {
157 t.Errorf("unexpected media info: expected %q, got %q", expected, string(jMediaInfo))
Jiri Simsa51d78fc2014-07-09 18:34:08 -0700158 }
Matt Rosencrantzc2ed03e2014-11-25 15:40:48 -0800159 if err := Delete(runtime.NewContext(), von); err != nil {
Robin Thellende2627892014-11-26 09:34:37 -0800160 t.Errorf("Delete(%v) failed: %v", von, err)
Jiri Simsa51d78fc2014-07-09 18:34:08 -0700161 }
162}