@@ -3,18 +3,14 @@ package e2e
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "io "
6
+ "github.com/operator-framework/operator-controller/test/utils "
7
7
"os"
8
- "path/filepath"
9
- "strings"
10
8
"testing"
11
9
"time"
12
10
13
11
"github.com/google/go-containerregistry/pkg/crane"
14
12
"github.com/stretchr/testify/assert"
15
13
"github.com/stretchr/testify/require"
16
- "gopkg.in/yaml.v2"
17
- appsv1 "k8s.io/api/apps/v1"
18
14
corev1 "k8s.io/api/core/v1"
19
15
rbacv1 "k8s.io/api/rbac/v1"
20
16
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -24,8 +20,6 @@ import (
24
20
"k8s.io/apimachinery/pkg/labels"
25
21
"k8s.io/apimachinery/pkg/types"
26
22
"k8s.io/apimachinery/pkg/util/rand"
27
- kubeclient "k8s.io/client-go/kubernetes"
28
- "k8s.io/utils/env"
29
23
"sigs.k8s.io/controller-runtime/pkg/client"
30
24
31
25
ocv1 "github.com/operator-framework/operator-controller/api/v1"
@@ -306,7 +300,7 @@ func TestClusterExtensionInstallRegistry(t *testing.T) {
306
300
307
301
clusterExtension , extensionCatalog , sa , ns := testInit (t )
308
302
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
309
- defer getArtifactsOutput ( t )
303
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
310
304
311
305
clusterExtension .Spec = ocv1.ClusterExtensionSpec {
312
306
Source : ocv1.SourceConfig {
@@ -365,7 +359,7 @@ func TestClusterExtensionInstallRegistryMultipleBundles(t *testing.T) {
365
359
require .NoError (t , err )
366
360
367
361
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
368
- defer getArtifactsOutput ( t )
362
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
369
363
defer func (cat * catalogd.ClusterCatalog ) {
370
364
require .NoError (t , c .Delete (context .Background (), cat ))
371
365
require .Eventually (t , func () bool {
@@ -413,7 +407,7 @@ func TestClusterExtensionBlockInstallNonSuccessorVersion(t *testing.T) {
413
407
414
408
clusterExtension , extensionCatalog , sa , ns := testInit (t )
415
409
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
416
- defer getArtifactsOutput ( t )
410
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
417
411
418
412
t .Log ("By creating an ClusterExtension at a specified version" )
419
413
clusterExtension .Spec = ocv1.ClusterExtensionSpec {
@@ -476,7 +470,7 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) {
476
470
477
471
clusterExtension , extensionCatalog , sa , ns := testInit (t )
478
472
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
479
- defer getArtifactsOutput ( t )
473
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
480
474
481
475
t .Log ("By creating an ClusterExtension at a specified version" )
482
476
clusterExtension .Spec = ocv1.ClusterExtensionSpec {
@@ -525,7 +519,7 @@ func TestClusterExtensionInstallSuccessorVersion(t *testing.T) {
525
519
t .Log ("When resolving upgrade edges" )
526
520
clusterExtension , extensionCatalog , sa , ns := testInit (t )
527
521
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
528
- defer getArtifactsOutput ( t )
522
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
529
523
530
524
t .Log ("By creating an ClusterExtension at a specified version" )
531
525
clusterExtension .Spec = ocv1.ClusterExtensionSpec {
@@ -573,7 +567,7 @@ func TestClusterExtensionInstallReResolvesWhenCatalogIsPatched(t *testing.T) {
573
567
t .Log ("It resolves again when a catalog is patched with new ImageRef" )
574
568
clusterExtension , extensionCatalog , sa , ns := testInit (t )
575
569
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
576
- defer getArtifactsOutput ( t )
570
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
577
571
578
572
clusterExtension .Spec = ocv1.ClusterExtensionSpec {
579
573
Source : ocv1.SourceConfig {
@@ -660,7 +654,7 @@ func TestClusterExtensionInstallReResolvesWhenNewCatalog(t *testing.T) {
660
654
sa , err := createServiceAccount (context .Background (), types.NamespacedName {Name : clusterExtensionName , Namespace : ns .Name }, clusterExtensionName )
661
655
require .NoError (t , err )
662
656
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
663
- defer getArtifactsOutput ( t )
657
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
664
658
665
659
clusterExtension .Spec = ocv1.ClusterExtensionSpec {
666
660
Source : ocv1.SourceConfig {
@@ -721,7 +715,7 @@ func TestClusterExtensionInstallReResolvesWhenManagedContentChanged(t *testing.T
721
715
t .Log ("It resolves again when managed content is changed" )
722
716
clusterExtension , extensionCatalog , sa , ns := testInit (t )
723
717
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
724
- defer getArtifactsOutput ( t )
718
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
725
719
726
720
clusterExtension .Spec = ocv1.ClusterExtensionSpec {
727
721
Source : ocv1.SourceConfig {
@@ -784,7 +778,7 @@ func TestClusterExtensionRecoversFromInitialInstallFailedWhenFailureFixed(t *tes
784
778
err := c .Create (context .Background (), sa )
785
779
require .NoError (t , err )
786
780
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
787
- defer getArtifactsOutput ( t )
781
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
788
782
789
783
clusterExtension .Spec = ocv1.ClusterExtensionSpec {
790
784
Source : ocv1.SourceConfig {
@@ -860,131 +854,3 @@ func TestClusterExtensionRecoversFromInitialInstallFailedWhenFailureFixed(t *tes
860
854
}
861
855
}, pollDuration , pollInterval )
862
856
}
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
- }
0 commit comments