Compare commits

...

2 Commits

Author SHA1 Message Date
Volodymyr Zotov
a5e4a352e9 Update readme 2025-09-30 16:59:30 -05:00
Volodymyr Zotov
edde903759 Do not use first pod, but look for matching pod in array 2025-09-30 16:56:10 -05:00
2 changed files with 26 additions and 15 deletions

View File

@@ -1,6 +1,6 @@
# OnePassword Operator Test Helper # OnePassword Operator Test Helper
This is a standalone Go module that provides testing utilities for Kubernetes operators, specifically designed for testing OnePassword operators but can be used for any Kubernetes operator testing. This is a standalone Go module that provides testing utilities for Kubernetes operators and webhooks. It's specifically designed for testing 1Password Kubernetes operator and secrets injector, but it can be used for any Kubernetes operator or webhook testing.
## Installation ## Installation
@@ -35,19 +35,19 @@ kubeClient := kube.NewKubeClient(&kube.Config{
### Working with Secrets ### Working with Secrets
```go ```go
// Create a secret from environment variable // Create k8s secret from environment variable
secret := kubeClient.Secret("my-secret") k8sSecret := kubeClient.Secret("my-secret")
secret.CreateFromEnvVar(ctx, "MY_ENV_VAR") k8sSecret.CreateFromEnvVar(ctx, "MY_ENV_VAR")
// Create a secret from file // Create a secret from file
data := []byte("secret content") data := []byte("secret content")
secret.CreateFromFile(ctx, "filename", data) k8sSecret.CreateFromFile(ctx, "filename", data)
// Check if secret exists // Check if secret exists
secret.CheckIfExists(ctx) k8sSecret.CheckIfExists(ctx)
// Get secret // Get secret
secretObj := secret.Get(ctx) secretObj := k8sSecret.Get(ctx)
``` ```
### Working with Deployments ### Working with Deployments

View File

@@ -60,13 +60,17 @@ func (p *Pod) GetPodLogs(ctx context.Context) string {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(pods.Items).NotTo(BeEmpty(), "no pods found with selector %q", labels.Set(p.selector).String()) Expect(pods.Items).NotTo(BeEmpty(), "no pods found with selector %q", labels.Set(p.selector).String())
// Use the first pod found // Find a running pod to get logs from
pod := pods.Items[0] var pod *corev1.Pod
for i := range pods.Items {
if pods.Items[i].Status.Phase == corev1.PodRunning {
pod = &pods.Items[i]
break
}
}
Expect(pod).NotTo(BeNil(), "no running pod found with selector %q", labels.Set(p.selector).String())
podName := pod.Name podName := pod.Name
// Verify pod is running before getting logs
Expect(pod.Status.Phase).To(Equal(corev1.PodRunning), "pod %s is not running (status: %s)", podName, pod.Status.Phase)
// Get logs using the Kubernetes clientset // Get logs using the Kubernetes clientset
req := p.clientset.CoreV1().Pods(p.config.Namespace).GetLogs(podName, &corev1.PodLogOptions{}) req := p.clientset.CoreV1().Pods(p.config.Namespace).GetLogs(podName, &corev1.PodLogOptions{})
stream, err := req.Stream(context.TODO()) stream, err := req.Stream(context.TODO())
@@ -97,8 +101,15 @@ func (p *Pod) VerifyWebhookInjection(ctx context.Context) {
g.Expect(p.client.List(attemptCtx, &pods, listOpts...)).To(Succeed()) g.Expect(p.client.List(attemptCtx, &pods, listOpts...)).To(Succeed())
g.Expect(pods.Items).NotTo(BeEmpty(), "no pods found with selector %q", labels.Set(p.selector).String()) g.Expect(pods.Items).NotTo(BeEmpty(), "no pods found with selector %q", labels.Set(p.selector).String())
// Use the first pod found // Find a running pod to verify webhook injection
pod := pods.Items[0] var pod *corev1.Pod
for i := range pods.Items {
if pods.Items[i].Status.Phase == corev1.PodRunning {
pod = &pods.Items[i]
break
}
}
g.Expect(pod).NotTo(BeNil(), "no running pod found with selector %q", labels.Set(p.selector).String())
// Check injection status annotation // Check injection status annotation
g.Expect(pod.Annotations).To(HaveKey("operator.1password.io/status")) g.Expect(pod.Annotations).To(HaveKey("operator.1password.io/status"))