|
| 1 | +// Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package config |
| 15 | + |
| 16 | +import ( |
| 17 | + "os" |
| 18 | + "path/filepath" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | +) |
| 23 | + |
| 24 | +func TestLoadHealthEventBehaviourOverrides(t *testing.T) { |
| 25 | + configPath := filepath.Join(t.TempDir(), "config.toml") |
| 26 | + tomlConfig := ` |
| 27 | +[[policies]] |
| 28 | +name = "operator-pod-unhealthy" |
| 29 | +enabled = true |
| 30 | +
|
| 31 | +[policies.resource] |
| 32 | +group = "" |
| 33 | +version = "v1" |
| 34 | +kind = "Pod" |
| 35 | +
|
| 36 | +[policies.predicate] |
| 37 | +expression = "true" |
| 38 | +
|
| 39 | +[policies.healthEvent] |
| 40 | +componentClass = "Software" |
| 41 | +isFatal = true |
| 42 | +message = "operator pod is unhealthy" |
| 43 | +recommendedAction = "CONTACT_SUPPORT" |
| 44 | +errorCode = ["OPERATOR_POD_UNHEALTHY"] |
| 45 | +
|
| 46 | +[policies.healthEvent.quarantineOverrides] |
| 47 | +force = true |
| 48 | +
|
| 49 | +[policies.healthEvent.drainOverrides] |
| 50 | +skip = true |
| 51 | +` |
| 52 | + require.NoError(t, os.WriteFile(configPath, []byte(tomlConfig), 0o600)) |
| 53 | + |
| 54 | + cfg, err := Load(configPath) |
| 55 | + require.NoError(t, err) |
| 56 | + require.Len(t, cfg.Policies, 1) |
| 57 | + |
| 58 | + healthEvent := cfg.Policies[0].HealthEvent |
| 59 | + require.NotNil(t, healthEvent.QuarantineOverrides) |
| 60 | + require.True(t, healthEvent.QuarantineOverrides.Force) |
| 61 | + require.False(t, healthEvent.QuarantineOverrides.Skip) |
| 62 | + require.NotNil(t, healthEvent.DrainOverrides) |
| 63 | + require.False(t, healthEvent.DrainOverrides.Force) |
| 64 | + require.True(t, healthEvent.DrainOverrides.Skip) |
| 65 | +} |
| 66 | + |
| 67 | +func TestLoadRejectsConflictingBehaviourOverrides(t *testing.T) { |
| 68 | + tests := []struct { |
| 69 | + name string |
| 70 | + overrideTOML string |
| 71 | + wantError string |
| 72 | + }{ |
| 73 | + { |
| 74 | + name: "quarantine overrides force and skip", |
| 75 | + overrideTOML: ` |
| 76 | +[policies.healthEvent.quarantineOverrides] |
| 77 | +force = true |
| 78 | +skip = true |
| 79 | +`, |
| 80 | + wantError: `healthEvent.quarantineOverrides cannot set both force and skip`, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "drain overrides force and skip", |
| 84 | + overrideTOML: ` |
| 85 | +[policies.healthEvent.drainOverrides] |
| 86 | +force = true |
| 87 | +skip = true |
| 88 | +`, |
| 89 | + wantError: `healthEvent.drainOverrides cannot set both force and skip`, |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + for _, tt := range tests { |
| 94 | + t.Run(tt.name, func(t *testing.T) { |
| 95 | + configPath := filepath.Join(t.TempDir(), "config.toml") |
| 96 | + tomlConfig := ` |
| 97 | +[[policies]] |
| 98 | +name = "operator-pod-unhealthy" |
| 99 | +enabled = true |
| 100 | +
|
| 101 | +[policies.resource] |
| 102 | +group = "" |
| 103 | +version = "v1" |
| 104 | +kind = "Pod" |
| 105 | +
|
| 106 | +[policies.predicate] |
| 107 | +expression = "true" |
| 108 | +
|
| 109 | +[policies.healthEvent] |
| 110 | +componentClass = "Software" |
| 111 | +isFatal = true |
| 112 | +message = "operator pod is unhealthy" |
| 113 | +recommendedAction = "CONTACT_SUPPORT" |
| 114 | +errorCode = ["OPERATOR_POD_UNHEALTHY"] |
| 115 | +` + tt.overrideTOML |
| 116 | + require.NoError(t, os.WriteFile(configPath, []byte(tomlConfig), 0o600)) |
| 117 | + |
| 118 | + _, err := Load(configPath) |
| 119 | + require.Error(t, err) |
| 120 | + require.Contains(t, err.Error(), tt.wantError) |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
0 commit comments