Bogdan Caprita | 5420f17 | 2014-10-10 15:58:14 -0700 | [diff] [blame] | 1 | // TODO(caprita): This file is becoming unmanageable; split into several test |
| 2 | // files. |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 3 | // TODO(rjkroege): Add a more extensive unit test case to exercise ACL logic. |
Bogdan Caprita | 5420f17 | 2014-10-10 15:58:14 -0700 | [diff] [blame] | 4 | |
Jiri Simsa | 70c3205 | 2014-06-18 11:38:21 -0700 | [diff] [blame] | 5 | package impl_test |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 6 | |
| 7 | import ( |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 8 | "bytes" |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 9 | "crypto/md5" |
| 10 | "encoding/base64" |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 11 | "encoding/hex" |
Robert Kroeger | 627a115 | 2014-10-01 14:57:55 -0700 | [diff] [blame] | 12 | "encoding/json" |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 13 | "flag" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 14 | "fmt" |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 15 | "io" |
Jiri Simsa | 70c3205 | 2014-06-18 11:38:21 -0700 | [diff] [blame] | 16 | "io/ioutil" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 17 | "os" |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 18 | goexec "os/exec" |
Robert Kroeger | 627a115 | 2014-10-01 14:57:55 -0700 | [diff] [blame] | 19 | "os/user" |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 20 | "path" |
Jiri Simsa | 70c3205 | 2014-06-18 11:38:21 -0700 | [diff] [blame] | 21 | "path/filepath" |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 22 | "reflect" |
Robin Thellend | 4c5266e | 2014-10-27 13:19:29 -0700 | [diff] [blame] | 23 | "regexp" |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 24 | "sort" |
Jiri Simsa | 70c3205 | 2014-06-18 11:38:21 -0700 | [diff] [blame] | 25 | "strings" |
Bogdan Caprita | 78b6216 | 2014-08-21 15:35:08 -0700 | [diff] [blame] | 26 | "syscall" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 27 | "testing" |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 28 | "time" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 29 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 30 | "v.io/core/veyron2" |
| 31 | "v.io/core/veyron2/context" |
| 32 | "v.io/core/veyron2/ipc" |
| 33 | "v.io/core/veyron2/naming" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 34 | "v.io/core/veyron2/security" |
| 35 | "v.io/core/veyron2/services/mgmt/application" |
| 36 | "v.io/core/veyron2/services/mgmt/device" |
| 37 | "v.io/core/veyron2/services/mgmt/logreader" |
| 38 | "v.io/core/veyron2/services/mgmt/pprof" |
| 39 | "v.io/core/veyron2/services/mgmt/stats" |
| 40 | "v.io/core/veyron2/services/security/access" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 41 | verror "v.io/core/veyron2/verror2" |
| 42 | "v.io/core/veyron2/vlog" |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 43 | |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 44 | "v.io/core/veyron/lib/expect" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 45 | "v.io/core/veyron/lib/modules" |
| 46 | "v.io/core/veyron/lib/signals" |
| 47 | "v.io/core/veyron/lib/testutil" |
| 48 | tsecurity "v.io/core/veyron/lib/testutil/security" |
| 49 | binaryimpl "v.io/core/veyron/services/mgmt/binary/impl" |
| 50 | "v.io/core/veyron/services/mgmt/device/config" |
| 51 | "v.io/core/veyron/services/mgmt/device/impl" |
| 52 | libbinary "v.io/core/veyron/services/mgmt/lib/binary" |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 53 | mgmttest "v.io/core/veyron/services/mgmt/lib/testutil" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 54 | suidhelper "v.io/core/veyron/services/mgmt/suidhelper/impl" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 55 | ) |
| 56 | |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 57 | const ( |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 58 | // Modules command names. |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 59 | execScriptCmd = "execScriptCmd" |
| 60 | deviceManagerCmd = "deviceManager" |
| 61 | appCmd = "app" |
| 62 | installerCmd = "installer" |
Bogdan Caprita | a40d338 | 2014-12-19 16:30:26 -0800 | [diff] [blame] | 63 | uninstallerCmd = "uninstaller" |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 64 | |
| 65 | testFlagName = "random_test_flag" |
| 66 | // VEYRON prefix is necessary to pass the env filtering. |
| 67 | testEnvVarName = "VEYRON_RANDOM_ENV_VALUE" |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 68 | ) |
Robert Kroeger | dd07b36 | 2014-09-18 17:34:42 -0700 | [diff] [blame] | 69 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 70 | var flagValue = flag.String(testFlagName, "default", "") |
| 71 | |
Robert Kroeger | dd07b36 | 2014-09-18 17:34:42 -0700 | [diff] [blame] | 72 | func init() { |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 73 | // The installer sets this flag on the installed device manager, so we |
| 74 | // need to ensure it's defined. |
| 75 | flag.String("name", "", "") |
| 76 | |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 77 | modules.RegisterChild(execScriptCmd, "", execScript) |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 78 | modules.RegisterChild(deviceManagerCmd, "", deviceManager) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 79 | modules.RegisterChild(appCmd, "", app) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 80 | testutil.Init() |
| 81 | |
| 82 | if modules.IsModulesProcess() { |
Robert Kroeger | dd07b36 | 2014-09-18 17:34:42 -0700 | [diff] [blame] | 83 | return |
| 84 | } |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // TestHelperProcess is the entrypoint for the modules commands in a |
| 88 | // a test subprocess. |
| 89 | func TestHelperProcess(t *testing.T) { |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 90 | modules.DispatchInTest() |
Robert Kroeger | dd07b36 | 2014-09-18 17:34:42 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // TestSuidHelper is testing boilerplate for suidhelper that does not |
Matt Rosencrantz | 5180d16 | 2014-12-03 13:48:40 -0800 | [diff] [blame] | 94 | // create a runtime because the suidhelper is not a Veyron application. |
Robert Kroeger | dd07b36 | 2014-09-18 17:34:42 -0700 | [diff] [blame] | 95 | func TestSuidHelper(t *testing.T) { |
| 96 | if os.Getenv("VEYRON_SUIDHELPER_TEST") != "1" { |
| 97 | return |
| 98 | } |
| 99 | vlog.VI(1).Infof("TestSuidHelper starting") |
| 100 | |
| 101 | if err := suidhelper.Run(os.Environ()); err != nil { |
| 102 | vlog.Fatalf("Failed to Run() setuidhelper: %v", err) |
| 103 | } |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // execScript launches the script passed as argument. |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 107 | func execScript(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
| 108 | args = args[1:] |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 109 | if want, got := 1, len(args); want != got { |
| 110 | vlog.Fatalf("execScript expected %d arguments, got %d instead", want, got) |
| 111 | } |
| 112 | script := args[0] |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 113 | osenv := []string{} |
| 114 | if env["PAUSE_BEFORE_STOP"] == "1" { |
| 115 | osenv = append(osenv, "PAUSE_BEFORE_STOP=1") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 116 | } |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 117 | |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 118 | cmd := goexec.Cmd{ |
| 119 | Path: script, |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 120 | Env: osenv, |
| 121 | Stdin: stdin, |
| 122 | Stderr: stderr, |
| 123 | Stdout: stdout, |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 124 | } |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 125 | |
| 126 | return cmd.Run() |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 129 | // deviceManager sets up a device manager server. It accepts the name to |
| 130 | // publish the server under as an argument. Additional arguments can optionally |
| 131 | // specify device manager config settings. |
| 132 | func deviceManager(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 133 | ctx, shutdown := veyron2.Init() |
| 134 | |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 135 | args = args[1:] |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 136 | if len(args) == 0 { |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 137 | vlog.Fatalf("deviceManager expected at least an argument") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 138 | } |
| 139 | publishName := args[0] |
Bogdan Caprita | 78b6216 | 2014-08-21 15:35:08 -0700 | [diff] [blame] | 140 | args = args[1:] |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 141 | defer fmt.Fprintf(stdout, "%v terminated\n", publishName) |
| 142 | defer vlog.VI(1).Infof("%v terminated", publishName) |
| 143 | defer shutdown() |
| 144 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 145 | |
| 146 | server, endpoint := mgmttest.NewServer(ctx) |
David Why Use Two When One Will Do Presotto | 8b4dbbf | 2014-11-06 10:50:14 -0800 | [diff] [blame] | 147 | name := naming.JoinAddressName(endpoint, "") |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 148 | vlog.VI(1).Infof("Device manager name: %v", name) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 149 | |
| 150 | // Satisfy the contract described in doc.go by passing the config state |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 151 | // through to the device manager dispatcher constructor. |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 152 | configState, err := config.Load() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 153 | if err != nil { |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 154 | vlog.Fatalf("Failed to decode config state: %v", err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 155 | } |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 156 | configState.Name = name |
| 157 | |
| 158 | // This exemplifies how to override or set specific config fields, if, |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 159 | // for example, the device manager is invoked 'by hand' instead of via a |
| 160 | // script prepared by a previous version of the device manager. |
Bogdan Caprita | 78b6216 | 2014-08-21 15:35:08 -0700 | [diff] [blame] | 161 | if len(args) > 0 { |
Bogdan Caprita | 962d5e0 | 2014-10-28 18:36:09 -0700 | [diff] [blame] | 162 | if want, got := 4, len(args); want != got { |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 163 | vlog.Fatalf("expected %d additional arguments, got %d instead", want, got) |
| 164 | } |
Bogdan Caprita | 962d5e0 | 2014-10-28 18:36:09 -0700 | [diff] [blame] | 165 | configState.Root, configState.Helper, configState.Origin, configState.CurrentLink = args[0], args[1], args[2], args[3] |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 166 | } |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 167 | dispatcher, err := impl.NewDispatcher(veyron2.GetPrincipal(ctx), configState, func() { fmt.Println("restart handler") }) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 168 | if err != nil { |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 169 | vlog.Fatalf("Failed to create device manager dispatcher: %v", err) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 170 | } |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 171 | if err := server.ServeDispatcher(publishName, dispatcher); err != nil { |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 172 | vlog.Fatalf("Serve(%v) failed: %v", publishName, err) |
| 173 | } |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 174 | impl.InvokeCallback(ctx, name) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 175 | |
| 176 | fmt.Fprintf(stdout, "ready:%d\n", os.Getpid()) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 177 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 178 | <-signals.ShutdownOnSignals(ctx) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 179 | |
| 180 | if val, present := env["PAUSE_BEFORE_STOP"]; present && val == "1" { |
| 181 | modules.WaitForEOF(stdin) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 182 | } |
Bogdan Caprita | 78b6216 | 2014-08-21 15:35:08 -0700 | [diff] [blame] | 183 | if dispatcher.Leaking() { |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 184 | vlog.Fatalf("device manager leaking resources") |
Bogdan Caprita | 78b6216 | 2014-08-21 15:35:08 -0700 | [diff] [blame] | 185 | } |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 186 | return nil |
| 187 | } |
| 188 | |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 189 | // appService defines a test service that the test app should be running. |
| 190 | // TODO(caprita): Use this to make calls to the app and verify how Suspend/Stop |
| 191 | // interact with an active service. |
| 192 | type appService struct{} |
| 193 | |
Todd Wang | 1fe7cdd | 2014-11-12 12:51:49 -0800 | [diff] [blame] | 194 | func (appService) Echo(_ ipc.ServerContext, message string) (string, error) { |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 195 | return message, nil |
| 196 | } |
| 197 | |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 198 | func (appService) Cat(_ ipc.ServerContext, file string) (string, error) { |
| 199 | if file == "" || file[0] == filepath.Separator || file[0] == '.' { |
| 200 | return "", fmt.Errorf("illegal file name: %q", file) |
| 201 | } |
| 202 | bytes, err := ioutil.ReadFile(file) |
| 203 | if err != nil { |
| 204 | return "", err |
| 205 | } |
| 206 | return string(bytes), nil |
| 207 | } |
| 208 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 209 | type pingArgs struct { |
Bogdan Caprita | e1960f5 | 2015-01-14 09:57:19 -0800 | [diff] [blame] | 210 | Username, FlagValue, EnvValue string |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 213 | func ping(ctx *context.T) { |
Bogdan Caprita | e1960f5 | 2015-01-14 09:57:19 -0800 | [diff] [blame] | 214 | helperEnv := os.Getenv(suidhelper.SavedArgs) |
| 215 | d := json.NewDecoder(strings.NewReader(helperEnv)) |
| 216 | var savedArgs suidhelper.ArgsSavedForTest |
| 217 | if err := d.Decode(&savedArgs); err != nil { |
| 218 | vlog.Fatalf("Failed to decode preserved argument %v: %v", helperEnv, err) |
| 219 | } |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 220 | args := &pingArgs{ |
Bogdan Caprita | e1960f5 | 2015-01-14 09:57:19 -0800 | [diff] [blame] | 221 | // TODO(rjkroege): Consider validating additional parameters |
| 222 | // from helper. |
| 223 | Username: savedArgs.Uname, |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 224 | FlagValue: *flagValue, |
| 225 | EnvValue: os.Getenv(testEnvVarName), |
| 226 | } |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 227 | client := veyron2.GetClient(ctx) |
| 228 | if call, err := client.StartCall(ctx, "pingserver", "Ping", []interface{}{args}); err != nil { |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 229 | vlog.Fatalf("StartCall failed: %v", err) |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 230 | } else if err := call.Finish(); err != nil { |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 231 | vlog.Fatalf("Finish failed: %v", err) |
| 232 | } |
| 233 | } |
| 234 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 235 | func cat(ctx *context.T, name, file string) (string, error) { |
| 236 | ctx, cancel := context.WithTimeout(ctx, time.Minute) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 237 | defer cancel() |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 238 | client := veyron2.GetClient(ctx) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 239 | call, err := client.StartCall(ctx, name, "Cat", []interface{}{file}) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 240 | if err != nil { |
| 241 | return "", err |
| 242 | } |
| 243 | var content string |
| 244 | if ferr := call.Finish(&content, &err); ferr != nil { |
| 245 | err = ferr |
| 246 | } |
| 247 | return content, err |
| 248 | } |
| 249 | |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 250 | func app(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 251 | ctx, shutdown := veyron2.Init() |
| 252 | defer shutdown() |
| 253 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 254 | |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 255 | args = args[1:] |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 256 | if expected, got := 1, len(args); expected != got { |
| 257 | vlog.Fatalf("Unexpected number of arguments: expected %d, got %d", expected, got) |
| 258 | } |
| 259 | publishName := args[0] |
| 260 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 261 | server, _ := mgmttest.NewServer(ctx) |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 262 | defer server.Stop() |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 263 | if err := server.Serve(publishName, new(appService), nil); err != nil { |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 264 | vlog.Fatalf("Serve(%v) failed: %v", publishName, err) |
| 265 | } |
Cosmos Nicolaou | e339153 | 2014-11-25 19:08:32 -0800 | [diff] [blame] | 266 | // Some of our tests look for log files, so make sure they are flushed |
| 267 | // to ensure that at least the files exist. |
| 268 | vlog.FlushLog() |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 269 | ping(ctx) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 270 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 271 | <-signals.ShutdownOnSignals(ctx) |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 272 | if err := ioutil.WriteFile("testfile", []byte("goodbye world"), 0600); err != nil { |
| 273 | vlog.Fatalf("Failed to write testfile: %v", err) |
| 274 | } |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 275 | return nil |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 278 | // TODO(rjkroege): generateDeviceManagerScript and generateSuidHelperScript have |
| 279 | // code similarity that might benefit from refactoring. |
| 280 | // generateDeviceManagerScript is very similar in behavior to generateScript in |
Bogdan Caprita | a456f47 | 2014-12-10 10:18:03 -0800 | [diff] [blame] | 281 | // device_invoker.go. However, we chose to re-implement it here for two |
| 282 | // reasons: (1) avoid making generateScript public; and (2) how the test choses |
| 283 | // to invoke the device manager subprocess the first time should be independent |
| 284 | // of how device manager implementation sets up its updated versions. |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 285 | func generateDeviceManagerScript(t *testing.T, root string, args, env []string) string { |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 286 | env = impl.VeyronEnvironment(env) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 287 | output := "#!/bin/bash\n" |
Bogdan Caprita | 069341a | 2014-12-09 22:59:17 -0800 | [diff] [blame] | 288 | output += strings.Join(config.QuoteEnv(env), " ") + " exec " |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 289 | output += strings.Join(args, " ") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 290 | if err := os.MkdirAll(filepath.Join(root, "factory"), 0755); err != nil { |
| 291 | t.Fatalf("MkdirAll failed: %v", err) |
| 292 | } |
| 293 | // Why pigeons? To show that the name we choose for the initial script |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 294 | // doesn't matter and in particular is independent of how device manager |
| 295 | // names its updated version scripts (deviced.sh). |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 296 | path := filepath.Join(root, "factory", "pigeons.sh") |
| 297 | if err := ioutil.WriteFile(path, []byte(output), 0755); err != nil { |
| 298 | t.Fatalf("WriteFile(%v) failed: %v", path, err) |
| 299 | } |
| 300 | return path |
| 301 | } |
| 302 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 303 | // TestDeviceManagerUpdateAndRevert makes the device manager go through the |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 304 | // motions of updating itself to newer versions (twice), and reverting itself |
| 305 | // back (twice). It also checks that update and revert fail when they're |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 306 | // supposed to. The initial device manager is started 'by hand' via a module |
| 307 | // command. Further versions are started through the soft link that the device |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 308 | // manager itself updates. |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 309 | func TestDeviceManagerUpdateAndRevert(t *testing.T) { |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 310 | ctx, shutdown := veyron2.Init() |
| 311 | defer shutdown() |
| 312 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 313 | |
| 314 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, veyron2.GetPrincipal(ctx)) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 315 | defer deferFn() |
| 316 | |
| 317 | // Set up mock application and binary repositories. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 318 | envelope, cleanup := startMockRepos(t, ctx) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 319 | defer cleanup() |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 320 | |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 321 | root, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Bogdan Caprita | 080a730 | 2014-08-20 15:35:37 -0700 | [diff] [blame] | 322 | defer cleanup() |
Jiri Simsa | e0fc61a | 2014-07-30 14:41:55 -0700 | [diff] [blame] | 323 | |
Bogdan Caprita | d2b9f03 | 2014-10-10 17:43:29 -0700 | [diff] [blame] | 324 | // Current link does not have to live in the root dir, but it's |
| 325 | // convenient to put it there so we have everything in one place. |
| 326 | currLink := filepath.Join(root, "current_link") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 327 | |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 328 | dmArgs := []string{"factoryDM", root, "unused_helper", mockApplicationRepoName, currLink} |
Ryan Brown | a08a221 | 2015-01-15 15:40:10 -0800 | [diff] [blame] | 329 | args, env := sh.CommandEnvelope(deviceManagerCmd, nil, dmArgs...) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 330 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 331 | scriptPathFactory := generateDeviceManagerScript(t, root, args, env) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 332 | |
| 333 | if err := os.Symlink(scriptPathFactory, currLink); err != nil { |
| 334 | t.Fatalf("Symlink(%q, %q) failed: %v", scriptPathFactory, currLink, err) |
| 335 | } |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 336 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 337 | // We instruct the initial device manager that we run to pause before |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 338 | // stopping its service, so that we get a chance to verify that |
| 339 | // attempting an update while another one is ongoing will fail. |
Ryan Brown | a08a221 | 2015-01-15 15:40:10 -0800 | [diff] [blame] | 340 | dmPauseBeforeStopEnv := []string{"PAUSE_BEFORE_STOP=1"} |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 341 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 342 | // Start the initial version of the device manager, the so-called |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 343 | // "factory" version. We use the modules-generated command to start it. |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 344 | // We could have also used the scriptPathFactory to start it, but this |
| 345 | // demonstrates that the initial device manager could be started by hand |
| 346 | // as long as the right initial configuration is passed into the device |
| 347 | // manager implementation. |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 348 | dmh, dms := mgmttest.RunShellCommand(t, sh, dmPauseBeforeStopEnv, deviceManagerCmd, dmArgs...) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 349 | defer func() { |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 350 | syscall.Kill(dmh.Pid(), syscall.SIGINT) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 351 | }() |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 352 | |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 353 | mgmttest.ReadPID(t, dms) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 354 | resolve(t, ctx, "factoryDM", 1) // Verify the device manager has published itself. |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 355 | |
| 356 | // Simulate an invalid envelope in the application repository. |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 357 | *envelope = envelopeFromShell(sh, dmPauseBeforeStopEnv, deviceManagerCmd, "bogus", dmArgs...) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 358 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 359 | updateDeviceExpectError(t, ctx, "factoryDM", impl.ErrAppTitleMismatch.ID) |
| 360 | revertDeviceExpectError(t, ctx, "factoryDM", impl.ErrUpdateNoOp.ID) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 361 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 362 | // Set up a second version of the device manager. The information in the |
| 363 | // envelope will be used by the device manager to stage the next |
| 364 | // version. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 365 | crDir, crEnv := mgmttest.CredentialsForChild(ctx, "child") |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 366 | defer os.RemoveAll(crDir) |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 367 | *envelope = envelopeFromShell(sh, crEnv, deviceManagerCmd, application.DeviceManagerTitle, "v2DM") |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 368 | updateDevice(t, ctx, "factoryDM") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 369 | |
| 370 | // Current link should have been updated to point to v2. |
| 371 | evalLink := func() string { |
| 372 | path, err := filepath.EvalSymlinks(currLink) |
| 373 | if err != nil { |
| 374 | t.Fatalf("EvalSymlinks(%v) failed: %v", currLink, err) |
| 375 | } |
| 376 | return path |
| 377 | } |
| 378 | scriptPathV2 := evalLink() |
| 379 | if scriptPathFactory == scriptPathV2 { |
Jiri Simsa | 6351ee7 | 2014-08-18 16:44:41 -0700 | [diff] [blame] | 380 | t.Fatalf("current link didn't change") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 383 | updateDeviceExpectError(t, ctx, "factoryDM", impl.ErrOperationInProgress.ID) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 384 | |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 385 | dmh.CloseStdin() |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 386 | |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 387 | dms.Expect("restart handler") |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 388 | dms.Expect("factoryDM terminated") |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 389 | dmh.Shutdown(os.Stderr, os.Stderr) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 390 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 391 | // A successful update means the device manager has stopped itself. We |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 392 | // relaunch it from the current link. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 393 | resolveExpectNotFound(t, ctx, "v2DM") // Ensure a clean slate. |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 394 | |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 395 | dmh, dms = mgmttest.RunShellCommand(t, sh, nil, execScriptCmd, currLink) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 396 | |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 397 | mgmttest.ReadPID(t, dms) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 398 | resolve(t, ctx, "v2DM", 1) // Current link should have been launching v2. |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 399 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 400 | // Try issuing an update without changing the envelope in the |
| 401 | // application repository: this should fail, and current link should be |
| 402 | // unchanged. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 403 | updateDeviceExpectError(t, ctx, "v2DM", impl.ErrUpdateNoOp.ID) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 404 | if evalLink() != scriptPathV2 { |
Jiri Simsa | 6351ee7 | 2014-08-18 16:44:41 -0700 | [diff] [blame] | 405 | t.Fatalf("script changed") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 408 | // Create a third version of the device manager and issue an update. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 409 | crDir, crEnv = mgmttest.CredentialsForChild(ctx, "child") |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 410 | defer os.RemoveAll(crDir) |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 411 | *envelope = envelopeFromShell(sh, crEnv, deviceManagerCmd, application.DeviceManagerTitle, "v3DM") |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 412 | updateDevice(t, ctx, "v2DM") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 413 | |
| 414 | scriptPathV3 := evalLink() |
| 415 | if scriptPathV3 == scriptPathV2 { |
Jiri Simsa | 6351ee7 | 2014-08-18 16:44:41 -0700 | [diff] [blame] | 416 | t.Fatalf("current link didn't change") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 419 | dms.Expect("restart handler") |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 420 | dms.Expect("v2DM terminated") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 421 | |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 422 | dmh.Shutdown(os.Stderr, os.Stderr) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 423 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 424 | resolveExpectNotFound(t, ctx, "v3DM") // Ensure a clean slate. |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 425 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 426 | // Re-lanuch the device manager from current link. We instruct the |
| 427 | // device manager to pause before stopping its server, so that we can |
| 428 | // verify that a second revert fails while a revert is in progress. |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 429 | dmh, dms = mgmttest.RunShellCommand(t, sh, dmPauseBeforeStopEnv, execScriptCmd, currLink) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 430 | |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 431 | mgmttest.ReadPID(t, dms) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 432 | resolve(t, ctx, "v3DM", 1) // Current link should have been launching v3. |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 433 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 434 | // Revert the device manager to its previous version (v2). |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 435 | revertDevice(t, ctx, "v3DM") |
| 436 | revertDeviceExpectError(t, ctx, "v3DM", impl.ErrOperationInProgress.ID) // Revert already in progress. |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 437 | dmh.CloseStdin() |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 438 | dms.Expect("restart handler") |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 439 | dms.Expect("v3DM terminated") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 440 | if evalLink() != scriptPathV2 { |
Jiri Simsa | 6351ee7 | 2014-08-18 16:44:41 -0700 | [diff] [blame] | 441 | t.Fatalf("current link was not reverted correctly") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 442 | } |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 443 | dmh.Shutdown(os.Stderr, os.Stderr) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 444 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 445 | resolveExpectNotFound(t, ctx, "v2DM") // Ensure a clean slate. |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 446 | |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 447 | dmh, dms = mgmttest.RunShellCommand(t, sh, nil, execScriptCmd, currLink) |
| 448 | mgmttest.ReadPID(t, dms) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 449 | resolve(t, ctx, "v2DM", 1) // Current link should have been launching v2. |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 450 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 451 | // Revert the device manager to its previous version (factory). |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 452 | revertDevice(t, ctx, "v2DM") |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 453 | dms.Expect("restart handler") |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 454 | dms.Expect("v2DM terminated") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 455 | if evalLink() != scriptPathFactory { |
Jiri Simsa | 6351ee7 | 2014-08-18 16:44:41 -0700 | [diff] [blame] | 456 | t.Fatalf("current link was not reverted correctly") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 457 | } |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 458 | dmh.Shutdown(os.Stderr, os.Stderr) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 459 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 460 | resolveExpectNotFound(t, ctx, "factoryDM") // Ensure a clean slate. |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 461 | |
| 462 | dmh, dms = mgmttest.RunShellCommand(t, sh, nil, execScriptCmd, currLink) |
| 463 | mgmttest.ReadPID(t, dms) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 464 | resolve(t, ctx, "factoryDM", 1) // Current link should have been launching factory version. |
| 465 | stopDevice(t, ctx, "factoryDM") |
| 466 | dms.Expect("factoryDM terminated") |
Bogdan Caprita | 4ea9b03 | 2014-12-27 14:56:51 -0800 | [diff] [blame] | 467 | dms.ExpectEOF() |
| 468 | |
| 469 | // Re-launch the device manager, to exercise the behavior of Suspend. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 470 | resolveExpectNotFound(t, ctx, "factoryDM") // Ensure a clean slate. |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 471 | dmh, dms = mgmttest.RunShellCommand(t, sh, nil, execScriptCmd, currLink) |
| 472 | mgmttest.ReadPID(t, dms) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 473 | resolve(t, ctx, "factoryDM", 1) |
| 474 | suspendDevice(t, ctx, "factoryDM") |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 475 | dms.Expect("restart handler") |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 476 | dms.Expect("factoryDM terminated") |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 477 | dms.ExpectEOF() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 478 | } |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 479 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 480 | type pingServer chan<- pingArgs |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 481 | |
Bogdan Caprita | 916e99f | 2014-11-24 15:47:19 -0800 | [diff] [blame] | 482 | // TODO(caprita): Set the timeout in a more principled manner. |
Bogdan Caprita | a5dd989 | 2015-01-02 11:16:25 -0800 | [diff] [blame] | 483 | const pingTimeout = 60 * time.Second |
Bogdan Caprita | 916e99f | 2014-11-24 15:47:19 -0800 | [diff] [blame] | 484 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 485 | func (p pingServer) Ping(_ ipc.ServerContext, arg pingArgs) { |
Robert Kroeger | 627a115 | 2014-10-01 14:57:55 -0700 | [diff] [blame] | 486 | p <- arg |
| 487 | } |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 488 | |
Bogdan Caprita | d2b9f03 | 2014-10-10 17:43:29 -0700 | [diff] [blame] | 489 | // setupPingServer creates a server listening for a ping from a child app; it |
| 490 | // returns a channel on which the app's ping message is returned, and a cleanup |
| 491 | // function. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 492 | func setupPingServer(t *testing.T, ctx *context.T) (<-chan pingArgs, func()) { |
| 493 | server, _ := mgmttest.NewServer(ctx) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 494 | pingCh := make(chan pingArgs, 1) |
Robin Thellend | 888f8cf | 2014-12-15 16:19:10 -0800 | [diff] [blame] | 495 | if err := server.Serve("pingserver", pingServer(pingCh), &openAuthorizer{}); err != nil { |
Bogdan Caprita | d2b9f03 | 2014-10-10 17:43:29 -0700 | [diff] [blame] | 496 | t.Fatalf("Serve(%q, <dispatcher>) failed: %v", "pingserver", err) |
| 497 | } |
| 498 | return pingCh, func() { |
| 499 | if err := server.Stop(); err != nil { |
| 500 | t.Fatalf("Stop() failed: %v", err) |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 505 | func verifyAppWorkspace(t *testing.T, root, appID, instanceID string) { |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 506 | // HACK ALERT: for now, we peek inside the device manager's directory |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 507 | // structure (which ought to be opaque) to check for what the app has |
| 508 | // written to its local root. |
| 509 | // |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 510 | // TODO(caprita): add support to device manager to browse logs/app local |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 511 | // root. |
| 512 | applicationDirName := func(title string) string { |
| 513 | h := md5.New() |
| 514 | h.Write([]byte(title)) |
| 515 | hash := strings.TrimRight(base64.URLEncoding.EncodeToString(h.Sum(nil)), "=") |
| 516 | return "app-" + hash |
| 517 | } |
| 518 | components := strings.Split(appID, "/") |
| 519 | appTitle, installationID := components[0], components[1] |
Bogdan Caprita | 268b419 | 2014-08-28 10:04:44 -0700 | [diff] [blame] | 520 | instanceDir := filepath.Join(root, applicationDirName(appTitle), "installation-"+installationID, "instances", "instance-"+instanceID) |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 521 | rootDir := filepath.Join(instanceDir, "root") |
| 522 | testFile := filepath.Join(rootDir, "testfile") |
| 523 | if read, err := ioutil.ReadFile(testFile); err != nil { |
Jiri Simsa | 6351ee7 | 2014-08-18 16:44:41 -0700 | [diff] [blame] | 524 | t.Fatalf("Failed to read %v: %v", testFile, err) |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 525 | } else if want, got := "goodbye world", string(read); want != got { |
Jiri Simsa | 6351ee7 | 2014-08-18 16:44:41 -0700 | [diff] [blame] | 526 | t.Fatalf("Expected to read %v, got %v instead", want, got) |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 527 | } |
| 528 | // END HACK |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 529 | } |
Bogdan Caprita | 78b6216 | 2014-08-21 15:35:08 -0700 | [diff] [blame] | 530 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 531 | func verifyPingArgs(t *testing.T, pingCh <-chan pingArgs, username, flagValue, envValue string) { |
| 532 | var args pingArgs |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 533 | select { |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 534 | case args = <-pingCh: |
Bogdan Caprita | 916e99f | 2014-11-24 15:47:19 -0800 | [diff] [blame] | 535 | case <-time.After(pingTimeout): |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 536 | t.Fatalf(testutil.FormatLogLine(2, "failed to get ping")) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 537 | } |
Bogdan Caprita | e1960f5 | 2015-01-14 09:57:19 -0800 | [diff] [blame] | 538 | wantArgs := pingArgs{ |
| 539 | Username: username, |
| 540 | FlagValue: flagValue, |
| 541 | EnvValue: envValue, |
Robert Kroeger | 627a115 | 2014-10-01 14:57:55 -0700 | [diff] [blame] | 542 | } |
Bogdan Caprita | e1960f5 | 2015-01-14 09:57:19 -0800 | [diff] [blame] | 543 | if !reflect.DeepEqual(args, wantArgs) { |
| 544 | t.Fatalf(testutil.FormatLogLine(2, "got ping args %q, expected %q", args, wantArgs)) |
Robert Kroeger | 627a115 | 2014-10-01 14:57:55 -0700 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 548 | // TestAppLifeCycle installs an app, starts it, suspends it, resumes it, and |
| 549 | // then stops it. |
| 550 | func TestAppLifeCycle(t *testing.T) { |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 551 | ctx, shutdown := veyron2.Init() |
| 552 | defer shutdown() |
| 553 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 554 | |
| 555 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, nil) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 556 | defer deferFn() |
| 557 | |
| 558 | // Set up mock application and binary repositories. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 559 | envelope, cleanup := startMockRepos(t, ctx) |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 560 | defer cleanup() |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 561 | |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 562 | root, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 563 | defer cleanup() |
| 564 | |
Robert Kroeger | dd07b36 | 2014-09-18 17:34:42 -0700 | [diff] [blame] | 565 | // Create a script wrapping the test target that implements suidhelper. |
Bogdan Caprita | 962d5e0 | 2014-10-28 18:36:09 -0700 | [diff] [blame] | 566 | helperPath := generateSuidHelperScript(t, root) |
Robert Kroeger | dd07b36 | 2014-09-18 17:34:42 -0700 | [diff] [blame] | 567 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 568 | crDir, crEnv := mgmttest.CredentialsForChild(ctx, "devicemanager") |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 569 | defer os.RemoveAll(crDir) |
| 570 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 571 | // Set up the device manager. Since we won't do device manager updates, |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 572 | // don't worry about its application envelope and current link. |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 573 | dmh, dms := mgmttest.RunShellCommand(t, sh, crEnv, deviceManagerCmd, "dm", root, helperPath, "unused_app_repo_name", "unused_curr_link") |
| 574 | mgmttest.ReadPID(t, dms) |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 575 | |
| 576 | // Create the local server that the app uses to let us know it's ready. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 577 | pingCh, cleanup := setupPingServer(t, ctx) |
Bogdan Caprita | d2b9f03 | 2014-10-10 17:43:29 -0700 | [diff] [blame] | 578 | defer cleanup() |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 579 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 580 | resolve(t, ctx, "pingserver", 1) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 581 | |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 582 | // Create an envelope for a first version of the app. |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 583 | *envelope = envelopeFromShell(sh, []string{testEnvVarName + "=env-val-envelope"}, appCmd, "google naps", fmt.Sprintf("--%s=flag-val-envelope", testFlagName), "appV1") |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 584 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 585 | // Install the app. The config-specified flag value for testFlagName |
| 586 | // should override the value specified in the envelope above. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 587 | appID := installApp(t, ctx, device.Config{testFlagName: "flag-val-install"}) |
Bogdan Caprita | 730bde1 | 2014-11-08 15:35:43 -0800 | [diff] [blame] | 588 | |
| 589 | // Start requires the caller to grant a blessing for the app instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 590 | if _, err := startAppImpl(t, ctx, appID, ""); err == nil || !verror.Is(err, impl.ErrInvalidBlessing.ID) { |
Todd Wang | 34ed4c6 | 2014-11-26 15:15:52 -0800 | [diff] [blame] | 591 | t.Fatalf("Start(%v) expected to fail with %v, got %v instead", appID, impl.ErrInvalidBlessing.ID, err) |
Bogdan Caprita | 730bde1 | 2014-11-08 15:35:43 -0800 | [diff] [blame] | 592 | } |
| 593 | |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 594 | // Start an instance of the app. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 595 | instance1ID := startApp(t, ctx, appID) |
Robert Kroeger | 627a115 | 2014-10-01 14:57:55 -0700 | [diff] [blame] | 596 | |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 597 | // Wait until the app pings us that it's ready. |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 598 | verifyPingArgs(t, pingCh, userName(t), "flag-val-install", "env-val-envelope") |
Robert Kroeger | 627a115 | 2014-10-01 14:57:55 -0700 | [diff] [blame] | 599 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 600 | v1EP1 := resolve(t, ctx, "appV1", 1)[0] |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 601 | |
| 602 | // Suspend the app instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 603 | suspendApp(t, ctx, appID, instance1ID) |
| 604 | resolveExpectNotFound(t, ctx, "appV1") |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 605 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 606 | resumeApp(t, ctx, appID, instance1ID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 607 | verifyPingArgs(t, pingCh, userName(t), "flag-val-install", "env-val-envelope") // Wait until the app pings us that it's ready. |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 608 | oldV1EP1 := v1EP1 |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 609 | if v1EP1 = resolve(t, ctx, "appV1", 1)[0]; v1EP1 == oldV1EP1 { |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 610 | t.Fatalf("Expected a new endpoint for the app after suspend/resume") |
| 611 | } |
| 612 | |
| 613 | // Start a second instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 614 | instance2ID := startApp(t, ctx, appID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 615 | verifyPingArgs(t, pingCh, userName(t), "flag-val-install", "env-val-envelope") // Wait until the app pings us that it's ready. |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 616 | |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 617 | // There should be two endpoints mounted as "appV1", one for each |
| 618 | // instance of the app. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 619 | endpoints := resolve(t, ctx, "appV1", 2) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 620 | v1EP2 := endpoints[0] |
| 621 | if endpoints[0] == v1EP1 { |
| 622 | v1EP2 = endpoints[1] |
| 623 | if v1EP2 == v1EP1 { |
| 624 | t.Fatalf("Both endpoints are the same") |
| 625 | } |
| 626 | } else if endpoints[1] != v1EP1 { |
| 627 | t.Fatalf("Second endpoint should have been v1EP1: %v, %v", endpoints, v1EP1) |
| 628 | } |
Bogdan Caprita | 268b419 | 2014-08-28 10:04:44 -0700 | [diff] [blame] | 629 | |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 630 | // TODO(caprita): test Suspend and Resume, and verify various |
| 631 | // non-standard combinations (suspend when stopped; resume while still |
| 632 | // running; stop while suspended). |
| 633 | |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 634 | // Suspend the first instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 635 | suspendApp(t, ctx, appID, instance1ID) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 636 | // Only the second instance should still be running and mounted. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 637 | if want, got := v1EP2, resolve(t, ctx, "appV1", 1)[0]; want != got { |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 638 | t.Fatalf("Resolve(%v): want: %v, got %v", "appV1", want, got) |
| 639 | } |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 640 | |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 641 | // Updating the installation to itself is a no-op. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 642 | updateAppExpectError(t, ctx, appID, impl.ErrUpdateNoOp.ID) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 643 | |
| 644 | // Updating the installation should not work with a mismatched title. |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 645 | *envelope = envelopeFromShell(sh, nil, appCmd, "bogus") |
| 646 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 647 | updateAppExpectError(t, ctx, appID, impl.ErrAppTitleMismatch.ID) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 648 | |
| 649 | // Create a second version of the app and update the app to it. |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 650 | *envelope = envelopeFromShell(sh, []string{testEnvVarName + "=env-val-envelope"}, appCmd, "google naps", "appV2") |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 651 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 652 | updateApp(t, ctx, appID) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 653 | |
| 654 | // Second instance should still be running. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 655 | if want, got := v1EP2, resolve(t, ctx, "appV1", 1)[0]; want != got { |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 656 | t.Fatalf("Resolve(%v): want: %v, got %v", "appV1", want, got) |
| 657 | } |
| 658 | |
| 659 | // Resume first instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 660 | resumeApp(t, ctx, appID, instance1ID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 661 | verifyPingArgs(t, pingCh, userName(t), "flag-val-install", "env-val-envelope") // Wait until the app pings us that it's ready. |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 662 | // Both instances should still be running the first version of the app. |
| 663 | // Check that the mounttable contains two endpoints, one of which is |
| 664 | // v1EP2. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 665 | endpoints = resolve(t, ctx, "appV1", 2) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 666 | if endpoints[0] == v1EP2 { |
| 667 | if endpoints[1] == v1EP2 { |
| 668 | t.Fatalf("Both endpoints are the same") |
| 669 | } |
| 670 | } else if endpoints[1] != v1EP2 { |
| 671 | t.Fatalf("Second endpoint should have been v1EP2: %v, %v", endpoints, v1EP2) |
| 672 | } |
| 673 | |
| 674 | // Stop first instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 675 | stopApp(t, ctx, appID, instance1ID) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 676 | verifyAppWorkspace(t, root, appID, instance1ID) |
| 677 | |
| 678 | // Only second instance is still running. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 679 | if want, got := v1EP2, resolve(t, ctx, "appV1", 1)[0]; want != got { |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 680 | t.Fatalf("Resolve(%v): want: %v, got %v", "appV1", want, got) |
| 681 | } |
| 682 | |
| 683 | // Start a third instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 684 | instance3ID := startApp(t, ctx, appID) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 685 | // Wait until the app pings us that it's ready. |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 686 | verifyPingArgs(t, pingCh, userName(t), "flag-val-install", "env-val-envelope") |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 687 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 688 | resolve(t, ctx, "appV2", 1) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 689 | |
| 690 | // Stop second instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 691 | stopApp(t, ctx, appID, instance2ID) |
| 692 | resolveExpectNotFound(t, ctx, "appV1") |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 693 | |
| 694 | // Stop third instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 695 | stopApp(t, ctx, appID, instance3ID) |
| 696 | resolveExpectNotFound(t, ctx, "appV2") |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 697 | |
Bogdan Caprita | 53b7b7e | 2014-09-03 20:51:16 -0700 | [diff] [blame] | 698 | // Revert the app. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 699 | revertApp(t, ctx, appID) |
Bogdan Caprita | 53b7b7e | 2014-09-03 20:51:16 -0700 | [diff] [blame] | 700 | |
| 701 | // Start a fourth instance. It should be started from version 1. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 702 | instance4ID := startApp(t, ctx, appID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 703 | verifyPingArgs(t, pingCh, userName(t), "flag-val-install", "env-val-envelope") // Wait until the app pings us that it's ready. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 704 | resolve(t, ctx, "appV1", 1) |
| 705 | stopApp(t, ctx, appID, instance4ID) |
| 706 | resolveExpectNotFound(t, ctx, "appV1") |
Bogdan Caprita | 53b7b7e | 2014-09-03 20:51:16 -0700 | [diff] [blame] | 707 | |
| 708 | // We are already on the first version, no further revert possible. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 709 | revertAppExpectError(t, ctx, appID, impl.ErrUpdateNoOp.ID) |
Bogdan Caprita | 53b7b7e | 2014-09-03 20:51:16 -0700 | [diff] [blame] | 710 | |
Bogdan Caprita | 8c776b2 | 2014-08-28 17:29:07 -0700 | [diff] [blame] | 711 | // Uninstall the app. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 712 | uninstallApp(t, ctx, appID) |
Bogdan Caprita | 8c776b2 | 2014-08-28 17:29:07 -0700 | [diff] [blame] | 713 | |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 714 | // Updating the installation should no longer be allowed. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 715 | updateAppExpectError(t, ctx, appID, impl.ErrInvalidOperation.ID) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 716 | |
Bogdan Caprita | 53b7b7e | 2014-09-03 20:51:16 -0700 | [diff] [blame] | 717 | // Reverting the installation should no longer be allowed. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 718 | revertAppExpectError(t, ctx, appID, impl.ErrInvalidOperation.ID) |
Bogdan Caprita | 53b7b7e | 2014-09-03 20:51:16 -0700 | [diff] [blame] | 719 | |
Bogdan Caprita | 8c776b2 | 2014-08-28 17:29:07 -0700 | [diff] [blame] | 720 | // Starting new instances should no longer be allowed. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 721 | startAppExpectError(t, ctx, appID, impl.ErrInvalidOperation.ID) |
Bogdan Caprita | 8c776b2 | 2014-08-28 17:29:07 -0700 | [diff] [blame] | 722 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 723 | // Cleanly shut down the device manager. |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 724 | syscall.Kill(dmh.Pid(), syscall.SIGINT) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 725 | dms.Expect("dm terminated") |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 726 | dms.ExpectEOF() |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 727 | } |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 728 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 729 | func startRealBinaryRepository(t *testing.T, ctx *context.T) func() { |
Jiri Simsa | 432cc2e | 2014-12-08 15:53:38 -0800 | [diff] [blame] | 730 | rootDir, err := binaryimpl.SetupRootDir("") |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 731 | if err != nil { |
Jiri Simsa | 432cc2e | 2014-12-08 15:53:38 -0800 | [diff] [blame] | 732 | t.Fatalf("binaryimpl.SetupRootDir failed: %v", err) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 733 | } |
Jiri Simsa | 432cc2e | 2014-12-08 15:53:38 -0800 | [diff] [blame] | 734 | state, err := binaryimpl.NewState(rootDir, "", 3) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 735 | if err != nil { |
| 736 | t.Fatalf("binaryimpl.NewState failed: %v", err) |
| 737 | } |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 738 | server, _ := mgmttest.NewServer(ctx) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 739 | name := "realbin" |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 740 | d, err := binaryimpl.NewDispatcher(veyron2.GetPrincipal(ctx), state) |
Robert Kroeger | 8d7a0ef | 2015-01-14 17:38:40 -0800 | [diff] [blame] | 741 | if err != nil { |
| 742 | t.Fatalf("server.NewDispatcher failed: %v", err) |
| 743 | } |
| 744 | if err := server.ServeDispatcher(name, d); err != nil { |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 745 | t.Fatalf("server.ServeDispatcher failed: %v", err) |
| 746 | } |
| 747 | |
| 748 | tmpdir, err := ioutil.TempDir("", "test-package-") |
| 749 | if err != nil { |
| 750 | t.Fatalf("ioutil.TempDir failed: %v", err) |
| 751 | } |
| 752 | defer os.RemoveAll(tmpdir) |
| 753 | if err := ioutil.WriteFile(filepath.Join(tmpdir, "hello.txt"), []byte("Hello World!"), 0600); err != nil { |
| 754 | t.Fatalf("ioutil.WriteFile failed: %v", err) |
| 755 | } |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 756 | if err := libbinary.UploadFromDir(ctx, naming.Join(name, "testpkg"), tmpdir); err != nil { |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 757 | t.Fatalf("libbinary.UploadFromDir failed: %v", err) |
| 758 | } |
| 759 | return func() { |
| 760 | if err := server.Stop(); err != nil { |
| 761 | t.Fatalf("server.Stop failed: %v", err) |
| 762 | } |
Jiri Simsa | 432cc2e | 2014-12-08 15:53:38 -0800 | [diff] [blame] | 763 | if err := os.RemoveAll(rootDir); err != nil { |
| 764 | t.Fatalf("os.RemoveAll(%q) failed: %v", rootDir, err) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 765 | } |
| 766 | } |
| 767 | } |
| 768 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 769 | // TestDeviceManagerClaim claims a devicemanager and tests ACL permissions on |
| 770 | // its methods. |
| 771 | func TestDeviceManagerClaim(t *testing.T) { |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 772 | ctx, shutdown := veyron2.Init() |
| 773 | defer shutdown() |
| 774 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 775 | |
| 776 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, nil) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 777 | defer deferFn() |
| 778 | |
| 779 | // Set up mock application and binary repositories. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 780 | envelope, cleanup := startMockRepos(t, ctx) |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 781 | defer cleanup() |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 782 | |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 783 | root, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 784 | defer cleanup() |
| 785 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 786 | crDir, crEnv := mgmttest.CredentialsForChild(ctx, "devicemanager") |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 787 | defer os.RemoveAll(crDir) |
| 788 | |
Bogdan Caprita | 962d5e0 | 2014-10-28 18:36:09 -0700 | [diff] [blame] | 789 | // Create a script wrapping the test target that implements suidhelper. |
| 790 | helperPath := generateSuidHelperScript(t, root) |
| 791 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 792 | // Set up the device manager. Since we won't do device manager updates, |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 793 | // don't worry about its application envelope and current link. |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 794 | _, dms := mgmttest.RunShellCommand(t, sh, crEnv, deviceManagerCmd, "dm", root, helperPath, "unused_app_repo_name", "unused_curr_link") |
| 795 | pid := mgmttest.ReadPID(t, dms) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 796 | defer syscall.Kill(pid, syscall.SIGINT) |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 797 | |
Bogdan Caprita | 2692910 | 2014-11-07 11:56:56 -0800 | [diff] [blame] | 798 | *envelope = envelopeFromShell(sh, nil, appCmd, "google naps", "trapp") |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 799 | |
Robin Thellend | 888f8cf | 2014-12-15 16:19:10 -0800 | [diff] [blame] | 800 | deviceStub := device.DeviceClient("dm/device") |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 801 | |
| 802 | claimantCtx, err := veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal("claimant")) |
| 803 | if err != nil { |
| 804 | t.Fatalf("Could not create claimant principal: %v", err) |
| 805 | } |
| 806 | octx, err := veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal("other")) |
| 807 | if err != nil { |
| 808 | t.Fatalf("Could not create other principal: %v", err) |
| 809 | } |
Matt Rosencrantz | 5180d16 | 2014-12-03 13:48:40 -0800 | [diff] [blame] | 810 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 811 | // Devicemanager should have open ACLs before we claim it and so an |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 812 | // Install from octx should succeed. |
| 813 | installApp(t, octx) |
Robin Thellend | 888f8cf | 2014-12-15 16:19:10 -0800 | [diff] [blame] | 814 | // Claim the devicemanager with claimantRT as <defaultblessing>/mydevice |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 815 | if err := deviceStub.Claim(claimantCtx, &granter{p: veyron2.GetPrincipal(claimantCtx), extension: "mydevice"}); err != nil { |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 816 | t.Fatal(err) |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 817 | } |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 818 | |
Robin Thellend | 888f8cf | 2014-12-15 16:19:10 -0800 | [diff] [blame] | 819 | // Installation should succeed since claimantRT is now the "owner" of |
| 820 | // the devicemanager. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 821 | appID := installApp(t, claimantCtx) |
Bogdan Caprita | b9501d1 | 2014-10-10 15:02:03 -0700 | [diff] [blame] | 822 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 823 | // octx should be unable to install though, since the ACLs have |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 824 | // changed now. |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 825 | installAppExpectError(t, octx, verror.NoAccess.ID) |
Bogdan Caprita | b9501d1 | 2014-10-10 15:02:03 -0700 | [diff] [blame] | 826 | |
Bogdan Caprita | b9501d1 | 2014-10-10 15:02:03 -0700 | [diff] [blame] | 827 | // Create the local server that the app uses to let us know it's ready. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 828 | pingCh, cleanup := setupPingServer(t, ctx) |
Bogdan Caprita | d2b9f03 | 2014-10-10 17:43:29 -0700 | [diff] [blame] | 829 | defer cleanup() |
Bogdan Caprita | b9501d1 | 2014-10-10 15:02:03 -0700 | [diff] [blame] | 830 | |
| 831 | // Start an instance of the app. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 832 | instanceID := startApp(t, claimantCtx, appID) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 833 | |
| 834 | // Wait until the app pings us that it's ready. |
| 835 | select { |
| 836 | case <-pingCh: |
Bogdan Caprita | 916e99f | 2014-11-24 15:47:19 -0800 | [diff] [blame] | 837 | case <-time.After(pingTimeout): |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 838 | t.Fatalf("failed to get ping") |
| 839 | } |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 840 | resolve(t, ctx, "trapp", 1) |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 841 | suspendApp(t, claimantCtx, appID, instanceID) |
Bogdan Caprita | b9501d1 | 2014-10-10 15:02:03 -0700 | [diff] [blame] | 842 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 843 | // TODO(gauthamt): Test that ACLs persist across devicemanager restarts |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 844 | } |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 845 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 846 | func TestDeviceManagerUpdateACL(t *testing.T) { |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 847 | ctx, shutdown := veyron2.Init() |
| 848 | defer shutdown() |
| 849 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 850 | |
| 851 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, nil) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 852 | defer deferFn() |
| 853 | |
| 854 | // Set up mock application and binary repositories. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 855 | envelope, cleanup := startMockRepos(t, ctx) |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 856 | defer cleanup() |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 857 | |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 858 | root, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 859 | defer cleanup() |
| 860 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 861 | // The two "processes"/runtimes which will act as IPC clients to |
| 862 | // the devicemanager process. |
| 863 | selfCtx := ctx |
| 864 | octx, err := veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal()) |
| 865 | if err != nil { |
| 866 | t.Fatalf("Could not create other principal: %v", err) |
| 867 | } |
| 868 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 869 | // By default, selfCtx and octx will have blessings generated based on |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 870 | // the username/machine name running this process. Since these blessings |
| 871 | // will appear in ACLs, give them recognizable names. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 872 | idp := tsecurity.NewIDProvider("root") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 873 | if err := idp.Bless(veyron2.GetPrincipal(selfCtx), "self"); err != nil { |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 874 | t.Fatal(err) |
| 875 | } |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 876 | if err := idp.Bless(veyron2.GetPrincipal(octx), "other"); err != nil { |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 877 | t.Fatal(err) |
| 878 | } |
| 879 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 880 | crDir, crEnv := mgmttest.CredentialsForChild(ctx, "devicemanager") |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 881 | defer os.RemoveAll(crDir) |
| 882 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 883 | // Set up the device manager. Since we won't do device manager updates, |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 884 | // don't worry about its application envelope and current link. |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 885 | _, dms := mgmttest.RunShellCommand(t, sh, crEnv, deviceManagerCmd, "dm", root, "unused_helper", "unused_app_repo_name", "unused_curr_link") |
| 886 | pid := mgmttest.ReadPID(t, dms) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 887 | defer syscall.Kill(pid, syscall.SIGINT) |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 888 | |
| 889 | // Create an envelope for an app. |
Bogdan Caprita | 2692910 | 2014-11-07 11:56:56 -0800 | [diff] [blame] | 890 | *envelope = envelopeFromShell(sh, nil, appCmd, "google naps") |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 891 | |
Robert Kroeger | e95ed6d | 2015-01-14 17:41:04 -0800 | [diff] [blame] | 892 | // On an unclaimed device manager, there will be no ACLs. |
| 893 | deviceStub := device.DeviceClient("dm/device") |
| 894 | if _, _, err := deviceStub.GetACL(selfCtx); err == nil { |
| 895 | t.Fatalf("GetACL should have failed but didn't.") |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 898 | // Claim the devicemanager as "root/self/mydevice" |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 899 | if err := deviceStub.Claim(selfCtx, &granter{p: veyron2.GetPrincipal(selfCtx), extension: "mydevice"}); err != nil { |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 900 | t.Fatal(err) |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 901 | } |
Asim Shankar | 6888519 | 2014-11-26 12:48:35 -0800 | [diff] [blame] | 902 | expectedACL := make(access.TaggedACLMap) |
| 903 | for _, tag := range access.AllTypicalTags() { |
| 904 | expectedACL[string(tag)] = access.ACL{In: []security.BlessingPattern{"root/self/mydevice"}} |
| 905 | } |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 906 | var b bytes.Buffer |
Asim Shankar | 6888519 | 2014-11-26 12:48:35 -0800 | [diff] [blame] | 907 | if err := expectedACL.WriteTo(&b); err != nil { |
| 908 | t.Fatalf("Failed to save ACL:%v", err) |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 909 | } |
| 910 | md5hash := md5.Sum(b.Bytes()) |
| 911 | expectedETAG := hex.EncodeToString(md5hash[:]) |
Robert Kroeger | e95ed6d | 2015-01-14 17:41:04 -0800 | [diff] [blame] | 912 | acl, etag, err := deviceStub.GetACL(selfCtx) |
| 913 | if err != nil { |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 914 | t.Fatal(err) |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 915 | } |
| 916 | if etag != expectedETAG { |
| 917 | t.Fatalf("getACL expected:%v(%v), got:%v(%v)", expectedACL, expectedETAG, acl, etag) |
| 918 | } |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 919 | // Install from octx should fail, since it does not match the ACL. |
| 920 | installAppExpectError(t, octx, verror.NoAccess.ID) |
| 921 | |
Asim Shankar | 6888519 | 2014-11-26 12:48:35 -0800 | [diff] [blame] | 922 | newACL := make(access.TaggedACLMap) |
| 923 | for _, tag := range access.AllTypicalTags() { |
| 924 | newACL.Add("root/other", string(tag)) |
| 925 | } |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 926 | if err := deviceStub.SetACL(selfCtx, newACL, "invalid"); err == nil { |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 927 | t.Fatalf("SetACL should have failed with invalid etag") |
| 928 | } |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 929 | if err := deviceStub.SetACL(selfCtx, newACL, etag); err != nil { |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 930 | t.Fatal(err) |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 931 | } |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 932 | // Install should now fail with selfCtx, which no longer matches the |
| 933 | // ACLs but succeed with octx, which does. |
| 934 | installAppExpectError(t, selfCtx, verror.NoAccess.ID) |
| 935 | installApp(t, octx) |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 936 | } |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 937 | |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 938 | type simpleRW chan []byte |
| 939 | |
| 940 | func (s simpleRW) Write(p []byte) (n int, err error) { |
| 941 | s <- p |
| 942 | return len(p), nil |
| 943 | } |
| 944 | func (s simpleRW) Read(p []byte) (n int, err error) { |
| 945 | return copy(p, <-s), nil |
| 946 | } |
| 947 | |
| 948 | // TestDeviceManagerInstallation verifies the 'self install' and 'uninstall' |
Bogdan Caprita | a40d338 | 2014-12-19 16:30:26 -0800 | [diff] [blame] | 949 | // functionality of the device manager: it runs SelfInstall in a child process, |
| 950 | // then runs the executable from the soft link that the installation created. |
| 951 | // This should bring up a functioning device manager. In the end it runs |
| 952 | // Uninstall and verifies that the installation is gone. |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 953 | func TestDeviceManagerInstallation(t *testing.T) { |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 954 | ctx, shutdown := veyron2.Init() |
| 955 | defer shutdown() |
| 956 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 957 | |
| 958 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, nil) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 959 | defer deferFn() |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 960 | testDir, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Bogdan Caprita | 5420f17 | 2014-10-10 15:58:14 -0700 | [diff] [blame] | 961 | defer cleanup() |
| 962 | |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 963 | // Create a script wrapping the test target that implements suidhelper. |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 964 | suidHelperPath := generateSuidHelperScript(t, testDir) |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 965 | // Create a dummy script mascarading as the security agent. |
| 966 | agentPath := generateAgentScript(t, testDir) |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 967 | initHelperPath := "" |
Bogdan Caprita | 5420f17 | 2014-10-10 15:58:14 -0700 | [diff] [blame] | 968 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 969 | // Create an 'envelope' for the device manager that we can pass to the |
| 970 | // installer, to ensure that the device manager that the installer |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 971 | // configures can run. |
| 972 | dmargs, dmenv := sh.CommandEnvelope(deviceManagerCmd, nil, "dm") |
| 973 | dmDir := filepath.Join(testDir, "dm") |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 974 | // TODO(caprita): Add test logic when initMode = true. |
| 975 | singleUser, sessionMode, initMode := true, true, false |
| 976 | if err := impl.SelfInstall(dmDir, suidHelperPath, agentPath, initHelperPath, singleUser, sessionMode, initMode, dmargs[1:], dmenv, os.Stderr, os.Stdout); err != nil { |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 977 | t.Fatalf("SelfInstall failed: %v", err) |
| 978 | } |
Bogdan Caprita | 5420f17 | 2014-10-10 15:58:14 -0700 | [diff] [blame] | 979 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 980 | resolveExpectNotFound(t, ctx, "dm") |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 981 | // Start the device manager. |
| 982 | stdout := make(simpleRW, 100) |
| 983 | if err := impl.Start(dmDir, os.Stderr, stdout); err != nil { |
| 984 | t.Fatalf("Start failed: %v", err) |
| 985 | } |
| 986 | dms := expect.NewSession(t, stdout, mgmttest.ExpectTimeout) |
| 987 | mgmttest.ReadPID(t, dms) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 988 | resolve(t, ctx, "dm", 1) |
| 989 | revertDeviceExpectError(t, ctx, "dm", impl.ErrUpdateNoOp.ID) // No previous version available. |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 990 | |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 991 | // Stop the device manager. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 992 | if err := impl.Stop(ctx, dmDir, os.Stderr, os.Stdout); err != nil { |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 993 | t.Fatalf("Stop failed: %v", err) |
| 994 | } |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 995 | dms.Expect("dm terminated") |
Bogdan Caprita | a40d338 | 2014-12-19 16:30:26 -0800 | [diff] [blame] | 996 | |
| 997 | // Uninstall. |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 998 | if err := impl.Uninstall(dmDir, os.Stderr, os.Stdout); err != nil { |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 999 | t.Fatalf("Uninstall failed: %v", err) |
Bogdan Caprita | a40d338 | 2014-12-19 16:30:26 -0800 | [diff] [blame] | 1000 | } |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 1001 | // Ensure that the installation is gone. |
| 1002 | if files, err := ioutil.ReadDir(dmDir); err != nil || len(files) > 0 { |
| 1003 | var finfo []string |
| 1004 | for _, f := range files { |
| 1005 | finfo = append(finfo, f.Name()) |
| 1006 | } |
| 1007 | t.Fatalf("ReadDir returned (%v, %v)", err, finfo) |
Bogdan Caprita | a40d338 | 2014-12-19 16:30:26 -0800 | [diff] [blame] | 1008 | } |
Bogdan Caprita | 5420f17 | 2014-10-10 15:58:14 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1011 | func TestDeviceManagerGlobAndDebug(t *testing.T) { |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1012 | ctx, shutdown := veyron2.Init() |
| 1013 | defer shutdown() |
| 1014 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 1015 | |
| 1016 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, nil) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1017 | defer deferFn() |
| 1018 | |
| 1019 | // Set up mock application and binary repositories. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1020 | envelope, cleanup := startMockRepos(t, ctx) |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1021 | defer cleanup() |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1022 | |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 1023 | root, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 1024 | defer cleanup() |
| 1025 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1026 | crDir, crEnv := mgmttest.CredentialsForChild(ctx, "devicemanager") |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1027 | defer os.RemoveAll(crDir) |
| 1028 | |
Bogdan Caprita | 962d5e0 | 2014-10-28 18:36:09 -0700 | [diff] [blame] | 1029 | // Create a script wrapping the test target that implements suidhelper. |
| 1030 | helperPath := generateSuidHelperScript(t, root) |
| 1031 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1032 | // Set up the device manager. Since we won't do device manager updates, |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 1033 | // don't worry about its application envelope and current link. |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 1034 | _, dms := mgmttest.RunShellCommand(t, sh, crEnv, deviceManagerCmd, "dm", root, helperPath, "unused_app_repo_name", "unused_curr_link") |
| 1035 | pid := mgmttest.ReadPID(t, dms) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1036 | defer syscall.Kill(pid, syscall.SIGINT) |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 1037 | |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1038 | // Create the local server that the app uses to let us know it's ready. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1039 | pingCh, cleanup := setupPingServer(t, ctx) |
Bogdan Caprita | d2b9f03 | 2014-10-10 17:43:29 -0700 | [diff] [blame] | 1040 | defer cleanup() |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 1041 | |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1042 | // Create the envelope for the first version of the app. |
Bogdan Caprita | 2692910 | 2014-11-07 11:56:56 -0800 | [diff] [blame] | 1043 | *envelope = envelopeFromShell(sh, nil, appCmd, "google naps", "appV1") |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1044 | |
| 1045 | // Install the app. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1046 | appID := installApp(t, ctx) |
Bogdan Caprita | 43bc737 | 2014-12-03 21:51:12 -0800 | [diff] [blame] | 1047 | install1ID := path.Base(appID) |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1048 | |
| 1049 | // Start an instance of the app. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1050 | instance1ID := startApp(t, ctx, appID) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1051 | |
| 1052 | // Wait until the app pings us that it's ready. |
| 1053 | select { |
| 1054 | case <-pingCh: |
Bogdan Caprita | 916e99f | 2014-11-24 15:47:19 -0800 | [diff] [blame] | 1055 | case <-time.After(pingTimeout): |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1056 | t.Fatalf("failed to get ping") |
| 1057 | } |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1058 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1059 | app2ID := installApp(t, ctx) |
Bogdan Caprita | 43bc737 | 2014-12-03 21:51:12 -0800 | [diff] [blame] | 1060 | install2ID := path.Base(app2ID) |
| 1061 | |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1062 | testcases := []struct { |
| 1063 | name, pattern string |
| 1064 | expected []string |
| 1065 | }{ |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 1066 | {"dm", "...", []string{ |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1067 | "", |
| 1068 | "apps", |
| 1069 | "apps/google naps", |
Bogdan Caprita | 43bc737 | 2014-12-03 21:51:12 -0800 | [diff] [blame] | 1070 | "apps/google naps/" + install1ID, |
| 1071 | "apps/google naps/" + install1ID + "/" + instance1ID, |
| 1072 | "apps/google naps/" + install1ID + "/" + instance1ID + "/logs", |
| 1073 | "apps/google naps/" + install1ID + "/" + instance1ID + "/logs/STDERR-<timestamp>", |
| 1074 | "apps/google naps/" + install1ID + "/" + instance1ID + "/logs/STDOUT-<timestamp>", |
| 1075 | "apps/google naps/" + install1ID + "/" + instance1ID + "/logs/bin.INFO", |
| 1076 | "apps/google naps/" + install1ID + "/" + instance1ID + "/logs/bin.<*>.INFO.<timestamp>", |
| 1077 | "apps/google naps/" + install1ID + "/" + instance1ID + "/pprof", |
| 1078 | "apps/google naps/" + install1ID + "/" + instance1ID + "/stats", |
| 1079 | "apps/google naps/" + install1ID + "/" + instance1ID + "/stats/ipc", |
| 1080 | "apps/google naps/" + install1ID + "/" + instance1ID + "/stats/system", |
| 1081 | "apps/google naps/" + install1ID + "/" + instance1ID + "/stats/system/start-time-rfc1123", |
| 1082 | "apps/google naps/" + install1ID + "/" + instance1ID + "/stats/system/start-time-unix", |
| 1083 | "apps/google naps/" + install2ID, |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 1084 | "device", |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1085 | }}, |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 1086 | {"dm/apps", "*", []string{"google naps"}}, |
| 1087 | {"dm/apps/google naps", "*", []string{install1ID, install2ID}}, |
| 1088 | {"dm/apps/google naps/" + install1ID, "*", []string{instance1ID}}, |
| 1089 | {"dm/apps/google naps/" + install1ID + "/" + instance1ID, "*", []string{"logs", "pprof", "stats"}}, |
| 1090 | {"dm/apps/google naps/" + install1ID + "/" + instance1ID + "/logs", "*", []string{ |
Robin Thellend | 5864732 | 2014-10-28 12:07:47 -0700 | [diff] [blame] | 1091 | "STDERR-<timestamp>", |
| 1092 | "STDOUT-<timestamp>", |
| 1093 | "bin.INFO", |
| 1094 | "bin.<*>.INFO.<timestamp>", |
| 1095 | }}, |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 1096 | {"dm/apps/google naps/" + install1ID + "/" + instance1ID + "/stats/system", "start-time*", []string{"start-time-rfc1123", "start-time-unix"}}, |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 1097 | } |
Robin Thellend | 5864732 | 2014-10-28 12:07:47 -0700 | [diff] [blame] | 1098 | logFileTimeStampRE := regexp.MustCompile("(STDOUT|STDERR)-[0-9]+$") |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 1099 | logFileTrimInfoRE := regexp.MustCompile(`bin\..*\.INFO\.[0-9.-]+$`) |
Robin Thellend | 5864732 | 2014-10-28 12:07:47 -0700 | [diff] [blame] | 1100 | logFileRemoveErrorFatalWarningRE := regexp.MustCompile("(ERROR|FATAL|WARNING)") |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 1101 | statsTrimRE := regexp.MustCompile("/stats/(ipc|system(/start-time.*)?)$") |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1102 | for _, tc := range testcases { |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1103 | results, err := testutil.GlobName(ctx, tc.name, tc.pattern) |
Robin Thellend | 5c95a6c | 2014-11-10 13:06:56 -0800 | [diff] [blame] | 1104 | if err != nil { |
| 1105 | t.Errorf("unexpected glob error for (%q, %q): %v", tc.name, tc.pattern, err) |
| 1106 | continue |
| 1107 | } |
Robin Thellend | 5864732 | 2014-10-28 12:07:47 -0700 | [diff] [blame] | 1108 | filteredResults := []string{} |
| 1109 | for _, name := range results { |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 1110 | // Keep only the stats object names that match this RE. |
| 1111 | if strings.Contains(name, "/stats/") && !statsTrimRE.MatchString(name) { |
| 1112 | continue |
| 1113 | } |
| 1114 | // Remove ERROR, WARNING, FATAL log files because |
| 1115 | // they're not consistently there. |
Robin Thellend | 5864732 | 2014-10-28 12:07:47 -0700 | [diff] [blame] | 1116 | if logFileRemoveErrorFatalWarningRE.MatchString(name) { |
| 1117 | continue |
| 1118 | } |
| 1119 | name = logFileTimeStampRE.ReplaceAllString(name, "$1-<timestamp>") |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 1120 | name = logFileTrimInfoRE.ReplaceAllString(name, "bin.<*>.INFO.<timestamp>") |
Robin Thellend | 5864732 | 2014-10-28 12:07:47 -0700 | [diff] [blame] | 1121 | filteredResults = append(filteredResults, name) |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1122 | } |
Robin Thellend | 12937a2 | 2014-10-29 17:53:23 -0700 | [diff] [blame] | 1123 | sort.Strings(filteredResults) |
| 1124 | sort.Strings(tc.expected) |
Robin Thellend | 5864732 | 2014-10-28 12:07:47 -0700 | [diff] [blame] | 1125 | if !reflect.DeepEqual(filteredResults, tc.expected) { |
| 1126 | t.Errorf("unexpected result for (%q, %q). Got %q, want %q", tc.name, tc.pattern, filteredResults, tc.expected) |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1127 | } |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 1128 | } |
Robin Thellend | 4c5266e | 2014-10-27 13:19:29 -0700 | [diff] [blame] | 1129 | |
Robin Thellend | 5864732 | 2014-10-28 12:07:47 -0700 | [diff] [blame] | 1130 | // Call Size() on the log file objects. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1131 | files, err := testutil.GlobName(ctx, "dm", "apps/google naps/"+install1ID+"/"+instance1ID+"/logs/*") |
Robin Thellend | 5c95a6c | 2014-11-10 13:06:56 -0800 | [diff] [blame] | 1132 | if err != nil { |
| 1133 | t.Errorf("unexpected glob error: %v", err) |
| 1134 | } |
Robin Thellend | 12937a2 | 2014-10-29 17:53:23 -0700 | [diff] [blame] | 1135 | if want, got := 4, len(files); got < want { |
| 1136 | t.Errorf("Unexpected number of matches. Got %d, want at least %d", got, want) |
| 1137 | } |
Robin Thellend | 4c5266e | 2014-10-27 13:19:29 -0700 | [diff] [blame] | 1138 | for _, file := range files { |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 1139 | name := naming.Join("dm", file) |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 1140 | c := logreader.LogFileClient(name) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1141 | if _, err := c.Size(ctx); err != nil { |
Robin Thellend | 4c5266e | 2014-10-27 13:19:29 -0700 | [diff] [blame] | 1142 | t.Errorf("Size(%q) failed: %v", name, err) |
| 1143 | } |
| 1144 | } |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 1145 | |
| 1146 | // Call Value() on some of the stats objects. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1147 | objects, err := testutil.GlobName(ctx, "dm", "apps/google naps/"+install1ID+"/"+instance1ID+"/stats/system/start-time*") |
Robin Thellend | 5c95a6c | 2014-11-10 13:06:56 -0800 | [diff] [blame] | 1148 | if err != nil { |
| 1149 | t.Errorf("unexpected glob error: %v", err) |
| 1150 | } |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 1151 | if want, got := 2, len(objects); got != want { |
| 1152 | t.Errorf("Unexpected number of matches. Got %d, want %d", got, want) |
| 1153 | } |
| 1154 | for _, obj := range objects { |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 1155 | name := naming.Join("dm", obj) |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 1156 | c := stats.StatsClient(name) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1157 | if _, err := c.Value(ctx); err != nil { |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 1158 | t.Errorf("Value(%q) failed: %v", name, err) |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | // Call CmdLine() on the pprof object. |
| 1163 | { |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 1164 | name := "dm/apps/google naps/" + install1ID + "/" + instance1ID + "/pprof" |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 1165 | c := pprof.PProfClient(name) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1166 | v, err := c.CmdLine(ctx) |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 1167 | if err != nil { |
| 1168 | t.Errorf("CmdLine(%q) failed: %v", name, err) |
| 1169 | } |
| 1170 | if len(v) == 0 { |
| 1171 | t.Fatalf("Unexpected empty cmdline: %v", v) |
| 1172 | } |
| 1173 | if got, want := filepath.Base(v[0]), "bin"; got != want { |
| 1174 | t.Errorf("Unexpected value for argv[0]. Got %v, want %v", got, want) |
| 1175 | } |
| 1176 | } |
Robin Thellend | 4c5266e | 2014-10-27 13:19:29 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1179 | func TestDeviceManagerPackages(t *testing.T) { |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1180 | ctx, shutdown := veyron2.Init() |
| 1181 | defer shutdown() |
| 1182 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 1183 | |
| 1184 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, nil) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1185 | defer deferFn() |
| 1186 | |
| 1187 | // Set up mock application and binary repositories. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1188 | envelope, cleanup := startMockRepos(t, ctx) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1189 | defer cleanup() |
| 1190 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1191 | defer startRealBinaryRepository(t, ctx)() |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1192 | |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 1193 | root, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1194 | defer cleanup() |
| 1195 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1196 | crDir, crEnv := mgmttest.CredentialsForChild(ctx, "devicemanager") |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1197 | defer os.RemoveAll(crDir) |
| 1198 | |
| 1199 | // Create a script wrapping the test target that implements suidhelper. |
| 1200 | helperPath := generateSuidHelperScript(t, root) |
| 1201 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1202 | // Set up the device manager. Since we won't do device manager updates, |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1203 | // don't worry about its application envelope and current link. |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 1204 | _, dms := mgmttest.RunShellCommand(t, sh, crEnv, deviceManagerCmd, "dm", root, helperPath, "unused_app_repo_name", "unused_curr_link") |
| 1205 | pid := mgmttest.ReadPID(t, dms) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1206 | defer syscall.Kill(pid, syscall.SIGINT) |
| 1207 | |
| 1208 | // Create the local server that the app uses to let us know it's ready. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1209 | pingCh, cleanup := setupPingServer(t, ctx) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1210 | defer cleanup() |
| 1211 | |
| 1212 | // Create the envelope for the first version of the app. |
| 1213 | *envelope = envelopeFromShell(sh, nil, appCmd, "google naps", "appV1") |
| 1214 | (*envelope).Packages = map[string]string{ |
| 1215 | "test": "realbin/testpkg", |
| 1216 | } |
| 1217 | |
| 1218 | // Install the app. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1219 | appID := installApp(t, ctx) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1220 | |
| 1221 | // Start an instance of the app. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1222 | startApp(t, ctx, appID) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1223 | |
| 1224 | // Wait until the app pings us that it's ready. |
| 1225 | select { |
| 1226 | case <-pingCh: |
| 1227 | case <-time.After(pingTimeout): |
| 1228 | t.Fatalf("failed to get ping") |
| 1229 | } |
| 1230 | |
| 1231 | // Ask the app to cat a file from the package. |
| 1232 | file := filepath.Join("packages", "test", "hello.txt") |
| 1233 | name := "appV1" |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1234 | content, err := cat(ctx, name, file) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1235 | if err != nil { |
| 1236 | t.Errorf("cat(%q, %q) failed: %v", name, file, err) |
| 1237 | } |
| 1238 | if expected := "Hello World!"; content != expected { |
| 1239 | t.Errorf("unexpected content: expected %q, got %q", expected, content) |
| 1240 | } |
| 1241 | } |
| 1242 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1243 | func listAndVerifyAssociations(t *testing.T, ctx *context.T, stub device.DeviceClientMethods, expected []device.Association) { |
| 1244 | assocs, err := stub.ListAssociations(ctx) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1245 | if err != nil { |
| 1246 | t.Fatalf("ListAssociations failed %v", err) |
| 1247 | } |
Robert Kroeger | 1cb4a0d | 2014-10-20 11:55:38 -0700 | [diff] [blame] | 1248 | compareAssociations(t, assocs, expected) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1251 | // TODO(rjkroege): Verify that associations persist across restarts once |
| 1252 | // permanent storage is added. |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1253 | func TestAccountAssociation(t *testing.T) { |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1254 | ctx, shutdown := veyron2.Init() |
| 1255 | defer shutdown() |
| 1256 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 1257 | |
| 1258 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, nil) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1259 | defer deferFn() |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1260 | |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 1261 | root, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1262 | defer cleanup() |
| 1263 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1264 | // The two "processes"/contexts which will act as IPC clients to |
| 1265 | // the devicemanager process. |
| 1266 | selfCtx := ctx |
| 1267 | otherCtx, err := veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal()) |
| 1268 | if err != nil { |
| 1269 | t.Fatalf("Could not create other principal: %v", err) |
| 1270 | } |
| 1271 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1272 | // By default, selfCtx and otherCtx will have blessings generated based |
| 1273 | // on the username/machine name running this process. Since these |
| 1274 | // blessings will appear in test expecations, give them readable names. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1275 | idp := tsecurity.NewIDProvider("root") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1276 | if err := idp.Bless(veyron2.GetPrincipal(selfCtx), "self"); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1277 | t.Fatal(err) |
| 1278 | } |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1279 | if err := idp.Bless(veyron2.GetPrincipal(otherCtx), "other"); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1280 | t.Fatal(err) |
| 1281 | } |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1282 | crFile, crEnv := mgmttest.CredentialsForChild(ctx, "devicemanager") |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1283 | defer os.RemoveAll(crFile) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1284 | |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 1285 | _, dms := mgmttest.RunShellCommand(t, sh, crEnv, deviceManagerCmd, "dm", root, "unused_helper", "unused_app_repo_name", "unused_curr_link") |
| 1286 | pid := mgmttest.ReadPID(t, dms) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1287 | defer syscall.Kill(pid, syscall.SIGINT) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1288 | |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 1289 | deviceStub := device.DeviceClient("dm//device") |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1290 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1291 | // Attempt to list associations on the device manager without having |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1292 | // claimed it. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1293 | if list, err := deviceStub.ListAssociations(otherCtx); err != nil || list != nil { |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1294 | t.Fatalf("ListAssociations should fail on unclaimed device manager but did not: %v", err) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1295 | } |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1296 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1297 | // self claims the device manager. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1298 | if err := deviceStub.Claim(selfCtx, &granter{p: veyron2.GetPrincipal(selfCtx), extension: "alice"}); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1299 | t.Fatalf("Claim failed: %v", err) |
| 1300 | } |
| 1301 | |
| 1302 | vlog.VI(2).Info("Verify that associations start out empty.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1303 | listAndVerifyAssociations(t, selfCtx, deviceStub, []device.Association(nil)) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1304 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1305 | if err := deviceStub.AssociateAccount(selfCtx, []string{"root/self", "root/other"}, "alice_system_account"); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1306 | t.Fatalf("ListAssociations failed %v", err) |
| 1307 | } |
| 1308 | vlog.VI(2).Info("Added association should appear.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1309 | listAndVerifyAssociations(t, selfCtx, deviceStub, []device.Association{ |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1310 | { |
| 1311 | "root/self", |
| 1312 | "alice_system_account", |
| 1313 | }, |
| 1314 | { |
| 1315 | "root/other", |
| 1316 | "alice_system_account", |
| 1317 | }, |
| 1318 | }) |
| 1319 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1320 | if err := deviceStub.AssociateAccount(selfCtx, []string{"root/self", "root/other"}, "alice_other_account"); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1321 | t.Fatalf("AssociateAccount failed %v", err) |
| 1322 | } |
| 1323 | vlog.VI(2).Info("Change the associations and the change should appear.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1324 | listAndVerifyAssociations(t, selfCtx, deviceStub, []device.Association{ |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1325 | { |
| 1326 | "root/self", |
| 1327 | "alice_other_account", |
| 1328 | }, |
| 1329 | { |
| 1330 | "root/other", |
| 1331 | "alice_other_account", |
| 1332 | }, |
| 1333 | }) |
| 1334 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1335 | if err := deviceStub.AssociateAccount(selfCtx, []string{"root/other"}, ""); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1336 | t.Fatalf("AssociateAccount failed %v", err) |
| 1337 | } |
| 1338 | vlog.VI(2).Info("Verify that we can remove an association.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1339 | listAndVerifyAssociations(t, selfCtx, deviceStub, []device.Association{ |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1340 | { |
| 1341 | "root/self", |
| 1342 | "alice_other_account", |
| 1343 | }, |
| 1344 | }) |
| 1345 | } |
| 1346 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1347 | // userName is a helper function to determine the system name that the test is |
| 1348 | // running under. |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1349 | func userName(t *testing.T) string { |
| 1350 | u, err := user.Current() |
| 1351 | if err != nil { |
| 1352 | t.Fatalf("user.Current() failed: %v", err) |
| 1353 | } |
| 1354 | return u.Username |
| 1355 | } |
| 1356 | |
| 1357 | func TestAppWithSuidHelper(t *testing.T) { |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1358 | ctx, shutdown := veyron2.Init() |
| 1359 | defer shutdown() |
| 1360 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 1361 | |
| 1362 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, nil) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1363 | defer deferFn() |
| 1364 | |
| 1365 | // Set up mock application and binary repositories. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1366 | envelope, cleanup := startMockRepos(t, ctx) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1367 | defer cleanup() |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1368 | |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 1369 | root, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1370 | defer cleanup() |
| 1371 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1372 | // The two "processes"/runtimes which will act as IPC clients to |
| 1373 | // the devicemanager process. |
| 1374 | selfCtx := ctx |
| 1375 | otherCtx, err := veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal()) |
| 1376 | if err != nil { |
| 1377 | t.Fatalf("Could not create other principal: %v", err) |
| 1378 | } |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1379 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1380 | // By default, selfCtx and otherCtx will have blessings generated based |
| 1381 | // on the username/machine name running this process. Since these |
| 1382 | // blessings can appear in debugging output, give them recognizable |
| 1383 | // names. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1384 | idp := tsecurity.NewIDProvider("root") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1385 | if err := idp.Bless(veyron2.GetPrincipal(selfCtx), "self"); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1386 | t.Fatal(err) |
| 1387 | } |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1388 | if err := idp.Bless(veyron2.GetPrincipal(otherCtx), "other"); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1389 | t.Fatal(err) |
| 1390 | } |
| 1391 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1392 | crDir, crEnv := mgmttest.CredentialsForChild(ctx, "devicemanager") |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1393 | defer os.RemoveAll(crDir) |
| 1394 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1395 | // Create a script wrapping the test target that implements suidhelper. |
Bogdan Caprita | 962d5e0 | 2014-10-28 18:36:09 -0700 | [diff] [blame] | 1396 | helperPath := generateSuidHelperScript(t, root) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1397 | |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 1398 | _, dms := mgmttest.RunShellCommand(t, sh, crEnv, deviceManagerCmd, "-mocksetuid", "dm", root, helperPath, "unused_app_repo_name", "unused_curr_link") |
| 1399 | pid := mgmttest.ReadPID(t, dms) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1400 | defer syscall.Kill(pid, syscall.SIGINT) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1401 | |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 1402 | deviceStub := device.DeviceClient("dm//device") |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1403 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1404 | // Create the local server that the app uses to tell us which system |
| 1405 | // name the device manager wished to run it as. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame^] | 1406 | pingCh, cleanup := setupPingServer(t, ctx) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1407 | defer cleanup() |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1408 | |
Bogdan Caprita | 2692910 | 2014-11-07 11:56:56 -0800 | [diff] [blame] | 1409 | // Create an envelope for a first version of the app. |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1410 | *envelope = envelopeFromShell(sh, []string{testEnvVarName + "=env-var"}, appCmd, "google naps", fmt.Sprintf("--%s=flag-val-envelope", testFlagName), "appV1") |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1411 | |
| 1412 | // Install and start the app as root/self. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1413 | appID := installApp(t, selfCtx) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1414 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1415 | // Claim the devicemanager with selfCtx as root/self/alice |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1416 | if err := deviceStub.Claim(selfCtx, &granter{p: veyron2.GetPrincipal(selfCtx), extension: "alice"}); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1417 | t.Fatal(err) |
| 1418 | } |
| 1419 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1420 | // Start an instance of the app but this time it should fail: we do not |
| 1421 | // have an associated uname for the invoking identity. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1422 | startAppExpectError(t, selfCtx, appID, verror.NoAccess.ID) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1423 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1424 | // Create an association for selfCtx |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1425 | if err := deviceStub.AssociateAccount(selfCtx, []string{"root/self"}, testUserName); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1426 | t.Fatalf("AssociateAccount failed %v", err) |
| 1427 | } |
| 1428 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1429 | instance1ID := startApp(t, selfCtx, appID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1430 | verifyPingArgs(t, pingCh, testUserName, "flag-val-envelope", "env-var") // Wait until the app pings us that it's ready. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1431 | stopApp(t, selfCtx, appID, instance1ID) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1432 | |
| 1433 | vlog.VI(2).Infof("other attempting to run an app without access. Should fail.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1434 | startAppExpectError(t, otherCtx, appID, verror.NoAccess.ID) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1435 | |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1436 | // Self will now let other also install apps. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1437 | if err := deviceStub.AssociateAccount(selfCtx, []string{"root/other"}, testUserName); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1438 | t.Fatalf("AssociateAccount failed %v", err) |
| 1439 | } |
| 1440 | // Add Start to the ACL list for root/other. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1441 | newACL, _, err := deviceStub.GetACL(selfCtx) |
Robert Kroeger | 1ce0bd7 | 2014-10-22 13:57:14 -0700 | [diff] [blame] | 1442 | if err != nil { |
| 1443 | t.Fatalf("GetACL failed %v", err) |
| 1444 | } |
Asim Shankar | 6888519 | 2014-11-26 12:48:35 -0800 | [diff] [blame] | 1445 | newACL.Add("root/other", string(access.Write)) |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1446 | if err := deviceStub.SetACL(selfCtx, newACL, ""); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1447 | t.Fatalf("SetACL failed %v", err) |
| 1448 | } |
| 1449 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1450 | // With the introduction of per installation and per instance ACLs, |
| 1451 | // while other now has administrator permissions on the device manager, |
| 1452 | // other doesn't have execution permissions for the app. So this will |
| 1453 | // fail. |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1454 | vlog.VI(2).Infof("other attempting to run an app still without access. Should fail.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1455 | startAppExpectError(t, otherCtx, appID, verror.NoAccess.ID) |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1456 | |
| 1457 | // But self can give other permissions to start applications. |
| 1458 | vlog.VI(2).Infof("self attempting to give other permission to start %s", appID) |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1459 | newACL, _, err = appStub(appID).GetACL(selfCtx) |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1460 | if err != nil { |
| 1461 | t.Fatalf("GetACL on appID: %v failed %v", appID, err) |
| 1462 | } |
Asim Shankar | 6888519 | 2014-11-26 12:48:35 -0800 | [diff] [blame] | 1463 | newACL.Add("root/other", string(access.Read)) |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1464 | if err = appStub(appID).SetACL(selfCtx, newACL, ""); err != nil { |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1465 | t.Fatalf("SetACL on appID: %v failed: %v", appID, err) |
| 1466 | } |
| 1467 | |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1468 | vlog.VI(2).Infof("other attempting to run an app with access. Should succeed.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1469 | instance2ID := startApp(t, otherCtx, appID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1470 | verifyPingArgs(t, pingCh, testUserName, "flag-val-envelope", "env-var") // Wait until the app pings us that it's ready. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1471 | suspendApp(t, otherCtx, appID, instance2ID) |
Robert Kroeger | 1ce0bd7 | 2014-10-22 13:57:14 -0700 | [diff] [blame] | 1472 | |
| 1473 | vlog.VI(2).Infof("Verify that Resume with the same systemName works.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1474 | resumeApp(t, otherCtx, appID, instance2ID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1475 | verifyPingArgs(t, pingCh, testUserName, "flag-val-envelope", "env-var") // Wait until the app pings us that it's ready. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1476 | suspendApp(t, otherCtx, appID, instance2ID) |
Robert Kroeger | 1ce0bd7 | 2014-10-22 13:57:14 -0700 | [diff] [blame] | 1477 | |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1478 | vlog.VI(2).Infof("Verify that other can install and run applications.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1479 | otherAppID := installApp(t, otherCtx) |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1480 | |
| 1481 | vlog.VI(2).Infof("other attempting to run an app that other installed. Should succeed.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1482 | instance4ID := startApp(t, otherCtx, otherAppID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1483 | verifyPingArgs(t, pingCh, testUserName, "flag-val-envelope", "env-var") // Wait until the app pings us that it's ready. |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1484 | |
| 1485 | // Clean up. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1486 | stopApp(t, otherCtx, otherAppID, instance4ID) |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1487 | |
Robert Kroeger | 1ce0bd7 | 2014-10-22 13:57:14 -0700 | [diff] [blame] | 1488 | // Change the associated system name. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1489 | if err := deviceStub.AssociateAccount(selfCtx, []string{"root/other"}, anotherTestUserName); err != nil { |
Robert Kroeger | 1ce0bd7 | 2014-10-22 13:57:14 -0700 | [diff] [blame] | 1490 | t.Fatalf("AssociateAccount failed %v", err) |
| 1491 | } |
| 1492 | |
| 1493 | vlog.VI(2).Infof("Show that Resume with a different systemName fails.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1494 | resumeAppExpectError(t, otherCtx, appID, instance2ID, verror.NoAccess.ID) |
Robert Kroeger | 1ce0bd7 | 2014-10-22 13:57:14 -0700 | [diff] [blame] | 1495 | |
| 1496 | // Clean up. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1497 | stopApp(t, otherCtx, appID, instance2ID) |
Robert Kroeger | 1ce0bd7 | 2014-10-22 13:57:14 -0700 | [diff] [blame] | 1498 | |
| 1499 | vlog.VI(2).Infof("Show that Start with different systemName works.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1500 | instance3ID := startApp(t, otherCtx, appID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1501 | verifyPingArgs(t, pingCh, anotherTestUserName, "flag-val-envelope", "env-var") // Wait until the app pings us that it's ready. |
Robert Kroeger | 1ce0bd7 | 2014-10-22 13:57:14 -0700 | [diff] [blame] | 1502 | |
| 1503 | // Clean up. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1504 | stopApp(t, otherCtx, appID, instance3ID) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1505 | } |