Skip to content

Commit

Permalink
fix(teamsnotifier): Sort update keys to avoid flaky tests (#2642)
Browse files Browse the repository at this point in the history
* fix(teamsnotifier): Sort update keys to avoid flaky tests

* add placeholder for hostname
  • Loading branch information
thomaspoignant authored Nov 10, 2024
1 parent 9901f0e commit 67e6395
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 6 additions & 0 deletions notifier/microsoftteamsnotifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ func convertToMicrosoftTeamsMessage(diffCache notifier.DiffCache) string {
value := diffCache.Updated[key]
msgText += fmt.Sprintf("\n * ✏️ Flag **%s** updated", key)
changelog, _ := diff.Diff(value.Before, value.After, diff.AllowTypeMismatch(true))

// sort the changelog by path
sort.Slice(changelog, func(i, j int) bool {
return strings.Join(changelog[i].Path, ".") < strings.Join(changelog[j].Path, ".")
})

for _, change := range changelog {
if change.Type == "update" {
msgText += fmt.Sprintf("\n * %s: %s => %s", strings.Join(change.Path, "."),
Expand Down
8 changes: 5 additions & 3 deletions notifier/microsoftteamsnotifier/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package microsoftteamsnotifier

import (
"bytes"
"fmt"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -100,9 +101,9 @@ func TestMicrosoftTeamsNotifier_Notify(t *testing.T) {
},
After: &flag.InternalFlag{
Variations: &map[string]*interface{}{
"Default": testconvert.Interface("strDefault"),
"False": testconvert.Interface("strFalse"),
"True": testconvert.Interface("strTrue"),
"Default": testconvert.Interface(true),
"False": testconvert.Interface(false),
"True": testconvert.Interface(true),
},
Rules: &[]flag.Rule{
{
Expand Down Expand Up @@ -173,6 +174,7 @@ func TestMicrosoftTeamsNotifier_Notify(t *testing.T) {
hostname, _ := os.Hostname()
wantBody, err := os.ReadFile(tt.want)
require.NoError(t, err)
fmt.Println(tt.roundTripper.requestBody)
assert.JSONEq(t, strings.ReplaceAll(string(wantBody), "{{hostname}}", hostname), tt.roundTripper.requestBody)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
{
"type": "TextBlock",
"text": "Changes detected in your feature flag file on: **{{hostname}}**\n * ❌ Flag **test-flag** deleted\n * 🆕 Flag **test-flag3** created\n * ✏️ Flag **test-flag2** updated\n * Variations.Default: false =\u003e \"strDefault\"\n * Variations.False: false =\u003e \"strFalse\"\n * Variations.True: true =\u003e \"strTrue\"\n * Rules: nil =\u003e (*[]flag.Rule){flag.Rule{Name:(*string)(\"rule1\"), Query:(*string)(\"key eq \\\"not-a-ke\\\"\"), VariationResult:(*string)(nil), Percentages:(*map[string]float64){\"False\":20, \"True\":80}, ProgressiveRollout:(*flag.ProgressiveRollout)(nil), Disable:(*bool)(nil)}}\n * DefaultRule.VariationResult: nil =\u003e (*string)(\"Default\")\n * DefaultRule.Percentages: (*map[string]float64){\"False\":0, \"True\":100} =\u003e nil\n * Experimentation: (*flag.ExperimentationRollout){Start:(*time.Time){wall:0, ext:63230976200, loc:(*time.Location){name:\"\", zone:[]time.zone(nil), tx:[]time.zoneTrans(nil), extend:\"\", cacheStart:0, cacheEnd:0, cacheZone:(*time.zone)(nil)}}, End:(*time.Time){wall:0, ext:63230967800, loc:(*time.Location){name:\"\", zone:[]time.zone(nil), tx:[]time.zoneTrans(nil), extend:\"\", cacheStart:0, cacheEnd:0, cacheZone:(*time.zone)(nil)}}} =\u003e nil\n * TrackEvents: nil =\u003e (*bool)(false)\n * Disable: nil =\u003e (*bool)(true)\n * Version: nil =\u003e (*string)(\"1.1\")",
"text": "Changes detected in your feature flag file on: **{{hostname}}**\n * ❌ Flag **test-flag** deleted\n * 🆕 Flag **test-flag3** created\n * ✏️ Flag **test-flag2** updated\n * DefaultRule.Percentages: (*map[string]float64){\"False\":0, \"True\":100} =\u003e nil\n * DefaultRule.VariationResult: nil =\u003e (*string)(\"Default\")\n * Disable: nil =\u003e (*bool)(true)\n * Experimentation: (*flag.ExperimentationRollout){Start:(*time.Time){wall:0, ext:63230976200, loc:(*time.Location){name:\"\", zone:[]time.zone(nil), tx:[]time.zoneTrans(nil), extend:\"\", cacheStart:0, cacheEnd:0, cacheZone:(*time.zone)(nil)}}, End:(*time.Time){wall:0, ext:63230967800, loc:(*time.Location){name:\"\", zone:[]time.zone(nil), tx:[]time.zoneTrans(nil), extend:\"\", cacheStart:0, cacheEnd:0, cacheZone:(*time.zone)(nil)}}} =\u003e nil\n * Rules: nil =\u003e (*[]flag.Rule){flag.Rule{Name:(*string)(\"rule1\"), Query:(*string)(\"key eq \\\"not-a-ke\\\"\"), VariationResult:(*string)(nil), Percentages:(*map[string]float64){\"False\":20, \"True\":80}, ProgressiveRollout:(*flag.ProgressiveRollout)(nil), Disable:(*bool)(nil)}}\n * TrackEvents: nil =\u003e (*bool)(false)\n * Variations.Default: false =\u003e true\n * Version: nil =\u003e (*string)(\"1.1\")",
"wrap": true
}
],
Expand Down

0 comments on commit 67e6395

Please sign in to comment.