-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Respect optional param for ConfigMaps and Secrets (#562)
* Respect optional in ConfigMaps and Secrets * add test for resolveEnv * remove name from testMetadata
- Loading branch information
1 parent
104c71b
commit 16cae71
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package handler | ||
|
||
import ( | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
) | ||
|
||
var ( | ||
namespace string = "test-namespace" | ||
trueValue bool = true | ||
falseValue bool = false | ||
) | ||
|
||
type testMetadata struct { | ||
isError bool | ||
comment string | ||
container *corev1.Container | ||
} | ||
|
||
var testMetadatas = []testMetadata{ | ||
{ | ||
isError: true, | ||
comment: "configmap does not exist, and it is not marked as an optional, there should be an error", | ||
container: &corev1.Container{ | ||
EnvFrom: []corev1.EnvFromSource{{ | ||
ConfigMapRef: &corev1.ConfigMapEnvSource{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "do-not-exist-not-optional", | ||
}, | ||
}, | ||
}}, | ||
}, | ||
}, | ||
{ | ||
isError: true, | ||
comment: "secret does not exist, and it is not marked as an optional, there should be an error", | ||
container: &corev1.Container{ | ||
EnvFrom: []corev1.EnvFromSource{{ | ||
SecretRef: &corev1.SecretEnvSource{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "do-not-exist-not-optional", | ||
}, | ||
}, | ||
}}, | ||
}, | ||
}, | ||
{ | ||
isError: false, | ||
comment: "configmap does not exist, but it is marked as an optional, there should not be an error", | ||
container: &corev1.Container{ | ||
EnvFrom: []corev1.EnvFromSource{{ | ||
ConfigMapRef: &corev1.ConfigMapEnvSource{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "do-not-exist-but-optional", | ||
}, | ||
Optional: &trueValue, | ||
}, | ||
}}, | ||
}, | ||
}, | ||
{ | ||
isError: false, | ||
comment: "secret does not exist, but it is marked as an optional, there should not be an error", | ||
container: &corev1.Container{ | ||
EnvFrom: []corev1.EnvFromSource{{ | ||
SecretRef: &corev1.SecretEnvSource{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "do-not-exist-but-optional", | ||
}, | ||
Optional: &trueValue, | ||
}, | ||
}}, | ||
}, | ||
}, | ||
{ | ||
isError: true, | ||
comment: "configmap does not exist, and it is not marked as an optional, there should be an error", | ||
container: &corev1.Container{ | ||
EnvFrom: []corev1.EnvFromSource{{ | ||
ConfigMapRef: &corev1.ConfigMapEnvSource{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "do-not-exist-and-not-optional-explicitly", | ||
}, | ||
Optional: &falseValue, | ||
}, | ||
}}, | ||
}, | ||
}, | ||
{ | ||
isError: true, | ||
comment: "secret does not exist, and it is not marked as an optional, there should be an error", | ||
container: &corev1.Container{ | ||
EnvFrom: []corev1.EnvFromSource{{ | ||
SecretRef: &corev1.SecretEnvSource{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "do-not-exist-and-not-optional-explicitly", | ||
}, | ||
Optional: &falseValue, | ||
}, | ||
}}, | ||
}, | ||
}, | ||
} | ||
|
||
func TestResolveNonExistingConfigMapsOrSecretsEnv(t *testing.T) { | ||
|
||
for _, testData := range testMetadatas { | ||
testScaleHandler := NewScaleHandler(fake.NewFakeClient(), scheme.Scheme) | ||
|
||
_, err := testScaleHandler.resolveEnv(testData.container, namespace) | ||
|
||
if err != nil && !testData.isError { | ||
t.Errorf("Expected success because %s got error, %s", testData.comment, err) | ||
} | ||
|
||
if testData.isError && err == nil { | ||
t.Errorf("Expected error because %s but got success, %#v", testData.comment, testData) | ||
} | ||
} | ||
} |