Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
platform: Check valid CPU manager policy
Browse files Browse the repository at this point in the history
Signed-off-by: Suraj Deshmukh <suraj@kinvolk.io>
  • Loading branch information
surajssd committed Aug 24, 2021
1 parent ec7c936 commit bc54c24
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
34 changes: 34 additions & 0 deletions pkg/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package platform
import (
"fmt"
"path/filepath"
"sort"

"github.com/hashicorp/hcl/v2"
"helm.sh/helm/v3/pkg/chart"
Expand Down Expand Up @@ -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
}
65 changes: 65 additions & 0 deletions pkg/platform/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit bc54c24

Please sign in to comment.