Skip to content

Commit f57bcac

Browse files
author
Per Goncalves da Silva
committed
add artifact collection to upgrade-e2e-tests
Signed-off-by: Per Goncalves da Silva <pegoncal@redhat.com>
1 parent 2c8e3b9 commit f57bcac

File tree

5 files changed

+176
-145
lines changed

5 files changed

+176
-145
lines changed

.github/workflows/e2e.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,10 @@ jobs:
5858
go-version-file: go.mod
5959

6060
- name: Run the upgrade e2e test
61-
run: make test-upgrade-e2e
61+
run: ARTIFACT_PATH=/tmp/artifacts make test-upgrade-e2e
62+
63+
- uses: cytopia/upload-artifact-retry-action@v0.1.7
64+
if: failure()
65+
with:
66+
name: upgrade-e2e-artifacts
67+
path: /tmp/artifacts/

test/e2e/cluster_extension_install_test.go

Lines changed: 10 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@ package e2e
33
import (
44
"context"
55
"fmt"
6-
"io"
6+
"github.com/operator-framework/operator-controller/test/utils"
77
"os"
8-
"path/filepath"
9-
"strings"
108
"testing"
119
"time"
1210

1311
"github.com/google/go-containerregistry/pkg/crane"
1412
"github.com/stretchr/testify/assert"
1513
"github.com/stretchr/testify/require"
16-
"gopkg.in/yaml.v2"
17-
appsv1 "k8s.io/api/apps/v1"
1814
corev1 "k8s.io/api/core/v1"
1915
rbacv1 "k8s.io/api/rbac/v1"
2016
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -24,8 +20,6 @@ import (
2420
"k8s.io/apimachinery/pkg/labels"
2521
"k8s.io/apimachinery/pkg/types"
2622
"k8s.io/apimachinery/pkg/util/rand"
27-
kubeclient "k8s.io/client-go/kubernetes"
28-
"k8s.io/utils/env"
2923
"sigs.k8s.io/controller-runtime/pkg/client"
3024

3125
ocv1 "github.com/operator-framework/operator-controller/api/v1"
@@ -306,7 +300,7 @@ func TestClusterExtensionInstallRegistry(t *testing.T) {
306300

307301
clusterExtension, extensionCatalog, sa, ns := testInit(t)
308302
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
309-
defer getArtifactsOutput(t)
303+
defer utils.CollectTestArtifacts(t, artifactName, c, cfg)
310304

311305
clusterExtension.Spec = ocv1.ClusterExtensionSpec{
312306
Source: ocv1.SourceConfig{
@@ -365,7 +359,7 @@ func TestClusterExtensionInstallRegistryMultipleBundles(t *testing.T) {
365359
require.NoError(t, err)
366360

367361
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
368-
defer getArtifactsOutput(t)
362+
defer utils.CollectTestArtifacts(t, artifactName, c, cfg)
369363
defer func(cat *catalogd.ClusterCatalog) {
370364
require.NoError(t, c.Delete(context.Background(), cat))
371365
require.Eventually(t, func() bool {
@@ -413,7 +407,7 @@ func TestClusterExtensionBlockInstallNonSuccessorVersion(t *testing.T) {
413407

414408
clusterExtension, extensionCatalog, sa, ns := testInit(t)
415409
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
416-
defer getArtifactsOutput(t)
410+
defer utils.CollectTestArtifacts(t, artifactName, c, cfg)
417411

418412
t.Log("By creating an ClusterExtension at a specified version")
419413
clusterExtension.Spec = ocv1.ClusterExtensionSpec{
@@ -476,7 +470,7 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) {
476470

477471
clusterExtension, extensionCatalog, sa, ns := testInit(t)
478472
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
479-
defer getArtifactsOutput(t)
473+
defer utils.CollectTestArtifacts(t, artifactName, c, cfg)
480474

481475
t.Log("By creating an ClusterExtension at a specified version")
482476
clusterExtension.Spec = ocv1.ClusterExtensionSpec{
@@ -525,7 +519,7 @@ func TestClusterExtensionInstallSuccessorVersion(t *testing.T) {
525519
t.Log("When resolving upgrade edges")
526520
clusterExtension, extensionCatalog, sa, ns := testInit(t)
527521
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
528-
defer getArtifactsOutput(t)
522+
defer utils.CollectTestArtifacts(t, artifactName, c, cfg)
529523

530524
t.Log("By creating an ClusterExtension at a specified version")
531525
clusterExtension.Spec = ocv1.ClusterExtensionSpec{
@@ -573,7 +567,7 @@ func TestClusterExtensionInstallReResolvesWhenCatalogIsPatched(t *testing.T) {
573567
t.Log("It resolves again when a catalog is patched with new ImageRef")
574568
clusterExtension, extensionCatalog, sa, ns := testInit(t)
575569
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
576-
defer getArtifactsOutput(t)
570+
defer utils.CollectTestArtifacts(t, artifactName, c, cfg)
577571

578572
clusterExtension.Spec = ocv1.ClusterExtensionSpec{
579573
Source: ocv1.SourceConfig{
@@ -660,7 +654,7 @@ func TestClusterExtensionInstallReResolvesWhenNewCatalog(t *testing.T) {
660654
sa, err := createServiceAccount(context.Background(), types.NamespacedName{Name: clusterExtensionName, Namespace: ns.Name}, clusterExtensionName)
661655
require.NoError(t, err)
662656
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
663-
defer getArtifactsOutput(t)
657+
defer utils.CollectTestArtifacts(t, artifactName, c, cfg)
664658

665659
clusterExtension.Spec = ocv1.ClusterExtensionSpec{
666660
Source: ocv1.SourceConfig{
@@ -721,7 +715,7 @@ func TestClusterExtensionInstallReResolvesWhenManagedContentChanged(t *testing.T
721715
t.Log("It resolves again when managed content is changed")
722716
clusterExtension, extensionCatalog, sa, ns := testInit(t)
723717
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
724-
defer getArtifactsOutput(t)
718+
defer utils.CollectTestArtifacts(t, artifactName, c, cfg)
725719

726720
clusterExtension.Spec = ocv1.ClusterExtensionSpec{
727721
Source: ocv1.SourceConfig{
@@ -784,7 +778,7 @@ func TestClusterExtensionRecoversFromInitialInstallFailedWhenFailureFixed(t *tes
784778
err := c.Create(context.Background(), sa)
785779
require.NoError(t, err)
786780
defer testCleanup(t, extensionCatalog, clusterExtension, sa, ns)
787-
defer getArtifactsOutput(t)
781+
defer utils.CollectTestArtifacts(t, artifactName, c, cfg)
788782

789783
clusterExtension.Spec = ocv1.ClusterExtensionSpec{
790784
Source: ocv1.SourceConfig{
@@ -860,131 +854,3 @@ func TestClusterExtensionRecoversFromInitialInstallFailedWhenFailureFixed(t *tes
860854
}
861855
}, pollDuration, pollInterval)
862856
}
863-
864-
// getArtifactsOutput gets all the artifacts from the test run and saves them to the artifact path.
865-
// Currently it saves:
866-
// - clusterextensions
867-
// - pods logs
868-
// - deployments
869-
// - catalogsources
870-
func getArtifactsOutput(t *testing.T) {
871-
basePath := env.GetString("ARTIFACT_PATH", "")
872-
if basePath == "" {
873-
return
874-
}
875-
876-
kubeClient, err := kubeclient.NewForConfig(cfg)
877-
require.NoError(t, err)
878-
879-
// sanitize the artifact name for use as a directory name
880-
testName := strings.ReplaceAll(strings.ToLower(t.Name()), " ", "-")
881-
// Get the test description and sanitize it for use as a directory name
882-
artifactPath := filepath.Join(basePath, artifactName, fmt.Sprint(time.Now().UnixNano()), testName)
883-
884-
// Create the full artifact path
885-
err = os.MkdirAll(artifactPath, 0755)
886-
require.NoError(t, err)
887-
888-
// Get all namespaces
889-
namespaces := corev1.NamespaceList{}
890-
if err := c.List(context.Background(), &namespaces); err != nil {
891-
fmt.Printf("Failed to list namespaces: %v", err)
892-
}
893-
894-
// get all cluster extensions save them to the artifact path.
895-
clusterExtensions := ocv1.ClusterExtensionList{}
896-
if err := c.List(context.Background(), &clusterExtensions, client.InNamespace("")); err != nil {
897-
fmt.Printf("Failed to list cluster extensions: %v", err)
898-
}
899-
for _, clusterExtension := range clusterExtensions.Items {
900-
// Save cluster extension to artifact path
901-
clusterExtensionYaml, err := yaml.Marshal(clusterExtension)
902-
if err != nil {
903-
fmt.Printf("Failed to marshal cluster extension: %v", err)
904-
continue
905-
}
906-
if err := os.WriteFile(filepath.Join(artifactPath, clusterExtension.Name+"-clusterextension.yaml"), clusterExtensionYaml, 0600); err != nil {
907-
fmt.Printf("Failed to write cluster extension to file: %v", err)
908-
}
909-
}
910-
911-
// get all catalogsources save them to the artifact path.
912-
catalogsources := catalogd.ClusterCatalogList{}
913-
if err := c.List(context.Background(), &catalogsources, client.InNamespace("")); err != nil {
914-
fmt.Printf("Failed to list catalogsources: %v", err)
915-
}
916-
for _, catalogsource := range catalogsources.Items {
917-
// Save catalogsource to artifact path
918-
catalogsourceYaml, err := yaml.Marshal(catalogsource)
919-
if err != nil {
920-
fmt.Printf("Failed to marshal catalogsource: %v", err)
921-
continue
922-
}
923-
if err := os.WriteFile(filepath.Join(artifactPath, catalogsource.Name+"-catalogsource.yaml"), catalogsourceYaml, 0600); err != nil {
924-
fmt.Printf("Failed to write catalogsource to file: %v", err)
925-
}
926-
}
927-
928-
for _, namespace := range namespaces.Items {
929-
// let's ignore kube-* namespaces.
930-
if strings.Contains(namespace.Name, "kube-") {
931-
continue
932-
}
933-
934-
namespacedArtifactPath := filepath.Join(artifactPath, namespace.Name)
935-
if err := os.Mkdir(namespacedArtifactPath, 0755); err != nil {
936-
fmt.Printf("Failed to create namespaced artifact path: %v", err)
937-
continue
938-
}
939-
940-
// get all deployments in the namespace and save them to the artifact path.
941-
deployments := appsv1.DeploymentList{}
942-
if err := c.List(context.Background(), &deployments, client.InNamespace(namespace.Name)); err != nil {
943-
fmt.Printf("Failed to list deployments %v in namespace: %q", err, namespace.Name)
944-
continue
945-
}
946-
947-
for _, deployment := range deployments.Items {
948-
// Save deployment to artifact path
949-
deploymentYaml, err := yaml.Marshal(deployment)
950-
if err != nil {
951-
fmt.Printf("Failed to marshal deployment: %v", err)
952-
continue
953-
}
954-
if err := os.WriteFile(filepath.Join(namespacedArtifactPath, deployment.Name+"-deployment.yaml"), deploymentYaml, 0600); err != nil {
955-
fmt.Printf("Failed to write deployment to file: %v", err)
956-
}
957-
}
958-
959-
// Get logs from all pods in all namespaces
960-
pods := corev1.PodList{}
961-
if err := c.List(context.Background(), &pods, client.InNamespace(namespace.Name)); err != nil {
962-
fmt.Printf("Failed to list pods %v in namespace: %q", err, namespace.Name)
963-
}
964-
for _, pod := range pods.Items {
965-
if pod.Status.Phase != corev1.PodRunning && pod.Status.Phase != corev1.PodSucceeded && pod.Status.Phase != corev1.PodFailed {
966-
continue
967-
}
968-
for _, container := range pod.Spec.Containers {
969-
logs, err := kubeClient.CoreV1().Pods(namespace.Name).GetLogs(pod.Name, &corev1.PodLogOptions{Container: container.Name}).Stream(context.Background())
970-
if err != nil {
971-
fmt.Printf("Failed to get logs for pod %q in namespace %q: %v", pod.Name, namespace.Name, err)
972-
continue
973-
}
974-
defer logs.Close()
975-
976-
outFile, err := os.Create(filepath.Join(namespacedArtifactPath, pod.Name+"-"+container.Name+"-logs.txt"))
977-
if err != nil {
978-
fmt.Printf("Failed to create file for pod %q in namespace %q: %v", pod.Name, namespace.Name, err)
979-
continue
980-
}
981-
defer outFile.Close()
982-
983-
if _, err := io.Copy(outFile, logs); err != nil {
984-
fmt.Printf("Failed to copy logs for pod %q in namespace %q: %v", pod.Name, namespace.Name, err)
985-
continue
986-
}
987-
}
988-
}
989-
}
990-
}

test/upgrade-e2e/post_upgrade_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"context"
66
"fmt"
7+
"github.com/operator-framework/operator-controller/test/utils"
78
"strings"
89
"testing"
910
"time"
@@ -22,9 +23,14 @@ import (
2223
catalogd "github.com/operator-framework/operator-controller/catalogd/api/v1"
2324
)
2425

26+
const (
27+
artifactName = "operator-controller-upgrade-e2e"
28+
)
29+
2530
func TestClusterExtensionAfterOLMUpgrade(t *testing.T) {
2631
t.Log("Starting checks after OLM upgrade")
2732
ctx := context.Background()
33+
defer utils.CollectTestArtifacts(t, artifactName, c, cfg)
2834

2935
managerLabelSelector := labels.Set{"control-plane": "operator-controller-controller-manager"}
3036

test/upgrade-e2e/upgrade_e2e_suite_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"k8s.io/client-go/kubernetes"
9+
"k8s.io/client-go/rest"
910
ctrl "sigs.k8s.io/controller-runtime"
1011
"sigs.k8s.io/controller-runtime/pkg/client"
1112

@@ -21,12 +22,14 @@ var (
2122
c client.Client
2223
kclientset kubernetes.Interface
2324

25+
cfg *rest.Config
2426
testClusterCatalogName string
2527
testClusterExtensionName string
2628
)
2729

2830
func TestMain(m *testing.M) {
2931
var ok bool
32+
cfg = ctrl.GetConfigOrDie()
3033
testClusterCatalogName, ok = os.LookupEnv(testClusterCatalogNameEnv)
3134
if !ok {
3235
fmt.Printf("%q is not set", testClusterCatalogNameEnv)

0 commit comments

Comments
 (0)