Skip to content

Commit e734312

Browse files
author
Per Goncalves da Silva
committed
add artifact collection to upgrade-e2e
Signed-off-by: Per Goncalves da Silva <[email protected]>
1 parent 877f85d commit e734312

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

test/upgrade-e2e/post_upgrade_test.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import (
44
"bufio"
55
"context"
66
"fmt"
7+
"gopkg.in/yaml.v2"
8+
"io"
9+
kubeclient "k8s.io/client-go/kubernetes"
10+
"k8s.io/utils/env"
11+
"os"
12+
"path/filepath"
713
"strings"
814
"testing"
915
"time"
@@ -22,9 +28,14 @@ import (
2228
catalogd "github.com/operator-framework/operator-controller/catalogd/api/v1"
2329
)
2430

31+
const (
32+
artifactName = "operator-controller-upgrade-e2e"
33+
)
34+
2535
func TestClusterExtensionAfterOLMUpgrade(t *testing.T) {
2636
t.Log("Starting checks after OLM upgrade")
2737
ctx := context.Background()
38+
defer getArtifactsOutput(t)
2839

2940
managerLabelSelector := labels.Set{"control-plane": "operator-controller-controller-manager"}
3041

@@ -141,3 +152,131 @@ func watchPodLogsForSubstring(ctx context.Context, pod *corev1.Pod, container st
141152

142153
return false, scanner.Err()
143154
}
155+
156+
// getArtifactsOutput gets all the artifacts from the test run and saves them to the artifact path.
157+
// Currently it saves:
158+
// - clusterextensions
159+
// - pods logs
160+
// - deployments
161+
// - catalogsources
162+
func getArtifactsOutput(t *testing.T) {
163+
basePath := env.GetString("ARTIFACT_PATH", "")
164+
if basePath == "" {
165+
return
166+
}
167+
168+
kubeClient, err := kubeclient.NewForConfig(cfg)
169+
require.NoError(t, err)
170+
171+
// sanitize the artifact name for use as a directory name
172+
testName := strings.ReplaceAll(strings.ToLower(t.Name()), " ", "-")
173+
// Get the test description and sanitize it for use as a directory name
174+
artifactPath := filepath.Join(basePath, artifactName, fmt.Sprint(time.Now().UnixNano()), testName)
175+
176+
// Create the full artifact path
177+
err = os.MkdirAll(artifactPath, 0755)
178+
require.NoError(t, err)
179+
180+
// Get all namespaces
181+
namespaces := corev1.NamespaceList{}
182+
if err := c.List(context.Background(), &namespaces); err != nil {
183+
fmt.Printf("Failed to list namespaces: %v", err)
184+
}
185+
186+
// get all cluster extensions save them to the artifact path.
187+
clusterExtensions := ocv1.ClusterExtensionList{}
188+
if err := c.List(context.Background(), &clusterExtensions, client.InNamespace("")); err != nil {
189+
fmt.Printf("Failed to list cluster extensions: %v", err)
190+
}
191+
for _, clusterExtension := range clusterExtensions.Items {
192+
// Save cluster extension to artifact path
193+
clusterExtensionYaml, err := yaml.Marshal(clusterExtension)
194+
if err != nil {
195+
fmt.Printf("Failed to marshal cluster extension: %v", err)
196+
continue
197+
}
198+
if err := os.WriteFile(filepath.Join(artifactPath, clusterExtension.Name+"-clusterextension.yaml"), clusterExtensionYaml, 0600); err != nil {
199+
fmt.Printf("Failed to write cluster extension to file: %v", err)
200+
}
201+
}
202+
203+
// get all catalogsources save them to the artifact path.
204+
catalogsources := catalogd.ClusterCatalogList{}
205+
if err := c.List(context.Background(), &catalogsources, client.InNamespace("")); err != nil {
206+
fmt.Printf("Failed to list catalogsources: %v", err)
207+
}
208+
for _, catalogsource := range catalogsources.Items {
209+
// Save catalogsource to artifact path
210+
catalogsourceYaml, err := yaml.Marshal(catalogsource)
211+
if err != nil {
212+
fmt.Printf("Failed to marshal catalogsource: %v", err)
213+
continue
214+
}
215+
if err := os.WriteFile(filepath.Join(artifactPath, catalogsource.Name+"-catalogsource.yaml"), catalogsourceYaml, 0600); err != nil {
216+
fmt.Printf("Failed to write catalogsource to file: %v", err)
217+
}
218+
}
219+
220+
for _, namespace := range namespaces.Items {
221+
// let's ignore kube-* namespaces.
222+
if strings.Contains(namespace.Name, "kube-") {
223+
continue
224+
}
225+
226+
namespacedArtifactPath := filepath.Join(artifactPath, namespace.Name)
227+
if err := os.Mkdir(namespacedArtifactPath, 0755); err != nil {
228+
fmt.Printf("Failed to create namespaced artifact path: %v", err)
229+
continue
230+
}
231+
232+
// get all deployments in the namespace and save them to the artifact path.
233+
deployments := appsv1.DeploymentList{}
234+
if err := c.List(context.Background(), &deployments, client.InNamespace(namespace.Name)); err != nil {
235+
fmt.Printf("Failed to list deployments %v in namespace: %q", err, namespace.Name)
236+
continue
237+
}
238+
239+
for _, deployment := range deployments.Items {
240+
// Save deployment to artifact path
241+
deploymentYaml, err := yaml.Marshal(deployment)
242+
if err != nil {
243+
fmt.Printf("Failed to marshal deployment: %v", err)
244+
continue
245+
}
246+
if err := os.WriteFile(filepath.Join(namespacedArtifactPath, deployment.Name+"-deployment.yaml"), deploymentYaml, 0600); err != nil {
247+
fmt.Printf("Failed to write deployment to file: %v", err)
248+
}
249+
}
250+
251+
// Get logs from all pods in all namespaces
252+
pods := corev1.PodList{}
253+
if err := c.List(context.Background(), &pods, client.InNamespace(namespace.Name)); err != nil {
254+
fmt.Printf("Failed to list pods %v in namespace: %q", err, namespace.Name)
255+
}
256+
for _, pod := range pods.Items {
257+
if pod.Status.Phase != corev1.PodRunning && pod.Status.Phase != corev1.PodSucceeded && pod.Status.Phase != corev1.PodFailed {
258+
continue
259+
}
260+
for _, container := range pod.Spec.Containers {
261+
logs, err := kubeClient.CoreV1().Pods(namespace.Name).GetLogs(pod.Name, &corev1.PodLogOptions{Container: container.Name}).Stream(context.Background())
262+
if err != nil {
263+
fmt.Printf("Failed to get logs for pod %q in namespace %q: %v", pod.Name, namespace.Name, err)
264+
continue
265+
}
266+
defer logs.Close()
267+
268+
outFile, err := os.Create(filepath.Join(namespacedArtifactPath, pod.Name+"-"+container.Name+"-logs.txt"))
269+
if err != nil {
270+
fmt.Printf("Failed to create file for pod %q in namespace %q: %v", pod.Name, namespace.Name, err)
271+
continue
272+
}
273+
defer outFile.Close()
274+
275+
if _, err := io.Copy(outFile, logs); err != nil {
276+
fmt.Printf("Failed to copy logs for pod %q in namespace %q: %v", pod.Name, namespace.Name, err)
277+
continue
278+
}
279+
}
280+
}
281+
}
282+
}

test/upgrade-e2e/upgrade_e2e_suite_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package upgradee2e
22

33
import (
44
"fmt"
5+
"k8s.io/client-go/rest"
56
"os"
67
"testing"
78

@@ -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)