* Add file parsing for targets & fix multi targets in docker * Remove deprecated info in README & update examples
This commit is contained in:
committed by
GitHub
parent
145724bc95
commit
1ff17c429b
+146
@@ -1,14 +1,89 @@
|
||||
package cmrdr
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// Setup Mock
|
||||
type mockedFS struct {
|
||||
osFS
|
||||
|
||||
fileExists bool
|
||||
openError bool
|
||||
|
||||
fileMock *fileMock
|
||||
|
||||
fileSize int64
|
||||
}
|
||||
|
||||
// fileMock mocks a file
|
||||
type fileMock struct {
|
||||
mock.Mock
|
||||
|
||||
readError bool
|
||||
|
||||
bytes.Buffer
|
||||
}
|
||||
|
||||
type mockedFileInfo struct {
|
||||
os.FileInfo
|
||||
}
|
||||
|
||||
func (m mockedFileInfo) Size() int64 { return 1 }
|
||||
|
||||
func (m mockedFS) Stat(name string) (os.FileInfo, error) {
|
||||
if !m.fileExists {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
return mockedFileInfo{}, nil
|
||||
}
|
||||
|
||||
func (m mockedFS) Open(name string) (file, error) {
|
||||
if m.openError {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
|
||||
return m.fileMock, nil
|
||||
}
|
||||
|
||||
func (m *fileMock) Read(p []byte) (n int, err error) {
|
||||
if m.readError {
|
||||
return 0, os.ErrNotExist
|
||||
}
|
||||
return m.Buffer.Read(p)
|
||||
}
|
||||
|
||||
func (m *fileMock) ReadAt(p []byte, off int64) (n int, err error) {
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
func (m *fileMock) Seek(offset int64, whence int) (int64, error) {
|
||||
return offset, nil
|
||||
}
|
||||
|
||||
func (m *fileMock) Stat() (os.FileInfo, error) {
|
||||
return mockedFileInfo{}, nil
|
||||
}
|
||||
|
||||
// Close mock
|
||||
func (m *fileMock) Close() error {
|
||||
args := m.Called()
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
// Sync mock
|
||||
func (m *fileMock) Sync() error {
|
||||
args := m.Called()
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func TestLoadCredentials(t *testing.T) {
|
||||
credentialsJSONString := []byte("{\"usernames\":[\"admin\",\"root\"],\"passwords\":[\"12345\",\"root\"]}")
|
||||
validCredentials := Credentials{
|
||||
@@ -260,3 +335,74 @@ func TestParseRoutesFromString(t *testing.T) {
|
||||
assert.Equal(t, test.expectedResult, parsedRoutes, "unexpected result, parse error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTargetsFile(t *testing.T) {
|
||||
|
||||
oldFS := fs
|
||||
mfs := &mockedFS{}
|
||||
fs = mfs
|
||||
defer func() {
|
||||
fs = oldFS
|
||||
}()
|
||||
|
||||
testCases := []struct {
|
||||
input string
|
||||
|
||||
fileExists bool
|
||||
openError bool
|
||||
readError bool
|
||||
|
||||
expectedResult string
|
||||
expectedError error
|
||||
}{
|
||||
{
|
||||
input: "0.0.0.0",
|
||||
|
||||
fileExists: false,
|
||||
|
||||
expectedResult: "0.0.0.0",
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
input: "test_does_not_really_exist",
|
||||
|
||||
fileExists: true,
|
||||
|
||||
expectedResult: "0.0.0.0 localhost 192.17.0.0/16 192.168.1.140-255 192.168.2-3.0-255",
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
input: "test_does_not_really_exist",
|
||||
|
||||
fileExists: true,
|
||||
openError: true,
|
||||
|
||||
expectedResult: "test_does_not_really_exist",
|
||||
expectedError: os.ErrNotExist,
|
||||
},
|
||||
{
|
||||
input: "test_does_not_really_exist",
|
||||
|
||||
fileExists: true,
|
||||
readError: true,
|
||||
|
||||
expectedResult: "test_does_not_really_exist",
|
||||
expectedError: os.ErrNotExist,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
mfs.fileExists = test.fileExists
|
||||
mfs.openError = test.openError
|
||||
|
||||
mfs.fileMock = &fileMock{
|
||||
readError: test.readError,
|
||||
}
|
||||
mfs.fileMock.On("Close").Return(nil)
|
||||
mfs.fileMock.WriteString("0.0.0.0 localhost 192.17.0.0/16 192.168.1.140-255 192.168.2-3.0-255")
|
||||
|
||||
result, err := ParseTargetsFile(test.input)
|
||||
assert.Equal(t, test.expectedResult, result, "unexpected result, parse error")
|
||||
assert.Equal(t, test.expectedError, err, "unexpected error")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user