Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib/resourcemerge: add extra merge testing
This aims to add some initial tests for EnvVar handling specifically, to make sure we don't regress proxy. Other unit tests should be added as we clean up the code. Signed-off-by: Yu Qi Zhang <jerzhang@redhat.com>
- Loading branch information
1 parent
64adf80
commit a01f4b8
Showing
1 changed file
with
133 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package resourcemerge | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/equality" | ||
) | ||
|
||
// Test added for now to test ensureContainer (mostly so we don't break Env Vars - proxy) | ||
func TestEnsureContainer(t *testing.T) { | ||
tests := []struct { | ||
existing corev1.Container | ||
input corev1.Container | ||
|
||
expectedModified bool | ||
expected corev1.Container | ||
}{ | ||
// Change in proxy | ||
{ | ||
existing: corev1.Container{ | ||
Env: []corev1.EnvVar{ | ||
{ | ||
Name: "Proxy", | ||
Value: "Proxy1", | ||
}, | ||
}, | ||
}, | ||
input: corev1.Container{ | ||
Env: []corev1.EnvVar{ | ||
{ | ||
Name: "Proxy", | ||
Value: "Proxy2", | ||
}, | ||
}, | ||
}, | ||
expectedModified: true, | ||
expected: corev1.Container{ | ||
Env: []corev1.EnvVar{ | ||
{ | ||
Name: "Proxy", | ||
Value: "Proxy2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
// Add Proxy | ||
{ | ||
existing: corev1.Container{ | ||
Env: []corev1.EnvVar{}, | ||
}, | ||
input: corev1.Container{ | ||
Env: []corev1.EnvVar{ | ||
{ | ||
Name: "Proxy", | ||
Value: "Proxy1", | ||
}, | ||
}, | ||
}, | ||
expectedModified: true, | ||
expected: corev1.Container{ | ||
Env: []corev1.EnvVar{ | ||
{ | ||
Name: "Proxy", | ||
Value: "Proxy1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
// Remove Proxy | ||
{ | ||
existing: corev1.Container{ | ||
Env: []corev1.EnvVar{ | ||
{ | ||
Name: "Proxy", | ||
Value: "Proxy1", | ||
}, | ||
}, | ||
}, | ||
input: corev1.Container{ | ||
Env: []corev1.EnvVar{}, | ||
}, | ||
expectedModified: true, | ||
expected: corev1.Container{ | ||
Env: []corev1.EnvVar{}, | ||
}, | ||
}, | ||
// No change | ||
{ | ||
existing: corev1.Container{ | ||
Env: []corev1.EnvVar{ | ||
{ | ||
Name: "Proxy", | ||
Value: "Proxy1", | ||
}, | ||
}, | ||
}, | ||
input: corev1.Container{ | ||
Env: []corev1.EnvVar{ | ||
{ | ||
Name: "Proxy", | ||
Value: "Proxy1", | ||
}, | ||
}, | ||
}, | ||
expectedModified: false, | ||
expected: corev1.Container{ | ||
Env: []corev1.EnvVar{ | ||
{ | ||
Name: "Proxy", | ||
Value: "Proxy1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for idx, test := range tests { | ||
t.Run(fmt.Sprintf("test#%d", idx), func(t *testing.T) { | ||
modified := BoolPtr(false) | ||
ensureContainer(modified, &test.existing, test.input) | ||
|
||
if *modified != test.expectedModified { | ||
t.Fatalf("mismatch container got: %v want: %v", *modified, test.expectedModified) | ||
} | ||
|
||
if !equality.Semantic.DeepEqual(test.existing, test.expected) { | ||
t.Fatalf("mismatch container got: %v want: %v", test.existing, test.expected) | ||
} | ||
}) | ||
} | ||
} |