Skip to content

Commit f4db022

Browse files
author
lamai93
committed
More manual tests for Equal.
1 parent 9428447 commit f4db022

File tree

10 files changed

+159
-22
lines changed

10 files changed

+159
-22
lines changed

pkg/apis/deployment/v1alpha/conditions.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package v1alpha
2424

2525
import (
26+
"github.com/arangodb/kube-arangodb/pkg/util"
2627
"k8s.io/api/core/v1"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2829
)
@@ -77,13 +78,18 @@ type Condition struct {
7778
type ConditionList []Condition
7879

7980
// Equal checks for equality
80-
func (cl ConditionList) Equal(other ConditionList) bool {
81-
if len(cl) != len(other) {
81+
func (list ConditionList) Equal(other ConditionList) bool {
82+
if len(list) != len(other) {
8283
return false
8384
}
8485

85-
for i := 0; i < len(cl); i++ {
86-
if !cl[i].Equal(other[i]) {
86+
for i := 0; i < len(list); i++ {
87+
c, found := other.Get(list[i].Type)
88+
if !found {
89+
return false
90+
}
91+
92+
if !list[i].Equal(c) {
8793
return false
8894
}
8995
}
@@ -95,8 +101,8 @@ func (cl ConditionList) Equal(other ConditionList) bool {
95101
func (c Condition) Equal(other Condition) bool {
96102
return c.Type == other.Type &&
97103
c.Status == other.Status &&
98-
c.LastUpdateTime.Time.Sub(other.LastUpdateTime.Time).Seconds() < 2 &&
99-
c.LastTransitionTime.Time.Sub(other.LastTransitionTime.Time).Seconds() < 2 &&
104+
util.TimeCompareEqual(c.LastUpdateTime, other.LastUpdateTime) &&
105+
util.TimeCompareEqual(c.LastTransitionTime, other.LastTransitionTime) &&
100106
c.Reason == other.Reason &&
101107
c.Message == other.Message
102108
}

pkg/apis/deployment/v1alpha/deployment_spec.go

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
package v1alpha
2424

2525
import (
26+
"reflect"
27+
2628
"github.com/arangodb/kube-arangodb/pkg/util"
2729
"github.com/pkg/errors"
2830
"k8s.io/api/core/v1"
@@ -69,6 +71,11 @@ type DeploymentSpec struct {
6971
Chaos ChaosSpec `json:"chaos"`
7072
}
7173

74+
// Equal compares two DeploymentSpec
75+
func (s *DeploymentSpec) Equal(other *DeploymentSpec) bool {
76+
return reflect.DeepEqual(s, other)
77+
}
78+
7279
// GetMode returns the value of mode.
7380
func (s DeploymentSpec) GetMode() DeploymentMode {
7481
return ModeOrDefault(s.Mode)

pkg/apis/deployment/v1alpha/deployment_status.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222

2323
package v1alpha
2424

25-
import (
26-
"reflect"
27-
)
28-
2925
// DeploymentStatus contains the status part of a Cluster resource.
3026
type DeploymentStatus struct {
3127
// Phase holds the current lifetime phase of the deployment
@@ -68,11 +64,11 @@ func (ds *DeploymentStatus) Equal(other DeploymentStatus) bool {
6864
ds.Reason == other.Reason &&
6965
ds.ServiceName == other.ServiceName &&
7066
ds.SyncServiceName == other.SyncServiceName &&
71-
reflect.DeepEqual(ds.Images, other.Images) &&
72-
reflect.DeepEqual(ds.CurrentImage, other.CurrentImage) &&
67+
ds.Images.Equal(other.Images) &&
68+
ds.CurrentImage.Equal(other.CurrentImage) &&
7369
ds.Members.Equal(other.Members) &&
7470
ds.Conditions.Equal(other.Conditions) &&
75-
reflect.DeepEqual(ds.Plan, other.Plan) &&
76-
reflect.DeepEqual(ds.AcceptedSpec, other.AcceptedSpec) &&
77-
reflect.DeepEqual(ds.SecretHashes, other.SecretHashes)
71+
ds.Plan.Equal(other.Plan) &&
72+
ds.AcceptedSpec.Equal(other.AcceptedSpec) &&
73+
ds.SecretHashes.Equal(other.SecretHashes)
7874
}

pkg/apis/deployment/v1alpha/image_info.go

+35
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,38 @@ func (l *ImageInfoList) AddOrUpdate(info ImageInfo) {
7171
// No existing entry found, add it
7272
*l = append(*l, info)
7373
}
74+
75+
// Equal compares to ImageInfo
76+
func (i *ImageInfo) Equal(other *ImageInfo) bool {
77+
if i == other {
78+
return true
79+
} else if i == nil {
80+
return false
81+
}
82+
83+
return i.ArangoDBVersion == other.ArangoDBVersion &&
84+
i.Enterprise == other.Enterprise &&
85+
i.Image == other.Image &&
86+
i.ImageID == other.ImageID
87+
}
88+
89+
// Equal compares to ImageInfoList
90+
func (l ImageInfoList) Equal(other ImageInfoList) bool {
91+
if len(l) != len(other) {
92+
return false
93+
}
94+
95+
for i := 0; i < len(l); i++ {
96+
ii, found := l.GetByImageID(l[i].ImageID)
97+
98+
if !found {
99+
return false
100+
}
101+
102+
if !l[i].Equal(&ii) {
103+
return false
104+
}
105+
}
106+
107+
return true
108+
}

pkg/apis/deployment/v1alpha/member_status.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package v1alpha
2525
import (
2626
"time"
2727

28+
"github.com/arangodb/kube-arangodb/pkg/util"
2829
"k8s.io/api/core/v1"
2930
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3031
)
@@ -58,7 +59,7 @@ type MemberStatus struct {
5859
func (s MemberStatus) Equal(other MemberStatus) bool {
5960
return s.ID == other.ID &&
6061
s.Phase == other.Phase &&
61-
s.CreatedAt.Time.Sub(other.CreatedAt.Time).Seconds() < 2 &&
62+
util.TimeCompareEqual(s.CreatedAt, other.CreatedAt) &&
6263
s.PersistentVolumeClaimName == other.PersistentVolumeClaimName &&
6364
s.PodName == other.PodName &&
6465
s.Conditions.Equal(other.Conditions) &&

pkg/apis/deployment/v1alpha/member_status_list.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,18 @@ import (
3333
type MemberStatusList []MemberStatus
3434

3535
// Equal checks for equality
36-
func (msl MemberStatusList) Equal(other MemberStatusList) bool {
37-
if len(msl) != len(other) {
36+
func (l MemberStatusList) Equal(other MemberStatusList) bool {
37+
if len(l) != len(other) {
3838
return false
3939
}
4040

41-
for i := 0; i < len(msl); i++ {
42-
if !msl[i].Equal(other[i]) {
41+
for i := 0; i < len(l); i++ {
42+
o, found := l.ElementByID(l[i].ID)
43+
if !found {
44+
return false
45+
}
46+
47+
if !l[i].Equal(o) {
4348
return false
4449
}
4550
}

pkg/apis/deployment/v1alpha/plan.go

+29
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package v1alpha
2424

2525
import (
26+
"github.com/arangodb/kube-arangodb/pkg/util"
2627
"github.com/dchest/uniuri"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2829
)
@@ -79,6 +80,18 @@ type Action struct {
7980
Image string `json:"image,omitempty"`
8081
}
8182

83+
// Equal compares two Actions
84+
func (a Action) Equal(other Action) bool {
85+
return a.ID == other.ID &&
86+
a.Type == other.Type &&
87+
a.MemberID == other.MemberID &&
88+
a.Group == other.Group &&
89+
util.TimeCompareEqual(a.CreationTime, other.CreationTime) &&
90+
util.TimeCompareEqualPointer(a.StartTime, other.StartTime) &&
91+
a.Reason == other.Reason &&
92+
a.Image == other.Image
93+
}
94+
8295
// NewAction instantiates a new Action.
8396
func NewAction(actionType ActionType, group ServerGroup, memberID string, reason ...string) Action {
8497
a := Action{
@@ -105,3 +118,19 @@ func (a Action) SetImage(image string) Action {
105118
// Only 1 action is in progress at a time. The operator will wait for that
106119
// action to be completely and then remove the action.
107120
type Plan []Action
121+
122+
// Equal compares two Plan
123+
func (p Plan) Equal(other Plan) bool {
124+
// For plan the order is relevant!
125+
if len(p) != len(other) {
126+
return false
127+
}
128+
129+
for i := 0; i < len(p); i++ {
130+
if !p[i].Equal(other[i]) {
131+
return false
132+
}
133+
}
134+
135+
return true
136+
}

pkg/apis/deployment/v1alpha/secret_hashes.go

+14
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,17 @@ type SecretHashes struct {
3535
// SyncTLSCA contains the hash of the sync.tls.caSecretName secret
3636
SyncTLSCA string `json:"sync-tls-ca,omitempty"`
3737
}
38+
39+
// Equal compares two SecretHashes
40+
func (sh *SecretHashes) Equal(other *SecretHashes) bool {
41+
if sh == other {
42+
return true
43+
} else if sh == nil {
44+
return false
45+
}
46+
47+
return sh.AuthJWT == other.AuthJWT &&
48+
sh.RocksDBEncryptionKey == other.RocksDBEncryptionKey &&
49+
sh.TLSCA == other.TLSCA &&
50+
sh.SyncTLSCA == other.SyncTLSCA
51+
}

pkg/deployment/deployment.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ package deployment
2424

2525
import (
2626
"fmt"
27-
"reflect"
2827
"sync"
2928
"sync/atomic"
3029
"time"
@@ -390,7 +389,7 @@ func (d *Deployment) updateCRStatus(force ...bool) error {
390389
// On success, d.apiObject is updated.
391390
func (d *Deployment) updateCRSpec(newSpec api.DeploymentSpec) error {
392391

393-
if reflect.DeepEqual(d.apiObject.Spec, newSpec) {
392+
if d.apiObject.Spec.Equal(&newSpec) {
394393
// Nothing to update
395394
return nil
396395
}

pkg/util/times.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Lars Maier
21+
//
22+
23+
package util
24+
25+
import (
26+
"math"
27+
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
)
30+
31+
// TimeCompareEqual compares two times, allowing an error of 1s
32+
func TimeCompareEqual(a, b metav1.Time) bool {
33+
return math.Abs(a.Time.Sub(b.Time).Seconds()) <= 1
34+
}
35+
36+
// TimeCompareEqualPointer compares two times, allowing an error of 1s
37+
func TimeCompareEqualPointer(a, b *metav1.Time) bool {
38+
if a == b {
39+
return true
40+
} else if a == nil {
41+
return false
42+
}
43+
44+
return TimeCompareEqual(*a, *b)
45+
}

0 commit comments

Comments
 (0)