Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #56 from kdembler/lib-go
Browse files Browse the repository at this point in the history
Remove legacy check and change mocking behaviour
  • Loading branch information
IzabellaRaulin authored Aug 24, 2017
2 parents 34ae01d + dc4513c commit 918fa98
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
17 changes: 8 additions & 9 deletions cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,21 @@ type CPUCollector struct {
snapMetricsNames []string
}

// cpuInfo source of data for metrics
var cpuInfo = "/proc/stat"
// defaultProcPath source of data for metrics
var defaultProcPath = "/proc"

// New creates instance of interface info plugin
func New() *CPUCollector {
return &CPUCollector{
proc_path: cpuInfo,
proc_path: defaultProcPath + "/stat",
}
}

// GetConfigPolicy returns config policy
// It returns error in case retrieval was not successful
func (p *CPUCollector) GetConfigPolicy() (plugin.ConfigPolicy, error) {
policy := plugin.NewConfigPolicy()
policy.AddNewStringRule([]string{vendor, fs, Name}, "proc_path", false, plugin.SetDefaultString("/proc"))
policy.AddNewStringRule([]string{vendor, fs, Name}, "proc_path", false, plugin.SetDefaultString(defaultProcPath))

return *policy, nil
}
Expand Down Expand Up @@ -238,13 +238,12 @@ func (p *CPUCollector) CollectMetrics(mts []plugin.Metric) ([]plugin.Metric, err
}

func (p *CPUCollector) init(cfg plugin.Config) error {
if _, ok := cfg["proc_path"]; ok {
procPath, err := cfg.GetString("proc_path")
if err != nil {
procPath = "/proc"
}
procPath, err := cfg.GetString("proc_path")
if err == nil {
// change default if proc_path supplied
p.proc_path = procPath + "/stat"
}

fh, err := os.Open(p.proc_path)
if err != nil {
return err
Expand Down
14 changes: 8 additions & 6 deletions cpu/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (

type CPUInfoSuite struct {
suite.Suite
MockCPUInfo string
}

const (
Expand All @@ -53,10 +52,11 @@ const (
defaultFormatCpuStatIndex = 0
narrowFormatCpuStatIndex = 7
eightColumnCpuStatIndex = 8

mockPath = "MockCPUInfo"
)

func (cis *CPUInfoSuite) SetupSuite() {
cpuInfo = cis.MockCPUInfo
loadMockCPUInfo(0)
}

Expand All @@ -65,17 +65,19 @@ func (cis *CPUInfoSuite) TearDownSuite() {
}

func removeMockCPUInfo() {
os.Remove(cpuInfo)
os.Remove(mockPath)
}

func TestGetStatsSuite(t *testing.T) {
suite.Run(t, &CPUInfoSuite{MockCPUInfo: "MockCPUInfo"})
suite.Run(t, &CPUInfoSuite{})
}

func mockNew() *CPUCollector {
p := New()
p.proc_path = mockPath
emptyCfg := plugin.Config{}
p.init(emptyCfg)
err := p.init(emptyCfg)
So(err, ShouldBeNil)
So(p, ShouldNotBeNil)
So(p.snapMetricsNames, ShouldNotBeNil)
return p
Expand Down Expand Up @@ -129,7 +131,7 @@ func loadMockCPUInfo(dataSetNumber int) {
}

cpuInfoContent := []byte(content)
f, err := os.Create(cpuInfo)
f, err := os.Create(mockPath)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 918fa98

Please sign in to comment.