Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 1 | package impl_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
Bogdan Caprita | 8964d3f | 2015-02-02 13:47:39 -0800 | [diff] [blame] | 5 | "encoding/json" |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 6 | "fmt" |
Bogdan Caprita | 50e2153 | 2015-02-06 17:33:27 -0800 | [diff] [blame] | 7 | "io/ioutil" |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 8 | "os" |
Bogdan Caprita | 50e2153 | 2015-02-06 17:33:27 -0800 | [diff] [blame] | 9 | "path/filepath" |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 10 | "reflect" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "v.io/core/veyron2/naming" |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 15 | "v.io/core/veyron2/security" |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 16 | "v.io/core/veyron2/services/mgmt/application" |
Bogdan Caprita | 8964d3f | 2015-02-02 13:47:39 -0800 | [diff] [blame] | 17 | "v.io/core/veyron2/services/mgmt/device" |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 18 | |
| 19 | "v.io/core/veyron/tools/mgmt/device/impl" |
| 20 | ) |
| 21 | |
Bogdan Caprita | 50e2153 | 2015-02-06 17:33:27 -0800 | [diff] [blame] | 22 | func createFile(t *testing.T, path string, contents string) { |
| 23 | if err := ioutil.WriteFile(path, []byte(contents), 0700); err != nil { |
| 24 | t.Fatalf("Failed to create %v: %v", path, err) |
| 25 | } |
| 26 | } |
| 27 | |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 28 | func TestInstallLocalCommand(t *testing.T) { |
| 29 | shutdown := initTest() |
| 30 | defer shutdown() |
| 31 | |
| 32 | tape := NewTape() |
| 33 | server, endpoint, err := startServer(t, gctx, tape) |
| 34 | if err != nil { |
| 35 | return |
| 36 | } |
| 37 | defer stopServer(t, server) |
| 38 | // Setup the command-line. |
| 39 | cmd := impl.Root() |
| 40 | var stdout, stderr bytes.Buffer |
| 41 | cmd.Init(nil, &stdout, &stderr) |
| 42 | deviceName := naming.JoinAddressName(endpoint.String(), "") |
Bogdan Caprita | 50e2153 | 2015-02-06 17:33:27 -0800 | [diff] [blame] | 43 | const appTitle = "Appo di tutti Appi" |
| 44 | binary := os.Args[0] |
| 45 | fi, err := os.Stat(binary) |
| 46 | if err != nil { |
| 47 | t.Fatalf("Failed to stat %v: %v", binary, err) |
| 48 | } |
| 49 | binarySize := fi.Size() |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 50 | for i, c := range []struct { |
| 51 | args []string |
| 52 | stderrSubstr string |
| 53 | }{ |
| 54 | { |
Bogdan Caprita | 8964d3f | 2015-02-02 13:47:39 -0800 | [diff] [blame] | 55 | []string{deviceName}, "incorrect number of arguments", |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 56 | }, |
| 57 | { |
Bogdan Caprita | 8964d3f | 2015-02-02 13:47:39 -0800 | [diff] [blame] | 58 | []string{deviceName, appTitle}, "missing binary", |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 59 | }, |
| 60 | { |
Bogdan Caprita | 8964d3f | 2015-02-02 13:47:39 -0800 | [diff] [blame] | 61 | []string{deviceName, appTitle, "a=b"}, "missing binary", |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 62 | }, |
| 63 | { |
Bogdan Caprita | 8964d3f | 2015-02-02 13:47:39 -0800 | [diff] [blame] | 64 | []string{deviceName, appTitle, "foo"}, "binary foo not found", |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 65 | }, |
Bogdan Caprita | 50e2153 | 2015-02-06 17:33:27 -0800 | [diff] [blame] | 66 | { |
| 67 | []string{deviceName, appTitle, binary, "PACKAGES", "foo"}, "foo not found", |
| 68 | }, |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 69 | } { |
Bogdan Caprita | 8964d3f | 2015-02-02 13:47:39 -0800 | [diff] [blame] | 70 | c.args = append([]string{"install-local"}, c.args...) |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 71 | if err := cmd.Execute(c.args); err == nil { |
| 72 | t.Fatalf("test case %d: wrongly failed to receive a non-nil error.", i) |
| 73 | } else { |
| 74 | fmt.Fprintln(&stderr, "ERROR:", err) |
| 75 | if want, got := c.stderrSubstr, stderr.String(); !strings.Contains(got, want) { |
| 76 | t.Errorf("test case %d: %q not found in stderr: %q", i, want, got) |
| 77 | } |
| 78 | } |
| 79 | if got, expected := len(tape.Play()), 0; got != expected { |
| 80 | t.Errorf("test case %d: invalid call sequence. Got %v, want %v", got, expected) |
| 81 | } |
| 82 | tape.Rewind() |
| 83 | stdout.Reset() |
| 84 | stderr.Reset() |
| 85 | } |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 86 | emptySig := security.Signature{Purpose: []uint8{}, Hash: "", R: []uint8{}, S: []uint8{}} |
| 87 | emptyBlessings := security.WireBlessings{} |
Bogdan Caprita | 8964d3f | 2015-02-02 13:47:39 -0800 | [diff] [blame] | 88 | cfg := device.Config{"someflag": "somevalue"} |
Bogdan Caprita | 50e2153 | 2015-02-06 17:33:27 -0800 | [diff] [blame] | 89 | |
| 90 | testPackagesDir, err := ioutil.TempDir("", "testdir") |
| 91 | if err != nil { |
| 92 | t.Fatalf("Failed to create temp dir: %v", err) |
| 93 | } |
| 94 | defer os.RemoveAll(testPackagesDir) |
| 95 | pkgFile1 := filepath.Join(testPackagesDir, "file1.txt") |
| 96 | createFile(t, pkgFile1, "1234567") |
| 97 | pkgFile2 := filepath.Join(testPackagesDir, "file2") |
| 98 | createFile(t, pkgFile2, string([]byte{0x01, 0x02, 0x03, 0x04})) |
| 99 | pkgDir := filepath.Join(testPackagesDir, "dir") |
| 100 | if err := os.Mkdir(pkgDir, 0700); err != nil { |
| 101 | t.Fatalf("Failed to create dir: %v", err) |
| 102 | } |
| 103 | createFile(t, filepath.Join(pkgDir, "f1"), "123") |
| 104 | createFile(t, filepath.Join(pkgDir, "f2"), "456") |
| 105 | createFile(t, filepath.Join(pkgDir, "f3"), "7890") |
| 106 | |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 107 | for i, c := range []struct { |
| 108 | args []string |
Bogdan Caprita | 8964d3f | 2015-02-02 13:47:39 -0800 | [diff] [blame] | 109 | config device.Config |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 110 | expectedTape interface{} |
| 111 | }{ |
| 112 | { |
Bogdan Caprita | 8964d3f | 2015-02-02 13:47:39 -0800 | [diff] [blame] | 113 | []string{deviceName, appTitle, binary}, |
| 114 | nil, |
Bogdan Caprita | 50e2153 | 2015-02-06 17:33:27 -0800 | [diff] [blame] | 115 | InstallStimulus{ |
| 116 | "Install", |
| 117 | appNameAfterFetch, |
| 118 | nil, |
| 119 | application.Envelope{ |
| 120 | Title: appTitle, |
| 121 | Binary: binaryNameAfterFetch, |
| 122 | Signature: emptySig, |
| 123 | Publisher: emptyBlessings, |
| 124 | }, |
| 125 | map[string]int64{"binary": binarySize}}, |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 126 | }, |
| 127 | { |
Bogdan Caprita | 8964d3f | 2015-02-02 13:47:39 -0800 | [diff] [blame] | 128 | []string{deviceName, appTitle, binary}, |
| 129 | cfg, |
Bogdan Caprita | 50e2153 | 2015-02-06 17:33:27 -0800 | [diff] [blame] | 130 | InstallStimulus{ |
| 131 | "Install", |
| 132 | appNameAfterFetch, |
| 133 | cfg, |
| 134 | application.Envelope{ |
| 135 | Title: appTitle, |
| 136 | Binary: binaryNameAfterFetch, |
| 137 | Signature: emptySig, |
| 138 | Publisher: emptyBlessings, |
| 139 | }, |
| 140 | map[string]int64{"binary": binarySize}}, |
Bogdan Caprita | 8964d3f | 2015-02-02 13:47:39 -0800 | [diff] [blame] | 141 | }, |
| 142 | { |
| 143 | []string{deviceName, appTitle, "ENV1=V1", "ENV2=V2", binary, "FLAG1=V1", "FLAG2=V2"}, |
| 144 | nil, |
Bogdan Caprita | 50e2153 | 2015-02-06 17:33:27 -0800 | [diff] [blame] | 145 | InstallStimulus{ |
| 146 | "Install", |
| 147 | appNameAfterFetch, |
| 148 | nil, |
| 149 | application.Envelope{ |
| 150 | Title: appTitle, |
| 151 | Binary: binaryNameAfterFetch, |
| 152 | Signature: emptySig, |
| 153 | Publisher: emptyBlessings, |
| 154 | Env: []string{"ENV1=V1", "ENV2=V2"}, |
| 155 | Args: []string{"FLAG1=V1", "FLAG2=V2"}, |
| 156 | }, |
| 157 | map[string]int64{"binary": binarySize}}, |
| 158 | }, |
| 159 | { |
| 160 | []string{deviceName, appTitle, "ENV=V", binary, "FLAG=V", "PACKAGES", pkgFile1, pkgFile2, pkgDir}, |
| 161 | nil, |
| 162 | InstallStimulus{"Install", |
| 163 | appNameAfterFetch, |
| 164 | nil, |
| 165 | application.Envelope{ |
| 166 | Title: appTitle, |
| 167 | Binary: binaryNameAfterFetch, |
| 168 | Signature: emptySig, |
| 169 | Publisher: emptyBlessings, |
| 170 | Env: []string{"ENV=V"}, |
| 171 | Args: []string{"FLAG=V"}, |
| 172 | }, |
| 173 | map[string]int64{"binary": binarySize, "packages/file1.txt": 7, "packages/file2": 4, "packages/dir": 10}}, |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 174 | }, |
| 175 | } { |
Bogdan Caprita | 50e2153 | 2015-02-06 17:33:27 -0800 | [diff] [blame] | 176 | const appId = "myBestAppID" |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 177 | tape.SetResponses([]interface{}{InstallResponse{appId, nil}}) |
Bogdan Caprita | 8964d3f | 2015-02-02 13:47:39 -0800 | [diff] [blame] | 178 | if c.config != nil { |
| 179 | jsonConfig, err := json.Marshal(c.config) |
| 180 | if err != nil { |
| 181 | t.Fatalf("test case %d: Marshal(%v) failed: %v", i, c.config, err) |
| 182 | } |
| 183 | c.args = append([]string{fmt.Sprintf("--config=%s", string(jsonConfig))}, c.args...) |
| 184 | } |
| 185 | c.args = append([]string{"install-local"}, c.args...) |
Bogdan Caprita | b646a1c | 2015-01-30 19:16:37 -0800 | [diff] [blame] | 186 | if err := cmd.Execute(c.args); err != nil { |
| 187 | t.Fatalf("test case %d: %v", i, err) |
| 188 | } |
| 189 | if expected, got := fmt.Sprintf("Successfully installed: %q", naming.Join(deviceName, appId)), strings.TrimSpace(stdout.String()); got != expected { |
| 190 | t.Fatalf("test case %d: Unexpected output from Install. Got %q, expected %q", i, got, expected) |
| 191 | } |
| 192 | if got, expected := tape.Play(), []interface{}{c.expectedTape}; !reflect.DeepEqual(expected, got) { |
| 193 | t.Errorf("test case %d: Invalid call sequence. Got %#v, want %#v", i, got, expected) |
| 194 | } |
| 195 | tape.Rewind() |
| 196 | stdout.Reset() |
| 197 | stderr.Reset() |
| 198 | } |
| 199 | } |