mirror of
https://github.com/kemko/xc.git
synced 2026-01-01 15:55:43 +03:00
180 lines
3.5 KiB
Go
180 lines
3.5 KiB
Go
package store
|
|
|
|
import (
|
|
"sort"
|
|
"testing"
|
|
)
|
|
|
|
type FakeBackend struct {
|
|
hosts []*Host
|
|
groups []*Group
|
|
workgroups []*WorkGroup
|
|
datacenters []*Datacenter
|
|
}
|
|
|
|
func (fb *FakeBackend) Hosts() []*Host {
|
|
return fb.hosts
|
|
}
|
|
|
|
func (fb *FakeBackend) Groups() []*Group {
|
|
return fb.groups
|
|
}
|
|
|
|
func (fb *FakeBackend) Datacenters() []*Datacenter {
|
|
return fb.datacenters
|
|
}
|
|
|
|
func (fb *FakeBackend) WorkGroups() []*WorkGroup {
|
|
return fb.workgroups
|
|
}
|
|
|
|
func (fb *FakeBackend) Load() error {
|
|
wg := &WorkGroup{
|
|
ID: "wg1",
|
|
Name: "workgroup",
|
|
Groups: make([]*Group, 0),
|
|
}
|
|
fb.workgroups = append(fb.workgroups, wg)
|
|
|
|
group1 := &Group{
|
|
ID: "g1",
|
|
Name: "group1",
|
|
WorkGroupID: "wg1",
|
|
ParentID: "",
|
|
Tags: []string{"tag1", "tag2"},
|
|
}
|
|
|
|
group2 := &Group{
|
|
ID: "g2",
|
|
Name: "group2",
|
|
WorkGroupID: "wg1",
|
|
ParentID: "g1",
|
|
Tags: []string{"tag3", "tag4"},
|
|
}
|
|
fb.groups = append(fb.groups, group1, group2)
|
|
|
|
dc1 := &Datacenter{
|
|
ID: "dc1",
|
|
Name: "datacenter1",
|
|
ParentID: "",
|
|
}
|
|
|
|
dc2 := &Datacenter{
|
|
ID: "dc2",
|
|
Name: "datacenter1.1",
|
|
ParentID: "dc1",
|
|
}
|
|
fb.datacenters = append(fb.datacenters, dc1, dc2)
|
|
|
|
host := &Host{
|
|
ID: "h1",
|
|
FQDN: "host1.example.com",
|
|
Aliases: []string{"host1", "host1.i"},
|
|
Tags: []string{"tag5"},
|
|
GroupID: "g2",
|
|
DatacenterID: "dc2",
|
|
}
|
|
fb.hosts = append(fb.hosts, host)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (fb *FakeBackend) Reload() error {
|
|
return fb.Load()
|
|
}
|
|
|
|
func newFB() *FakeBackend {
|
|
fb := new(FakeBackend)
|
|
fb.hosts = make([]*Host, 0)
|
|
fb.groups = make([]*Group, 0)
|
|
fb.datacenters = make([]*Datacenter, 0)
|
|
fb.workgroups = make([]*WorkGroup, 0)
|
|
return fb
|
|
}
|
|
|
|
func TestStoreRelations(t *testing.T) {
|
|
var found bool
|
|
fb := newFB()
|
|
fb.Load()
|
|
|
|
s, err := CreateStore(fb)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
wg, found := s.workgroups._id["wg1"]
|
|
if !found {
|
|
t.Error("Workgroup wg1 not found")
|
|
}
|
|
|
|
g1, found := s.groups._id["g1"]
|
|
if !found {
|
|
t.Error("Group g1 not found")
|
|
}
|
|
|
|
g2, found := s.groups._id["g2"]
|
|
if !found {
|
|
t.Error("Group g2 not found")
|
|
}
|
|
|
|
h1, found := s.hosts._id["h1"]
|
|
if !found {
|
|
t.Error("Host h1 not found")
|
|
}
|
|
|
|
if g1.WorkGroup != wg {
|
|
t.Error("Group g1 should be connected to workgroup wg1")
|
|
}
|
|
if g2.WorkGroup != wg {
|
|
t.Error("Group g2 should be connected to workgroup wg1")
|
|
}
|
|
if g2.Parent != g1 {
|
|
t.Error("Group g2 parent must be g1")
|
|
}
|
|
|
|
found = false
|
|
for _, chg := range g1.Children {
|
|
if chg == g2 {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Error("g2 should exist in g1.Children")
|
|
}
|
|
|
|
var tagsReal []string
|
|
var tagsExpected []string
|
|
|
|
tagsReal = g2.AllTags
|
|
tagsExpected = []string{"tag1", "tag2", "tag3", "tag4"}
|
|
sort.Strings(tagsReal)
|
|
|
|
if len(tagsReal) != len(tagsExpected) {
|
|
t.Errorf("Group2 AllTags expected to be of length %d, however its length is %d", len(tagsExpected), len(tagsReal))
|
|
return
|
|
}
|
|
|
|
for i := 0; i < len(tagsExpected); i++ {
|
|
if tagsReal[i] != tagsExpected[i] {
|
|
t.Errorf("Expected tag %s at position %d, found %s", tagsExpected[i], i, tagsReal[i])
|
|
}
|
|
}
|
|
|
|
tagsReal = h1.AllTags
|
|
tagsExpected = []string{"tag1", "tag2", "tag3", "tag4", "tag5"}
|
|
sort.Strings(tagsReal)
|
|
if len(tagsReal) != len(tagsExpected) {
|
|
t.Errorf("Host1 AllTags expected to be of length %d, however its length is %d", len(tagsExpected), len(tagsReal))
|
|
return
|
|
}
|
|
|
|
for i := 0; i < len(tagsExpected); i++ {
|
|
if tagsReal[i] != tagsExpected[i] {
|
|
t.Errorf("Expected tag %s at position %d, found %s", tagsExpected[i], i, tagsReal[i])
|
|
}
|
|
}
|
|
|
|
}
|