Add host IDs to plugin host sets read response (#1568)
This also includes tweaks here and there within the service and repos.
This commit is contained in:
1 parent
7d71618df0
commit
8b1ca4b0b8
7 files changed
+269
-43
No files matched your search
@@ -197,3 +197,142 @@ func TestLoopbackPlugin(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoopbackPluginArrays(t *testing.T) {
|
||||
require := tr.New(t)
|
||||
ctx := context.Background()
|
||||
|
||||
plg := NewLoopbackPlugin()
|
||||
|
||||
// Add data to some sets
|
||||
hostInfo1 := map[string]interface{}{
|
||||
loopbackPluginHostInfoAttrField: []interface{}{
|
||||
map[string]interface{}{
|
||||
"set_ids": []interface{}{"set1"},
|
||||
"external_id": "host1a",
|
||||
"ip_addresses": []interface{}{"1.2.3.4", "2.3.4.5"},
|
||||
"dns_names": []interface{}{"foo.com"},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"set_ids": []interface{}{"set1"},
|
||||
"external_id": "host1b",
|
||||
"ip_addresses": []interface{}{"3.4.5.6", "4.5.6.7"},
|
||||
"dns_names": []interface{}{"bar.com"},
|
||||
},
|
||||
},
|
||||
}
|
||||
attrs, err := structpb.NewStruct(hostInfo1)
|
||||
require.NoError(err)
|
||||
_, err = plg.OnCreateSet(ctx, &plgpb.OnCreateSetRequest{
|
||||
Set: &hostsets.HostSet{
|
||||
Id: "set1",
|
||||
Attributes: attrs,
|
||||
},
|
||||
})
|
||||
require.NoError(err)
|
||||
hostInfo2 := map[string]interface{}{
|
||||
loopbackPluginHostInfoAttrField: []interface{}{
|
||||
map[string]interface{}{
|
||||
"set_ids": []interface{}{"set2"},
|
||||
"external_id": "host2a",
|
||||
"ip_addresses": []interface{}{"10.20.30.40", "20.30.40.50"},
|
||||
"dns_names": []interface{}{"foz.com"},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"set_ids": []interface{}{"set2"},
|
||||
"external_id": "host2b",
|
||||
"ip_addresses": []interface{}{"30.40.50.60", "40.50.60.70"},
|
||||
"dns_names": []interface{}{"baz.com"},
|
||||
},
|
||||
},
|
||||
}
|
||||
attrs, err = structpb.NewStruct(hostInfo2)
|
||||
require.NoError(err)
|
||||
_, err = plg.OnCreateSet(ctx, &plgpb.OnCreateSetRequest{
|
||||
Set: &hostsets.HostSet{
|
||||
Id: "set2",
|
||||
Attributes: attrs,
|
||||
},
|
||||
})
|
||||
require.NoError(err)
|
||||
|
||||
// Define test struct and validation function
|
||||
type testInfo struct {
|
||||
name string
|
||||
sets []string
|
||||
found []interface{}
|
||||
}
|
||||
validateSets := func(t *testing.T, tt testInfo) {
|
||||
require, assert := tr.New(t), ta.New(t)
|
||||
var hostSets []*hostsets.HostSet
|
||||
for _, set := range tt.sets {
|
||||
hostSets = append(hostSets, &hostsets.HostSet{Id: set})
|
||||
}
|
||||
resp, err := plg.ListHosts(ctx, &plgpb.ListHostsRequest{
|
||||
Sets: hostSets,
|
||||
})
|
||||
require.NoError(err)
|
||||
if len(tt.found) == 0 {
|
||||
assert.Len(resp.GetHosts(), 0)
|
||||
return
|
||||
}
|
||||
|
||||
require.Greater(len(resp.GetHosts()), 0)
|
||||
|
||||
var found []interface{}
|
||||
for _, host := range resp.GetHosts() {
|
||||
hostMap := map[string]interface{}{
|
||||
"external_id": host.GetExternalId(),
|
||||
}
|
||||
var sets []interface{}
|
||||
for _, set := range host.SetIds {
|
||||
sets = append(sets, set)
|
||||
}
|
||||
var ips []interface{}
|
||||
for _, ip := range host.GetIpAddresses() {
|
||||
ips = append(ips, ip)
|
||||
}
|
||||
var names []interface{}
|
||||
for _, name := range host.GetDnsNames() {
|
||||
names = append(names, name)
|
||||
}
|
||||
if len(sets) > 0 {
|
||||
hostMap["set_ids"] = sets
|
||||
}
|
||||
if len(ips) > 0 {
|
||||
hostMap["ip_addresses"] = ips
|
||||
}
|
||||
if len(names) > 0 {
|
||||
hostMap["dns_names"] = names
|
||||
}
|
||||
found = append(found, hostMap)
|
||||
}
|
||||
assert.ElementsMatch(tt.found, found)
|
||||
}
|
||||
|
||||
// First set of tests: check that we can look up sets individually and
|
||||
// together
|
||||
setTests := []testInfo{
|
||||
{
|
||||
name: "set 1",
|
||||
sets: []string{"set1"},
|
||||
found: hostInfo1[loopbackPluginHostInfoAttrField].([]interface{}),
|
||||
},
|
||||
{
|
||||
name: "set 2",
|
||||
sets: []string{"set2"},
|
||||
found: hostInfo2[loopbackPluginHostInfoAttrField].([]interface{}),
|
||||
},
|
||||
{
|
||||
name: "sets 1 and 2",
|
||||
sets: []string{"set1", "set2"},
|
||||
found: append(hostInfo1[loopbackPluginHostInfoAttrField].([]interface{}),
|
||||
hostInfo2[loopbackPluginHostInfoAttrField].([]interface{})...),
|
||||
},
|
||||
}
|
||||
for _, tt := range setTests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
validateSets(t, tt)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user