-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: introduce suite for each controller component test (#1019)
Signed-off-by: odubajDT <[email protected]>
- Loading branch information
Showing
21 changed files
with
890 additions
and
727 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package app_test | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"github.com/keptn/lifecycle-toolkit/operator/controllers/lifecycle/keptnapp" | ||
"github.com/keptn/lifecycle-toolkit/operator/test/component/common" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
otelsdk "go.opentelemetry.io/otel/sdk/trace" | ||
sdktest "go.opentelemetry.io/otel/sdk/trace/tracetest" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
// nolint:gci | ||
// +kubebuilder:scaffold:imports | ||
) | ||
|
||
func TestApp(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "App Suite") | ||
} | ||
|
||
var ( | ||
k8sManager ctrl.Manager | ||
tracer *otelsdk.TracerProvider | ||
k8sClient client.Client | ||
ctx context.Context | ||
spanRecorder *sdktest.SpanRecorder | ||
) | ||
|
||
var _ = BeforeSuite(func() { | ||
ctx, k8sManager, tracer, spanRecorder, k8sClient, _ = common.InitSuite() | ||
|
||
////setup controllers here | ||
controller := &keptnapp.KeptnAppReconciler{ | ||
Client: k8sManager.GetClient(), | ||
Scheme: k8sManager.GetScheme(), | ||
Recorder: k8sManager.GetEventRecorderFor("test-app-controller"), | ||
Log: GinkgoLogr, | ||
TracerFactory: &common.TracerFactory{Tracer: tracer}, | ||
} | ||
err := controller.SetupWithManager(k8sManager) | ||
Expect(err).To(BeNil()) | ||
|
||
}) | ||
|
||
var _ = ReportAfterSuite("custom report", func(report Report) { | ||
f, err := os.Create("report.app-operator") | ||
Expect(err).ToNot(HaveOccurred(), "failed to generate report") | ||
for _, specReport := range report.SpecReports { | ||
common.WriteReport(specReport, f) | ||
} | ||
f.Close() | ||
}) |
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,101 @@ | ||
package app_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
klcv1alpha3 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3" | ||
apicommon "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3/common" | ||
"github.com/keptn/lifecycle-toolkit/operator/test/component/common" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
otelsdk "go.opentelemetry.io/otel/sdk/trace" | ||
sdktest "go.opentelemetry.io/otel/sdk/trace/tracetest" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apiserver/pkg/storage/names" | ||
) | ||
|
||
var _ = Describe("App", Ordered, func() { | ||
var ( | ||
name string | ||
namespace string | ||
version string | ||
) | ||
BeforeEach(func() { // list var here they will be copied for every spec | ||
name = names.SimpleNameGenerator.GenerateName("my-app-") | ||
namespace = "default" // namespaces are not deleted in the api so be careful | ||
// when creating you can use ignoreAlreadyExists(err error) | ||
version = "1.0.0" | ||
}) | ||
Describe("Creation of AppVersion from a new App", func() { | ||
var ( | ||
instance *klcv1alpha3.KeptnApp | ||
) | ||
|
||
BeforeEach(func() { | ||
instance = createInstanceInCluster(name, namespace, version) | ||
fmt.Println("created ", instance.Name) | ||
}) | ||
|
||
Context("with a new App CRD", func() { | ||
|
||
It("should update the spans", func() { | ||
By("creating a new app version") | ||
common.AssertResourceUpdated(ctx, k8sClient, instance) | ||
assertAppSpan(instance, spanRecorder) | ||
fmt.Println("spanned ", instance.Name) | ||
}) | ||
|
||
}) | ||
AfterEach(func() { | ||
// Remember to clean up the cluster after each test | ||
common.DeleteAppInCluster(ctx, k8sClient, instance) | ||
// Reset span recorder after each spec | ||
common.ResetSpanRecords(tracer, spanRecorder) | ||
}) | ||
|
||
}) | ||
}) | ||
|
||
func assertAppSpan(instance *klcv1alpha3.KeptnApp, spanRecorder *sdktest.SpanRecorder) { | ||
By("Comparing spans") | ||
var spans []otelsdk.ReadOnlySpan | ||
Eventually(func() bool { | ||
spans = spanRecorder.Ended() | ||
return len(spans) >= 3 | ||
}, "10s").Should(BeTrue()) | ||
|
||
Expect(spans[0].Name()).To(Equal(fmt.Sprintf("%s-%s-%d", instance.Name, instance.Spec.Version, instance.Generation))) | ||
Expect(spans[0].Attributes()).To(ContainElement(apicommon.AppName.String(instance.Name))) | ||
Expect(spans[0].Attributes()).To(ContainElement(apicommon.AppVersion.String(instance.Spec.Version))) | ||
|
||
Expect(spans[1].Name()).To(Equal("create_app_version")) | ||
Expect(spans[1].Attributes()).To(ContainElement(apicommon.AppName.String(instance.Name))) | ||
Expect(spans[1].Attributes()).To(ContainElement(apicommon.AppVersion.String(instance.Spec.Version))) | ||
|
||
Expect(spans[2].Name()).To(Equal("reconcile_app")) | ||
Expect(spans[2].Attributes()).To(ContainElement(apicommon.AppName.String(instance.Name))) | ||
Expect(spans[2].Attributes()).To(ContainElement(apicommon.AppVersion.String(instance.Spec.Version))) | ||
} | ||
|
||
func createInstanceInCluster(name string, namespace string, version string) *klcv1alpha3.KeptnApp { | ||
instance := &klcv1alpha3.KeptnApp{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: namespace, | ||
Generation: 1, | ||
}, | ||
Spec: klcv1alpha3.KeptnAppSpec{ | ||
Version: version, | ||
Workloads: []klcv1alpha3.KeptnWorkloadRef{ | ||
{ | ||
Name: "app-wname", | ||
Version: "2.0", | ||
}, | ||
}, | ||
}, | ||
} | ||
By("Invoking Reconciling for Create") | ||
|
||
Expect(k8sClient.Create(ctx, instance)).Should(Succeed()) | ||
return instance | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.