Added unit tests
This commit is contained in:
parent
6a99a96af8
commit
87f34a6f3a
2
Godeps
2
Godeps
|
@ -28,6 +28,7 @@ github.com/hashicorp/consul 5aa90455ce78d4d41578bafc86305e6e6b28d7d2
|
|||
github.com/hpcloud/tail b2940955ab8b26e19d43a43c4da0475dd81bdb56
|
||||
github.com/influxdata/config b79f6829346b8d6e78ba73544b1e1038f1f1c9da
|
||||
github.com/influxdata/influxdb e094138084855d444195b252314dfee9eae34cab
|
||||
github.com/influxdata/telegraf 59e981d971ee6b1f97427b7cf2f15783234d59ca
|
||||
github.com/influxdata/toml af4df43894b16e3fd2b788d01bd27ad0776ef2d0
|
||||
github.com/klauspost/crc32 19b0b332c9e4516a6370a0456e6182c3b5036720
|
||||
github.com/lib/pq e182dc4027e2ded4b19396d638610f2653295f36
|
||||
|
@ -38,7 +39,6 @@ github.com/naoina/go-stringutil 6b638e95a32d0c1131db0e7fe83775cbea4a0d0b
|
|||
github.com/nats-io/nats b13fc9d12b0b123ebc374e6b808c6228ae4234a3
|
||||
github.com/nats-io/nuid 4f84f5f3b2786224e336af2e13dba0a0a80b76fa
|
||||
github.com/nsqio/go-nsq 0b80d6f05e15ca1930e0c5e1d540ed627e299980
|
||||
github.com/opencontainers/runc 89ab7f2ccc1e45ddf6485eaa802c35dcf321dfc8
|
||||
github.com/prometheus/client_golang 18acf9993a863f4c4b40612e19cdd243e7c86831
|
||||
github.com/prometheus/client_model fa8ad6fec33561be4280a8f0514318c79d7f6cb6
|
||||
github.com/prometheus/common e8eabff8812b05acf522b45fdcd725a785188e37
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* @Author: Jim Weber
|
||||
* @Date: 2016-05-18 22:07:31
|
||||
* @Last Modified by: Jim Weber
|
||||
* @Last Modified time: 2016-08-07 20:20:26
|
||||
* @Last Modified time: 2016-08-08 14:09:03
|
||||
*/
|
||||
|
||||
package fleet
|
||||
|
@ -145,6 +145,11 @@ func getContainerCount(fleetUnits FleetStates) map[string]int {
|
|||
containerCount := make(map[string]int)
|
||||
for _, fleetUnit := range fleetUnits.States {
|
||||
shortNameParts := strings.Split(fleetUnit.Name, "@")
|
||||
if len(shortNameParts) == 0 {
|
||||
// global units do not use the '@' symbol because they do not have instance ids
|
||||
// instead just split off the .server porition
|
||||
shortNameParts = strings.Split(fleetUnit.Name, ".")
|
||||
}
|
||||
shortName := shortNameParts[0]
|
||||
if fleetUnit.SystemdSubState == "running" {
|
||||
containerCount[shortName]++
|
|
@ -0,0 +1,205 @@
|
|||
/*
|
||||
* @Author: Jim Weber
|
||||
* @Date: 2016-08-08 09:42:04
|
||||
* @Last Modified by: Jim Weber
|
||||
* @Last Modified time: 2016-08-08 14:09:59
|
||||
*/
|
||||
|
||||
package fleet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, fleetResponseData)
|
||||
}))
|
||||
|
||||
var fleetResponseData = `{
|
||||
"states": [
|
||||
{
|
||||
"hash": "67e33cb6ce9104fa451765128159eaccad11dee8",
|
||||
"machineID": "2d69b20e090a4859b2c9ec7d48b0188c",
|
||||
"name": "auth-api@34.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "67e33cb6ce9104fa451765128159eaccad11dee8",
|
||||
"machineID": "68d18238915842298dc6cd3b90824237",
|
||||
"name": "auth-api@35.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "1ebad1f4aa1b11af59c12e3d0fa58985807b54bb",
|
||||
"machineID": "635a42fc35b241ffa170c1dc1befa01c",
|
||||
"name": "ident@34.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "8a053bc6517baf8473d0ea7872acbd8c31dba0f8",
|
||||
"machineID": "39515ef8debc423c961543d45e382c63",
|
||||
"name": "help-api@55.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "30bf8d8bb392eb65497f2d0e4ea508401054949c",
|
||||
"machineID": "885814e701d94d67bd1264fb1b9c9958",
|
||||
"name": "fixer@50.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "30bf8d8bb392eb65497f2d0e4ea508401054949c",
|
||||
"machineID": "39515ef8debc423c961543d45e382c63",
|
||||
"name": "fixer@51.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "67cc24e573c05fba29de2bbc5cc4b522601ffcf4",
|
||||
"machineID": "2d69b20e090a4859b2c9ec7d48b0188c",
|
||||
"name": "logspout.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "67cc24e573c05fba29de2bbc5cc4b522601ffcf4",
|
||||
"machineID": "39515ef8debc423c961543d45e382c63",
|
||||
"name": "logspout.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "67cc24e573c05fba29de2bbc5cc4b522601ffcf4",
|
||||
"machineID": "635a42fc35b241ffa170c1dc1befa01c",
|
||||
"name": "logspout.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "67cc24e573c05fba29de2bbc5cc4b522601ffcf4",
|
||||
"machineID": "68d18238915842298dc6cd3b90824237",
|
||||
"name": "logspout.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "67cc24e573c05fba29de2bbc5cc4b522601ffcf4",
|
||||
"machineID": "885814e701d94d67bd1264fb1b9c9958",
|
||||
"name": "logspout.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "e6cd43573b54647d4508617f98ed6bae9db1be18",
|
||||
"machineID": "2d69b20e090a4859b2c9ec7d48b0188c",
|
||||
"name": "logstash@56.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "c40cddaed92a845a8ac93eccdc7a5a5517697816",
|
||||
"machineID": "635a42fc35b241ffa170c1dc1befa01c",
|
||||
"name": "logstash-serverlogs@10.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "f9a0c0c9f105bfac4133d5f23856146b27c48931",
|
||||
"machineID": "68d18238915842298dc6cd3b90824237",
|
||||
"name": "nginx@19.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "f9a0c0c9f105bfac4133d5f23856146b27c48931",
|
||||
"machineID": "39515ef8debc423c961543d45e382c63",
|
||||
"name": "nginx@20.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "failed"
|
||||
},
|
||||
{
|
||||
"hash": "7a2914683ef7bae3576bd1e48269839349f58752",
|
||||
"machineID": "885814e701d94d67bd1264fb1b9c9958",
|
||||
"name": "nginx@18.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "7a2914683ef7bae3576bd1e48269839349f58752",
|
||||
"machineID": "68d18238915842298dc6cd3b90824237",
|
||||
"name": "nginx@19.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "8a6b627f67b6ab113f083bef1d7c2e583a12eea5",
|
||||
"machineID": "39515ef8debc423c961543d45e382c63",
|
||||
"name": "weave.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "8a6b627f67b6ab113f083bef1d7c2e583a12eea5",
|
||||
"machineID": "635a42fc35b241ffa170c1dc1befa01c",
|
||||
"name": "weave.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
},
|
||||
{
|
||||
"hash": "8a6b627f67b6ab113f083bef1d7c2e583a12eea5",
|
||||
"machineID": "68d18238915842298dc6cd3b90824237",
|
||||
"name": "weave.service",
|
||||
"systemdActiveState": "active",
|
||||
"systemdLoadState": "loaded",
|
||||
"systemdSubState": "running"
|
||||
}
|
||||
]
|
||||
}`
|
||||
|
||||
func TestGetInstanceStates(t *testing.T) {
|
||||
fleetStates := getInstanceStates(ts.URL, nil)
|
||||
if fleetStates.States[0].MachineID != "2d69b20e090a4859b2c9ec7d48b0188c" {
|
||||
t.Errorf("First machine id json response to be 2d69b20e090a4859b2c9ec7d48b0188c got %v instead", fleetStates.States[0].MachineID)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestGetContainerCount(t *testing.T) {
|
||||
fleetStates := getInstanceStates(ts.URL, nil)
|
||||
containerCounts := getContainerCount(fleetStates)
|
||||
|
||||
if containerCounts["auth-api"] != 2 {
|
||||
t.Errorf("Auth api count is incorrect got %d instead of 2", containerCounts["auth-api"])
|
||||
}
|
||||
|
||||
if containerCounts["nginx"] != 3 {
|
||||
t.Errorf("nginx count is incorrect got %d instead of 4", containerCounts["nginx"])
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue