diff --git a/hscontrol/util/util.go b/hscontrol/util/util.go new file mode 100644 index 00000000000..09fc87d3e8e --- /dev/null +++ b/hscontrol/util/util.go @@ -0,0 +1,14 @@ +package util + +import "tailscale.com/util/cmpver" + +func TailscaleVersionNewerOrEqual(minimum, toCheck string) bool { + if cmpver.Compare(minimum, toCheck) <= 0 || + toCheck == "unstable" || + toCheck == "head" { + + return true + } + + return false +} diff --git a/hscontrol/util/util_test.go b/hscontrol/util/util_test.go new file mode 100644 index 00000000000..282b52e655b --- /dev/null +++ b/hscontrol/util/util_test.go @@ -0,0 +1,95 @@ +package util + +import "testing" + +func TestTailscaleVersionNewerOrEqual(t *testing.T) { + type args struct { + minimum string + toCheck string + } + tests := []struct { + name string + args args + want bool + }{ + { + name: "is-equal", + args: args{ + minimum: "1.56", + toCheck: "1.56", + }, + want: true, + }, + { + name: "is-newer-head", + args: args{ + minimum: "1.56", + toCheck: "head", + }, + want: true, + }, + { + name: "is-newer-unstable", + args: args{ + minimum: "1.56", + toCheck: "unstable", + }, + want: true, + }, + { + name: "is-newer-patch", + args: args{ + minimum: "1.56.1", + toCheck: "1.56.1", + }, + want: true, + }, + { + name: "is-older-patch-same-minor", + args: args{ + minimum: "1.56.1", + toCheck: "1.56.0", + }, + want: false, + }, + { + name: "is-older-unstable", + args: args{ + minimum: "1.56", + toCheck: "1.55", + }, + want: false, + }, + { + name: "is-older-one-stable", + args: args{ + minimum: "1.56", + toCheck: "1.54", + }, + want: false, + }, + { + name: "is-older-five-stable", + args: args{ + minimum: "1.56", + toCheck: "1.46", + }, + want: false, + }, + { + name: "is-older-patch", + args: args{ + minimum: "1.56", + toCheck: "1.48.1", + }, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := TailscaleVersionNewerOrEqual(tt.args.minimum, tt.args.toCheck); got != tt.want { + t.Errorf("TailscaleVersionNewerThan() = %v, want %v", got, tt.want) + } + }) + } +}