Skip to content

Commit

Permalink
fix: monitors & notifiers test suits
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmalkmus committed Jul 8, 2024
1 parent ecb4e1f commit 8e5edaf
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 48 deletions.
25 changes: 9 additions & 16 deletions axiom/annotations_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type AnnotationsTestSuite struct {
IntegrationTestSuite

// Setup once per test.
datasetA *axiom.Dataset
datasetB *axiom.Dataset
annotation *axiom.Annotation
Expand All @@ -24,27 +25,19 @@ func TestAnnotationsTestSuite(t *testing.T) {
suite.Run(t, new(AnnotationsTestSuite))
}

func (s *AnnotationsTestSuite) SetupSuite() {
s.IntegrationTestSuite.SetupSuite()
}

func (s *AnnotationsTestSuite) TearDownSuite() {
s.IntegrationTestSuite.TearDownSuite()
}

func (s *AnnotationsTestSuite) SetupTest() {
s.IntegrationTestSuite.SetupTest()

var err error

s.datasetA, err = s.client.Datasets.Create(s.suiteCtx, axiom.DatasetCreateRequest{
s.datasetA, err = s.client.Datasets.Create(s.ctx, axiom.DatasetCreateRequest{
Name: "test-axiom-go-annotations-a-" + datasetSuffix,
Description: "This is a test dataset for annotations integration tests.",
})
s.Require().NoError(err)
s.Require().NotNil(s.datasetA)

s.datasetB, err = s.client.Datasets.Create(s.suiteCtx, axiom.DatasetCreateRequest{
s.datasetB, err = s.client.Datasets.Create(s.ctx, axiom.DatasetCreateRequest{
Name: "test-axiom-go-annotations-b-" + datasetSuffix,
Description: "This is a test dataset for annotations integration tests.",
})
Expand All @@ -63,7 +56,7 @@ func (s *AnnotationsTestSuite) SetupTest() {
func (s *AnnotationsTestSuite) TearDownTest() {
// Teardown routines use their own context to avoid not being run at all
// when the suite gets cancelled or times out.
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
ctx, cancel := context.WithTimeout(context.WithoutCancel(s.ctx), time.Second*15)
defer cancel()

err := s.client.Datasets.Delete(ctx, s.datasetA.ID)
Expand All @@ -79,18 +72,18 @@ func (s *AnnotationsTestSuite) TearDownTest() {
}

func (s *AnnotationsTestSuite) Test() {
// Get annotation
// Get annotation.
annotation, err := s.client.Annotations.Get(s.ctx, s.annotation.ID)
s.Require().NoError(err)
s.Require().Equal(s.annotation.ID, annotation.ID)
s.Require().Equal(s.annotation.Title, annotation.Title)

// List annotations without filterr
// List annotations without filter.
annotations, err := s.client.Annotations.List(s.ctx, nil)
s.Require().NoError(err)
s.Greater(len(annotations), 0)

// List annotations with filter
// List annotations with filter.
annotations, err = s.client.Annotations.List(s.ctx, &axiom.AnnotationsFilter{
Datasets: []string{s.datasetA.ID},
})
Expand All @@ -99,13 +92,13 @@ func (s *AnnotationsTestSuite) Test() {
s.Equal(s.annotation.ID, annotations[0].ID)
}

// Update annotation
// Update annotation.
_, err = s.client.Annotations.Update(s.ctx, s.annotation.ID, &axiom.AnnotationUpdateRequest{
Datasets: []string{s.datasetB.ID},
})
s.Require().NoError(err)

// List annotations with filter, this should return 0 items now
// List annotations with filter, this should return 0 items now.
annotations, err = s.client.Annotations.List(s.ctx, &axiom.AnnotationsFilter{
Datasets: []string{s.datasetA.ID},
})
Expand Down
13 changes: 3 additions & 10 deletions axiom/datasets_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,19 @@ var ingestEvents = []axiom.Event{
type DatasetsTestSuite struct {
IntegrationTestSuite

// Setup once per test.
dataset *axiom.Dataset
}

func TestDatasetsTestSuite(t *testing.T) {
suite.Run(t, new(DatasetsTestSuite))
}

func (s *DatasetsTestSuite) SetupSuite() {
s.IntegrationTestSuite.SetupSuite()
}

func (s *DatasetsTestSuite) TearDownSuite() {
s.IntegrationTestSuite.TearDownSuite()
}

func (s *DatasetsTestSuite) SetupTest() {
s.IntegrationTestSuite.SetupTest()

var err error
s.dataset, err = s.client.Datasets.Create(s.suiteCtx, axiom.DatasetCreateRequest{
s.dataset, err = s.client.Datasets.Create(s.ctx, axiom.DatasetCreateRequest{
Name: "test-axiom-go-datasets-" + datasetSuffix,
Description: "This is a test dataset for datasets integration tests.",
})
Expand All @@ -107,7 +100,7 @@ func (s *DatasetsTestSuite) SetupTest() {
func (s *DatasetsTestSuite) TearDownTest() {
// Teardown routines use their own context to avoid not being run at all
// when the suite gets cancelled or times out.
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
ctx, cancel := context.WithTimeout(context.WithoutCancel(s.ctx), time.Second*15)
defer cancel()

err := s.client.Datasets.Delete(ctx, s.dataset.ID)
Expand Down
4 changes: 2 additions & 2 deletions axiom/monitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ func (m *Monitor) UnmarshalJSON(b []byte) error {

// Set to a proper time.Duration value by interpreting the server response
// value in seconds.
m.Range = m.Range * time.Minute
m.Interval = m.Interval * time.Minute
m.Range *= time.Minute
m.Interval *= time.Minute

return nil
}
Expand Down
40 changes: 27 additions & 13 deletions axiom/monitors_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build integration

package axiom_test

import (
Expand All @@ -18,8 +16,10 @@ import (
type MonitorsTestSuite struct {
IntegrationTestSuite

// Setup once per suite.
datasetID string

// Setup once per test.
monitor *axiom.Monitor
}

Expand All @@ -38,50 +38,64 @@ func (s *MonitorsTestSuite) SetupSuite() {
s.Require().NotNil(dataset)

s.datasetID = dataset.ID
}

func (s *MonitorsTestSuite) TearDownSuite() {
// Teardown routines use their own context to avoid not being run at all
// when the suite gets cancelled or times out.
ctx, cancel := context.WithTimeout(context.WithoutCancel(s.suiteCtx), time.Second*15)
defer cancel()

err := s.client.Datasets.Delete(ctx, s.datasetID)
s.NoError(err)

s.monitor, err = s.client.Monitors.Create(s.suiteCtx, axiom.MonitorCreateRequest{
s.IntegrationTestSuite.TearDownSuite()
}

func (s *MonitorsTestSuite) SetupTest() {
s.IntegrationTestSuite.SetupTest()

var err error
s.monitor, err = s.client.Monitors.Create(s.ctx, axiom.MonitorCreateRequest{
Monitor: axiom.Monitor{
AlertOnNoData: false,
APLQuery: fmt.Sprintf("['%s'] | summarize count() by bin_auto(_time)", s.datasetID),
Description: "A test monitor",
Interval: time.Minute,
Name: "Test Monitor",
Operator: axiom.BelowOrEqual,
Range: 5 * time.Minute,
Range: time.Minute * 5,
Threshold: 1,
},
})
s.Require().NoError(err)
s.Require().NotNil(s.monitor)
}

func (s *MonitorsTestSuite) TearDownSuite() {
func (s *MonitorsTestSuite) TearDownTest() {
// Teardown routines use their own context to avoid not being run at all
// when the suite gets cancelled or times out.
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
ctx, cancel := context.WithTimeout(context.WithoutCancel(s.ctx), time.Second*15)
defer cancel()

err := s.client.Monitors.Delete(ctx, s.monitor.ID)
s.NoError(err)

err = s.client.Datasets.Delete(ctx, s.datasetID)
s.NoError(err)

s.IntegrationTestSuite.TearDownSuite()
s.IntegrationTestSuite.TearDownTest()
}

func (s *MonitorsTestSuite) Test() {
// Let's update the monitor.
monitor, err := s.client.Monitors.Update(s.suiteCtx, s.monitor.ID, axiom.MonitorUpdateRequest{
monitor, err := s.client.Monitors.Update(s.ctx, s.monitor.ID, axiom.MonitorUpdateRequest{
Monitor: axiom.Monitor{
AlertOnNoData: false,
APLQuery: fmt.Sprintf("['%s'] | summarize count() by bin_auto(_time)", s.datasetID),
Description: "A very good test monitor",
DisabledUntil: time.Now().Add(10 * time.Minute),
DisabledUntil: time.Now().Add(time.Minute * 10),
Interval: time.Minute,
Name: "Test Monitor",
Operator: axiom.BelowOrEqual,
Range: 10 * time.Minute,
Range: time.Minute * 10,
Threshold: 5,
},
})
Expand Down
21 changes: 14 additions & 7 deletions axiom/notifiers_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build integration

package axiom_test

import (
Expand All @@ -17,6 +15,7 @@ import (
type NotifiersTestSuite struct {
IntegrationTestSuite

// Setup once per test.
notifier *axiom.Notifier
}

Expand All @@ -26,9 +25,17 @@ func TestNotifiersTestSuite(t *testing.T) {

func (s *NotifiersTestSuite) SetupSuite() {
s.IntegrationTestSuite.SetupSuite()
}

func (s *NotifiersTestSuite) TearDownSuite() {
s.IntegrationTestSuite.TearDownSuite()
}

func (s *NotifiersTestSuite) SetupTest() {
s.IntegrationTestSuite.SetupTest()

var err error
s.notifier, err = s.client.Notifiers.Create(s.suiteCtx, axiom.Notifier{
s.notifier, err = s.client.Notifiers.Create(s.ctx, axiom.Notifier{
Name: "Test Notifier",
Properties: axiom.NotifierProperties{
Email: &axiom.EmailConfig{
Expand All @@ -40,21 +47,21 @@ func (s *NotifiersTestSuite) SetupSuite() {
s.Require().NotNil(s.notifier)
}

func (s *NotifiersTestSuite) TearDownSuite() {
func (s *NotifiersTestSuite) TearDownTest() {
// Teardown routines use their own context to avoid not being run at all
// when the suite gets cancelled or times out.
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
ctx, cancel := context.WithTimeout(context.WithoutCancel(s.ctx), time.Second*15)
defer cancel()

err := s.client.Notifiers.Delete(ctx, s.notifier.ID)
s.NoError(err)

s.IntegrationTestSuite.TearDownSuite()
s.IntegrationTestSuite.TearDownTest()
}

func (s *NotifiersTestSuite) Test() {
// Let's update the notifier.
notifier, err := s.client.Notifiers.Update(s.suiteCtx, s.notifier.ID, axiom.Notifier{
notifier, err := s.client.Notifiers.Update(s.ctx, s.notifier.ID, axiom.Notifier{
Name: "Updated Test Notifier",
Properties: axiom.NotifierProperties{
Email: &axiom.EmailConfig{
Expand Down

0 comments on commit 8e5edaf

Please sign in to comment.