Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
Unit tests that verify correct Diff() behavior on Kubernetes Resources (
Browse files Browse the repository at this point in the history
#1)

Adds unit tests to verify that the Diff() function works correctly on Kubernetes Resources - namely, that it will treat equivalent Resources, but specified in different units, as equal.

A new import has been added for k8s.io/apimachinery 0.18.6 - this is the same version already approved internally and used in https://github.com/verrazzano/verrazzano: https://github.com/verrazzano/verrazzano/blob/master/go.mod#L44
  • Loading branch information
Dan Simone authored May 25, 2021
1 parent dddce4b commit 9286021
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
7 changes: 4 additions & 3 deletions THIRD_PARTY_LICENSES.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
github.com/verrazzano/pkg
-------- Copyrights
Copyright (c) 2020 Oracle America, Inc. and its affiliates.
Copyright (c) 2021 Oracle America, Inc. and its affiliates.
Copyright (c) 2021, Oracle and/or its affiliates.
Copyright (C) 2021, Oracle and/or its affiliates.

-------- License
Copyright (c) 2020 Oracle America, Inc. and its affiliates.
Copyright (c) 2021 Oracle America, Inc. and its affiliates.

The Universal Permissive License (UPL), Version 1.0

Expand Down Expand Up @@ -85,4 +86,4 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

ATTRIBUTION-HELPER-GENERATED:
License file based on go.mod with md5 sum: ceeb284685d01c120a0c982ef88a56e7
License file based on go.mod with md5 sum: ad8ff9601475c864dcd8d768548e6017
28 changes: 28 additions & 0 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"

"testing"
)
Expand Down Expand Up @@ -185,6 +186,33 @@ func TestTimes(t *testing.T) {
verifyDiff(t, fromObj, toObj, true, "Time changed")
}

func TestResources(t *testing.T) {
r1 := corev1.ResourceRequirements{Limits: corev1.ResourceList{corev1.ResourceMemory: resource.MustParse("1024Mi")}}
r2 := corev1.ResourceRequirements{Limits: corev1.ResourceList{corev1.ResourceMemory: resource.MustParse("1Gi")}}
verifyDiff(t, r1, r2, false, "Same resource, different units")
r1 = corev1.ResourceRequirements{Limits: corev1.ResourceList{corev1.ResourceMemory: resource.MustParse("1028Mi")}}
r2 = corev1.ResourceRequirements{Limits: corev1.ResourceList{corev1.ResourceMemory: resource.MustParse("1Gi")}}
verifyDiff(t, r1, r2, true, "Different resource, different units")
r1 = corev1.ResourceRequirements{Limits: corev1.ResourceList{corev1.ResourceMemory: resource.MustParse("1028Mi")}}
r2 = corev1.ResourceRequirements{Limits: corev1.ResourceList{corev1.ResourceMemory: resource.MustParse("1030Mi")}}
verifyDiff(t, r1, r2, true, "Different resource, same units")
r1 = corev1.ResourceRequirements{Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1000m")}}
r2 = corev1.ResourceRequirements{Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}}
verifyDiff(t, r1, r2, false, "Same resource, different units")
r1 = corev1.ResourceRequirements{Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1001m")}}
r2 = corev1.ResourceRequirements{Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}}
verifyDiff(t, r1, r2, true, "Different resource, different units")
r1 = corev1.ResourceRequirements{Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1001m")}}
r2 = corev1.ResourceRequirements{Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1002m")}}
verifyDiff(t, r1, r2, true, "Different resource, same units")
r1 = corev1.ResourceRequirements{Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1000m")}, Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("3000m")}}
r2 = corev1.ResourceRequirements{Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("3")}}
verifyDiff(t, r1, r2, false, "Same resources, different units")
r1 = corev1.ResourceRequirements{Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1001m")}, Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1000m")}}
r2 = corev1.ResourceRequirements{Requests: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}}
verifyDiff(t, r1, r2, true, "Different units for limits (but same resource), different resource for requests")
}

// One unit test that contains a complex diff and actually verifies the full output
func TestOutput(t *testing.T) {
fromObj := X{
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ require (
github.com/google/go-cmp v0.5.0
github.com/stretchr/testify v1.6.1
k8s.io/api v0.19.0
k8s.io/apimachinery v0.21.1
)

replace k8s.io/api => k8s.io/api v0.18.6
replace (
k8s.io/api => k8s.io/api v0.18.6
k8s.io/apimachinery => k8s.io/apimachinery v0.18.6
)

0 comments on commit 9286021

Please sign in to comment.