Merge lp:~elopio/snappy/serve_daemon_test into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by Leo Arias
Status: Rejected
Rejected by: Leo Arias
Proposed branch: lp:~elopio/snappy/serve_daemon_test
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Prerequisite: lp:~chipaca/snappy/serve
Diff against target: 77 lines (+61/-0)
2 files modified
cmd/snapd/main.go (+11/-0)
cmd/snapd/main_test.go (+50/-0)
To merge this branch: bzr merge lp:~elopio/snappy/serve_daemon_test
Reviewer Review Type Date Requested Status
Leo Arias (community) Disapprove
Review via email: mp+270484@code.launchpad.net

This proposal supersedes a proposal from 2015-09-09.

To post a comment you must log in.
Revision history for this message
Leo Arias (elopio) wrote :

Seems not worth it. It's easier to test the daemon on the snappy system:
https://code.launchpad.net/~fgimenez/snappy/snapd_integration_test/+merge/270515

review: Disapprove

Unmerged revisions

651. By Leo Arias

Playing with the daemon.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'cmd/snapd/main.go'
2--- cmd/snapd/main.go 2015-09-09 06:34:47 +0000
3+++ cmd/snapd/main.go 2015-09-09 06:34:47 +0000
4@@ -36,7 +36,18 @@
5 }
6 }
7
8+// XXX what are the alternatives here? use real systemd to run the binary? --elopio- 2015-09-09
9+func fixListenPid() {
10+ if os.Getenv("FIX_LISTEN_PID") != "" {
11+ // HACK: real systemd would set LISTEN_PID before exec'ing but
12+ // this is too difficult in golang for the purpose of a test.
13+ // Do not do this in real code.
14+ os.Setenv("LISTEN_PID", fmt.Sprintf("%d", os.Getpid()))
15+ }
16+}
17+
18 func main() {
19+ fixListenPid()
20 if err := run(); err != nil {
21 fmt.Fprintf(os.Stderr, "error: %v\n", err)
22 os.Exit(1)
23
24=== added file 'cmd/snapd/main_test.go'
25--- cmd/snapd/main_test.go 1970-01-01 00:00:00 +0000
26+++ cmd/snapd/main_test.go 2015-09-09 06:34:47 +0000
27@@ -0,0 +1,50 @@
28+package main
29+
30+import (
31+ "io/ioutil"
32+ "net"
33+ "net/http"
34+ "os"
35+ "os/exec"
36+ "syscall"
37+ "testing"
38+
39+ "gopkg.in/check.v1"
40+)
41+
42+// Hook up check.v1 into the "go test" runner
43+func Test(t *testing.T) { check.TestingT(t) }
44+
45+type snapdSuite struct{}
46+
47+var _ = check.Suite(&snapdSuite{})
48+
49+func (s *snapdSuite) Test(c *check.C) {
50+ cmd := exec.Command("go", "run", "main.go")
51+
52+ // TODO get a random port. Not yet because this is useful to know if the server is being killed.
53+ // --elopio - 2015-09-09
54+ listener, err := net.Listen("tcp", ":9999")
55+ c.Assert(err, check.IsNil)
56+
57+ tcpListener := listener.(*net.TCPListener)
58+ f, err := tcpListener.File()
59+ c.Assert(err, check.IsNil)
60+ cmd.ExtraFiles = []*os.File{f}
61+
62+ cmd.Env = os.Environ()
63+ cmd.Env = append(cmd.Env, "LISTEN_FDS=1", "FIX_LISTEN_PID=1")
64+
65+ cmd.Start()
66+ // FIXME this is not killing the server. How do we get the pid? Close the listener? Close the file?
67+ // --elopio - 2015-09-09
68+ defer cmd.Process.Signal(syscall.SIGTERM)
69+
70+ resp, err := http.Get("http://127.0.0.1:9999")
71+ defer resp.Body.Close()
72+ c.Assert(err, check.IsNil)
73+ body, err := ioutil.ReadAll(resp.Body)
74+ c.Assert(err, check.IsNil)
75+ expected := `{"metadata":["/1.0"],"status":"OK","status_code":200,"type":"sync"}`
76+ c.Assert(string(body), check.Equals, expected)
77+}

Subscribers

People subscribed via source and target branches