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 ff42929
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 104 deletions.
6 changes: 2 additions & 4 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 Expand Up @@ -325,11 +323,11 @@ func setTsuruAppLabelOnBuildKitPod(ctx context.Context, cs kubernetes.Interface,
},
}

if app.Team != nil {
if app.Team != "" {
changes = append(changes, map[string]any{
"op": "replace",
"path": fmt.Sprintf("/metadata/labels/%s", normalizeAppLabelForJSONPatch(metadata.TsuruAppTeamLabelKey)),
"value": *app.Team,
"value": app.Team,
})
}

Expand Down
93 changes: 93 additions & 0 deletions pkg/build/buildkit/autodiscovery/k8s_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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"
)

func TestK8sDiscoverer_Discover(t *testing.T) {

Check failure on line 20 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 20 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: "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"])
}
Loading

0 comments on commit ff42929

Please sign in to comment.