| 1 |
/* |
| 2 |
Copyright 2013-2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
package legacy |
| 18 |
|
| 19 |
import ( |
| 20 |
"io/ioutil" |
| 21 |
"path/filepath" |
| 22 |
"testing" |
| 23 |
"time" |
| 24 |
|
| 25 |
. "gopkg.in/check.v1" |
| 26 |
|
| 27 |
clickhelp "gitlab.com/ubports/development/core/lomiri-push-service/click/testing" |
| 28 |
helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 29 |
) |
| 30 |
|
| 31 |
func takeNext(ch chan string, c *C) string { |
| 32 |
select { |
| 33 |
case s := <-ch: |
| 34 |
return s |
| 35 |
case <-time.After(10 * time.Second): |
| 36 |
c.Fatal("timed out waiting for value") |
| 37 |
return "" |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func Test(t *testing.T) { TestingT(t) } |
| 42 |
|
| 43 |
type legacySuite struct { |
| 44 |
lhl *legacyHelperLauncher |
| 45 |
log *helpers.TestLogger |
| 46 |
} |
| 47 |
|
| 48 |
var _ = Suite(&legacySuite{}) |
| 49 |
|
| 50 |
func (ls *legacySuite) SetUpTest(c *C) { |
| 51 |
ls.log = helpers.NewTestLogger(c, "info") |
| 52 |
ls.lhl = New(ls.log) |
| 53 |
ls.lhl.SetHelperDir(helpers.ScriptAbsPath("legacy-helpers")) |
| 54 |
} |
| 55 |
|
| 56 |
func (ls *legacySuite) TestInstallObserver(c *C) { |
| 57 |
c.Check(ls.lhl.done, IsNil) |
| 58 |
c.Check(ls.lhl.InstallObserver(func(string) {}), IsNil) |
| 59 |
c.Check(ls.lhl.done, NotNil) |
| 60 |
} |
| 61 |
|
| 62 |
func (s *legacySuite) TestHelperAppID(c *C) { |
| 63 |
appname := "lomiri-system-settings" |
| 64 |
app := clickhelp.MustParseAppId("_" + appname) |
| 65 |
hid := s.lhl.HelperAppID(app) |
| 66 |
c.Check(hid, Equals, appname) |
| 67 |
} |
| 68 |
|
| 69 |
func (ls *legacySuite) TestLaunch(c *C) { |
| 70 |
ch := make(chan string, 1) |
| 71 |
c.Assert(ls.lhl.InstallObserver(func(id string) { ch <- id }), IsNil) |
| 72 |
|
| 73 |
d := c.MkDir() |
| 74 |
f1 := filepath.Join(d, "one") |
| 75 |
f2 := filepath.Join(d, "two") |
| 76 |
|
| 77 |
d1 := []byte(`potato`) |
| 78 |
c.Assert(ioutil.WriteFile(f1, d1, 0644), IsNil) |
| 79 |
|
| 80 |
id, err := ls.lhl.Launch("trivial-helper.sh", f1, f2) |
| 81 |
c.Assert(err, IsNil) |
| 82 |
c.Check(id, Not(Equals), "") |
| 83 |
|
| 84 |
id2 := takeNext(ch, c) |
| 85 |
c.Check(id, Equals, id2) |
| 86 |
|
| 87 |
d2, err := ioutil.ReadFile(f2) |
| 88 |
c.Assert(err, IsNil) |
| 89 |
c.Check(string(d2), Equals, string(d1)) |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
func (ls *legacySuite) TestLaunchFails(c *C) { |
| 94 |
_, err := ls.lhl.Launch("does-not-exist", "", "") |
| 95 |
c.Assert(err, NotNil) |
| 96 |
} |
| 97 |
|
| 98 |
func (ls *legacySuite) TestHelperFails(c *C) { |
| 99 |
ch := make(chan string, 1) |
| 100 |
c.Assert(ls.lhl.InstallObserver(func(id string) { ch <- id }), IsNil) |
| 101 |
|
| 102 |
_, err := ls.lhl.Launch("fail.sh", "", "") |
| 103 |
c.Assert(err, IsNil) |
| 104 |
|
| 105 |
takeNext(ch, c) |
| 106 |
c.Check(ls.log.Captured(), Matches, "(?si).*Legacy helper failed.*") |
| 107 |
} |
| 108 |
|
| 109 |
func (ls *legacySuite) TestHelperFailsLog(c *C) { |
| 110 |
ch := make(chan string, 1) |
| 111 |
c.Assert(ls.lhl.InstallObserver(func(id string) { ch <- id }), IsNil) |
| 112 |
|
| 113 |
_, err := ls.lhl.Launch("noisy-helper.sh", "", "") |
| 114 |
c.Assert(err, IsNil) |
| 115 |
|
| 116 |
takeNext(ch, c) |
| 117 |
c.Check(ls.log.Captured(), Matches, "(?s).*BOOM-1.*") |
| 118 |
c.Check(ls.log.Captured(), Matches, "(?s).*BANG-1.*") |
| 119 |
c.Check(ls.log.Captured(), Matches, "(?s).*BOOM-20.*") |
| 120 |
c.Check(ls.log.Captured(), Matches, "(?s).*BANG-20.*") |
| 121 |
} |
| 122 |
|
| 123 |
func (ls *legacySuite) TestStop(c *C) { |
| 124 |
ch := make(chan string, 1) |
| 125 |
c.Assert(ls.lhl.InstallObserver(func(id string) { ch <- id }), IsNil) |
| 126 |
|
| 127 |
// exe := helpers.ScriptAbsPath("slow-helper.sh") |
| 128 |
id, err := ls.lhl.Launch("stop.sh", "9", "1") |
| 129 |
c.Assert(err, IsNil) |
| 130 |
|
| 131 |
err = ls.lhl.Stop("", "===") |
| 132 |
c.Check(err, NotNil) // not a valid id |
| 133 |
|
| 134 |
err = ls.lhl.Stop("", id) |
| 135 |
c.Check(err, IsNil) |
| 136 |
takeNext(ch, c) |
| 137 |
err = ls.lhl.Stop("", id) |
| 138 |
c.Check(err, NotNil) // no such processs |
| 139 |
} |