Commit c3c2b5c2 authored by pfandzelter's avatar pfandzelter
Browse files

Merge branch 'tp/more-fred-tests' into 'main'

improve test coverage

See merge request !156
parents 304f3ca3 94cf0128
Pipeline #39334 passed with stages
in 19 minutes and 54 seconds
......@@ -34,7 +34,7 @@ func TestMain(m *testing.M) {
},
)
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
zerolog.SetGlobalLevel(zerolog.FatalLevel)
fInfo, err := os.Stat(etcdDir)
......@@ -469,6 +469,203 @@ func TestKeygroupReplicas(t *testing.T) {
assert.Equal(t, expiries[nodes[0].ID], 0)
}
func TestTriggers(t *testing.T) {
user := "user1"
var kg fred.KeygroupName = "triggerkeygroup"
tid := "trigger1"
thost := "5.5.5.5:9000"
err := f.E.HandleAddTrigger(user, fred.Keygroup{
Name: kg,
}, fred.Trigger{
ID: tid,
Host: thost,
})
assert.Error(t, err)
err = f.E.HandleCreateKeygroup(user, fred.Keygroup{
Name: kg,
Mutable: true,
Expiry: 0,
})
assert.NoError(t, err)
err = f.E.HandleAddTrigger(user, fred.Keygroup{
Name: kg,
}, fred.Trigger{
ID: tid,
Host: thost,
})
assert.NoError(t, err)
triggers, err := f.E.HandleGetKeygroupTriggers(user, fred.Keygroup{
Name: kg,
})
assert.NoError(t, err)
assert.Len(t, triggers, 1)
assert.Equal(t, triggers[0].ID, tid)
assert.Equal(t, triggers[0].Host, thost)
err = f.E.HandleRemoveTrigger(user, fred.Keygroup{
Name: kg,
}, fred.Trigger{
ID: tid,
})
assert.NoError(t, err)
triggers, err = f.E.HandleGetKeygroupTriggers(user, fred.Keygroup{
Name: kg,
})
assert.NoError(t, err)
assert.Len(t, triggers, 0)
}
func TestInternalPut(t *testing.T) {
kg := fred.KeygroupName("kginternalput")
id := "item1"
val := "value1"
err := f.E.HandleCreateKeygroup("user", fred.Keygroup{
Name: kg,
Mutable: true,
Expiry: 0,
})
assert.NoError(t, err)
err = f.I.HandleUpdate(fred.Item{
Keygroup: kg,
ID: id,
Val: val,
})
assert.NoError(t, err)
i, err := f.I.HandleGet(fred.Item{
Keygroup: kg,
ID: id,
})
assert.NoError(t, err)
assert.Equal(t, kg, i.Keygroup)
assert.Equal(t, id, i.ID)
assert.Equal(t, val, i.Val)
items, err := f.I.HandleGetAllItems(fred.Keygroup{
Name: kg,
})
assert.NoError(t, err)
assert.Len(t, items, 1)
assert.Equal(t, kg, items[0].Keygroup)
assert.Equal(t, id, items[0].ID)
assert.Equal(t, val, items[0].Val)
}
func TestInternalDelete(t *testing.T) {
kg := fred.KeygroupName("kginternaldelete")
id := "item1"
val := "value1"
err := f.E.HandleCreateKeygroup("user", fred.Keygroup{
Name: kg,
Mutable: true,
Expiry: 0,
})
assert.NoError(t, err)
err = f.E.HandleUpdate("user", fred.Item{
Keygroup: kg,
ID: id,
Val: val,
})
assert.NoError(t, err)
items, err := f.I.HandleGetAllItems(fred.Keygroup{
Name: kg,
})
assert.NoError(t, err)
assert.Len(t, items, 1)
err = f.I.HandleDelete(fred.Item{
Keygroup: kg,
ID: id,
})
assert.NoError(t, err)
items, err = f.I.HandleGetAllItems(fred.Keygroup{
Name: kg,
})
assert.NoError(t, err)
assert.Len(t, items, 0)
}
func TestInternalAppend(t *testing.T) {
kg := fred.KeygroupName("kginternappend")
id := "0"
val := "value1"
err := f.E.HandleCreateKeygroup("user", fred.Keygroup{
Name: kg,
Mutable: false,
Expiry: 0,
})
assert.NoError(t, err)
err = f.I.HandleAppend(fred.Item{
Keygroup: kg,
ID: id,
Val: val,
})
assert.NoError(t, err)
i, err := f.I.HandleGet(fred.Item{
Keygroup: kg,
ID: id,
})
assert.NoError(t, err)
assert.Equal(t, kg, i.Keygroup)
assert.Equal(t, id, i.ID)
assert.Equal(t, val, i.Val)
items, err := f.I.HandleGetAllItems(fred.Keygroup{
Name: kg,
})
assert.NoError(t, err)
assert.Len(t, items, 1)
assert.Equal(t, kg, items[0].Keygroup)
assert.Equal(t, id, items[0].ID)
assert.Equal(t, val, items[0].Val)
i, err = f.E.HandleAppend("user", fred.Item{
Keygroup: kg,
Val: "value2",
})
assert.NoError(t, err)
assert.Equal(t, kg, i.Keygroup)
assert.Equal(t, "1", i.ID)
assert.Equal(t, "value2", i.Val)
}
func BenchmarkPut(b *testing.B) {
user := "user"
kg := "benchmarkPut"
......
package fred
import (
"net"
"regexp"
"github.com/go-errors/errors"
"github.com/rs/zerolog/log"
)
// ParseAddress parses a string and returns it as an Address if it is a valid address.
// It returns true if the address is an IP address (opposed to a hostname).
// If it cannot parse the address, it returns an errors != nil.
// https://stackoverflow.com/questions/42479410/how-to-determine-if-given-string-is-a-hostname-or-an-ip-address
func ParseAddress(a string) (Address, error) {
if a == "localhost" {
return Address{
Addr: "localhost",
IsIP: false,
}, nil
}
ip := net.ParseIP(a)
if ip != nil {
return Address{
Addr: ip.String(),
IsIP: true,
}, nil
}
// i hate regex
// https://stackoverflow.com/questions/3026957/how-to-validate-a-domain-name-using-regex-php
matched, err := regexp.MatchString(`^(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,}$`, a)
if err != nil {
log.Err(err).Msg("")
return Address{}, err
}
if !matched {
return Address{}, errors.Errorf("replication.address: could not validate %s as IP address or hostname", a)
}
return Address{
Addr: a,
IsIP: false,
}, nil
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment