From 3be9a480064add3f4c956edcf13e5be38eac1869 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Tue, 18 Apr 2017 08:05:45 +0200 Subject: [PATCH] further type adaptions --- api/api.go | 41 ++------- nflog/nflog_test.go | 46 +++------- notify/notify_test.go | 19 +--- silence/silence_test.go | 199 +++++++++++++++++++--------------------- 4 files changed, 121 insertions(+), 184 deletions(-) diff --git a/api/api.go b/api/api.go index f4d1827a..4ced08f2 100644 --- a/api/api.go +++ b/api/api.go @@ -20,7 +20,6 @@ import ( "sync" "time" - "github.com/golang/protobuf/ptypes" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/log" "github.com/prometheus/common/model" @@ -393,7 +392,7 @@ func (api *API) addSilence(w http.ResponseWriter, r *http.Request) { } // Drop start time for new silences so we default to now. if sil.ID == "" && sil.StartsAt.Before(time.Now()) { - psil.StartsAt = nil + psil.StartsAt = time.Time{} } sid, err := api.silences.Create(psil) @@ -502,23 +501,11 @@ func matchesFilterLabels(s *types.Silence, matchers []*labels.Matcher) bool { } func silenceToProto(s *types.Silence) (*silencepb.Silence, error) { - startsAt, err := ptypes.TimestampProto(s.StartsAt) - if err != nil { - return nil, err - } - endsAt, err := ptypes.TimestampProto(s.EndsAt) - if err != nil { - return nil, err - } - updatedAt, err := ptypes.TimestampProto(s.UpdatedAt) - if err != nil { - return nil, err - } sil := &silencepb.Silence{ Id: s.ID, - StartsAt: startsAt, - EndsAt: endsAt, - UpdatedAt: updatedAt, + StartsAt: s.StartsAt, + EndsAt: s.EndsAt, + UpdatedAt: s.UpdatedAt, } for _, m := range s.Matchers { matcher := &silencepb.Matcher{ @@ -532,7 +519,7 @@ func silenceToProto(s *types.Silence) (*silencepb.Silence, error) { sil.Matchers = append(sil.Matchers, matcher) } sil.Comments = append(sil.Comments, &silencepb.Comment{ - Timestamp: updatedAt, + Timestamp: s.UpdatedAt, Author: s.CreatedBy, Comment: s.Comment, }) @@ -540,23 +527,11 @@ func silenceToProto(s *types.Silence) (*silencepb.Silence, error) { } func silenceFromProto(s *silencepb.Silence) (*types.Silence, error) { - startsAt, err := ptypes.Timestamp(s.StartsAt) - if err != nil { - return nil, err - } - endsAt, err := ptypes.Timestamp(s.EndsAt) - if err != nil { - return nil, err - } - updatedAt, err := ptypes.Timestamp(s.UpdatedAt) - if err != nil { - return nil, err - } sil := &types.Silence{ ID: s.Id, - StartsAt: startsAt, - EndsAt: endsAt, - UpdatedAt: updatedAt, + StartsAt: s.StartsAt, + EndsAt: s.EndsAt, + UpdatedAt: s.UpdatedAt, } for _, m := range s.Matchers { matcher := &types.Matcher{ diff --git a/nflog/nflog_test.go b/nflog/nflog_test.go index 2addd05c..1afd7fba 100644 --- a/nflog/nflog_test.go +++ b/nflog/nflog_test.go @@ -20,8 +20,6 @@ import ( "testing" "time" - "github.com/golang/protobuf/ptypes" - "github.com/golang/protobuf/ptypes/timestamp" pb "github.com/prometheus/alertmanager/nflog/nflogpb" "github.com/stretchr/testify/require" ) @@ -31,7 +29,7 @@ func TestNlogGC(t *testing.T) { // We only care about key names and expiration timestamps. newEntry := func(ts time.Time) *pb.MeshEntry { return &pb.MeshEntry{ - ExpiresAt: mustTimestampProto(ts), + ExpiresAt: ts, } } @@ -69,27 +67,27 @@ func TestNlogSnapshot(t *testing.T) { Receiver: &pb.Receiver{GroupName: "abc", Integration: "test1", Idx: 1}, GroupHash: []byte("126a8a51b9d1bbd07fddc65819a542c3"), Resolved: false, - Timestamp: mustTimestampProto(now), + Timestamp: now, }, - ExpiresAt: mustTimestampProto(now), + ExpiresAt: now, }, { Entry: &pb.Entry{ GroupKey: []byte("d8e8fca2dc0f8abce7cb4cb0031ba249"), Receiver: &pb.Receiver{GroupName: "def", Integration: "test2", Idx: 29}, GroupHash: []byte("122c2331b9d1bbd07fddc65819a542c3"), Resolved: true, - Timestamp: mustTimestampProto(now), + Timestamp: now, }, - ExpiresAt: mustTimestampProto(now), + ExpiresAt: now, }, { Entry: &pb.Entry{ GroupKey: []byte("aaaaaca2dc0f896fd7cb4cb0031ba249"), Receiver: &pb.Receiver{GroupName: "ghi", Integration: "test3", Idx: 0}, GroupHash: []byte("126a8a51b9d1bbd07fddc6e3e3e542c3"), Resolved: false, - Timestamp: mustTimestampProto(now), + Timestamp: now, }, - ExpiresAt: mustTimestampProto(now), + ExpiresAt: now, }, }, }, @@ -160,7 +158,7 @@ func TestGossipDataMerge(t *testing.T) { // merging logic. newEntry := func(ts time.Time) *pb.MeshEntry { return &pb.MeshEntry{ - Entry: &pb.Entry{Timestamp: mustTimestampProto(ts)}, + Entry: &pb.Entry{Timestamp: ts}, } } cases := []struct { @@ -225,27 +223,27 @@ func TestGossipDataCoding(t *testing.T) { Receiver: &pb.Receiver{GroupName: "abc", Integration: "test1", Idx: 1}, GroupHash: []byte("126a8a51b9d1bbd07fddc65819a542c3"), Resolved: false, - Timestamp: mustTimestampProto(now), + Timestamp: now, }, - ExpiresAt: mustTimestampProto(now), + ExpiresAt: now, }, { Entry: &pb.Entry{ GroupKey: []byte("d8e8fca2dc0f8abce7cb4cb0031ba249"), Receiver: &pb.Receiver{GroupName: "def", Integration: "test2", Idx: 29}, GroupHash: []byte("122c2331b9d1bbd07fddc65819a542c3"), Resolved: true, - Timestamp: mustTimestampProto(now), + Timestamp: now, }, - ExpiresAt: mustTimestampProto(now), + ExpiresAt: now, }, { Entry: &pb.Entry{ GroupKey: []byte("aaaaaca2dc0f896fd7cb4cb0031ba249"), Receiver: &pb.Receiver{GroupName: "ghi", Integration: "test3", Idx: 0}, GroupHash: []byte("126a8a51b9d1bbd07fddc6e3e3e542c3"), Resolved: false, - Timestamp: mustTimestampProto(now), + Timestamp: now, }, - ExpiresAt: mustTimestampProto(now), + ExpiresAt: now, }, }, }, @@ -266,19 +264,3 @@ func TestGossipDataCoding(t *testing.T) { require.Equal(t, in, out, "decoded data doesn't match encoded data") } } - -func mustTimestamp(ts *timestamp.Timestamp) time.Time { - res, err := ptypes.Timestamp(ts) - if err != nil { - panic(err) - } - return res -} - -func mustTimestampProto(ts time.Time) *timestamp.Timestamp { - res, err := ptypes.TimestampProto(ts) - if err != nil { - panic(err) - } - return res -} diff --git a/notify/notify_test.go b/notify/notify_test.go index 9536c2b9..8290da1d 100644 --- a/notify/notify_test.go +++ b/notify/notify_test.go @@ -20,8 +20,6 @@ import ( "testing" "time" - "github.com/golang/protobuf/ptypes" - "github.com/golang/protobuf/ptypes/timestamp" "github.com/prometheus/common/model" "github.com/stretchr/testify/require" "golang.org/x/net/context" @@ -78,15 +76,6 @@ func (l *testNflog) GC() (int, error) { func (l *testNflog) Snapshot(w io.Writer) (int, error) { return 0, nil } - -func mustTimestampProto(ts time.Time) *timestamp.Timestamp { - tspb, err := ptypes.TimestampProto(ts) - if err != nil { - panic(err) - } - return tspb -} - func TestDedupStageNeedsUpdate(t *testing.T) { now := utcNow() @@ -109,14 +98,14 @@ func TestDedupStageNeedsUpdate(t *testing.T) { }, { entry: &nflogpb.Entry{ GroupHash: []byte{1, 2, 3}, - Timestamp: nil, // parsing will error + Timestamp: time.Time{}, // parsing will error }, hash: []byte{1, 2, 3}, resErr: true, }, { entry: &nflogpb.Entry{ GroupHash: []byte{1, 2, 3}, - Timestamp: mustTimestampProto(now.Add(-9 * time.Minute)), + Timestamp: now.Add(-9 * time.Minute), }, repeat: 10 * time.Minute, hash: []byte{1, 2, 3}, @@ -124,7 +113,7 @@ func TestDedupStageNeedsUpdate(t *testing.T) { }, { entry: &nflogpb.Entry{ GroupHash: []byte{1, 2, 3}, - Timestamp: mustTimestampProto(now.Add(-11 * time.Minute)), + Timestamp: now.Add(-11 * time.Minute), }, repeat: 10 * time.Minute, hash: []byte{1, 2, 3}, @@ -413,7 +402,7 @@ func TestSilenceStage(t *testing.T) { t.Fatal(err) } if _, err := silences.Create(&silencepb.Silence{ - EndsAt: mustTimestampProto(utcNow().Add(time.Hour)), + EndsAt: utcNow().Add(time.Hour), Matchers: []*silencepb.Matcher{{Name: "mute", Pattern: "me"}}, }); err != nil { t.Fatal(err) diff --git a/silence/silence_test.go b/silence/silence_test.go index 824045f6..c47b5ef1 100644 --- a/silence/silence_test.go +++ b/silence/silence_test.go @@ -23,7 +23,6 @@ import ( "time" "github.com/golang/protobuf/ptypes" - "github.com/golang/protobuf/ptypes/timestamp" pb "github.com/prometheus/alertmanager/silence/silencepb" "github.com/prometheus/common/model" "github.com/stretchr/testify/require" @@ -80,7 +79,7 @@ func TestSilencesGC(t *testing.T) { s.now = func() time.Time { return now } newSilence := func(exp time.Time) *pb.MeshSilence { - return &pb.MeshSilence{ExpiresAt: mustTimeProto(exp)} + return &pb.MeshSilence{ExpiresAt: exp} } s.st = gossipData{ "1": newSilence(now), @@ -113,11 +112,11 @@ func TestSilencesSnapshot(t *testing.T) { {Name: "label1", Pattern: "val1", Type: pb.Matcher_EQUAL}, {Name: "label2", Pattern: "val.+", Type: pb.Matcher_REGEXP}, }, - StartsAt: mustTimeProto(now), - EndsAt: mustTimeProto(now), - UpdatedAt: mustTimeProto(now), + StartsAt: now, + EndsAt: now, + UpdatedAt: now, }, - ExpiresAt: mustTimeProto(now), + ExpiresAt: now, }, { Silence: &pb.Silence{ @@ -125,11 +124,11 @@ func TestSilencesSnapshot(t *testing.T) { Matchers: []*pb.Matcher{ {Name: "label1", Pattern: "val1", Type: pb.Matcher_EQUAL}, }, - StartsAt: mustTimeProto(now.Add(time.Hour)), - EndsAt: mustTimeProto(now.Add(2 * time.Hour)), - UpdatedAt: mustTimeProto(now), + StartsAt: now.Add(time.Hour), + EndsAt: now.Add(2 * time.Hour), + UpdatedAt: now, }, - ExpiresAt: mustTimeProto(now.Add(24 * time.Hour)), + ExpiresAt: now.Add(24 * time.Hour), }, }, }, @@ -176,7 +175,7 @@ func TestSilencesSetSilence(t *testing.T) { require.NoError(t, err) now := utcNow() - nowpb := mustTimeProto(now) + nowpb := now sil := &pb.Silence{ Id: "some_id", @@ -186,7 +185,7 @@ func TestSilencesSetSilence(t *testing.T) { want := gossipData{ "some_id": &pb.MeshSilence{ Silence: sil, - ExpiresAt: mustTimeProto(now.Add(time.Minute)), + ExpiresAt: now.Add(time.Minute), }, } @@ -217,8 +216,8 @@ func TestSilenceCreate(t *testing.T) { // Insert silence with fixed start time. sil1 := &pb.Silence{ Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}}, - StartsAt: mustTimeProto(now.Add(2 * time.Minute)), - EndsAt: mustTimeProto(now.Add(5 * time.Minute)), + StartsAt: now.Add(2 * time.Minute), + EndsAt: now.Add(5 * time.Minute), } id1, err := s.Create(sil1) require.NoError(t, err) @@ -229,11 +228,11 @@ func TestSilenceCreate(t *testing.T) { Silence: &pb.Silence{ Id: id1, Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}}, - StartsAt: mustTimeProto(now.Add(2 * time.Minute)), - EndsAt: mustTimeProto(now.Add(5 * time.Minute)), - UpdatedAt: mustTimeProto(now), + StartsAt: now.Add(2 * time.Minute), + EndsAt: now.Add(5 * time.Minute), + UpdatedAt: now, }, - ExpiresAt: mustTimeProto(now.Add(5*time.Minute + s.retention)), + ExpiresAt: now.Add(5*time.Minute + s.retention), }, } require.Equal(t, want, s.st, "unexpected state after silence creation") @@ -241,7 +240,7 @@ func TestSilenceCreate(t *testing.T) { // Insert silence with unset start time. Must be set to now. sil2 := &pb.Silence{ Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}}, - EndsAt: mustTimeProto(now.Add(1 * time.Minute)), + EndsAt: now.Add(1 * time.Minute), } id2, err := s.Create(sil2) require.NoError(t, err) @@ -252,21 +251,21 @@ func TestSilenceCreate(t *testing.T) { Silence: &pb.Silence{ Id: id1, Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}}, - StartsAt: mustTimeProto(now.Add(2 * time.Minute)), - EndsAt: mustTimeProto(now.Add(5 * time.Minute)), - UpdatedAt: mustTimeProto(now), + StartsAt: now.Add(2 * time.Minute), + EndsAt: now.Add(5 * time.Minute), + UpdatedAt: now, }, - ExpiresAt: mustTimeProto(now.Add(5*time.Minute + s.retention)), + ExpiresAt: now.Add(5*time.Minute + s.retention), }, id2: &pb.MeshSilence{ Silence: &pb.Silence{ Id: id2, Matchers: []*pb.Matcher{{Name: "a", Pattern: "b"}}, - StartsAt: mustTimeProto(now), - EndsAt: mustTimeProto(now.Add(1 * time.Minute)), - UpdatedAt: mustTimeProto(now), + StartsAt: now, + EndsAt: now.Add(1 * time.Minute), + UpdatedAt: now, }, - ExpiresAt: mustTimeProto(now.Add(1*time.Minute + s.retention)), + ExpiresAt: now.Add(1*time.Minute + s.retention), }, } require.Equal(t, want, s.st, "unexpected state after silence creation") @@ -288,7 +287,7 @@ func TestSilencesCreateFail(t *testing.T) { s: &pb.Silence{Id: "some_id"}, err: "unexpected ID in new silence", }, { - s: &pb.Silence{StartsAt: mustTimeProto(now.Add(-time.Minute))}, + s: &pb.Silence{StartsAt: now.Add(-time.Minute)}, err: "new silence must not start in the past", }, { s: &pb.Silence{}, // Silence without matcher. @@ -323,24 +322,24 @@ func TestQState(t *testing.T) { }{ { sil: &pb.Silence{ - StartsAt: mustTimeProto(now.Add(time.Minute)), - EndsAt: mustTimeProto(now.Add(time.Hour)), + StartsAt: now.Add(time.Minute), + EndsAt: now.Add(time.Hour), }, states: []SilenceState{StateActive, StateExpired}, keep: false, }, { sil: &pb.Silence{ - StartsAt: mustTimeProto(now.Add(time.Minute)), - EndsAt: mustTimeProto(now.Add(time.Hour)), + StartsAt: now.Add(time.Minute), + EndsAt: now.Add(time.Hour), }, states: []SilenceState{StatePending}, keep: true, }, { sil: &pb.Silence{ - StartsAt: mustTimeProto(now.Add(time.Minute)), - EndsAt: mustTimeProto(now.Add(time.Hour)), + StartsAt: now.Add(time.Minute), + EndsAt: now.Add(time.Hour), }, states: []SilenceState{StateExpired, StatePending}, keep: true, @@ -351,7 +350,7 @@ func TestQState(t *testing.T) { QState(c.states...)(q) f := q.filters[0] - keep, err := f(c.sil, nil, mustTimeProto(now)) + keep, err := f(c.sil, nil, now) require.NoError(t, err) require.Equal(t, c.keep, keep, "unexpected filter result for case %d", i) } @@ -409,7 +408,7 @@ func TestQMatches(t *testing.T) { }, } for _, c := range cases { - drop, err := f(c.sil, &Silences{mc: matcherCache{}}, nil) + drop, err := f(c.sil, &Silences{mc: matcherCache{}}, time.Time{}) require.NoError(t, err) require.Equal(t, c.drop, drop, "unexpected filter result") } @@ -455,7 +454,7 @@ func TestSilencesQuery(t *testing.T) { // Retrieve all and filter q: &query{ filters: []silenceFilter{ - func(sil *pb.Silence, _ *Silences, _ *timestamp.Timestamp) (bool, error) { + func(sil *pb.Silence, _ *Silences, _ time.Time) (bool, error) { return sil.Id == "1" || sil.Id == "2", nil }, }, @@ -470,7 +469,7 @@ func TestSilencesQuery(t *testing.T) { q: &query{ ids: []string{"2", "5"}, filters: []silenceFilter{ - func(sil *pb.Silence, _ *Silences, _ *timestamp.Timestamp) (bool, error) { + func(sil *pb.Silence, _ *Silences, _ time.Time) (bool, error) { return sil.Id == "1" || sil.Id == "2", nil }, }, @@ -483,7 +482,7 @@ func TestSilencesQuery(t *testing.T) { for _, c := range cases { // Run default query of retrieving all silences. - res, err := s.query(c.q, nil) + res, err := s.query(c.q, time.Time{}) require.NoError(t, err, "unexpected error on querying") // Currently there are no sorting guarantees in the querying API. @@ -504,99 +503,99 @@ func TestSilenceSetTimeRange(t *testing.T) { cases := []struct { sil *pb.Silence - start, end *timestamp.Timestamp + start, end time.Time err string }{ // Bad arguments. { sil: &pb.Silence{}, - start: mustTimeProto(now), - end: mustTimeProto(now.Add(-time.Minute)), + start: now, + end: now.Add(-time.Minute), err: "end time must not be before start time", }, // Expired silence. { sil: &pb.Silence{ - StartsAt: mustTimeProto(now.Add(-time.Hour)), - EndsAt: mustTimeProto(now.Add(-time.Second)), + StartsAt: now.Add(-time.Hour), + EndsAt: now.Add(-time.Second), }, - start: mustTimeProto(now), - end: mustTimeProto(now), + start: now, + end: now, err: "expired silence must not be modified", }, // Pending silences. { sil: &pb.Silence{ - StartsAt: mustTimeProto(now.Add(time.Hour)), - EndsAt: mustTimeProto(now.Add(2 * time.Hour)), - UpdatedAt: mustTimeProto(now.Add(-time.Hour)), + StartsAt: now.Add(time.Hour), + EndsAt: now.Add(2 * time.Hour), + UpdatedAt: now.Add(-time.Hour), }, - start: mustTimeProto(now.Add(-time.Minute)), - end: mustTimeProto(now.Add(time.Hour)), + start: now.Add(-time.Minute), + end: now.Add(time.Hour), err: "start time cannot be set into the past", }, { sil: &pb.Silence{ - StartsAt: mustTimeProto(now.Add(time.Hour)), - EndsAt: mustTimeProto(now.Add(2 * time.Hour)), - UpdatedAt: mustTimeProto(now.Add(-time.Hour)), + StartsAt: now.Add(time.Hour), + EndsAt: now.Add(2 * time.Hour), + UpdatedAt: now.Add(-time.Hour), }, - start: mustTimeProto(now.Add(time.Minute)), - end: mustTimeProto(now.Add(time.Minute)), + start: now.Add(time.Minute), + end: now.Add(time.Minute), }, { sil: &pb.Silence{ - StartsAt: mustTimeProto(now.Add(time.Hour)), - EndsAt: mustTimeProto(now.Add(2 * time.Hour)), - UpdatedAt: mustTimeProto(now.Add(-time.Hour)), + StartsAt: now.Add(time.Hour), + EndsAt: now.Add(2 * time.Hour), + UpdatedAt: now.Add(-time.Hour), }, - start: mustTimeProto(now), // set to exactly start now. - end: mustTimeProto(now.Add(2 * time.Hour)), + start: now, // set to exactly start now. + end: now.Add(2 * time.Hour), }, // Active silences. { sil: &pb.Silence{ - StartsAt: mustTimeProto(now.Add(-time.Hour)), - EndsAt: mustTimeProto(now.Add(2 * time.Hour)), - UpdatedAt: mustTimeProto(now.Add(-time.Hour)), + StartsAt: now.Add(-time.Hour), + EndsAt: now.Add(2 * time.Hour), + UpdatedAt: now.Add(-time.Hour), }, - start: mustTimeProto(now.Add(-time.Minute)), - end: mustTimeProto(now.Add(2 * time.Hour)), + start: now.Add(-time.Minute), + end: now.Add(2 * time.Hour), err: "start time of active silence cannot be modified", }, { sil: &pb.Silence{ - StartsAt: mustTimeProto(now.Add(-time.Hour)), - EndsAt: mustTimeProto(now.Add(2 * time.Hour)), - UpdatedAt: mustTimeProto(now.Add(-time.Hour)), + StartsAt: now.Add(-time.Hour), + EndsAt: now.Add(2 * time.Hour), + UpdatedAt: now.Add(-time.Hour), }, - start: mustTimeProto(now.Add(-time.Hour)), - end: mustTimeProto(now.Add(-time.Second)), + start: now.Add(-time.Hour), + end: now.Add(-time.Second), err: "end time cannot be set into the past", }, { sil: &pb.Silence{ - StartsAt: mustTimeProto(now.Add(-time.Hour)), - EndsAt: mustTimeProto(now.Add(2 * time.Hour)), - UpdatedAt: mustTimeProto(now.Add(-time.Hour)), + StartsAt: now.Add(-time.Hour), + EndsAt: now.Add(2 * time.Hour), + UpdatedAt: now.Add(-time.Hour), }, - start: mustTimeProto(now.Add(-time.Hour)), - end: mustTimeProto(now), + start: now.Add(-time.Hour), + end: now, }, { sil: &pb.Silence{ - StartsAt: mustTimeProto(now.Add(-time.Hour)), - EndsAt: mustTimeProto(now.Add(2 * time.Hour)), - UpdatedAt: mustTimeProto(now.Add(-time.Hour)), + StartsAt: now.Add(-time.Hour), + EndsAt: now.Add(2 * time.Hour), + UpdatedAt: now.Add(-time.Hour), }, - start: mustTimeProto(now.Add(-time.Hour)), - end: mustTimeProto(now.Add(3 * time.Hour)), + start: now.Add(-time.Hour), + end: now.Add(3 * time.Hour), }, } for _, c := range cases { origSilence := cloneSilence(c.sil) - sil, err := silenceSetTimeRange(c.sil, mustTimeProto(now), c.start, c.end) + sil, err := silenceSetTimeRange(c.sil, now, c.start, c.end) if err == nil { if c.err != "" { t.Errorf("expected error containing %q but got none", c.err) @@ -606,7 +605,7 @@ func TestSilenceSetTimeRange(t *testing.T) { require.Equal(t, sil.StartsAt, c.start) require.Equal(t, sil.EndsAt, c.end) - require.Equal(t, sil.UpdatedAt, mustTimeProto(now)) + require.Equal(t, sil.UpdatedAt, now) continue } if err != nil && c.err == "" { @@ -684,8 +683,8 @@ func TestValidateMatcher(t *testing.T) { func TestValidateSilence(t *testing.T) { var ( now = utcNow() - invalidTimestamp = ×tamp.Timestamp{Nanos: 1 << 30} - validTimestamp = mustTimeProto(now) + invalidTimestamp = time.Unix(0, 1<<30) + validTimestamp = now ) cases := []struct { s *pb.Silence @@ -744,8 +743,8 @@ func TestValidateSilence(t *testing.T) { Matchers: []*pb.Matcher{ &pb.Matcher{Name: "a", Pattern: "b"}, }, - StartsAt: mustTimeProto(now), - EndsAt: mustTimeProto(now.Add(-time.Second)), + StartsAt: now, + EndsAt: now.Add(-time.Second), UpdatedAt: validTimestamp, }, err: "end time must not be before start time", @@ -812,7 +811,7 @@ func TestGossipDataMerge(t *testing.T) { // merging logic. newSilence := func(ts time.Time) *pb.MeshSilence { return &pb.MeshSilence{ - Silence: &pb.Silence{UpdatedAt: mustTimeProto(ts)}, + Silence: &pb.Silence{UpdatedAt: ts}, } } cases := []struct { @@ -878,11 +877,11 @@ func TestGossipDataCoding(t *testing.T) { {Name: "label1", Pattern: "val1", Type: pb.Matcher_EQUAL}, {Name: "label2", Pattern: "val.+", Type: pb.Matcher_REGEXP}, }, - StartsAt: mustTimeProto(now), - EndsAt: mustTimeProto(now), - UpdatedAt: mustTimeProto(now), + StartsAt: now, + EndsAt: now, + UpdatedAt: now, }, - ExpiresAt: mustTimeProto(now), + ExpiresAt: now, }, { Silence: &pb.Silence{ @@ -890,11 +889,11 @@ func TestGossipDataCoding(t *testing.T) { Matchers: []*pb.Matcher{ {Name: "label1", Pattern: "val1", Type: pb.Matcher_EQUAL}, }, - StartsAt: mustTimeProto(now.Add(time.Hour)), - EndsAt: mustTimeProto(now.Add(2 * time.Hour)), - UpdatedAt: mustTimeProto(now), + StartsAt: now.Add(time.Hour), + EndsAt: now.Add(2 * time.Hour), + UpdatedAt: now, }, - ExpiresAt: mustTimeProto(now.Add(24 * time.Hour)), + ExpiresAt: now.Add(24 * time.Hour), }, }, }, @@ -952,11 +951,3 @@ func TestProtoBefore(t *testing.T) { require.Equal(t, c.before, res, "protoBefore returned unexpected result") } } - -func mustTimeProto(ts time.Time) *timestamp.Timestamp { - pt, err := ptypes.TimestampProto(ts) - if err != nil { - panic(err) - } - return pt -}