~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/lxc/lxd/lxd/rsync_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package main
 
2
 
 
3
import (
 
4
        "io"
 
5
        "io/ioutil"
 
6
        "os"
 
7
        "path"
 
8
        "testing"
 
9
 
 
10
        "github.com/lxc/lxd/shared"
 
11
)
 
12
 
 
13
const helloWorld = "hello world\n"
 
14
 
 
15
func TestRsyncSendRecv(t *testing.T) {
 
16
        source, err := ioutil.TempDir("", "lxd_test_source_")
 
17
        if err != nil {
 
18
                t.Error(err)
 
19
                return
 
20
        }
 
21
        defer os.RemoveAll(source)
 
22
 
 
23
        sink, err := ioutil.TempDir("", "lxd_test_sink_")
 
24
        if err != nil {
 
25
                t.Error(err)
 
26
                return
 
27
        }
 
28
        defer os.RemoveAll(sink)
 
29
 
 
30
        /* now, write something to rsync over */
 
31
        f, err := os.Create(path.Join(source, "foo"))
 
32
        if err != nil {
 
33
                t.Error(err)
 
34
                return
 
35
        }
 
36
        f.Write([]byte(helloWorld))
 
37
        f.Close()
 
38
 
 
39
        send, sendConn, _, err := rsyncSendSetup(shared.AddSlash(source))
 
40
        if err != nil {
 
41
                t.Error(err)
 
42
                return
 
43
        }
 
44
 
 
45
        recv := rsyncRecvCmd(sink)
 
46
 
 
47
        recvOut, err := recv.StdoutPipe()
 
48
        if err != nil {
 
49
                t.Error(err)
 
50
                return
 
51
        }
 
52
 
 
53
        recvIn, err := recv.StdinPipe()
 
54
        if err != nil {
 
55
                t.Error(err)
 
56
                return
 
57
        }
 
58
 
 
59
        if err := recv.Start(); err != nil {
 
60
                t.Error(err)
 
61
                return
 
62
        }
 
63
 
 
64
        go func() {
 
65
                defer sendConn.Close()
 
66
                if _, err := io.Copy(sendConn, recvOut); err != nil {
 
67
                        t.Error(err)
 
68
                }
 
69
 
 
70
                if err := recv.Wait(); err != nil {
 
71
                        t.Error(err)
 
72
                }
 
73
 
 
74
        }()
 
75
 
 
76
        /*
 
77
         * We close the socket in the above gofunc, but go tells us
 
78
         * https://github.com/golang/go/issues/4373 that this is an error
 
79
         * because we were reading from a socket that was closed. Thus, we
 
80
         * ignore it
 
81
         */
 
82
        io.Copy(recvIn, sendConn)
 
83
 
 
84
        if err := send.Wait(); err != nil {
 
85
                t.Error(err)
 
86
                return
 
87
        }
 
88
 
 
89
        f, err = os.Open(path.Join(sink, "foo"))
 
90
        if err != nil {
 
91
                t.Error(err)
 
92
                return
 
93
        }
 
94
        defer f.Close()
 
95
 
 
96
        buf, err := ioutil.ReadAll(f)
 
97
        if err != nil {
 
98
                t.Error(err)
 
99
                return
 
100
        }
 
101
 
 
102
        if string(buf) != helloWorld {
 
103
                t.Errorf("expected %s got %s", helloWorld, buf)
 
104
                return
 
105
        }
 
106
}