Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux_Wireless Plugin #3650

Closed
wants to merge 15 commits into from
Prev Previous commit
Next Next commit
updates
  • Loading branch information
David G. Simmons committed Apr 3, 2018
commit 6f8c8ae7f69651470bd5ba7e5fe1bcf655d6709f
143 changes: 65 additions & 78 deletions plugins/inputs/linux_battery/linux_battery_test.go
Original file line number Diff line number Diff line change
@@ -1,89 +1,76 @@
package linux_battery
package linux_wireless

import (
"net/http"
"net/http/httptest"
"strings"
//"fmt"
"testing"

"github.com/influxdata/telegraf/testutil"
)



func TestNewItem(t *testing.T) {
t.Parallel()
var acc testutil.Accumulator
rb := &ParticleWebhook{Path: "/particle", acc: &acc}
resp := postWebhooks(rb, NewItemJSON())
if resp.Code != http.StatusOK {
t.Errorf("POST new_item returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
func TestLoadWirelessTable(t *testing.T) {
// line of input
input := `Inter-| sta-| Quality | Discarded packets | Missed | WE
face | tus | link level noise | nwid crypt frag retry misc | beacon | 22
wlan0: 0000 0. -256. -256. 0 0 0 0 0 0`
// the headers we expect from that line of input
headers := []string{"status", "quality_link", "quality_level", "quality_noise", "discarded_packets_nwid", "discarded_packets_crypt",
"discarded_packets_frag", "discarded_packets_retry", "discarded_packets_misc", "missed_beacon", "we_22"}
// the map of data we expect.
parsed := map[string]interface{}{
"status": int64(0),
"quality_link": int64(0),
"quality_level": int64(-256),
"quality_noise": int64(-256),
"discarded_packets_nwid": int64(0),
"discarded_packets_crypt": int64(0),
"discarded_packets_frag": int64(0),
"discarded_packets_retry": int64(0),
"discarded_packets_misc": int64(0),
"missed_beacon": int64(0),
"we_22": int64(0),
}

fields := map[string]interface{}{
"temp_c": 26.680000,
"temp_f": 80.024001,
"infrared": 528.0,
"lux": 0.0,
"humidity": 44.937500,
"pressure": 998.998901,
"altitude": 119.331436,
"broadband": 1266.0,
// the tags we expect
test_tags := map[string]interface{}{
"interface": "wlan0:",
}
// Map of the entries we get back from the table
entries := map[string]interface{}{}

tags := map[string]string{
"id": "230035001147343438323536",
"location": "TravelingWilbury",
// load the table from the input.
got, err := loadWirelessTable([]byte(input), true)
if err != nil {
t.Fatal(err)
}

acc.AssertContainsTaggedFields(t, "temperature", fields, tags)
}

func TestUnknowItem(t *testing.T) {
t.Parallel()
var acc testutil.Accumulator
rb := &ParticleWebhook{Path: "/particle", acc: &acc}
resp := postWebhooks(rb, UnknowJSON())
if resp.Code != http.StatusOK {
t.Errorf("POST unknown returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
// the WirelessData struct holds arrays of the values, so
// move them into appropriate maps.
tags := map[string]string{}
for x := 0; x < len(got.Tags); x++ {
entries := map[string]interface{}{}
tags = map[string]string{
"interface": got.Tags[x],
}
for z := 0; z < len(got.Data[x]); z++ {
entries[got.Headers[z]] = got.Data[x][z]
//fmt.Println(entries[got.Headers[z]])
}
}
// make sure we got the same number of headers back we expect.
if len(got.Headers) != len(headers) {
t.Fatalf("want %+v, got %+v", headers, got.Headers)
}
// create the data map
for z := 0; z < len(got.Data[0]); z++ {
entries[got.Headers[z]] = got.Data[0][z]
//fmt.Println(entries[got.Headers[z]])
}
// verify the data map
for key := range parsed {
if parsed[key] != entries[key] {
t.Fatalf("want %+v, got %+v", parsed[key], entries[key])
}
}
// verify the tag map
for key := range tags {
if test_tags[key] != tags[key] {
t.Fatalf("want %+v, got %+v", test_tags[key], tags[key])
}
}
}

func NewItemJSON() string {
return `
{
"event": "temperature",
"data": {
"tags": {
"id": "230035001147343438323536",
"location": "TravelingWilbury"
},
"values": {
"temp_c": 26.680000,
"temp_f": 80.024001,
"humidity": 44.937500,
"pressure": 998.998901,
"altitude": 119.331436,
"broadband": 1266.0,
"infrared": 528.0,
"lux": 0.0
}
},
"ttl": 60,
"published_at": "2017-09-28T21:54:10.897Z",
"coreid": "123456789938323536",
"userid": "1234ee123ac8e5ec1231a123d",
"version": 10,
"public": false,
"productID": 1234,
"name": "sensor",
"measurement": "mydata"
}`
}

func UnknowJSON() string {
return `
{
"event": "roger"
}`
}
1 change: 0 additions & 1 deletion plugins/inputs/linux_wireless/linux_wireless.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package linux_wireless

import (
//"fmt"
"github.com/influxdata/telegraf"
davidgs marked this conversation as resolved.
Show resolved Hide resolved
"github.com/influxdata/telegraf/plugins/inputs"
"io/ioutil"
Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/linux_wireless/linux_wireless_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package linux_wireless

import (
"fmt"
//"fmt"
"testing"
)

Expand Down Expand Up @@ -49,7 +49,7 @@ func TestLoadWirelessTable(t *testing.T) {
}
for z := 0; z < len(got.Data[x]); z++ {
entries[got.Headers[z]] = got.Data[x][z]
fmt.Println(entries[got.Headers[z]])
//fmt.Println(entries[got.Headers[z]])
}
}
// make sure we got the same number of headers back we expect.
Expand All @@ -59,7 +59,7 @@ func TestLoadWirelessTable(t *testing.T) {
// create the data map
for z := 0; z < len(got.Data[0]); z++ {
entries[got.Headers[z]] = got.Data[0][z]
fmt.Println(entries[got.Headers[z]])
//fmt.Println(entries[got.Headers[z]])
}
// verify the data map
for key := range parsed {
Expand Down