From 30e20c6a20c53adddc386f95057b159dcb393753 Mon Sep 17 00:00:00 2001 From: Zhen Wang Date: Tue, 30 Jul 2019 15:11:48 -0700 Subject: [PATCH] Validate that permanent problem has preset default condition --- pkg/custompluginmonitor/types/config.go | 17 +++++++ pkg/custompluginmonitor/types/config_test.go | 51 ++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/pkg/custompluginmonitor/types/config.go b/pkg/custompluginmonitor/types/config.go index de37169f..bdbbcf66 100644 --- a/pkg/custompluginmonitor/types/config.go +++ b/pkg/custompluginmonitor/types/config.go @@ -141,5 +141,22 @@ func (cpc CustomPluginConfig) Validate() error { } } + for _, rule := range cpc.Rules { + if rule.Type != types.Perm { + continue + } + conditionType := rule.Condition + defaultConditionExists := false + for _, cond := range cpc.DefaultConditions { + if conditionType == cond.Type { + defaultConditionExists = true + break + } + } + if !defaultConditionExists { + return fmt.Errorf("Permanent problem %s does not have preset default condition.", conditionType) + } + } + return nil } diff --git a/pkg/custompluginmonitor/types/config_test.go b/pkg/custompluginmonitor/types/config_test.go index 1deeffd3..dae09d52 100644 --- a/pkg/custompluginmonitor/types/config_test.go +++ b/pkg/custompluginmonitor/types/config_test.go @@ -20,6 +20,8 @@ import ( "reflect" "testing" "time" + + "k8s.io/node-problem-detector/pkg/types" ) func TestCustomPluginConfigApplyConfiguration(t *testing.T) { @@ -279,6 +281,55 @@ func TestCustomPluginConfigValidate(t *testing.T) { }, IsError: true, }, + "permanent problem has preset default condition": { + Conf: CustomPluginConfig{ + Plugin: customPluginName, + PluginGlobalConfig: pluginGlobalConfig{ + InvokeInterval: &defaultInvokeInterval, + Timeout: &defaultGlobalTimeout, + MaxOutputLength: &defaultMaxOutputLength, + Concurrency: &defaultConcurrency, + }, + DefaultConditions: []types.Condition{ + { + Type: "TestCondition", + Reason: "TestConditionOK", + Message: "Test condition is OK.", + }, + }, + Rules: []*CustomRule{ + { + Type: types.Perm, + Condition: "TestCondition", + Reason: "TestConditionFail", + Path: "../plugin/test-data/ok.sh", + Timeout: &normalRuleTimeout, + }, + }, + }, + IsError: false, + }, + "permanent problem does not have preset default condition": { + Conf: CustomPluginConfig{ + Plugin: customPluginName, + PluginGlobalConfig: pluginGlobalConfig{ + InvokeInterval: &defaultInvokeInterval, + Timeout: &defaultGlobalTimeout, + MaxOutputLength: &defaultMaxOutputLength, + Concurrency: &defaultConcurrency, + }, + Rules: []*CustomRule{ + { + Type: types.Perm, + Condition: "TestCondition", + Reason: "TestConditionFail", + Path: "../plugin/test-data/ok.sh", + Timeout: &normalRuleTimeout, + }, + }, + }, + IsError: true, + }, } for desp, utMeta := range utMetas {