forked from jirs5/gosigar-freebsd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor test cases to use the standard testing package and testify.
Add test coverage reporting from AppVeyor and Travis. Change package name used in file from sigar to gosigar. Separate examples into separate directories so that multiple main functions do not exist in the same package.
- Loading branch information
1 parent
276ecb4
commit aa48a5e
Showing
23 changed files
with
532 additions
and
588 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,33 @@ | ||
.vagrant | ||
# Directories | ||
/.vagrant | ||
/.idea | ||
/build | ||
|
||
# Files | ||
.DS_Store | ||
/*.iml | ||
|
||
# Editor swap files | ||
*.swp | ||
*.swo | ||
*.swn | ||
|
||
# Compiled Object files, Static and Dynamic libs (Shared Objects) | ||
*.o | ||
*.a | ||
*.so | ||
*.exe | ||
*.test | ||
*.prof | ||
*.pyc | ||
*.swp | ||
|
||
# Example binaries | ||
examples/df/df | ||
examples/df/df.exe | ||
examples/free/free | ||
examples/free/free.exe | ||
examples/ps/ps | ||
examples/ps/ps.exe | ||
examples/uptime/uptime | ||
examples/uptime/uptime.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package sigar | ||
package gosigar | ||
|
||
import ( | ||
"time" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,76 @@ | ||
package sigar_test | ||
package gosigar_test | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
sigar "github.com/elastic/gosigar" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var _ = Describe("ConcreteSigar", func() { | ||
var concreteSigar *sigar.ConcreteSigar | ||
|
||
BeforeEach(func() { | ||
concreteSigar = &sigar.ConcreteSigar{} | ||
}) | ||
|
||
Describe("CollectCpuStats", func() { | ||
It("immediately makes first CPU usage available even though it's not very accurate", func() { | ||
samplesCh, stop := concreteSigar.CollectCpuStats(500 * time.Millisecond) | ||
|
||
firstValue := <-samplesCh | ||
Expect(firstValue.User).To(BeNumerically(">", 0)) | ||
|
||
stop <- struct{}{} | ||
}) | ||
|
||
It("makes CPU usage delta values available", func() { | ||
samplesCh, stop := concreteSigar.CollectCpuStats(500 * time.Millisecond) | ||
|
||
firstValue := <-samplesCh | ||
|
||
secondValue := <-samplesCh | ||
Expect(secondValue.User).To(BeNumerically("<", firstValue.User)) | ||
|
||
stop <- struct{}{} | ||
}) | ||
|
||
It("does not block", func() { | ||
_, stop := concreteSigar.CollectCpuStats(10 * time.Millisecond) | ||
|
||
// Sleep long enough for samplesCh to fill at least 2 values | ||
time.Sleep(20 * time.Millisecond) | ||
|
||
stop <- struct{}{} | ||
|
||
// If CollectCpuStats blocks it will never get here | ||
Expect(true).To(BeTrue()) | ||
}) | ||
}) | ||
|
||
It("GetLoadAverage", func() { | ||
avg, err := concreteSigar.GetLoadAverage() | ||
Expect(avg.One).ToNot(BeNil()) | ||
Expect(avg.Five).ToNot(BeNil()) | ||
Expect(avg.Fifteen).ToNot(BeNil()) | ||
|
||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
|
||
It("GetMem", func() { | ||
mem, err := concreteSigar.GetMem() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
Expect(mem.Total).To(BeNumerically(">", 0)) | ||
Expect(mem.Used + mem.Free).To(BeNumerically("<=", mem.Total)) | ||
}) | ||
|
||
It("GetSwap", func() { | ||
swap, err := concreteSigar.GetSwap() | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(swap.Used + swap.Free).To(BeNumerically("<=", swap.Total)) | ||
}) | ||
|
||
It("GetSwap", func() { | ||
fsusage, err := concreteSigar.GetFileSystemUsage("/") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(fsusage.Total).ToNot(BeNil()) | ||
|
||
fsusage, err = concreteSigar.GetFileSystemUsage("T O T A L L Y B O G U S") | ||
Expect(err).To(HaveOccurred()) | ||
Expect(fsusage.Total).To(Equal(uint64(0))) | ||
}) | ||
}) | ||
func TestConcreteCollectCpuStats(t *testing.T) { | ||
concreteSigar := &sigar.ConcreteSigar{} | ||
|
||
// Immediately makes first CPU usage available even though it's not very accurate. | ||
samplesCh, stop := concreteSigar.CollectCpuStats(500 * time.Millisecond) | ||
firstValue := <-samplesCh | ||
assert.True(t, firstValue.User > 0) | ||
stop <- struct{}{} | ||
|
||
// Makes CPU usage delta values available | ||
samplesCh, stop = concreteSigar.CollectCpuStats(500 * time.Millisecond) | ||
firstValue = <-samplesCh | ||
secondValue := <-samplesCh | ||
assert.True(t, secondValue.User < firstValue.User) | ||
stop <- struct{}{} | ||
|
||
// Does not block. | ||
_, stop = concreteSigar.CollectCpuStats(10 * time.Millisecond) | ||
// Sleep long enough for samplesCh to fill at least 2 values | ||
time.Sleep(20 * time.Millisecond) | ||
stop <- struct{}{} | ||
} | ||
|
||
func TestConcreteGetLoadAverage(t *testing.T) { | ||
concreteSigar := &sigar.ConcreteSigar{} | ||
avg, err := concreteSigar.GetLoadAverage() | ||
if assert.NoError(t, err) { | ||
assert.NotNil(t, avg.One) | ||
assert.NotNil(t, avg.Five) | ||
assert.NotNil(t, avg.Fifteen) | ||
} | ||
} | ||
|
||
func TestConcreteGetMem(t *testing.T) { | ||
concreteSigar := &sigar.ConcreteSigar{} | ||
mem, err := concreteSigar.GetMem() | ||
if assert.NoError(t, err) { | ||
assert.True(t, mem.Total > 0) | ||
assert.True(t, mem.Used+mem.Free <= mem.Total) | ||
} | ||
} | ||
|
||
func TestConcreteGetSwap(t *testing.T) { | ||
concreteSigar := &sigar.ConcreteSigar{} | ||
swap, err := concreteSigar.GetSwap() | ||
if assert.NoError(t, err) { | ||
assert.True(t, swap.Used+swap.Free <= swap.Total) | ||
} | ||
} | ||
|
||
func TestConcreteFileSystemUsage(t *testing.T) { | ||
root := "/" | ||
if runtime.GOOS == "windows" { | ||
root = "C:\\" | ||
} | ||
|
||
concreteSigar := &sigar.ConcreteSigar{} | ||
fsusage, err := concreteSigar.GetFileSystemUsage(root) | ||
if assert.NoError(t, err, "Error is %v", err) { | ||
assert.True(t, fsusage.Total > 0) | ||
} | ||
|
||
fsusage, err = concreteSigar.GetFileSystemUsage("T O T A L L Y B O G U S") | ||
assert.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.