-
Notifications
You must be signed in to change notification settings - Fork 126
/
command_builder_test.go
98 lines (82 loc) · 2.56 KB
/
command_builder_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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 firecracker
import (
"bytes"
"context"
"reflect"
"testing"
)
func TestVMCommandBuilder(t *testing.T) {
t.Run("immutability", testVMCommandBuilderImmutable)
t.Run("chaining", testVMCommandBuilderChaining)
t.Run("build", testVMCommandBuilderBuild)
}
func testVMCommandBuilderImmutable(t *testing.T) {
b := VMCommandBuilder{}
b.WithSocketPath("foo").
WithArgs([]string{"baz", "qux"}).
AddArgs("moo", "cow")
if e, a := []string(nil), b.SocketPath(); !reflect.DeepEqual(e, a) {
t.Errorf("expected immutable builder, but socket path was set: %q", a)
}
if e, a := ([]string)(nil), b.Args(); !reflect.DeepEqual(e, a) {
t.Errorf("args should have been empty, but received %v", a)
}
}
func testVMCommandBuilderChaining(t *testing.T) {
b := VMCommandBuilder{}.
WithSocketPath("socket-path").
WithBin("bin")
if e, a := []string{"--api-sock", "socket-path"}, b.SocketPath(); !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, but received %v", e, a)
}
if e, a := "bin", b.Bin(); e != a {
t.Errorf("expected %v, but received %v", e, a)
}
}
func testVMCommandBuilderBuild(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
stdin := &bytes.Buffer{}
ctx := context.Background()
b := VMCommandBuilder{}.
WithSocketPath("socket-path").
WithBin("bin").
WithStdout(stdout).
WithStderr(stderr).
WithStdin(stdin).
WithArgs([]string{"foo"}).
AddArgs("--bar", "baz")
cmd := b.Build(ctx)
expectedArgs := []string{
"bin",
"--api-sock",
"socket-path",
"foo",
"--bar",
"baz",
}
if e, a := stdout, cmd.Stdout; !reflect.DeepEqual(e, a) {
t.Errorf("expected stdout, %v, but received %v", e, a)
}
if e, a := stderr, cmd.Stderr; !reflect.DeepEqual(e, a) {
t.Errorf("expected stderr, %v, but received %v", e, a)
}
if e, a := stdin, cmd.Stdin; !reflect.DeepEqual(e, a) {
t.Errorf("expected stdin, %v, but received %v", e, a)
}
if e, a := expectedArgs, cmd.Args; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, but received an invalid set of arguments %v", e, a)
}
}