Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Sep 27, 2024
1 parent 82b566e commit 2e11495
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
2 changes: 0 additions & 2 deletions pkg/build/buildkit/autodiscovery/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,6 @@ func watchBuildKitPods(ctx context.Context, cs kubernetes.Interface, opts Kubern
continue
}

fmt.Println("****", e.Object)

pod := e.Object.(*corev1.Pod)
if isPodReady(pod) {
pods <- pod
Expand Down
94 changes: 94 additions & 0 deletions pkg/build/buildkit/autodiscovery/k8s_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package autodiscovery

Check failure on line 1 in pkg/build/buildkit/autodiscovery/k8s_test.go

View workflow job for this annotation

GitHub Actions / lint

Missed header for check (goheader)

Check failure on line 1 in pkg/build/buildkit/autodiscovery/k8s_test.go

View workflow job for this annotation

GitHub Actions / lint

Missed header for check (goheader)

import (
"context"
"os"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/tsuru/deploy-agent/pkg/build/grpc_build_v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
fakeDynamic "k8s.io/client-go/dynamic/fake"
"k8s.io/client-go/kubernetes/fake"
kuberntesTesting "k8s.io/client-go/testing"
"k8s.io/utils/ptr"
)

func TestK8sDiscoverer_Discover(t *testing.T) {

Check failure on line 21 in pkg/build/buildkit/autodiscovery/k8s_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

Check failure on line 21 in pkg/build/buildkit/autodiscovery/k8s_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

buildKitPod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-app",
Namespace: "tsuru",
Labels: map[string]string{
"app": "test-app",
},
Annotations: map[string]string{
"foo": "bar",
},
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
PodIP: "127.0.0.1",
Conditions: []corev1.PodCondition{
{
Type: corev1.PodReady,
Status: corev1.ConditionTrue,
},
},
},
}
fakeClient := fake.NewSimpleClientset(buildKitPod)

fakeClient.PrependWatchReactor("*", func(action kuberntesTesting.Action) (handled bool, ret watch.Interface, err error) {
watcher := watch.NewFake()

go func() {
time.Sleep(time.Millisecond * 100)
watcher.Add(buildKitPod)
}()
return true, watcher, nil
})

fakeDynamicClient := fakeDynamic.NewSimpleDynamicClient(runtime.NewScheme())

discoverer := K8sDiscoverer{
KubernetesInterface: fakeClient,
DynamicInterface: fakeDynamicClient,
}

_, _, err := discoverer.Discover(
context.TODO(),
KubernertesDiscoveryOptions{
PodSelector: "app=test-app",
Namespace: "tsuru",
Timeout: time.Second * 2,
SetTsuruAppLabel: true,
},
&grpc_build_v1.BuildRequest{
App: &grpc_build_v1.TsuruApp{
Name: "test-app",
Team: ptr.To("test-team"),
},
},
os.Stdout,
)
assert.NoError(t, err)

existingPod, err := fakeClient.CoreV1().Pods("tsuru").Get(context.TODO(), "test-app", metav1.GetOptions{})
assert.NoError(t, err)

assert.Equal(t, map[string]string{
"app": "test-app",
"tsuru.io/app-name": "test-app",
"tsuru.io/app-team": "test-team",
"tsuru.io/is-build": "true",
}, existingPod.Labels)

assert.Equal(t, "", existingPod.Annotations["deploy-agent.tsuru.io/last-build-ending-time"])
assert.NotEqual(t, "", existingPod.Annotations["deploy-agent.tsuru.io/last-build-starting-time"])
}

0 comments on commit 2e11495

Please sign in to comment.