diff --git a/CHANGELOG.md b/CHANGELOG.md index b301640..6dab2d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/internal/ingressmonitor/operator.go b/internal/ingressmonitor/operator.go index 0630f61..000391d 100644 --- a/internal/ingressmonitor/operator.go +++ b/internal/ingressmonitor/operator.go @@ -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) diff --git a/internal/provider/statuscake/statuscake.go b/internal/provider/statuscake/statuscake.go index e7cb8fd..bb29dc9 100644 --- a/internal/provider/statuscake/statuscake.go +++ b/internal/provider/statuscake/statuscake.go @@ -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) @@ -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 } @@ -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 { diff --git a/internal/provider/statuscake/statuscake_test.go b/internal/provider/statuscake/statuscake_test.go index 96448ca..e7e6783 100644 --- a/internal/provider/statuscake/statuscake_test.go +++ b/internal/provider/statuscake/statuscake_test.go @@ -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) } }) } @@ -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 {