Skip to content

Commit

Permalink
fix git controller test
Browse files Browse the repository at this point in the history
  • Loading branch information
nabuskey committed Oct 20, 2023
1 parent 1859b59 commit c8effb6
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions pkg/controllers/gitserver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import (
"testing/fstest"
"time"

"github.com/cnoe-io/idpbuilder/globals"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/kubernetes"

"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/k8s"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
)

Expand Down Expand Up @@ -94,31 +99,50 @@ func TestGitServerController(t *testing.T) {
Namespace: ResourceNamespace,
UID: "test-uid",
},
Spec: v1alpha1.GitServerSpec{Source: v1alpha1.GitServerSource{Image: "fake", Embedded: false}},
}
if err := k8sClient.Create(ctx, resource); err != nil {
t.Fatalf("Creating resource: %v", err)
}

// Wait for GitServer to become available
labelReq, err := labels.NewRequirement("app", selection.Equals, []string{fmt.Sprintf("%s-%s", globals.ProjectName, resource.Name)})
if err != nil {
t.Fatalf("Failed constructing label selector requrirement: %v", err)
}
selector := labels.NewSelector()
selector.Add(*labelReq)
clientSet, err := kubernetes.NewForConfig(cfg)
if err != nil {
t.Fatalf("Failed creating deployment client: %v", err)
}

// Wait for GitServer deployment object to be created.
endTime := time.Now().Add(timeout)
for {
if time.Now().After(endTime) {
t.Fatal("Timed out waiting for resource available")
}

var gotResource v1alpha1.GitServer
if err = k8sClient.Get(ctx, client.ObjectKey{
Name: ResourceName,
Namespace: ResourceNamespace,
}, &gotResource); err != nil {
t.Logf("Failed getting resource (might be ok though): %v", err)
deployment, err := clientSet.AppsV1().Deployments(resource.Namespace).Get(ctx, managedResourceName(resource), v1.GetOptions{})
if err != nil {
if !errors.IsNotFound(err) {
t.Fatalf("Failed getting deployment: %v", err)
}
t.Log("Waiting for deployment object...")
time.Sleep(interval)
continue
}

if gotResource.Status.DeploymentAvailable {
break
for k, v := range GetGitServerLabels(resource) {
val, ok := deployment.Spec.Selector.MatchLabels[k]
if !ok {
t.Fatalf("Expected deployment labels selector key, %s, not found", k)
}
if val != v {
t.Fatalf("Expected deployment labels selector val not found: %s", v)
}
}

time.Sleep(interval)
break
}
}

0 comments on commit c8effb6

Please sign in to comment.