|
| 1 | +package bundle_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + |
| 8 | + "github.com/operator-framework/api/pkg/operators/v1alpha1" |
| 9 | + |
| 10 | + "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/bundle" |
| 11 | + . "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/util/testing" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + installNamespace = "install-namespace" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + requiredConfigSet = []v1alpha1.InstallModeType{ |
| 20 | + v1alpha1.InstallModeTypeSingleNamespace, |
| 21 | + } |
| 22 | + |
| 23 | + optionalConfigSet = []v1alpha1.InstallModeType{ |
| 24 | + v1alpha1.InstallModeTypeAllNamespaces, |
| 25 | + v1alpha1.InstallModeTypeSingleNamespace, |
| 26 | + } |
| 27 | + |
| 28 | + optionalRestrictedConfigSet = []v1alpha1.InstallModeType{ |
| 29 | + v1alpha1.InstallModeTypeAllNamespaces, |
| 30 | + v1alpha1.InstallModeTypeOwnNamespace, |
| 31 | + } |
| 32 | + |
| 33 | + notRequiredConfigSet = []v1alpha1.InstallModeType{ |
| 34 | + v1alpha1.InstallModeTypeAllNamespaces, |
| 35 | + } |
| 36 | +) |
| 37 | + |
| 38 | +func Test_ValidatedBundleConfigFromRaw_WatchNamespace_Configuration(t *testing.T) { |
| 39 | + // The behavior of watchNamespace is dynamic and depends on the install modes supported by |
| 40 | + // the bundle (declared in its ClusterServiceVersion). For instance, a bundle that only |
| 41 | + // supports AllNamespaces or only supports OwnNamespace mode does not need a watchNamespace configuration, or |
| 42 | + // a bundle that supports AllNamespaces and OwnNamespace install modes will have an optional watchNamespace |
| 43 | + // configuration, however when set, the value must be equal to the install namespace |
| 44 | + for _, tt := range []struct { |
| 45 | + name string |
| 46 | + supportedInstallModes []v1alpha1.InstallModeType |
| 47 | + rawConfig map[string]interface{} |
| 48 | + expectedErrMsgFragment string |
| 49 | + expectedConfig *bundle.Config |
| 50 | + }{ |
| 51 | + { |
| 52 | + name: "watchNamespace is required and provided", |
| 53 | + supportedInstallModes: requiredConfigSet, |
| 54 | + rawConfig: map[string]interface{}{ |
| 55 | + "watchNamespace": "some-namespace", |
| 56 | + }, |
| 57 | + expectedConfig: &bundle.Config{ |
| 58 | + WatchNamespace: "some-namespace", |
| 59 | + }, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "watchNamespace is required and provided but invalid", |
| 63 | + supportedInstallModes: requiredConfigSet, |
| 64 | + rawConfig: map[string]interface{}{ |
| 65 | + "watchNamespace": "not a valid namespace name", |
| 66 | + }, |
| 67 | + expectedErrMsgFragment: "'not a valid namespace name' is not valid RFC-1123", |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "watchNamespace is required and provided but empty", |
| 71 | + supportedInstallModes: requiredConfigSet, |
| 72 | + rawConfig: map[string]interface{}{ |
| 73 | + "watchNamespace": "", |
| 74 | + }, |
| 75 | + expectedErrMsgFragment: "'' is not valid RFC-1123", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "watchNamespace is required and not provided (nil)", |
| 79 | + supportedInstallModes: requiredConfigSet, |
| 80 | + rawConfig: nil, |
| 81 | + expectedConfig: nil, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "watchNamespace is required and not provided (empty config)", |
| 85 | + supportedInstallModes: requiredConfigSet, |
| 86 | + rawConfig: map[string]interface{}{}, |
| 87 | + expectedConfig: nil, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "watchNamespace is optional and provided", |
| 91 | + supportedInstallModes: optionalConfigSet, |
| 92 | + rawConfig: map[string]interface{}{ |
| 93 | + "watchNamespace": "some-namespace", |
| 94 | + }, |
| 95 | + expectedConfig: &bundle.Config{ |
| 96 | + WatchNamespace: "some-namespace", |
| 97 | + }, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "watchNamespace is optional and provided but invalid", |
| 101 | + supportedInstallModes: optionalConfigSet, |
| 102 | + rawConfig: map[string]interface{}{ |
| 103 | + "watchNamespace": "not a valid namespace name", |
| 104 | + }, |
| 105 | + expectedErrMsgFragment: "'not a valid namespace name' is not valid RFC-1123", |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "watchNamespace is optional and not provided (nil config)", |
| 109 | + supportedInstallModes: optionalConfigSet, |
| 110 | + rawConfig: nil, |
| 111 | + expectedConfig: nil, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "watchNamespace is optional and not provided (empty config)", |
| 115 | + supportedInstallModes: optionalConfigSet, |
| 116 | + rawConfig: map[string]interface{}{}, |
| 117 | + expectedConfig: nil, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "watchNamespace is optional and not provided (nil)", |
| 121 | + supportedInstallModes: optionalConfigSet, |
| 122 | + rawConfig: map[string]interface{}{ |
| 123 | + "watchNamespace": nil, |
| 124 | + }, |
| 125 | + expectedConfig: &bundle.Config{}, |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "watchNamespace is optional and restricted to install-namespace and correctly provided", |
| 129 | + supportedInstallModes: optionalRestrictedConfigSet, |
| 130 | + rawConfig: map[string]interface{}{ |
| 131 | + "watchNamespace": "install-namespace", |
| 132 | + }, |
| 133 | + expectedConfig: &bundle.Config{ |
| 134 | + WatchNamespace: "install-namespace", |
| 135 | + }, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "watchNamespace is optional and restricted to install-namespace and incorrectly provided", |
| 139 | + supportedInstallModes: optionalRestrictedConfigSet, |
| 140 | + rawConfig: map[string]interface{}{ |
| 141 | + "watchNamespace": "not-install-namespace", |
| 142 | + }, |
| 143 | + expectedErrMsgFragment: "value must be one of 'install-namespace', <nil>", |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "watchNamespace is optional and restricted to install-namespace and not provided (nil)", |
| 147 | + supportedInstallModes: optionalRestrictedConfigSet, |
| 148 | + rawConfig: map[string]interface{}{ |
| 149 | + "watchNamespace": nil, |
| 150 | + }, |
| 151 | + expectedConfig: &bundle.Config{}, |
| 152 | + }, |
| 153 | + { |
| 154 | + name: "watchNamespace is optional and restricted to install-namespace and not provided (nil config)", |
| 155 | + supportedInstallModes: optionalRestrictedConfigSet, |
| 156 | + rawConfig: nil, |
| 157 | + expectedConfig: nil, |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "watchNamespace is optional and restricted to install-namespace and not provided (empty config)", |
| 161 | + supportedInstallModes: optionalRestrictedConfigSet, |
| 162 | + rawConfig: map[string]interface{}{}, |
| 163 | + expectedConfig: nil, |
| 164 | + }, |
| 165 | + { |
| 166 | + name: "watchNamespace is not a config option and it is set", |
| 167 | + supportedInstallModes: notRequiredConfigSet, |
| 168 | + rawConfig: map[string]interface{}{ |
| 169 | + "watchNamespace": "some-namespace", |
| 170 | + }, |
| 171 | + expectedErrMsgFragment: "additional properties 'watchNamespace' not allowed", |
| 172 | + }, |
| 173 | + { |
| 174 | + name: "watchNamespace is not a config option and it is not set (nil)", |
| 175 | + supportedInstallModes: notRequiredConfigSet, |
| 176 | + rawConfig: map[string]interface{}{ |
| 177 | + "watchNamespace": nil, |
| 178 | + }, |
| 179 | + expectedErrMsgFragment: "additional properties 'watchNamespace' not allowed", |
| 180 | + }, |
| 181 | + { |
| 182 | + name: "watchNamespace is not a config option and it is not set (config empty)", |
| 183 | + supportedInstallModes: notRequiredConfigSet, |
| 184 | + rawConfig: map[string]interface{}{}, |
| 185 | + expectedConfig: nil, |
| 186 | + }, |
| 187 | + { |
| 188 | + name: "watchNamespace is not a config option and it is not set (config nil)", |
| 189 | + supportedInstallModes: notRequiredConfigSet, |
| 190 | + rawConfig: nil, |
| 191 | + expectedConfig: nil, |
| 192 | + }, |
| 193 | + } { |
| 194 | + t.Run(tt.name, func(t *testing.T) { |
| 195 | + rv1 := bundle.RegistryV1{ |
| 196 | + CSV: MakeCSV(WithInstallModeSupportFor(tt.supportedInstallModes...)), |
| 197 | + } |
| 198 | + |
| 199 | + cfg, err := bundle.ValidatedBundleConfigFromRaw(rv1, installNamespace, tt.rawConfig) |
| 200 | + |
| 201 | + if tt.expectedErrMsgFragment == "" { |
| 202 | + require.NoError(t, err) |
| 203 | + require.Equal(t, tt.expectedConfig, cfg) |
| 204 | + } else { |
| 205 | + require.Error(t, err) |
| 206 | + require.Contains(t, err.Error(), tt.expectedErrMsgFragment) |
| 207 | + } |
| 208 | + }) |
| 209 | + } |
| 210 | +} |
0 commit comments