Skip to content

Commit d8283a8

Browse files
mattmoorknative-prow-robot
authored andcommitted
Move the VerifyType checks into tests. (knative#2175)
This tracks the model changes that have already been made to `knative/pkg`. Fixes: knative#2174
1 parent ddf1582 commit d8283a8

16 files changed

+189
-151
lines changed

Gopkg.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ required = [
2323

2424
[[override]]
2525
name = "github.com/knative/pkg"
26-
# HEAD as of 2018-10-01
27-
revision = "f00975a13e4c7284aac5b8d01181590869472291"
26+
# HEAD as of 2018-10-06
27+
revision = "46bd3d242bb31222602a53154e598d3cab585ae6"
2828

2929
[[override]]
3030
name = "go.uber.org/zap"

pkg/apis/autoscaling/v1alpha1/kpa_types.go

-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626

2727
"github.com/knative/pkg/apis"
28-
"github.com/knative/pkg/apis/duck"
2928
duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1"
3029
"github.com/knative/serving/pkg/apis/autoscaling"
3130
servingv1alpha1 "github.com/knative/serving/pkg/apis/serving/v1alpha1"
@@ -60,13 +59,6 @@ var _ apis.Immutable = (*PodAutoscaler)(nil)
6059
// Check that ConfigurationStatus may have its conditions managed.
6160
var _ duckv1alpha1.ConditionsAccessor = (*PodAutoscalerStatus)(nil)
6261

63-
// Check that PodAutoscaler implements the Conditions duck type.
64-
var _ = duck.VerifyType(&PodAutoscaler{}, &duckv1alpha1.Conditions{})
65-
66-
// Check that PodAutoscaler implements the Generation duck type.
67-
var emptyGen duckv1alpha1.Generation
68-
var _ = duck.VerifyType(&PodAutoscaler{}, &emptyGen)
69-
7062
// PodAutoscalerSpec holds the desired state of the PodAutoscaler (from the client).
7163
type PodAutoscalerSpec struct {
7264
// TODO: Generation does not work correctly with CRD. They are scrubbed

pkg/apis/autoscaling/v1alpha1/kpa_types_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,36 @@ import (
2020
"time"
2121

2222
"github.com/knative/pkg/apis"
23+
"github.com/knative/pkg/apis/duck"
2324
duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1"
2425
corev1 "k8s.io/api/core/v1"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627
)
2728

29+
func TestPodAutoscalerDuckTypes(t *testing.T) {
30+
var emptyGen duckv1alpha1.Generation
31+
32+
tests := []struct {
33+
name string
34+
t duck.Implementable
35+
}{{
36+
name: "generations",
37+
t: &emptyGen,
38+
}, {
39+
name: "conditions",
40+
t: &duckv1alpha1.Conditions{},
41+
}}
42+
43+
for _, test := range tests {
44+
t.Run(test.name, func(t *testing.T) {
45+
err := duck.VerifyType(&PodAutoscaler{}, test.t)
46+
if err != nil {
47+
t.Errorf("VerifyType(PodAutoscaler, %T) = %v", test.t, err)
48+
}
49+
})
50+
}
51+
}
52+
2853
func TestGeneration(t *testing.T) {
2954
r := PodAutoscaler{}
3055
if a := r.GetGeneration(); a != 0 {

pkg/apis/networking/v1alpha1/clusteringress_types.go

-22
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20-
"encoding/json"
21-
2220
"github.com/knative/pkg/apis"
23-
"github.com/knative/pkg/apis/duck"
2421
duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1"
2522
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2623
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -302,25 +299,6 @@ var clusterIngressCondSet = duckv1alpha1.NewLivingConditionSet(
302299
var _ apis.Validatable = (*ClusterIngress)(nil)
303300
var _ apis.Defaultable = (*ClusterIngress)(nil)
304301

305-
// Check that ClusterIngress implements the Conditions duck type.
306-
var _ = duck.VerifyType(&ClusterIngress{}, &duckv1alpha1.Conditions{})
307-
308-
// Check that ClusterIngress implements the Generation duck type.
309-
var emptyGen duckv1alpha1.Generation
310-
var _ = duck.VerifyType(&ClusterIngress{}, &emptyGen)
311-
312-
func (ci *ClusterIngress) GetGeneration() int64 {
313-
return ci.Spec.Generation
314-
}
315-
316-
func (ci *ClusterIngress) SetGeneration(generation int64) {
317-
ci.Spec.Generation = generation
318-
}
319-
320-
func (ci *ClusterIngress) GetSpecJSON() ([]byte, error) {
321-
return json.Marshal(ci.Spec)
322-
}
323-
324302
func (ci *ClusterIngress) GetGroupVersionKind() schema.GroupVersionKind {
325303
return SchemeGroupVersion.WithKind("ClusterIngress")
326304
}

pkg/apis/networking/v1alpha1/clusteringress_types_test.go

+22-49
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,36 @@ limitations under the License.
1616
package v1alpha1
1717

1818
import (
19-
"encoding/json"
2019
"testing"
2120

2221
"github.com/google/go-cmp/cmp"
22+
"github.com/knative/pkg/apis/duck"
2323
duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1"
2424
corev1 "k8s.io/api/core/v1"
25-
"k8s.io/apimachinery/pkg/util/intstr"
2625
)
2726

28-
func TestGeneration(t *testing.T) {
29-
r := ClusterIngress{}
30-
if a := r.GetGeneration(); a != 0 {
31-
t.Errorf("empty ClusterIngress generation should be 0 was: %d", a)
27+
func TestClusterIngressDuckTypes(t *testing.T) {
28+
var emptyGen duckv1alpha1.Generation
29+
30+
tests := []struct {
31+
name string
32+
t duck.Implementable
33+
}{{
34+
name: "generational",
35+
t: &emptyGen,
36+
}, {
37+
name: "conditions",
38+
t: &duckv1alpha1.Conditions{},
39+
}}
40+
41+
for _, test := range tests {
42+
t.Run(test.name, func(t *testing.T) {
43+
err := duck.VerifyType(&ClusterIngress{}, test.t)
44+
if err != nil {
45+
t.Errorf("VerifyType(ClusterIngress, %T) = %v", test.t, err)
46+
}
47+
})
3248
}
33-
34-
r.SetGeneration(5)
35-
if e, a := int64(5), r.GetGeneration(); e != a {
36-
t.Errorf("getgeneration mismatch expected: %d got: %d", e, a)
37-
}
38-
3949
}
4050

4151
func TestGetGroupVersionKind(t *testing.T) {
@@ -46,43 +56,6 @@ func TestGetGroupVersionKind(t *testing.T) {
4656
}
4757
}
4858

49-
func TestGetSpecJSON(t *testing.T) {
50-
ci := ClusterIngress{
51-
Spec: IngressSpec{
52-
TLS: []ClusterIngressTLS{{
53-
SecretNamespace: "secret-space",
54-
SecretName: "secret-name",
55-
}},
56-
Rules: []ClusterIngressRule{{
57-
Hosts: []string{"example.com"},
58-
HTTP: &HTTPClusterIngressRuleValue{
59-
Paths: []HTTPClusterIngressPath{{
60-
Splits: []ClusterIngressBackendSplit{{
61-
Backend: &ClusterIngressBackend{
62-
ServiceName: "revision-000",
63-
ServiceNamespace: "default",
64-
ServicePort: intstr.FromInt(8080),
65-
},
66-
}},
67-
Retries: &HTTPRetry{
68-
Attempts: 3,
69-
},
70-
}},
71-
},
72-
}},
73-
},
74-
}
75-
bytes, err := ci.GetSpecJSON()
76-
if err != nil {
77-
t.Fatalf("Saw %v, wanted: nil", err)
78-
}
79-
cis := IngressSpec{}
80-
json.Unmarshal(bytes, &cis)
81-
if diff := cmp.Diff(ci.Spec, cis); diff != "" {
82-
t.Errorf("Unexpected diff (-want, +got) = %v", diff)
83-
}
84-
}
85-
8659
func TestTypicalFlow(t *testing.T) {
8760
r := &ClusterIngress{}
8861
r.Status.InitializeConditions()

pkg/apis/serving/v1alpha1/configuration_types.go

-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"k8s.io/apimachinery/pkg/runtime/schema"
2222

2323
"github.com/knative/pkg/apis"
24-
"github.com/knative/pkg/apis/duck"
2524
duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1"
2625
"github.com/knative/pkg/kmeta"
2726
)
@@ -59,13 +58,6 @@ var _ kmeta.OwnerRefable = (*Configuration)(nil)
5958
// Check that ConfigurationStatus may have its conditions managed.
6059
var _ duckv1alpha1.ConditionsAccessor = (*ConfigurationStatus)(nil)
6160

62-
// Check that Configuration implements the Conditions duck type.
63-
var _ = duck.VerifyType(&Configuration{}, &duckv1alpha1.Conditions{})
64-
65-
// Check that Configuration implements the Generation duck type.
66-
var emptyGenConfig duckv1alpha1.Generation
67-
var _ = duck.VerifyType(&Configuration{}, &emptyGenConfig)
68-
6961
// ConfigurationSpec holds the desired state of the Configuration (from the client).
7062
type ConfigurationSpec struct {
7163
// TODO: Generation does not work correctly with CRD. They are scrubbed

pkg/apis/serving/v1alpha1/configuration_types_test.go

+20-8
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,32 @@ import (
1919
"strings"
2020
"testing"
2121

22+
"github.com/knative/pkg/apis/duck"
2223
duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1"
2324
corev1 "k8s.io/api/core/v1"
2425
"k8s.io/apimachinery/pkg/runtime/schema"
2526
)
2627

27-
func TestConfigurationGeneration(t *testing.T) {
28-
config := Configuration{}
29-
if e, a := int64(0), config.GetGeneration(); e != a {
30-
t.Errorf("empty revision generation should be 0 was: %d", a)
31-
}
28+
func TestConfigurationDuckTypes(t *testing.T) {
29+
var emptyGen duckv1alpha1.Generation
30+
tests := []struct {
31+
name string
32+
t duck.Implementable
33+
}{{
34+
name: "generation",
35+
t: &emptyGen,
36+
}, {
37+
name: "conditions",
38+
t: &duckv1alpha1.Conditions{},
39+
}}
3240

33-
config.SetGeneration(5)
34-
if e, a := int64(5), config.GetGeneration(); e != a {
35-
t.Errorf("getgeneration mismatch expected: %d got: %d", e, a)
41+
for _, test := range tests {
42+
t.Run(test.name, func(t *testing.T) {
43+
err := duck.VerifyType(&Configuration{}, test.t)
44+
if err != nil {
45+
t.Errorf("VerifyType(Configuration, %T) = %v", test.t, err)
46+
}
47+
})
3648
}
3749
}
3850

pkg/apis/serving/v1alpha1/revision_types.go

-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"k8s.io/apimachinery/pkg/runtime/schema"
2323

2424
"github.com/knative/pkg/apis"
25-
"github.com/knative/pkg/apis/duck"
2625
duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1"
2726
"github.com/knative/pkg/kmeta"
2827
)
@@ -58,13 +57,6 @@ var _ apis.Immutable = (*Revision)(nil)
5857
// Check that RevisionStatus may have its conditions managed.
5958
var _ duckv1alpha1.ConditionsAccessor = (*RevisionStatus)(nil)
6059

61-
// Check that Revision implements the Conditions duck type.
62-
var _ = duck.VerifyType(&Revision{}, &duckv1alpha1.Conditions{})
63-
64-
// Check that Revision implements the Generation duck type.
65-
var emptyGenRev duckv1alpha1.Generation
66-
var _ = duck.VerifyType(&Revision{}, &emptyGenRev)
67-
6860
// Check that we can create OwnerReferences to a Revision.
6961
var _ kmeta.OwnerRefable = (*Revision)(nil)
7062

pkg/apis/serving/v1alpha1/revision_types_test.go

+20-9
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,31 @@ import (
2424

2525
"github.com/google/go-cmp/cmp"
2626
"github.com/google/go-cmp/cmp/cmpopts"
27+
"github.com/knative/pkg/apis/duck"
2728
duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1"
2829
)
2930

30-
func TestGeneration(t *testing.T) {
31-
r := Revision{}
32-
if a := r.GetGeneration(); a != 0 {
33-
t.Errorf("empty revision generation should be 0 was: %d", a)
34-
}
31+
func TestRevisionDuckTypes(t *testing.T) {
32+
var emptyGen duckv1alpha1.Generation
33+
tests := []struct {
34+
name string
35+
t duck.Implementable
36+
}{{
37+
name: "generation",
38+
t: &emptyGen,
39+
}, {
40+
name: "conditions",
41+
t: &duckv1alpha1.Conditions{},
42+
}}
3543

36-
r.SetGeneration(5)
37-
if e, a := int64(5), r.GetGeneration(); e != a {
38-
t.Errorf("getgeneration mismatch expected: %d got: %d", e, a)
44+
for _, test := range tests {
45+
t.Run(test.name, func(t *testing.T) {
46+
err := duck.VerifyType(&Revision{}, test.t)
47+
if err != nil {
48+
t.Errorf("VerifyType(Revision, %T) = %v", test.t, err)
49+
}
50+
})
3951
}
40-
4152
}
4253

4354
func TestIsActivationRequired(t *testing.T) {

pkg/apis/serving/v1alpha1/route_types.go

-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"k8s.io/apimachinery/pkg/runtime/schema"
2222

2323
"github.com/knative/pkg/apis"
24-
"github.com/knative/pkg/apis/duck"
2524
duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1"
2625
"github.com/knative/pkg/kmeta"
2726
)
@@ -56,17 +55,6 @@ var _ apis.Defaultable = (*Route)(nil)
5655
// Check that we can create OwnerReferences to a Route.
5756
var _ kmeta.OwnerRefable = (*Route)(nil)
5857

59-
// Check that Route implements the Conditions duck type.
60-
var _ = duck.VerifyType(&Route{}, &duckv1alpha1.Conditions{})
61-
62-
// Check that Route implements the [Legacy]Targetable duck type.
63-
var _ = duck.VerifyType(&Route{}, &duckv1alpha1.LegacyTargetable{})
64-
var _ = duck.VerifyType(&Route{}, &duckv1alpha1.Targetable{})
65-
66-
// Check that Route implements the Generation duck type.
67-
var emptyGenRoute duckv1alpha1.Generation
68-
var _ = duck.VerifyType(&Route{}, &emptyGenRoute)
69-
7058
// Check that RouteStatus may have its conditions managed.
7159
var _ duckv1alpha1.ConditionsAccessor = (*RouteStatus)(nil)
7260

0 commit comments

Comments
 (0)