Skip to content

Commit

Permalink
Merge pull request #13 from jelmersnoeck/add-statuscake-fixes
Browse files Browse the repository at this point in the history
Add fixes for StatusCake.
  • Loading branch information
jelmersnoeck authored Sep 2, 2018
2 parents d729e48 + ef1ff53 commit f62de0f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## v0.1.1 - 2018-09-02

### Fixed

- Always post StatusCodes to StatusCake, ensuring we error on faulty codes.
- Handle monitor data changes with StatusCake, where an update caused the ID to reset to 0 and create a new monitor.

## v0.1.0 - 2018-09-01

### Added
Expand Down
3 changes: 3 additions & 0 deletions internal/ingressmonitor/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ func (o *Operator) handleIngressMonitor(obj *v1alpha1.IngressMonitor) error {
return err
}

// The ID has changed, update the status. This could happen when the test
// has been removed from the provider. The operator ensures that the test
// will be present, and thus create a new one.
if obj.Status.ID != id {
obj.Status.ID = id
_, err = o.imClient.Ingressmonitor().IngressMonitors(obj.Namespace).Update(obj)
Expand Down
11 changes: 11 additions & 0 deletions internal/provider/statuscake/statuscake.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
"github.com/DreamItGetIT/statuscake"
)

// The StatusCake Client we're using expects us to set this.
// XXX remove this once we move to our own internal client.
const statusCodes = "204,205,206,303,400,401,403,404,405,406,408,410,413,444,429,494,495,496,499,500,501,502,503,504,505,506,507,508,509,510,511,521,522,523,524,520,598,599"

// Register registers the provider with a certain factory using the FactoryFunc.
func Register(fact provider.FactoryInterface) {
fact.Register("StatusCake", FactoryFunc)
Expand Down Expand Up @@ -136,6 +140,12 @@ func (c *Client) Update(id string, spec v1alpha1.MonitorTemplateSpec) (string, e
return id, err
}

// The StatusCake API returns no ID if there is an update to the item. This
// means we need to check for this and actually avoid returning a "0" id.
if sct.TestID == 0 {
return id, nil
}

return strconv.Itoa(sct.TestID), nil
}

Expand All @@ -146,6 +156,7 @@ func (c *Client) translateSpec(spec v1alpha1.MonitorTemplateSpec) (*statuscake.T
WebsiteName: spec.Name,
TestType: spec.Type,
ContactGroup: c.groups,
StatusCodes: statusCodes,
}

if spec.Timeout != nil {
Expand Down
43 changes: 41 additions & 2 deletions internal/provider/statuscake/statuscake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,11 @@ func TestTranslateSpec(t *testing.T) {
return
}

if !reflect.DeepEqual(translation, tc.expected) {
t.Errorf("Expected translation to equal \n%#v\ngot\n%#v", tc.expected, translation)
exp := tc.expected
exp.StatusCodes = statusCodes

if !reflect.DeepEqual(translation, exp) {
t.Errorf("Expected translation to equal \n%#v\ngot\n%#v", exp, translation)
}
})
}
Expand Down Expand Up @@ -451,6 +454,42 @@ func TestClient_Update(t *testing.T) {
t.Errorf("Expected 1 udpate call, got %d", fc.updateCount)
}
})

t.Run("with changed fields", func(t *testing.T) {
defer fc.flush()

tpl := v1alpha1.MonitorTemplateSpec{
Type: "HTTP",
HTTP: &v1alpha1.HTTPTemplate{
CustomHeader: "Test-Header",
UserAgent: "(Test User Agent)", URL: "http://fully-qualified-url.com",
FollowRedirects: true,
},
}

fc.updateFunc = func(sct *statuscake.Test) (*statuscake.Test, error) {
if sct.TestID != 12345 {
t.Errorf("Expected TestID to be `12345`, got `%d`", sct.TestID)
}

// StatusCake sets the ID to 0 if there's changes.
sct.TestID = 0
return sct, nil
}

id, err := cl.Update("12345", tpl)
if err != nil {
t.Errorf("Expected no error, got %s", err)
}

if fc.updateCount != 1 {
t.Errorf("Expected 1 udpate call, got %d", fc.updateCount)
}

if id != "12345" {
t.Errorf("Expected ID to be `12345`, got `%s`", id)
}
})
}

type fakeClient struct {
Expand Down

0 comments on commit f62de0f

Please sign in to comment.