/
mock_machine.go
130 lines (107 loc) · 3.04 KB
/
mock_machine.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
// Copyright 2019 Shanghai JingDuo Information Technology co., Ltd.
//
// 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 machine
import (
"fmt"
"io"
"strings"
dockerclient "github.com/docker/docker/client"
pb "github.com/kpaas-io/kpaas/pkg/deploy/protos"
)
var (
errMachineErr = fmt.Errorf("this is an error machine")
//IsTesting only set it in test package
IsTesting = false
)
type MockMachine struct {
*pb.Node
DockerClient *dockerclient.Client
}
func newMockMachine(node *pb.Node) (IMachine, error) {
if node.Name == "error" {
return nil, errMachineErr
}
return &MockMachine{
Node: node,
}, nil
}
func (m *MockMachine) GetName() string {
return m.Name
}
func (m *MockMachine) GetIp() string {
return m.Ip
}
func (m *MockMachine) Close() {}
func (m *MockMachine) StartDockerTunnel() error {
return nil
}
// Run return different response by node name
func (m *MockMachine) Run(cmd string) (stdout, stderr []byte, err error) {
if m.Name == "error" {
err = fmt.Errorf("this is an error machine")
}
switch {
case m.Name == "error":
return nil, nil, errMachineErr
case strings.HasPrefix(cmd, "cat /proc/cpuinfo"):
return []byte("8"), nil, nil
case strings.HasPrefix(cmd, "docker version"):
return []byte("18.09.0"), nil, nil
case strings.HasPrefix(cmd, "uname -r"):
return []byte("5.18.5-041805-generic"), nil, nil
case strings.HasPrefix(cmd, "free -b"):
return []byte("270455574528"), nil, nil
case strings.HasPrefix(cmd, "df -B1"):
return []byte("294605168640"), nil, nil
case strings.HasPrefix(cmd, "ps -p 1"):
return []byte("systemd"), nil, nil
case strings.HasPrefix(cmd, "cat /etc/*-release"):
return []byte("ubuntu"), nil, nil
}
return []byte(""), []byte(""), nil
}
func (m *MockMachine) FetchDir(localDir, remoteDir string, fileNeeded func(path string) bool) error {
if m.Name == "error" {
return errMachineErr
}
return nil
}
func (m *MockMachine) FetchFile(dst io.Writer, remotePath string) error {
if m.Name == "error" {
return errMachineErr
}
dst.Write([]byte("this is test data"))
return nil
}
func (m *MockMachine) FetchFileToLocalPath(localPath, remotePath string) error {
if m.Name == "error" {
return errMachineErr
}
return nil
}
func (m *MockMachine) PutDir(localDir, remoteDir string, fileNeeded func(path string) bool) error {
if m.Name == "error" {
return errMachineErr
}
return nil
}
func (m *MockMachine) PutFile(content io.Reader, remotePath string) error {
if m.Name == "error" {
return errMachineErr
}
return nil
}
func (m *MockMachine) GetNode() *pb.Node {
return m.Node
}