Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions internal/diff/differ.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"maps"
"os"
"reflect"
"regexp"
"slices"
"sort"
Expand Down Expand Up @@ -2037,6 +2038,7 @@ func removeEmptyJSONValues(v any) any {
}
out[k] = removeEmptyJSONValues(val)
}
foldTeamsAlias(out)
return out
case []any:
out := make([]any, 0, len(x))
Expand All @@ -2058,6 +2060,25 @@ func removeEmptyJSONValues(v any) any {
}
}

// foldTeamsAlias treats `teams` as the legacy spelling of `fleets` (Fleet
// 4.92 renamed it, e.g. on VPP tokens, and returns both with the same
// members). A lone `teams` becomes `fleets`; a `teams` equal to `fleets` is
// dropped. A `teams` that differs is left alone so the difference shows.
func foldTeamsAlias(m map[string]any) {
teams, ok := m["teams"]
if !ok {
return
}
fleets, ok := m["fleets"]
switch {
case !ok:
m["fleets"] = teams
case !reflect.DeepEqual(teams, fleets):
return
}
delete(m, "teams")
}

// flattenMap recursively flattens a nested map into dot-separated key paths.
// Calls fn(key, value) for each leaf value. Slices are serialized to JSON
// for stable, order-independent comparison.
Expand Down
49 changes: 49 additions & 0 deletions internal/diff/differ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3841,3 +3841,52 @@ func TestDiffNewTeamBaselineDrift(t *testing.T) {
t.Errorf("queries added: got %+v, want Q flagged", q)
}
}

// Fleet 4.92 renamed a VPP token's `teams` to `fleets` and returns both, with
// the same members. YAML with only `fleets` (or only the old `teams`) must not
// read as a change to volume_purchasing_program.
func TestDiffConfigVPPTeamsAlias(t *testing.T) {
api := map[string]any{"mdm": map[string]any{"volume_purchasing_program": []any{
map[string]any{"location": "Fleet macOS", "fleets": []any{"Workstations"}, "teams": []any{"Workstations"}},
}}}
tests := []struct {
name string
entry map[string]any
want int
}{
{"fleets only", map[string]any{"location": "Fleet macOS", "fleets": []any{"Workstations"}}, 0},
{"legacy teams only", map[string]any{"location": "Fleet macOS", "teams": []any{"Workstations"}}, 0},
{"real membership change", map[string]any{"location": "Fleet macOS", "fleets": []any{"Servers"}}, 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
proposed := &parser.ParsedGlobal{OrgSettings: map[string]any{
"mdm": map[string]any{"volume_purchasing_program": []any{tt.entry}},
}}
changes, _ := diffConfig(api, proposed)
if len(changes) != tt.want {
t.Errorf("got %d changes %+v, want %d", len(changes), changes, tt.want)
}
})
}
}

func TestFoldTeamsAlias(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{"both equal drops teams", `{"fleets":["a"],"teams":["a"]}`, `{"fleets":["a"]}`},
{"lone teams renamed", `{"teams":["a"]}`, `{"fleets":["a"]}`},
{"differing teams kept", `{"fleets":["a"],"teams":["b"]}`, `{"fleets":["a"],"teams":["b"]}`},
{"no teams untouched", `{"fleets":["a"]}`, `{"fleets":["a"]}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := normalizeJSON(tt.in); got != tt.want {
t.Errorf("got %s, want %s", got, tt.want)
}
})
}
}
Loading