use gomock
This commit is contained in:
parent
32ae136095
commit
de867f19af
35
utils/fs/mockfs/fakefile.go
Normal file
35
utils/fs/mockfs/fakefile.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
// Copyright 2014 Google Inc. 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.
|
||||||
|
// 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 mockfs
|
||||||
|
|
||||||
|
import "bytes"
|
||||||
|
|
||||||
|
type FakeFile struct {
|
||||||
|
bytes.Buffer
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *FakeFile) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddTextFile(mockfs *MockFileSystem, name, content string) *FakeFile {
|
||||||
|
f := &FakeFile{
|
||||||
|
Name: name,
|
||||||
|
Buffer: *bytes.NewBufferString(content),
|
||||||
|
}
|
||||||
|
mockfs.EXPECT().Open(name).Return(f, nil).AnyTimes()
|
||||||
|
return f
|
||||||
|
}
|
@ -1,65 +1,41 @@
|
|||||||
// Copyright 2014 Google Inc. All Rights Reserved.
|
// Automatically generated by MockGen. DO NOT EDIT!
|
||||||
//
|
// Source: github.com/google/cadvisor/utils/fs (interfaces: FileSystem)
|
||||||
// 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 mockfs
|
package mockfs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
gomock "code.google.com/p/gomock/gomock"
|
||||||
"fmt"
|
fs "github.com/google/cadvisor/utils/fs"
|
||||||
|
|
||||||
"github.com/google/cadvisor/utils/fs"
|
|
||||||
"github.com/stretchr/testify/mock"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type MockFile struct {
|
// Mock of FileSystem interface
|
||||||
mock.Mock
|
|
||||||
bytes.Buffer
|
|
||||||
checkClose bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *MockFile) CheckClose() *MockFile {
|
|
||||||
self.checkClose = true
|
|
||||||
return self
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *MockFile) Close() error {
|
|
||||||
if self.checkClose {
|
|
||||||
args := self.Called()
|
|
||||||
return args.Error(0)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type MockFileSystem struct {
|
type MockFileSystem struct {
|
||||||
mock.Mock
|
ctrl *gomock.Controller
|
||||||
files map[string]*MockFile
|
recorder *_MockFileSystemRecorder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *MockFileSystem) AddTextFile(name, content string) *MockFile {
|
// Recorder for MockFileSystem (not exported)
|
||||||
if self.files == nil {
|
type _MockFileSystemRecorder struct {
|
||||||
self.files = make(map[string]*MockFile, 4)
|
mock *MockFileSystem
|
||||||
}
|
|
||||||
f := &MockFile{
|
|
||||||
Buffer: *bytes.NewBufferString(content),
|
|
||||||
}
|
|
||||||
self.files[name] = f
|
|
||||||
return f
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *MockFileSystem) Open(name string) (fs.File, error) {
|
func NewMockFileSystem(ctrl *gomock.Controller) *MockFileSystem {
|
||||||
if f, ok := self.files[name]; ok {
|
mock := &MockFileSystem{ctrl: ctrl}
|
||||||
return f, nil
|
mock.recorder = &_MockFileSystemRecorder{mock}
|
||||||
|
return mock
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("cannot open file %v", name)
|
|
||||||
|
func (_m *MockFileSystem) EXPECT() *_MockFileSystemRecorder {
|
||||||
|
return _m.recorder
|
||||||
|
}
|
||||||
|
|
||||||
|
func (_m *MockFileSystem) Open(_param0 string) (fs.File, error) {
|
||||||
|
ret := _m.ctrl.Call(_m, "Open", _param0)
|
||||||
|
ret0, _ := ret[0].(fs.File)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (_mr *_MockFileSystemRecorder) Open(arg0 interface{}) *gomock.Call {
|
||||||
|
return _mr.mock.ctrl.RecordCall(_mr.mock, "Open", arg0)
|
||||||
}
|
}
|
||||||
|
@ -19,13 +19,18 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"code.google.com/p/gomock/gomock"
|
||||||
|
|
||||||
"github.com/google/cadvisor/utils/fs"
|
"github.com/google/cadvisor/utils/fs"
|
||||||
"github.com/google/cadvisor/utils/fs/mockfs"
|
"github.com/google/cadvisor/utils/fs/mockfs"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestReadProcessSchedStat(t *testing.T) {
|
func TestReadProcessSchedStat(t *testing.T) {
|
||||||
|
|
||||||
mockfs := &mockfs.MockFileSystem{}
|
mockCtrl := gomock.NewController(t)
|
||||||
|
defer mockCtrl.Finish()
|
||||||
|
|
||||||
|
mfs := mockfs.NewMockFileSystem(mockCtrl)
|
||||||
|
|
||||||
pid := 10
|
pid := 10
|
||||||
|
|
||||||
@ -38,8 +43,8 @@ func TestReadProcessSchedStat(t *testing.T) {
|
|||||||
|
|
||||||
path := fmt.Sprintf("/proc/%v/schedstat", pid)
|
path := fmt.Sprintf("/proc/%v/schedstat", pid)
|
||||||
content := fmt.Sprintf("%v %v %v\n", stat.Running, stat.RunWait, stat.NumTimeSlices)
|
content := fmt.Sprintf("%v %v %v\n", stat.Running, stat.RunWait, stat.NumTimeSlices)
|
||||||
mockfs.AddTextFile(path, content)
|
mockfs.AddTextFile(mfs, path, content)
|
||||||
fs.ChangeFileSystem(mockfs)
|
fs.ChangeFileSystem(mfs)
|
||||||
|
|
||||||
receivedStat := &ProcessSchedStat{}
|
receivedStat := &ProcessSchedStat{}
|
||||||
err := receivedStat.Add(pid)
|
err := receivedStat.Add(pid)
|
||||||
|
Loading…
Reference in New Issue
Block a user