Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions pkg/apis/serving/v1alpha1/testing/configuration_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
Copyright 2018 The Knative 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 testing

import (
buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1"
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgotesting "k8s.io/client-go/testing"
)

type ConfigToBuildFunc func(*v1alpha1.Configuration) *buildv1alpha1.Build
type ConfigToRevisionFunc func(*v1alpha1.Configuration) *v1alpha1.Revision

type ConfigBuilder struct {
obj *v1alpha1.Configuration
}

func Config(name string) ConfigBuilder {
return ConfigBuilder{&v1alpha1.Configuration{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: "default",
},
Spec: v1alpha1.ConfigurationSpec{
RevisionTemplate: v1alpha1.RevisionTemplateSpec{
Spec: v1alpha1.RevisionSpec{
Container: corev1.Container{
Image: "busybox",
},
},
},
},
Status: v1alpha1.ConfigurationStatus{
Conditions: []v1alpha1.ConfigurationCondition{},
},
}}
}

func (bldr ConfigBuilder) Build() *v1alpha1.Configuration {
return bldr.obj.DeepCopy()
}

func (bldr ConfigBuilder) Object() runtime.Object {
return bldr.Build()
}

func (bldr ConfigBuilder) ToChildBuild(convert ConfigToBuildFunc) *buildv1alpha1.Build {
return convert(bldr.Build())
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like this should maybe return a BuildBuilder, WDYT?


func (bldr ConfigBuilder) ToChildRevision(convert ConfigToRevisionFunc) RevisionBuilder {
return RevisionBuilder{convert(bldr.Build())}
}

func (bldr ConfigBuilder) ToUpdateAction() clientgotesting.UpdateActionImpl {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like this should be part of an interface this implements

action := clientgotesting.UpdateActionImpl{}
action.Verb = "update"
action.Object = bldr.Build()
return action
}

func (bldr ConfigBuilder) WithNamespace(namespace string) ConfigBuilder {
bldr.obj.ObjectMeta.Namespace = namespace
return bldr
}

func (bldr ConfigBuilder) WithGeneration(generation int64) ConfigBuilder {
bldr.obj.Spec.Generation = generation
return bldr
}

func (bldr ConfigBuilder) WithStatus(s v1alpha1.ConfigurationStatus) ConfigBuilder {
bldr.obj.Status = s
return bldr
}

func (bldr ConfigBuilder) WithBuild(b buildv1alpha1.BuildSpec) ConfigBuilder {
bldr.obj.Spec.Build = &b
return bldr
}

func (bldr ConfigBuilder) WithObservedGeneration(gen int64) ConfigBuilder {
bldr.obj.Status.ObservedGeneration = gen
return bldr
}

func (bldr ConfigBuilder) WithLatestCreatedRevisionName(name string) ConfigBuilder {
bldr.obj.Status.LatestCreatedRevisionName = name
return bldr
}

func (bldr ConfigBuilder) WithLatestReadyRevisionName(name string) ConfigBuilder {
bldr.obj.Status.LatestReadyRevisionName = name
return bldr
}

func (bldr ConfigBuilder) WithConcurrencyModel(cm v1alpha1.RevisionRequestConcurrencyModelType) ConfigBuilder {
bldr.obj.Spec.RevisionTemplate.Spec.ConcurrencyModel = cm
return bldr
}

func (bldr ConfigBuilder) WithReadyConditionUnknown() ConfigBuilder {
bldr.obj.Status.Conditions = []v1alpha1.ConfigurationCondition{
{
Type: v1alpha1.ConfigurationConditionReady,
Status: corev1.ConditionUnknown,
},
}
return bldr
}

func (bldr ConfigBuilder) WithReadyConditionTrue() ConfigBuilder {
bldr.obj.Status.Conditions = []v1alpha1.ConfigurationCondition{
{
Type: v1alpha1.ConfigurationConditionReady,
Status: corev1.ConditionTrue,
},
}
return bldr
}

func (bldr ConfigBuilder) WithReadyConditionFalse(reason, message string) ConfigBuilder {
bldr.obj.Status.Conditions = []v1alpha1.ConfigurationCondition{
{
Type: v1alpha1.ConfigurationConditionReady,
Status: corev1.ConditionFalse,
Reason: reason,
Message: message,
},
}
return bldr
}
72 changes: 72 additions & 0 deletions pkg/apis/serving/v1alpha1/testing/revision_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2018 The Knative 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 testing

import (
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
)

type RevisionBuilder struct {
obj *v1alpha1.Revision
}

func (bldr RevisionBuilder) Build() *v1alpha1.Revision {
return bldr.obj.DeepCopy()
}

func (bldr RevisionBuilder) Object() runtime.Object {
return bldr.Build()
}

func (bldr RevisionBuilder) WithConcurrencyModel(cm v1alpha1.RevisionRequestConcurrencyModelType) RevisionBuilder {
bldr.obj.Spec.ConcurrencyModel = cm
return bldr
}

func (bldr RevisionBuilder) WithReadyCondition(status corev1.ConditionStatus) RevisionBuilder {
bldr.obj.Status.Conditions = []v1alpha1.RevisionCondition{
{
Type: v1alpha1.RevisionConditionReady,
Status: status,
},
}
return bldr
}

func (bldr RevisionBuilder) WithReadyConditionTrue() RevisionBuilder {
bldr.obj.Status.Conditions = []v1alpha1.RevisionCondition{
{
Type: v1alpha1.RevisionConditionReady,
Status: corev1.ConditionTrue,
},
}
return bldr
}

func (bldr RevisionBuilder) WithReadyConditionFalse(reason, message string) RevisionBuilder {
bldr.obj.Status.Conditions = []v1alpha1.RevisionCondition{
{
Type: v1alpha1.RevisionConditionReady,
Status: corev1.ConditionFalse,
Reason: reason,
Message: message,
},
}
return bldr
}
4 changes: 2 additions & 2 deletions pkg/reconciler/testing/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (r *TableRow) Test(t *testing.T, ctor Ctor) {
continue
}
got := deleteActions[i]
if got.GetName() != want.Name {
if got.GetName() != want.GetName() {
t.Errorf("unexpected delete[%d]: %#v", i, got)
}
if got.GetNamespace() != expectedNamespace && got.GetNamespace() != system.Namespace {
Expand All @@ -171,7 +171,7 @@ func (r *TableRow) Test(t *testing.T, ctor Ctor) {
}

got := patchActions[i]
if got.GetName() != want.Name {
if got.GetName() != want.GetName() {
t.Errorf("unexpected patch[%d]: %#v", i, got)
}
if got.GetNamespace() != expectedNamespace && got.GetNamespace() != system.Namespace {
Expand Down
Loading