provider/boltmem: Implement silence deletion

This commit is contained in:
Fabian Reinartz 2016-04-29 15:40:21 +02:00
parent 9e4dfb32ca
commit f833fe5073
2 changed files with 44 additions and 2 deletions

View File

@ -106,8 +106,16 @@ func (s *Silences) Set(sil *types.Silence) (uint64, error) {
}
// Del removes a silence.
func (s *Silences) Del(uint64) error {
return nil
func (s *Silences) Del(uid uint64) error {
err := s.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(bktSilences)
k := make([]byte, 8)
binary.BigEndian.PutUint64(k, uid)
return b.Delete(k)
})
return err
}
// Get a silence associated with a fingerprint.

View File

@ -7,6 +7,7 @@ import (
"time"
"github.com/kylelemons/godebug/pretty"
"github.com/prometheus/alertmanager/provider"
"github.com/prometheus/alertmanager/types"
"github.com/prometheus/common/model"
)
@ -161,3 +162,36 @@ func TestSilencesSet(t *testing.T) {
}
}
}
func TestSilencesDelete(t *testing.T) {
dir, err := ioutil.TempDir("", "silences_test")
if err != nil {
t.Fatal(err)
}
silences, err := NewSilences(dir, nil)
if err != nil {
t.Fatal(err)
}
uid, err := silences.Set(&types.Silence{
Matchers: []*types.Matcher{
{Name: "key", Value: "val"},
},
Silence: model.Silence{
CreatedBy: "user",
Comment: "test comment",
},
})
if err != nil {
t.Fatalf("Insert failed: %s", err)
}
if err := silences.Del(uid); err != nil {
t.Fatalf("Delete failed: %s", err)
}
if s, err := silences.Get(uid); err != provider.ErrNotFound {
t.Fatalf("Expected 'not found' error but got: %v, %s", s, err)
}
}