From bc54c24f5c4ef11094ed7a5246d7b54b09d49659 Mon Sep 17 00:00:00 2001 From: Suraj Deshmukh Date: Mon, 23 Aug 2021 19:47:29 +0530 Subject: [PATCH] platform: Check valid CPU manager policy Signed-off-by: Suraj Deshmukh --- pkg/platform/platform.go | 34 ++++++++++++++++++ pkg/platform/platform_test.go | 65 +++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/pkg/platform/platform.go b/pkg/platform/platform.go index 4fbbc6f0d..c2caeff7b 100644 --- a/pkg/platform/platform.go +++ b/pkg/platform/platform.go @@ -17,6 +17,7 @@ package platform import ( "fmt" "path/filepath" + "sort" "github.com/hashicorp/hcl/v2" "helm.sh/helm/v3/pkg/chart" @@ -281,3 +282,36 @@ func WorkerPoolNamesUnique(pools []WorkerPool) hcl.Diagnostics { return d } + +// CheckCPUManagerPolicy takes a map with key as worker pool name and value as the CPU Manager +// Policy value. This function checks if the values are valid. +func CheckCPUManagerPolicy(wp map[string]string) hcl.Diagnostics { + var diagnostics hcl.Diagnostics + + // Gather all worker pool names. + wps := make([]string, len(wp)) + + for k := range wp { + wps = append(wps, k) + } + + // Sort in ascending order to return consistent results. + sort.Strings(wps) + + for _, name := range wps { + policy := wp[name] + + switch policy { + case "", "none", "static": + continue + default: + diagnostics = append(diagnostics, &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "invalid cpu_manager_policy", + Detail: fmt.Sprintf("Worker pool %q has invalid cpu_manager_policy %q", name, policy), + }) + } + } + + return diagnostics +} diff --git a/pkg/platform/platform_test.go b/pkg/platform/platform_test.go index 0f23a0856..49bed9421 100644 --- a/pkg/platform/platform_test.go +++ b/pkg/platform/platform_test.go @@ -17,6 +17,8 @@ package platform_test import ( "testing" + "github.com/google/go-cmp/cmp" + "github.com/hashicorp/hcl/v2" "github.com/kinvolk/lokomotive/pkg/platform" ) @@ -178,3 +180,66 @@ func TestNodeLocalDNSIsIncludedInCommonChartsWhenRequested(t *testing.T) { t.Fatalf("NodeLocalDNS should be included in charts list when requested") } } + +func TestCheckCPUManagerPolicy(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + wp map[string]string + want hcl.Diagnostics + }{ + { + name: "valid_cpu_manager_policy_values", + wp: map[string]string{ + "wp_a": "", + "wp_b": "static", + "wp_c": "none", + }, + want: nil, + }, + { + name: "invalid_cpu_manager_policy_value", + wp: map[string]string{ + "wp_a": "", + "foobar": "foobar", + }, + want: hcl.Diagnostics{&hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "invalid cpu_manager_policy", + Detail: "Worker pool \"foobar\" has invalid cpu_manager_policy \"foobar\"", + }}, + }, + { + name: "all_invalid_values", + wp: map[string]string{ + "foo": "foo", + "bar": "bar", + }, + want: hcl.Diagnostics{ + &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "invalid cpu_manager_policy", + Detail: "Worker pool \"bar\" has invalid cpu_manager_policy \"bar\"", + }, + &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "invalid cpu_manager_policy", + Detail: "Worker pool \"foo\" has invalid cpu_manager_policy \"foo\"", + }, + }, + }, + } + for _, tt := range tests { + tt := tt + + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + got := platform.CheckCPUManagerPolicy(tt.wp) + if diff := cmp.Diff(got, tt.want); diff != "" { + t.Errorf("got unexpected diagnostics: -want +got\n%s", diff) + } + }) + } +}