-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
writer_test.go
200 lines (157 loc) 路 5.38 KB
/
writer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2016-2022, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rpcutil
import (
"bytes"
"context"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"golang.org/x/term"
)
func makeStreamMock() *streamMock {
return &streamMock{
ctx: context.Background(),
}
}
type streamMock struct {
grpc.ServerStream
ctx context.Context
stdout bytes.Buffer
stderr bytes.Buffer
}
func (m *streamMock) Context() context.Context {
return m.ctx
}
func (m *streamMock) Send(resp *pulumirpc.InstallDependenciesResponse) error {
if _, err := m.stdout.Write(resp.Stdout); err != nil {
return err
}
if _, err := m.stderr.Write(resp.Stderr); err != nil {
return err
}
return nil
}
func TestWriter_NoTerminal(t *testing.T) {
t.Parallel()
server := makeStreamMock()
closer, stdout, stderr, err := MakeInstallDependenciesStreams(server, false)
assert.NoError(t, err)
// stdout and stderr should just write to server
l, err := stdout.Write([]byte("hello"))
assert.NoError(t, err)
assert.Equal(t, 5, l)
l, err = stderr.Write([]byte("world"))
assert.NoError(t, err)
assert.Equal(t, 5, l)
err = closer.Close()
assert.NoError(t, err)
outBytes, err := ioutil.ReadAll(&server.stdout)
assert.NoError(t, err)
assert.Equal(t, []byte("hello"), outBytes)
errBytes, err := ioutil.ReadAll(&server.stderr)
assert.NoError(t, err)
assert.Equal(t, []byte("world"), errBytes)
}
func TestWriter_Terminal(t *testing.T) {
t.Parallel()
server := makeStreamMock()
closer, stdout, stderr, err := MakeInstallDependenciesStreams(server, true)
assert.NoError(t, err)
// We _may_ have made a pty and stdout and stderr are the same and both send to the server as stdout
if stdout == stderr {
// osx behaves strangely reading and writing a pty in the same process, and we want to check that this
// is still a tty when invoking other processes so invoke the "test" program.
// We can't use the tty program because that tests stdin and we aren't setting stdin, but "test" can
// check if file descriptor 1 (i.e. stdout) is a tty with -t.
cmd := exec.Command("test", "-t", "1")
cmd.Stdin = nil
cmd.Stdout = stdout
cmd.Stderr = stdout
err := cmd.Run()
assert.NoError(t, err)
exitcode := cmd.ProcessState.ExitCode()
assert.Equal(t, 0, exitcode)
// Now check we can reuse the stream to echo some text back
text := "Lorem ipsum dolor sit amet, consectetur adipiscing elit," +
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n" +
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n" +
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n" +
"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n"
cmd = exec.Command("echo", text)
cmd.Stdin = nil
cmd.Stdout = stdout
cmd.Stderr = stdout
err = cmd.Run()
assert.NoError(t, err)
exitcode = cmd.ProcessState.ExitCode()
assert.Equal(t, 0, exitcode)
err = closer.Close()
assert.NoError(t, err)
outBytes, err := ioutil.ReadAll(&server.stdout)
assert.NoError(t, err)
// echo adds an extra \n at the end, and line discipline will cause \n to come back as \r\n
expected := strings.Replace(text+"\n", "\n", "\r\n", -1)
assert.Equal(t, []byte(expected), outBytes)
errBytes, err := ioutil.ReadAll(&server.stderr)
assert.NoError(t, err)
assert.Equal(t, []byte{}, errBytes)
} else {
// else they are separate and should behave just like the NoTerminal case
l, err := stdout.Write([]byte("hello"))
assert.NoError(t, err)
assert.Equal(t, 5, l)
l, err = stderr.Write([]byte("world"))
assert.NoError(t, err)
assert.Equal(t, 5, l)
err = closer.Close()
assert.NoError(t, err)
outBytes, err := ioutil.ReadAll(&server.stdout)
assert.NoError(t, err)
assert.Equal(t, []byte("hello"), outBytes)
errBytes, err := ioutil.ReadAll(&server.stderr)
assert.NoError(t, err)
assert.Equal(t, []byte("world"), errBytes)
}
}
func TestWriter_IsPTY(t *testing.T) {
t.Parallel()
server := makeStreamMock()
closer, stdout, stderr, err := MakeInstallDependenciesStreams(server, true)
assert.NoError(t, err)
// We _may_ have made a pty, check IsTerminal returns true
if stdout == stderr {
// These will be os.Files if a pty
file, ok := stdout.(*os.File)
assert.True(t, ok, "stdout was not a File")
assert.True(t, term.IsTerminal(int(file.Fd())), "stdout was not a terminal")
}
err = closer.Close()
assert.NoError(t, err)
}
func TestWriter_SafeToCloseTwice(t *testing.T) {
t.Parallel()
server := makeStreamMock()
closer, _, _, err := MakeInstallDependenciesStreams(server, true)
assert.NoError(t, err)
err = closer.Close()
assert.NoError(t, err)
err = closer.Close()
assert.NoError(t, err)
}