@@ -3,18 +3,13 @@ package e2e
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "io"
7
6
"os"
8
- "path/filepath"
9
- "strings"
10
7
"testing"
11
8
"time"
12
9
13
10
"github.com/google/go-containerregistry/pkg/crane"
14
11
"github.com/stretchr/testify/assert"
15
12
"github.com/stretchr/testify/require"
16
- "gopkg.in/yaml.v2"
17
- appsv1 "k8s.io/api/apps/v1"
18
13
corev1 "k8s.io/api/core/v1"
19
14
rbacv1 "k8s.io/api/rbac/v1"
20
15
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -24,12 +19,11 @@ import (
24
19
"k8s.io/apimachinery/pkg/labels"
25
20
"k8s.io/apimachinery/pkg/types"
26
21
"k8s.io/apimachinery/pkg/util/rand"
27
- kubeclient "k8s.io/client-go/kubernetes"
28
- "k8s.io/utils/env"
29
22
"sigs.k8s.io/controller-runtime/pkg/client"
30
23
31
24
ocv1 "github.com/operator-framework/operator-controller/api/v1"
32
25
catalogd "github.com/operator-framework/operator-controller/catalogd/api/v1"
26
+ "github.com/operator-framework/operator-controller/test/utils"
33
27
)
34
28
35
29
const (
@@ -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 {
@@ -366,7 +360,7 @@ func TestClusterExtensionInstallRegistryDynamic(t *testing.T) {
366
360
367
361
clusterExtension , extensionCatalog , sa , ns := testInit (t )
368
362
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
369
- defer getArtifactsOutput ( t )
363
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
370
364
371
365
clusterExtension .Spec = ocv1.ClusterExtensionSpec {
372
366
Source : ocv1.SourceConfig {
@@ -441,7 +435,7 @@ func TestClusterExtensionInstallRegistryMultipleBundles(t *testing.T) {
441
435
require .NoError (t , err )
442
436
443
437
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
444
- defer getArtifactsOutput ( t )
438
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
445
439
defer func (cat * catalogd.ClusterCatalog ) {
446
440
require .NoError (t , c .Delete (context .Background (), cat ))
447
441
require .Eventually (t , func () bool {
@@ -489,7 +483,7 @@ func TestClusterExtensionBlockInstallNonSuccessorVersion(t *testing.T) {
489
483
490
484
clusterExtension , extensionCatalog , sa , ns := testInit (t )
491
485
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
492
- defer getArtifactsOutput ( t )
486
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
493
487
494
488
t .Log ("By creating an ClusterExtension at a specified version" )
495
489
clusterExtension .Spec = ocv1.ClusterExtensionSpec {
@@ -552,7 +546,7 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) {
552
546
553
547
clusterExtension , extensionCatalog , sa , ns := testInit (t )
554
548
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
555
- defer getArtifactsOutput ( t )
549
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
556
550
557
551
t .Log ("By creating an ClusterExtension at a specified version" )
558
552
clusterExtension .Spec = ocv1.ClusterExtensionSpec {
@@ -601,7 +595,7 @@ func TestClusterExtensionInstallSuccessorVersion(t *testing.T) {
601
595
t .Log ("When resolving upgrade edges" )
602
596
clusterExtension , extensionCatalog , sa , ns := testInit (t )
603
597
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
604
- defer getArtifactsOutput ( t )
598
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
605
599
606
600
t .Log ("By creating an ClusterExtension at a specified version" )
607
601
clusterExtension .Spec = ocv1.ClusterExtensionSpec {
@@ -649,7 +643,7 @@ func TestClusterExtensionInstallReResolvesWhenCatalogIsPatched(t *testing.T) {
649
643
t .Log ("It resolves again when a catalog is patched with new ImageRef" )
650
644
clusterExtension , extensionCatalog , sa , ns := testInit (t )
651
645
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
652
- defer getArtifactsOutput ( t )
646
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
653
647
654
648
clusterExtension .Spec = ocv1.ClusterExtensionSpec {
655
649
Source : ocv1.SourceConfig {
@@ -736,7 +730,7 @@ func TestClusterExtensionInstallReResolvesWhenNewCatalog(t *testing.T) {
736
730
sa , err := createServiceAccount (context .Background (), types.NamespacedName {Name : clusterExtensionName , Namespace : ns .Name }, clusterExtensionName )
737
731
require .NoError (t , err )
738
732
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
739
- defer getArtifactsOutput ( t )
733
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
740
734
741
735
clusterExtension .Spec = ocv1.ClusterExtensionSpec {
742
736
Source : ocv1.SourceConfig {
@@ -797,7 +791,7 @@ func TestClusterExtensionInstallReResolvesWhenManagedContentChanged(t *testing.T
797
791
t .Log ("It resolves again when managed content is changed" )
798
792
clusterExtension , extensionCatalog , sa , ns := testInit (t )
799
793
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
800
- defer getArtifactsOutput ( t )
794
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
801
795
802
796
clusterExtension .Spec = ocv1.ClusterExtensionSpec {
803
797
Source : ocv1.SourceConfig {
@@ -860,7 +854,7 @@ func TestClusterExtensionRecoversFromInitialInstallFailedWhenFailureFixed(t *tes
860
854
err := c .Create (context .Background (), sa )
861
855
require .NoError (t , err )
862
856
defer testCleanup (t , extensionCatalog , clusterExtension , sa , ns )
863
- defer getArtifactsOutput ( t )
857
+ defer utils . CollectTestArtifacts ( t , artifactName , c , cfg )
864
858
865
859
clusterExtension .Spec = ocv1.ClusterExtensionSpec {
866
860
Source : ocv1.SourceConfig {
@@ -936,131 +930,3 @@ func TestClusterExtensionRecoversFromInitialInstallFailedWhenFailureFixed(t *tes
936
930
}
937
931
}, pollDuration , pollInterval )
938
932
}
939
-
940
- // getArtifactsOutput gets all the artifacts from the test run and saves them to the artifact path.
941
- // Currently it saves:
942
- // - clusterextensions
943
- // - pods logs
944
- // - deployments
945
- // - catalogsources
946
- func getArtifactsOutput (t * testing.T ) {
947
- basePath := env .GetString ("ARTIFACT_PATH" , "" )
948
- if basePath == "" {
949
- return
950
- }
951
-
952
- kubeClient , err := kubeclient .NewForConfig (cfg )
953
- require .NoError (t , err )
954
-
955
- // sanitize the artifact name for use as a directory name
956
- testName := strings .ReplaceAll (strings .ToLower (t .Name ()), " " , "-" )
957
- // Get the test description and sanitize it for use as a directory name
958
- artifactPath := filepath .Join (basePath , artifactName , fmt .Sprint (time .Now ().UnixNano ()), testName )
959
-
960
- // Create the full artifact path
961
- err = os .MkdirAll (artifactPath , 0755 )
962
- require .NoError (t , err )
963
-
964
- // Get all namespaces
965
- namespaces := corev1.NamespaceList {}
966
- if err := c .List (context .Background (), & namespaces ); err != nil {
967
- fmt .Printf ("Failed to list namespaces: %v" , err )
968
- }
969
-
970
- // get all cluster extensions save them to the artifact path.
971
- clusterExtensions := ocv1.ClusterExtensionList {}
972
- if err := c .List (context .Background (), & clusterExtensions , client .InNamespace ("" )); err != nil {
973
- fmt .Printf ("Failed to list cluster extensions: %v" , err )
974
- }
975
- for _ , clusterExtension := range clusterExtensions .Items {
976
- // Save cluster extension to artifact path
977
- clusterExtensionYaml , err := yaml .Marshal (clusterExtension )
978
- if err != nil {
979
- fmt .Printf ("Failed to marshal cluster extension: %v" , err )
980
- continue
981
- }
982
- if err := os .WriteFile (filepath .Join (artifactPath , clusterExtension .Name + "-clusterextension.yaml" ), clusterExtensionYaml , 0600 ); err != nil {
983
- fmt .Printf ("Failed to write cluster extension to file: %v" , err )
984
- }
985
- }
986
-
987
- // get all catalogsources save them to the artifact path.
988
- catalogsources := catalogd.ClusterCatalogList {}
989
- if err := c .List (context .Background (), & catalogsources , client .InNamespace ("" )); err != nil {
990
- fmt .Printf ("Failed to list catalogsources: %v" , err )
991
- }
992
- for _ , catalogsource := range catalogsources .Items {
993
- // Save catalogsource to artifact path
994
- catalogsourceYaml , err := yaml .Marshal (catalogsource )
995
- if err != nil {
996
- fmt .Printf ("Failed to marshal catalogsource: %v" , err )
997
- continue
998
- }
999
- if err := os .WriteFile (filepath .Join (artifactPath , catalogsource .Name + "-catalogsource.yaml" ), catalogsourceYaml , 0600 ); err != nil {
1000
- fmt .Printf ("Failed to write catalogsource to file: %v" , err )
1001
- }
1002
- }
1003
-
1004
- for _ , namespace := range namespaces .Items {
1005
- // let's ignore kube-* namespaces.
1006
- if strings .Contains (namespace .Name , "kube-" ) {
1007
- continue
1008
- }
1009
-
1010
- namespacedArtifactPath := filepath .Join (artifactPath , namespace .Name )
1011
- if err := os .Mkdir (namespacedArtifactPath , 0755 ); err != nil {
1012
- fmt .Printf ("Failed to create namespaced artifact path: %v" , err )
1013
- continue
1014
- }
1015
-
1016
- // get all deployments in the namespace and save them to the artifact path.
1017
- deployments := appsv1.DeploymentList {}
1018
- if err := c .List (context .Background (), & deployments , client .InNamespace (namespace .Name )); err != nil {
1019
- fmt .Printf ("Failed to list deployments %v in namespace: %q" , err , namespace .Name )
1020
- continue
1021
- }
1022
-
1023
- for _ , deployment := range deployments .Items {
1024
- // Save deployment to artifact path
1025
- deploymentYaml , err := yaml .Marshal (deployment )
1026
- if err != nil {
1027
- fmt .Printf ("Failed to marshal deployment: %v" , err )
1028
- continue
1029
- }
1030
- if err := os .WriteFile (filepath .Join (namespacedArtifactPath , deployment .Name + "-deployment.yaml" ), deploymentYaml , 0600 ); err != nil {
1031
- fmt .Printf ("Failed to write deployment to file: %v" , err )
1032
- }
1033
- }
1034
-
1035
- // Get logs from all pods in all namespaces
1036
- pods := corev1.PodList {}
1037
- if err := c .List (context .Background (), & pods , client .InNamespace (namespace .Name )); err != nil {
1038
- fmt .Printf ("Failed to list pods %v in namespace: %q" , err , namespace .Name )
1039
- }
1040
- for _ , pod := range pods .Items {
1041
- if pod .Status .Phase != corev1 .PodRunning && pod .Status .Phase != corev1 .PodSucceeded && pod .Status .Phase != corev1 .PodFailed {
1042
- continue
1043
- }
1044
- for _ , container := range pod .Spec .Containers {
1045
- logs , err := kubeClient .CoreV1 ().Pods (namespace .Name ).GetLogs (pod .Name , & corev1.PodLogOptions {Container : container .Name }).Stream (context .Background ())
1046
- if err != nil {
1047
- fmt .Printf ("Failed to get logs for pod %q in namespace %q: %v" , pod .Name , namespace .Name , err )
1048
- continue
1049
- }
1050
- defer logs .Close ()
1051
-
1052
- outFile , err := os .Create (filepath .Join (namespacedArtifactPath , pod .Name + "-" + container .Name + "-logs.txt" ))
1053
- if err != nil {
1054
- fmt .Printf ("Failed to create file for pod %q in namespace %q: %v" , pod .Name , namespace .Name , err )
1055
- continue
1056
- }
1057
- defer outFile .Close ()
1058
-
1059
- if _ , err := io .Copy (outFile , logs ); err != nil {
1060
- fmt .Printf ("Failed to copy logs for pod %q in namespace %q: %v" , pod .Name , namespace .Name , err )
1061
- continue
1062
- }
1063
- }
1064
- }
1065
- }
1066
- }
0 commit comments