Fix tests

Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
This commit is contained in:
Kazuyoshi Kato 2022-04-21 17:36:37 +00:00
parent aefd1849b6
commit 7a4f81d8ba
6 changed files with 127 additions and 49 deletions

View File

@ -19,7 +19,6 @@ package exchange
import ( import (
"context" "context"
"errors" "errors"
"reflect"
"testing" "testing"
"time" "time"
@ -27,7 +26,9 @@ import (
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/events" "github.com/containerd/containerd/events"
"github.com/containerd/containerd/namespaces" "github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/protobuf"
"github.com/containerd/typeurl" "github.com/containerd/typeurl"
"github.com/google/go-cmp/cmp"
) )
func TestExchangeBasic(t *testing.T) { func TestExchangeBasic(t *testing.T) {
@ -102,7 +103,7 @@ func TestExchangeBasic(t *testing.T) {
break subscribercheck break subscribercheck
} }
if reflect.DeepEqual(received, testevents) { if cmp.Equal(received, testevents, protobuf.Compare) {
// when we do this, we expect the errs channel to be closed and // when we do this, we expect the errs channel to be closed and
// this will return. // this will return.
subscriber.cancel() subscriber.cancel()
@ -260,7 +261,7 @@ func TestExchangeFilters(t *testing.T) {
break subscribercheck break subscribercheck
} }
if reflect.DeepEqual(received, subscriber.expectEvents) { if cmp.Equal(received, subscriber.expectEvents, protobuf.Compare) {
// when we do this, we expect the errs channel to be closed and // when we do this, we expect the errs channel to be closed and
// this will return. // this will return.
subscriber.cancel() subscriber.cancel()

74
metadata/compare_test.go Normal file
View File

@ -0,0 +1,74 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package metadata
import (
"bytes"
"reflect"
"time"
"github.com/containerd/typeurl"
"github.com/google/go-cmp/cmp"
)
// isNil returns true if the given parameter is nil or typed nil.
func isNil(x interface{}) bool {
if x == nil {
return true
}
v := reflect.ValueOf(x)
return v.Kind() == reflect.Ptr && v.IsNil()
}
var compareNil = cmp.FilterValues(
func(x, y interface{}) bool {
return isNil(x) && isNil(y)
},
cmp.Comparer(func(_, _ interface{}) bool {
return true
}),
)
var ignoreTime = cmp.FilterValues(
func(x, y interface{}) bool {
_, xok := x.(time.Time)
_, yok := y.(time.Time)
return xok && yok
},
cmp.Comparer(func(_, _ interface{}) bool {
return true
}),
)
var compareAny = cmp.FilterValues(
func(x, y interface{}) bool {
_, xok := x.(typeurl.Any)
_, yok := y.(typeurl.Any)
return xok && yok
},
cmp.Comparer(func(x, y interface{}) bool {
vx, ok := x.(typeurl.Any)
if !ok {
return false
}
vy, ok := y.(typeurl.Any)
if !ok {
return false
}
return vx.GetTypeUrl() == vy.GetTypeUrl() && bytes.Equal(vx.GetValue(), vy.GetValue())
}),
)

View File

@ -21,7 +21,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"path/filepath" "path/filepath"
"reflect"
"testing" "testing"
"time" "time"
@ -705,27 +704,8 @@ func checkContainerTimestamps(t *testing.T, c *containers.Container, now time.Ti
} }
} }
// isNil returns true if the given parameter is nil or typed nil.
func isNil(x interface{}) bool {
if x == nil {
return true
}
v := reflect.ValueOf(x)
return v.Kind() == reflect.Ptr && v.IsNil()
}
func checkContainersEqual(t *testing.T, a, b *containers.Container, format string, args ...interface{}) { func checkContainersEqual(t *testing.T, a, b *containers.Container, format string, args ...interface{}) {
// Ignore the difference of nil and typed nil. assert.True(t, cmp.Equal(a, b, compareNil, compareAny))
opt := cmp.FilterValues(
func(x, y interface{}) bool {
return isNil(x) && isNil(y)
},
cmp.Comparer(func(_, _ interface{}) bool {
return true
}),
)
assert.True(t, cmp.Equal(a, b, opt))
} }
func testEnv(t *testing.T) (context.Context, *bolt.DB, func()) { func testEnv(t *testing.T) (context.Context, *bolt.DB, func()) {

View File

@ -17,14 +17,13 @@
package metadata package metadata
import ( import (
"reflect"
"testing" "testing"
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/protobuf"
"github.com/containerd/containerd/protobuf/types" "github.com/containerd/containerd/protobuf/types"
api "github.com/containerd/containerd/sandbox" api "github.com/containerd/containerd/sandbox"
"github.com/containerd/typeurl" "github.com/containerd/typeurl"
"github.com/google/go-cmp/cmp"
) )
func TestSandboxCreate(t *testing.T) { func TestSandboxCreate(t *testing.T) {
@ -266,27 +265,8 @@ func TestSandboxDelete(t *testing.T) {
} }
func assertEqualInstances(t *testing.T, x, y api.Sandbox) { func assertEqualInstances(t *testing.T, x, y api.Sandbox) {
if x.ID != y.ID { diff := cmp.Diff(x, y, compareNil, compareAny, ignoreTime)
t.Fatalf("ids are not equal: %q != %q", x.ID, y.ID) if diff != "" {
} t.Fatalf("x and y are different: %s", diff)
if !reflect.DeepEqual(x.Labels, y.Labels) {
t.Fatalf("labels are not equal: %+v != %+v", x.Labels, y.Labels)
}
if !reflect.DeepEqual(x.Spec, y.Spec) {
t.Fatalf("specs are not equal: %+v != %+v", x.Spec, y.Spec)
}
if !reflect.DeepEqual(x.Extensions, y.Extensions) {
t.Fatalf("extensions are not equal: %+v != %+v", x.Extensions, y.Extensions)
}
if x.Runtime.Name != y.Runtime.Name {
t.Fatalf("runtime names are not equal: %q != %q", x.Runtime.Name, y.Runtime.Name)
}
if !reflect.DeepEqual(protobuf.FromAny(x.Runtime.Options), protobuf.FromAny(y.Runtime.Options)) {
t.Fatalf("runtime options are not equal: %+v != %+v", x.Runtime.Options, y.Runtime.Options)
} }
} }

View File

@ -21,7 +21,9 @@ import (
"time" "time"
eventtypes "github.com/containerd/containerd/api/events" eventtypes "github.com/containerd/containerd/api/events"
"github.com/containerd/containerd/protobuf"
"github.com/containerd/typeurl" "github.com/containerd/typeurl"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
testingclock "k8s.io/utils/clock/testing" testingclock "k8s.io/utils/clock/testing"
) )
@ -107,7 +109,7 @@ func TestBackOff(t *testing.T) {
for k := range inputQueues { for k := range inputQueues {
actQueue := actual.deBackOff(k) actQueue := actual.deBackOff(k)
doneQueues[k] = actQueue doneQueues[k] = actQueue
assert.Equal(t, actQueue, expectedQueues[k]) assert.True(t, cmp.Equal(actQueue.events, expectedQueues[k].events, protobuf.Compare))
} }
t.Logf("Should not get out the event again after having got out the backOff event") t.Logf("Should not get out the event again after having got out the backOff event")

41
protobuf/compare.go Normal file
View File

@ -0,0 +1,41 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package protobuf
import (
"github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/proto"
)
var Compare = cmp.FilterValues(
func(x, y interface{}) bool {
_, xok := x.(proto.Message)
_, yok := y.(proto.Message)
return xok && yok
},
cmp.Comparer(func(x, y interface{}) bool {
vx, ok := x.(proto.Message)
if !ok {
return false
}
vy, ok := y.(proto.Message)
if !ok {
return false
}
return proto.Equal(vx, vy)
}),
)