Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 1 | package binary |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io/ioutil" |
| 6 | "os" |
| 7 | "path/filepath" |
Robin Thellend | e262789 | 2014-11-26 09:34:37 -0800 | [diff] [blame] | 8 | "reflect" |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 9 | "testing" |
| 10 | |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 11 | "veyron.io/veyron/veyron2" |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 12 | "veyron.io/veyron/veyron2/naming" |
| 13 | "veyron.io/veyron/veyron2/rt" |
Robin Thellend | e262789 | 2014-11-26 09:34:37 -0800 | [diff] [blame] | 14 | "veyron.io/veyron/veyron2/services/mgmt/repository" |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 15 | "veyron.io/veyron/veyron2/vlog" |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 16 | |
| 17 | "veyron.io/veyron/veyron/lib/testutil" |
| 18 | "veyron.io/veyron/veyron/profiles" |
| 19 | "veyron.io/veyron/veyron/services/mgmt/binary/impl" |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | const ( |
| 23 | veyronPrefix = "veyron_binary_repository" |
| 24 | ) |
| 25 | |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 26 | var runtime veyron2.Runtime |
| 27 | |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 28 | func init() { |
Asim Shankar | c920db3 | 2014-10-16 19:18:21 -0700 | [diff] [blame] | 29 | testutil.Init() |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 30 | |
| 31 | var err error |
| 32 | runtime, err = rt.New() |
| 33 | if err != nil { |
| 34 | panic(err) |
| 35 | } |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | func 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 Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 49 | server, err := runtime.NewServer() |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 50 | if err != nil { |
| 51 | t.Fatalf("NewServer() failed: %v", err) |
| 52 | } |
| 53 | depth := 2 |
Bogdan Caprita | e783dcc | 2014-11-04 14:16:55 -0800 | [diff] [blame] | 54 | state, err := impl.NewState(root, depth) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 55 | if err != nil { |
Bogdan Caprita | e783dcc | 2014-11-04 14:16:55 -0800 | [diff] [blame] | 56 | t.Fatalf("NewState(%v, %v) failed: %v", root, depth, err) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 57 | } |
Bogdan Caprita | e783dcc | 2014-11-04 14:16:55 -0800 | [diff] [blame] | 58 | dispatcher := impl.NewDispatcher(state, nil) |
Cosmos Nicolaou | f8d4c2b | 2014-10-23 22:36:38 -0700 | [diff] [blame] | 59 | endpoint, err := server.Listen(profiles.LocalListenSpec) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 60 | if err != nil { |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 61 | t.Fatalf("Listen(%s) failed: %v", profiles.LocalListenSpec, err) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 62 | } |
| 63 | suffix := "" |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 64 | if err := server.ServeDispatcher(suffix, dispatcher); err != nil { |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 65 | t.Fatalf("Serve(%v, %v) failed: %v", suffix, dispatcher, err) |
| 66 | } |
David Why Use Two When One Will Do Presotto | adf0ca1 | 2014-11-13 10:49:01 -0800 | [diff] [blame] | 67 | von := naming.JoinAddressName(endpoint.String(), "test") |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 68 | 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. |
| 86 | func TestBufferAPI(t *testing.T) { |
| 87 | von, cleanup := setupRepository(t) |
| 88 | defer cleanup() |
| 89 | data := testutil.RandomBytes(testutil.Rand.Intn(10 << 20)) |
Robin Thellend | e262789 | 2014-11-26 09:34:37 -0800 | [diff] [blame] | 90 | mediaInfo := repository.MediaInfo{Type: "application/octet-stream"} |
| 91 | if err := Upload(runtime.NewContext(), von, data, mediaInfo); err != nil { |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 92 | t.Fatalf("Upload(%v) failed: %v", von, err) |
| 93 | } |
Robin Thellend | e262789 | 2014-11-26 09:34:37 -0800 | [diff] [blame] | 94 | output, outInfo, err := Download(runtime.NewContext(), von) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 95 | if err != nil { |
| 96 | t.Fatalf("Download(%v) failed: %v", von, err) |
| 97 | } |
| 98 | if bytes.Compare(data, output) != 0 { |
Robin Thellend | e262789 | 2014-11-26 09:34:37 -0800 | [diff] [blame] | 99 | t.Errorf("Data mismatch:\nexpected %v %v\ngot %v %v", len(data), data[:100], len(output), output[:100]) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 100 | } |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 101 | if err := Delete(runtime.NewContext(), von); err != nil { |
Robin Thellend | e262789 | 2014-11-26 09:34:37 -0800 | [diff] [blame] | 102 | t.Errorf("Delete(%v) failed: %v", von, err) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 103 | } |
Robin Thellend | e262789 | 2014-11-26 09:34:37 -0800 | [diff] [blame] | 104 | 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 Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | // TestFileAPI tests the binary repository client-side library |
| 113 | // interface using files. |
| 114 | func 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 Thellend | e262789 | 2014-11-26 09:34:37 -0800 | [diff] [blame] | 126 | dstdir, err := ioutil.TempDir(dir, prefix) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 127 | if err != nil { |
Robin Thellend | e262789 | 2014-11-26 09:34:37 -0800 | [diff] [blame] | 128 | t.Fatalf("TempDir(%v, %v) failed: %v", dir, prefix, err) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 129 | } |
Robin Thellend | e262789 | 2014-11-26 09:34:37 -0800 | [diff] [blame] | 130 | 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 Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 135 | defer dst.Close() |
| 136 | if _, err := src.Write(data); err != nil { |
| 137 | t.Fatalf("Write() failed: %v", err) |
| 138 | } |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 139 | if err := UploadFromFile(runtime.NewContext(), von, src.Name()); err != nil { |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 140 | t.Fatalf("UploadFromFile(%v, %v) failed: %v", von, src.Name(), err) |
| 141 | } |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 142 | if err := DownloadToFile(runtime.NewContext(), von, dst.Name()); err != nil { |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 143 | t.Fatalf("DownloadToFile(%v, %v) failed: %v", von, dst.Name(), err) |
| 144 | } |
| 145 | output, err := ioutil.ReadFile(dst.Name()) |
| 146 | if err != nil { |
Robin Thellend | e262789 | 2014-11-26 09:34:37 -0800 | [diff] [blame] | 147 | t.Errorf("ReadFile(%v) failed: %v", dst.Name(), err) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 148 | } |
| 149 | if bytes.Compare(data, output) != 0 { |
Robin Thellend | e262789 | 2014-11-26 09:34:37 -0800 | [diff] [blame] | 150 | 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 Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 158 | } |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 159 | if err := Delete(runtime.NewContext(), von); err != nil { |
Robin Thellend | e262789 | 2014-11-26 09:34:37 -0800 | [diff] [blame] | 160 | t.Errorf("Delete(%v) failed: %v", von, err) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 161 | } |
| 162 | } |