-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This add new unit-tests to ensure a decent code coverage for all the different classes under the resource pkg of buildrun.
- Loading branch information
Showing
12 changed files
with
518 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright The Shipwright Contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package resources_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
build "github.com/shipwright-io/build/pkg/apis/build/v1alpha1" | ||
"github.com/shipwright-io/build/pkg/controller/fakes" | ||
"github.com/shipwright-io/build/pkg/reconciler/buildrun/resources" | ||
"github.com/shipwright-io/build/test" | ||
k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
var _ = Describe("Build Resource", func() { | ||
|
||
var ( | ||
client *fakes.FakeClient | ||
ctl test.Catalog | ||
buildName string | ||
) | ||
|
||
Context("Operating on Build resources", func() { | ||
// init vars | ||
buildName = "foobuild" | ||
client = &fakes.FakeClient{} | ||
|
||
It("should be able to retrieve a build object if exists", func() { | ||
buildSample := ctl.DefaultBuild(buildName, "foostrategy", build.ClusterBuildStrategyKind) | ||
|
||
// stub a GET API call with buildSample contents | ||
getClientStub := func(context context.Context, nn types.NamespacedName, object runtime.Object) error { | ||
switch object := object.(type) { | ||
case *build.Build: | ||
buildSample.DeepCopyInto(object) | ||
return nil | ||
} | ||
return k8serrors.NewNotFound(schema.GroupResource{}, nn.Name) | ||
} | ||
|
||
// fake the calls with the above stub definition | ||
client.GetCalls(getClientStub) | ||
|
||
build := &build.Build{} | ||
Expect(resources.GetBuildObject(context.TODO(), client, buildName, "default", build)).To(BeNil()) | ||
}) | ||
It("should not retrieve a missing build object when missing", func() { | ||
// stub a GET API call with buildSample contents that returns "not found" | ||
getClientStub := func(context context.Context, nn types.NamespacedName, object runtime.Object) error { | ||
switch object.(type) { | ||
case *build.Build: | ||
return errors.New("not found") | ||
} | ||
return k8serrors.NewNotFound(schema.GroupResource{}, nn.Name) | ||
} | ||
// fake the calls with the above stub | ||
client.GetCalls(getClientStub) | ||
|
||
build := &build.Build{} | ||
Expect(resources.GetBuildObject(context.TODO(), client, buildName, "default", build)).ToNot(BeNil()) | ||
}) | ||
It("should be able to verify valid ownerships", func() { | ||
managingController := true | ||
|
||
buildSample := &build.Build{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "fakekind", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: buildName, | ||
}, | ||
} | ||
// fake an instance of OwnerReference with a | ||
// well known fake Kind and Name | ||
fakeOwnerRef := []metav1.OwnerReference{ | ||
{ | ||
Kind: "fakekind", | ||
Name: buildName, | ||
Controller: &managingController, | ||
}, | ||
} | ||
// Assert that our Build is owned by an owner | ||
Expect(resources.IsOwnedByBuild(buildSample, fakeOwnerRef)).To(BeTrue()) | ||
}) | ||
It("should be able to verify invalid ownerships", func() { | ||
managingController := true | ||
|
||
buildSample := &build.Build{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "notthatkind", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: buildName, | ||
}, | ||
} | ||
// fake an instance of OwnerReference with a | ||
// well known fake Kind and Name | ||
fakeOwnerRef := []metav1.OwnerReference{ | ||
{ | ||
Kind: "fakekind", | ||
Name: buildName, | ||
Controller: &managingController, | ||
}, | ||
} | ||
// Assert that our Build is not owned by an owner | ||
Expect(resources.IsOwnedByBuild(buildSample, fakeOwnerRef)).To(BeFalse()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright The Shipwright Contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package resources_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/shipwright-io/build/pkg/reconciler/buildrun/resources" | ||
) | ||
|
||
var _ = Describe("Multiple errors", func() { | ||
Context("Dealing with multiple errors", func() { | ||
It("should handle multiple errors", func() { | ||
error01 := errors.New("error01") | ||
error02 := errors.New("error02") | ||
msg := "handling multiple errors" | ||
customError := resources.HandleError(msg, error01, error02) | ||
Expect(customError).To(Equal(fmt.Errorf("errors: %s, %s, msg: %s", error01, error02, msg))) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.