Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 1 | package impl_test |
| 2 | |
| 3 | import ( |
Bogdan Caprita | d2b9f03 | 2014-10-10 17:43:29 -0700 | [diff] [blame] | 4 | "io/ioutil" |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 5 | "os" |
Bogdan Caprita | d2b9f03 | 2014-10-10 17:43:29 -0700 | [diff] [blame] | 6 | "path/filepath" |
Robert Kroeger | 1cb4a0d | 2014-10-20 11:55:38 -0700 | [diff] [blame] | 7 | "reflect" |
| 8 | "sort" |
Shyam Jayaraman | dbae76b | 2014-11-17 12:51:29 -0800 | [diff] [blame] | 9 | "strings" |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 10 | "testing" |
| 11 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 12 | "v.io/core/veyron2" |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 13 | "v.io/core/veyron2/context" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 14 | "v.io/core/veyron2/ipc" |
| 15 | "v.io/core/veyron2/naming" |
| 16 | "v.io/core/veyron2/security" |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 17 | "v.io/core/veyron2/services/mgmt/application" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 18 | "v.io/core/veyron2/services/mgmt/device" |
Todd Wang | 1577651 | 2015-02-10 16:52:55 -0800 | [diff] [blame^] | 19 | verror "v.io/core/veyron2/verror2" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 20 | "v.io/core/veyron2/vlog" |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 21 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 22 | "v.io/core/veyron/lib/modules" |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 23 | "v.io/core/veyron/lib/testutil" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 24 | _ "v.io/core/veyron/profiles/static" |
| 25 | "v.io/core/veyron/services/mgmt/device/impl" |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
Bogdan Caprita | 916e99f | 2014-11-24 15:47:19 -0800 | [diff] [blame] | 28 | const ( |
Bogdan Caprita | 916e99f | 2014-11-24 15:47:19 -0800 | [diff] [blame] | 29 | // TODO(caprita): Set the timeout in a more principled manner. |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 30 | stopTimeout = 20 // In seconds. |
Bogdan Caprita | 916e99f | 2014-11-24 15:47:19 -0800 | [diff] [blame] | 31 | ) |
Bogdan Caprita | d2b9f03 | 2014-10-10 17:43:29 -0700 | [diff] [blame] | 32 | |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 33 | func envelopeFromShell(sh *modules.Shell, env []string, cmd, title string, args ...string) application.Envelope { |
| 34 | args, nenv := sh.CommandEnvelope(cmd, env, args...) |
| 35 | return application.Envelope{ |
| 36 | Title: title, |
| 37 | Args: args[1:], |
| 38 | // TODO(caprita): revisit how the environment is sanitized for arbirary |
| 39 | // apps. |
| 40 | Env: impl.VeyronEnvironment(nenv), |
| 41 | Binary: mockBinaryRepoName, |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
Bogdan Caprita | 474ad11 | 2014-12-03 19:24:12 -0800 | [diff] [blame] | 45 | // resolveExpectNotFound verifies that the given name is not in the mounttable. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 46 | func resolveExpectNotFound(t *testing.T, ctx *context.T, name string) { |
| 47 | if me, err := veyron2.GetNamespace(ctx).Resolve(ctx, name); err == nil { |
| 48 | t.Fatalf(testutil.FormatLogLine(2, "Resolve(%v) succeeded with results %v when it was expected to fail", name, me.Names)) |
Todd Wang | 1577651 | 2015-02-10 16:52:55 -0800 | [diff] [blame^] | 49 | } else if expectErr := naming.ErrNoSuchName.ID; !verror.Is(err, expectErr) { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 50 | t.Fatalf(testutil.FormatLogLine(2, "Resolve(%v) failed with error %v, expected error ID %v", name, err, expectErr)) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | // resolve looks up the given name in the mounttable. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 55 | func resolve(t *testing.T, ctx *context.T, name string, replicas int) []string { |
| 56 | me, err := veyron2.GetNamespace(ctx).Resolve(ctx, name) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 57 | if err != nil { |
| 58 | t.Fatalf("Resolve(%v) failed: %v", name, err) |
| 59 | } |
Shyam Jayaraman | dbae76b | 2014-11-17 12:51:29 -0800 | [diff] [blame] | 60 | |
| 61 | filteredResults := []string{} |
David Why Use Two When One Will Do Presotto | 8de8585 | 2015-01-21 11:05:09 -0800 | [diff] [blame] | 62 | for _, r := range me.Names() { |
Shyam Jayaraman | dbae76b | 2014-11-17 12:51:29 -0800 | [diff] [blame] | 63 | if strings.Index(r, "@tcp") != -1 { |
| 64 | filteredResults = append(filteredResults, r) |
| 65 | } |
| 66 | } |
| 67 | // We are going to get a websocket and a tcp endpoint for each replica. |
| 68 | if want, got := replicas, len(filteredResults); want != got { |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 69 | t.Fatalf("Resolve(%v) expected %d result(s), got %d instead", name, want, got) |
| 70 | } |
Shyam Jayaraman | dbae76b | 2014-11-17 12:51:29 -0800 | [diff] [blame] | 71 | return filteredResults |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // The following set of functions are convenience wrappers around Update and |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 75 | // Revert for device manager. |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 76 | |
Bogdan Caprita | a456f47 | 2014-12-10 10:18:03 -0800 | [diff] [blame] | 77 | func deviceStub(name string) device.DeviceClientMethods { |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 78 | deviceName := naming.Join(name, "device") |
Bogdan Caprita | a456f47 | 2014-12-10 10:18:03 -0800 | [diff] [blame] | 79 | return device.DeviceClient(deviceName) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 82 | func updateDeviceExpectError(t *testing.T, ctx *context.T, name string, errID verror.ID) { |
Todd Wang | 1577651 | 2015-02-10 16:52:55 -0800 | [diff] [blame^] | 83 | if err := deviceStub(name).Update(ctx); !verror.Is(err, errID) { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 84 | t.Fatalf(testutil.FormatLogLine(2, "Update(%v) expected to fail with %v, got %v instead", name, errID, err)) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 88 | func updateDevice(t *testing.T, ctx *context.T, name string) { |
| 89 | if err := deviceStub(name).Update(ctx); err != nil { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 90 | t.Fatalf(testutil.FormatLogLine(2, "Update(%v) failed: %v", name, err)) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 94 | func revertDeviceExpectError(t *testing.T, ctx *context.T, name string, errID verror.ID) { |
Todd Wang | 1577651 | 2015-02-10 16:52:55 -0800 | [diff] [blame^] | 95 | if err := deviceStub(name).Revert(ctx); !verror.Is(err, errID) { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 96 | t.Fatalf(testutil.FormatLogLine(2, "Revert(%v) expected to fail with %v, got %v instead", name, errID, err)) |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 100 | func revertDevice(t *testing.T, ctx *context.T, name string) { |
| 101 | if err := deviceStub(name).Revert(ctx); err != nil { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 102 | t.Fatalf(testutil.FormatLogLine(2, "Revert(%v) failed: %v", name, err)) |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 106 | func stopDevice(t *testing.T, ctx *context.T, name string) { |
| 107 | if err := deviceStub(name).Stop(ctx, stopTimeout); err != nil { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 108 | t.Fatalf(testutil.FormatLogLine(1+1, "%s: Stop(%v) failed: %v", name, err)) |
Bogdan Caprita | 4ea9b03 | 2014-12-27 14:56:51 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 112 | func suspendDevice(t *testing.T, ctx *context.T, name string) { |
| 113 | if err := deviceStub(name).Suspend(ctx); err != nil { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 114 | t.Fatalf(testutil.FormatLogLine(1+1, "%s: Suspend(%v) failed: %v", name, err)) |
Bogdan Caprita | 4ea9b03 | 2014-12-27 14:56:51 -0800 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 118 | // The following set of functions are convenience wrappers around various app |
| 119 | // management methods. |
| 120 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 121 | func ocfg(opt []interface{}) device.Config { |
| 122 | for _, o := range opt { |
| 123 | if c, ok := o.(device.Config); ok { |
| 124 | return c |
| 125 | } |
| 126 | } |
| 127 | return device.Config{} |
| 128 | } |
| 129 | |
Bogdan Caprita | a456f47 | 2014-12-10 10:18:03 -0800 | [diff] [blame] | 130 | func appStub(nameComponents ...string) device.ApplicationClientMethods { |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 131 | appsName := "dm//apps" |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 132 | appName := naming.Join(append([]string{appsName}, nameComponents...)...) |
Bogdan Caprita | a456f47 | 2014-12-10 10:18:03 -0800 | [diff] [blame] | 133 | return device.ApplicationClient(appName) |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 136 | func installApp(t *testing.T, ctx *context.T, opt ...interface{}) string { |
| 137 | appID, err := appStub().Install(ctx, mockApplicationRepoName, ocfg(opt)) |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 138 | if err != nil { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 139 | t.Fatalf(testutil.FormatLogLine(2, "Install failed: %v", err)) |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 140 | } |
| 141 | return appID |
| 142 | } |
| 143 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 144 | func installAppExpectError(t *testing.T, ctx *context.T, expectedError verror.ID, opt ...interface{}) { |
Todd Wang | 1577651 | 2015-02-10 16:52:55 -0800 | [diff] [blame^] | 145 | if _, err := appStub().Install(ctx, mockApplicationRepoName, ocfg(opt)); err == nil || !verror.Is(err, expectedError) { |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 146 | t.Fatalf(testutil.FormatLogLine(2, "Install expected to fail with %v, got %v instead", expectedError, err)) |
| 147 | } |
| 148 | } |
| 149 | |
Bogdan Caprita | 2692910 | 2014-11-07 11:56:56 -0800 | [diff] [blame] | 150 | type granter struct { |
| 151 | ipc.CallOpt |
| 152 | p security.Principal |
| 153 | extension string |
| 154 | } |
| 155 | |
| 156 | func (g *granter) Grant(other security.Blessings) (security.Blessings, error) { |
| 157 | return g.p.Bless(other.PublicKey(), g.p.BlessingStore().Default(), g.extension, security.UnconstrainedUse()) |
| 158 | } |
| 159 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 160 | func startAppImpl(t *testing.T, ctx *context.T, appID, grant string) (string, error) { |
Bogdan Caprita | 730bde1 | 2014-11-08 15:35:43 -0800 | [diff] [blame] | 161 | var opts []ipc.CallOpt |
| 162 | if grant != "" { |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 163 | opts = append(opts, &granter{p: veyron2.GetPrincipal(ctx), extension: grant}) |
Bogdan Caprita | 730bde1 | 2014-11-08 15:35:43 -0800 | [diff] [blame] | 164 | } |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 165 | if instanceIDs, err := appStub(appID).Start(ctx, opts...); err != nil { |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 166 | return "", err |
| 167 | } else { |
| 168 | if want, got := 1, len(instanceIDs); want != got { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 169 | t.Fatalf(testutil.FormatLogLine(2, "Start(%v): expected %v instance ids, got %v instead", appID, want, got)) |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 170 | } |
| 171 | return instanceIDs[0], nil |
| 172 | } |
| 173 | } |
| 174 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 175 | func startApp(t *testing.T, ctx *context.T, appID string) string { |
| 176 | instanceID, err := startAppImpl(t, ctx, appID, "forapp") |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 177 | if err != nil { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 178 | t.Fatalf(testutil.FormatLogLine(2, "Start(%v) failed: %v", appID, err)) |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 179 | } |
| 180 | return instanceID |
| 181 | } |
| 182 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 183 | func startAppExpectError(t *testing.T, ctx *context.T, appID string, expectedError verror.ID) { |
Todd Wang | 1577651 | 2015-02-10 16:52:55 -0800 | [diff] [blame^] | 184 | if _, err := startAppImpl(t, ctx, appID, "forapp"); err == nil || !verror.Is(err, expectedError) { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 185 | t.Fatalf(testutil.FormatLogLine(2, "Start(%v) expected to fail with %v, got %v instead", appID, expectedError, err)) |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 189 | func stopApp(t *testing.T, ctx *context.T, appID, instanceID string) { |
| 190 | if err := appStub(appID, instanceID).Stop(ctx, stopTimeout); err != nil { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 191 | t.Fatalf(testutil.FormatLogLine(2, "Stop(%v/%v) failed: %v", appID, instanceID, err)) |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 195 | func suspendApp(t *testing.T, ctx *context.T, appID, instanceID string) { |
| 196 | if err := appStub(appID, instanceID).Suspend(ctx); err != nil { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 197 | t.Fatalf(testutil.FormatLogLine(2, "Suspend(%v/%v) failed: %v", appID, instanceID, err)) |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 201 | func resumeApp(t *testing.T, ctx *context.T, appID, instanceID string) { |
| 202 | if err := appStub(appID, instanceID).Resume(ctx); err != nil { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 203 | t.Fatalf(testutil.FormatLogLine(2, "Resume(%v/%v) failed: %v", appID, instanceID, err)) |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 207 | func resumeAppExpectError(t *testing.T, ctx *context.T, appID, instanceID string, expectedError verror.ID) { |
Todd Wang | 1577651 | 2015-02-10 16:52:55 -0800 | [diff] [blame^] | 208 | if err := appStub(appID, instanceID).Resume(ctx); err == nil || !verror.Is(err, expectedError) { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 209 | t.Fatalf(testutil.FormatLogLine(2, "Resume(%v/%v) expected to fail with %v, got %v instead", appID, instanceID, expectedError, err)) |
Robert Kroeger | 1ce0bd7 | 2014-10-22 13:57:14 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 213 | func updateApp(t *testing.T, ctx *context.T, appID string) { |
| 214 | if err := appStub(appID).Update(ctx); err != nil { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 215 | t.Fatalf(testutil.FormatLogLine(2, "Update(%v) failed: %v", appID, err)) |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 219 | func updateAppExpectError(t *testing.T, ctx *context.T, appID string, expectedError verror.ID) { |
Todd Wang | 1577651 | 2015-02-10 16:52:55 -0800 | [diff] [blame^] | 220 | if err := appStub(appID).Update(ctx); err == nil || !verror.Is(err, expectedError) { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 221 | t.Fatalf(testutil.FormatLogLine(2, "Update(%v) expected to fail with %v, got %v instead", appID, expectedError, err)) |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 225 | func revertApp(t *testing.T, ctx *context.T, appID string) { |
| 226 | if err := appStub(appID).Revert(ctx); err != nil { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 227 | t.Fatalf(testutil.FormatLogLine(2, "Revert(%v) failed: %v", appID, err)) |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 231 | func revertAppExpectError(t *testing.T, ctx *context.T, appID string, expectedError verror.ID) { |
Todd Wang | 1577651 | 2015-02-10 16:52:55 -0800 | [diff] [blame^] | 232 | if err := appStub(appID).Revert(ctx); err == nil || !verror.Is(err, expectedError) { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 233 | t.Fatalf(testutil.FormatLogLine(2, "Revert(%v) expected to fail with %v, got %v instead", appID, expectedError, err)) |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 237 | func uninstallApp(t *testing.T, ctx *context.T, appID string) { |
| 238 | if err := appStub(appID).Uninstall(ctx); err != nil { |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 239 | t.Fatalf(testutil.FormatLogLine(2, "Uninstall(%v) failed: %v", appID, err)) |
Bogdan Caprita | 48bbd14 | 2014-09-04 16:07:23 -0700 | [diff] [blame] | 240 | } |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 241 | } |
Robert Kroeger | 1cb4a0d | 2014-10-20 11:55:38 -0700 | [diff] [blame] | 242 | |
Bogdan Caprita | d8373a1 | 2015-01-28 19:52:37 -0800 | [diff] [blame] | 243 | func debug(t *testing.T, ctx *context.T, nameComponents ...string) string { |
| 244 | dbg, err := appStub(nameComponents...).Debug(ctx) |
| 245 | if err != nil { |
| 246 | t.Fatalf(testutil.FormatLogLine(2, "Debug(%v) failed: %v", nameComponents, err)) |
| 247 | } |
| 248 | return dbg |
| 249 | } |
| 250 | |
Robert Kroeger | 1cb4a0d | 2014-10-20 11:55:38 -0700 | [diff] [blame] | 251 | // Code to make Association lists sortable. |
Bogdan Caprita | a456f47 | 2014-12-10 10:18:03 -0800 | [diff] [blame] | 252 | type byIdentity []device.Association |
Robert Kroeger | 1cb4a0d | 2014-10-20 11:55:38 -0700 | [diff] [blame] | 253 | |
| 254 | func (a byIdentity) Len() int { return len(a) } |
| 255 | func (a byIdentity) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 256 | func (a byIdentity) Less(i, j int) bool { return a[i].IdentityName < a[j].IdentityName } |
| 257 | |
Bogdan Caprita | a456f47 | 2014-12-10 10:18:03 -0800 | [diff] [blame] | 258 | func compareAssociations(t *testing.T, got, expected []device.Association) { |
Robert Kroeger | 1cb4a0d | 2014-10-20 11:55:38 -0700 | [diff] [blame] | 259 | sort.Sort(byIdentity(got)) |
| 260 | sort.Sort(byIdentity(expected)) |
| 261 | if !reflect.DeepEqual(got, expected) { |
| 262 | t.Fatalf("ListAssociations() got %v, expected %v", got, expected) |
| 263 | } |
| 264 | } |
Robert Kroeger | 94ec756 | 2014-10-28 17:58:44 -0700 | [diff] [blame] | 265 | |
| 266 | // generateSuidHelperScript builds a script to execute the test target as |
| 267 | // a suidhelper instance and returns the path to the script. |
| 268 | func generateSuidHelperScript(t *testing.T, root string) string { |
| 269 | output := "#!/bin/bash\n" |
| 270 | output += "VEYRON_SUIDHELPER_TEST=1" |
| 271 | output += " " |
Bogdan Caprita | 069341a | 2014-12-09 22:59:17 -0800 | [diff] [blame] | 272 | output += "exec " + os.Args[0] + " -minuid=1 -test.run=TestSuidHelper $*" |
Robert Kroeger | 94ec756 | 2014-10-28 17:58:44 -0700 | [diff] [blame] | 273 | output += "\n" |
| 274 | |
| 275 | vlog.VI(1).Infof("script\n%s", output) |
| 276 | |
| 277 | if err := os.MkdirAll(root, 0755); err != nil { |
| 278 | t.Fatalf("MkdirAll failed: %v", err) |
| 279 | } |
Robert Kroeger | 94ec756 | 2014-10-28 17:58:44 -0700 | [diff] [blame] | 280 | path := filepath.Join(root, "helper.sh") |
| 281 | if err := ioutil.WriteFile(path, []byte(output), 0755); err != nil { |
| 282 | t.Fatalf("WriteFile(%v) failed: %v", path, err) |
| 283 | } |
| 284 | return path |
| 285 | } |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 286 | |
| 287 | // generateAgentScript creates a simple script that acts as the security agent |
| 288 | // for tests. It blackholes arguments meant for the agent. |
| 289 | func generateAgentScript(t *testing.T, root string) string { |
| 290 | output := ` |
| 291 | #!/bin/bash |
| 292 | ARGS=$* |
| 293 | for ARG in ${ARGS[@]}; do |
| 294 | if [[ ${ARG} = -- ]]; then |
| 295 | ARGS=(${ARGS[@]/$ARG}) |
| 296 | break |
| 297 | elif [[ ${ARG} == --* ]]; then |
| 298 | ARGS=(${ARGS[@]/$ARG}) |
| 299 | else |
| 300 | break |
| 301 | fi |
| 302 | done |
| 303 | |
| 304 | exec ${ARGS[@]} |
| 305 | ` |
| 306 | if err := os.MkdirAll(root, 0755); err != nil { |
| 307 | t.Fatalf("MkdirAll failed: %v", err) |
| 308 | } |
| 309 | path := filepath.Join(root, "agenthelper.sh") |
| 310 | if err := ioutil.WriteFile(path, []byte(output), 0755); err != nil { |
| 311 | t.Fatalf("WriteFile(%v) failed: %v", path, err) |
| 312 | } |
| 313 | return path |
| 314 | } |