mirror of
https://github.com/empayre/fleet.git
synced 2024-11-07 17:28:54 +00:00
404afe3e22
* Fix issue where config interval can be number or string * Implemented @groob code review suggestions * Added type assertions with graceful failure if something slips through validation * Implemented code review changes per @zwass
131 lines
2.9 KiB
Go
131 lines
2.9 KiB
Go
package kolide
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestIntervalUnmarshal(t *testing.T) {
|
|
scenarios := []struct {
|
|
name string
|
|
testVal interface{}
|
|
errExpected bool
|
|
expectedResult OsQueryConfigInt
|
|
}{
|
|
{"string to uint", "100", false, 100},
|
|
{"float to uint", float64(123), false, 123},
|
|
{"nil to zero value int", nil, false, 0},
|
|
{"invalid string", "hi there", true, 0},
|
|
}
|
|
for _, scenario := range scenarios {
|
|
t.Run(fmt.Sprintf(": %s", scenario.name), func(tt *testing.T) {
|
|
v, e := unmarshalInteger(scenario.testVal)
|
|
if scenario.errExpected {
|
|
assert.NotNil(t, e)
|
|
} else {
|
|
require.Nil(t, e)
|
|
assert.Equal(t, scenario.expectedResult, v)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type importIntTest struct {
|
|
Val OsQueryConfigInt `json:"val"`
|
|
}
|
|
|
|
func TestConfigImportInt(t *testing.T) {
|
|
buff := bytes.NewBufferString(`{"val":"23"}`)
|
|
var ts importIntTest
|
|
err := json.NewDecoder(buff).Decode(&ts)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, 23, int(ts.Val))
|
|
|
|
buff = bytes.NewBufferString(`{"val":456}`)
|
|
err = json.NewDecoder(buff).Decode(&ts)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, 456, int(ts.Val))
|
|
|
|
buff = bytes.NewBufferString(`{"val":"hi 456"}`)
|
|
err = json.NewDecoder(buff).Decode(&ts)
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
func TestPackNameMapUnmarshal(t *testing.T) {
|
|
s2p := func(s string) *string { return &s }
|
|
u2p := func(ui uint) *OsQueryConfigInt { ci := OsQueryConfigInt(ui); return &ci }
|
|
|
|
pnm := PackNameMap{
|
|
"path": "/this/is/a/path",
|
|
"details": PackDetails{
|
|
Queries: QueryNameToQueryDetailsMap{
|
|
"q1": QueryDetails{
|
|
Query: "select from foo",
|
|
Interval: 100,
|
|
Removed: new(bool),
|
|
Platform: s2p("linux"),
|
|
Shard: new(OsQueryConfigInt),
|
|
Snapshot: new(bool),
|
|
},
|
|
},
|
|
Discovery: []string{
|
|
"select from something",
|
|
},
|
|
},
|
|
}
|
|
b, _ := json.Marshal(pnm)
|
|
actual := make(PackNameMap)
|
|
err := json.Unmarshal(b, &actual)
|
|
require.Nil(t, err)
|
|
assert.Len(t, actual, 2)
|
|
|
|
pnm = PackNameMap{
|
|
"path": "/this/is/a/path",
|
|
"details": PackDetails{
|
|
Queries: QueryNameToQueryDetailsMap{
|
|
"q1": QueryDetails{
|
|
Query: "select from foo",
|
|
Interval: 100,
|
|
Removed: new(bool),
|
|
Platform: s2p("linux"),
|
|
Shard: new(OsQueryConfigInt),
|
|
Snapshot: new(bool),
|
|
},
|
|
},
|
|
Shard: u2p(10),
|
|
Version: s2p("1.0"),
|
|
Platform: "linux",
|
|
Discovery: []string{
|
|
"select from something",
|
|
},
|
|
},
|
|
"details2": PackDetails{
|
|
Queries: QueryNameToQueryDetailsMap{
|
|
"q1": QueryDetails{
|
|
Query: "select from bar",
|
|
Interval: 100,
|
|
Removed: new(bool),
|
|
Platform: s2p("linux"),
|
|
Shard: new(OsQueryConfigInt),
|
|
Snapshot: new(bool),
|
|
},
|
|
},
|
|
Shard: u2p(10),
|
|
Version: s2p("1.0"),
|
|
Platform: "linux",
|
|
},
|
|
}
|
|
|
|
b, _ = json.Marshal(pnm)
|
|
actual = make(PackNameMap)
|
|
err = json.Unmarshal(b, &actual)
|
|
require.Nil(t, err)
|
|
assert.Len(t, actual, 3)
|
|
}
|