Skip to content

Commit 385a741

Browse files
committed
Wait sample app pod in integration tests
1 parent c572a8a commit 385a741

5 files changed

Lines changed: 27 additions & 13 deletions

File tree

‎cli/cmd/run.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func run(
4545
}
4646

4747
fmt.Println("Waiting for a job to start")
48-
jobPodName, err := k8s.WaitPod(jobName)
48+
jobPodName, err := k8s.WaitPod(map[string]string{"job-name": jobName})
4949
if err != nil {
5050
return err
5151
}

‎go.sum‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,8 @@ k8s.io/cli-runtime v0.19.0 h1:wLe+osHSqcItyS3MYQXVyGFa54fppORVA8Jn7DBGSWw=
484484
k8s.io/cli-runtime v0.19.0/go.mod h1:tun9l0eUklT8IHIM0jors17KmUjcrAxn0myoBYwuNuo=
485485
k8s.io/client-go v0.19.0 h1:1+0E0zfWFIWeyRhQYWzimJOyAk2UT7TiARaLNwJCf7k=
486486
k8s.io/client-go v0.19.0/go.mod h1:H9E/VT95blcFQnlyShFgnFT9ZnJOAceiUHM3MlRC+mU=
487+
k8s.io/client-go v1.5.1 h1:XaX/lo2/u3/pmFau8HN+sB5C/b4dc4Dmm2eXjBH4p1E=
488+
k8s.io/client-go v11.0.0+incompatible h1:LBbX2+lOwY9flffWlJM7f1Ct8V2SRNiMRDFeiwnJo9o=
487489
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
488490
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
489491
k8s.io/klog/v2 v2.2.0 h1:XRvcwJozkgZ1UQJmfMGpvRthQHOvihEhYtDfAaxMz/A=

‎internal/kubernetes/pods.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
apiv1 "k8s.io/api/core/v1"
1010
v1 "k8s.io/api/core/v1"
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"k8s.io/apimachinery/pkg/labels"
1213
"k8s.io/apimachinery/pkg/util/wait"
1314
)
1415

@@ -25,7 +26,7 @@ func (k8s *Client) GetPodInfo(podName string) (*v1.Pod, error) {
2526
}
2627

2728
// WaitPod will wait pod to start
28-
func (k8s *Client) WaitPod(jobName string) (string, error) {
29+
func (k8s *Client) WaitPod(labelSelector map[string]string) (string, error) {
2930
var pod *v1.Pod
3031
err := wait.Poll(1*time.Second, 5*time.Minute,
3132
func() (bool, error) {
@@ -35,7 +36,7 @@ func (k8s *Client) WaitPod(jobName string) (string, error) {
3536
List(
3637
context.Background(),
3738
metav1.ListOptions{
38-
LabelSelector: fmt.Sprintf("job-name=%s", jobName),
39+
LabelSelector: labels.Set(labelSelector).String(),
3940
},
4041
)
4142
if err != nil {

‎test/integration/helpers_test.go‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,29 @@ package integration_test
55
import (
66
"path/filepath"
77

8+
"github.com/dodopizza/kubectl-shovel/internal/kubernetes"
89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9-
"k8s.io/client-go/kubernetes"
10+
k8s "k8s.io/client-go/kubernetes"
1011
"k8s.io/client-go/tools/clientcmd"
1112
"k8s.io/client-go/util/homedir"
1213
)
1314

1415
var deletePolicy = metav1.DeletePropagationForeground
1516

16-
func newTestKubeClient() *kubernetes.Clientset {
17+
func newTestKubeClient() *kubernetes.Client {
1718
kubeconfig := filepath.Join(homedir.HomeDir(), ".kube", "config")
1819
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
1920
if err != nil {
2021
panic(err.Error())
2122
}
2223

23-
clientset, err := kubernetes.NewForConfig(config)
24+
clientset, err := k8s.NewForConfig(config)
2425
if err != nil {
2526
panic(err.Error())
2627
}
2728

28-
return clientset
29+
return &kubernetes.Client{
30+
Namespace: namespace,
31+
Clientset: clientset,
32+
}
2933
}

‎test/integration/shovel_test.go‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"os"
1010
"path/filepath"
1111
"testing"
12-
"time"
1312

1413
"github.com/stretchr/testify/require"
1514

@@ -29,13 +28,14 @@ var (
2928

3029
func TestMain(m *testing.M) {
3130
k8s := newTestKubeClient()
31+
labels := map[string]string{
32+
"app": sampleAppName,
33+
}
3234

3335
sampleAppPod := &v1.Pod{
3436
ObjectMeta: metav1.ObjectMeta{
35-
Name: sampleAppName,
36-
Labels: map[string]string{
37-
"app": sampleAppName,
38-
},
37+
Name: sampleAppName,
38+
Labels: labels,
3939
},
4040
Spec: v1.PodSpec{
4141
Containers: []v1.Container{
@@ -47,12 +47,19 @@ func TestMain(m *testing.M) {
4747
},
4848
}
4949

50+
fmt.Println("Deploying dotnet sample app to cluster")
5051
_, err := k8s.CoreV1().Pods(namespace).Create(context.TODO(), sampleAppPod, metav1.CreateOptions{})
5152
if err != nil {
5253
fmt.Println(err)
5354
os.Exit(1)
5455
}
55-
<-time.After(10 * time.Second)
56+
57+
fmt.Println("Waiting app to start")
58+
_, err = k8s.WaitPod(labels)
59+
if err != nil {
60+
fmt.Println(err)
61+
os.Exit(1)
62+
}
5663
exitCode := m.Run()
5764
_ = k8s.CoreV1().Pods(namespace).Delete(context.TODO(), sampleAppName, metav1.DeleteOptions{PropagationPolicy: &deletePolicy})
5865

0 commit comments

Comments
 (0)