|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + controlv1 "github.com/appuio/control-api/apis/v1" |
| 8 | + userv1 "github.com/openshift/api/user/v1" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/types" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 14 | +) |
| 15 | + |
| 16 | +func Test_GroupSyncReconciler_Reconcile(t *testing.T) { |
| 17 | + upstreamTeam := controlv1.Team{ |
| 18 | + ObjectMeta: metav1.ObjectMeta{ |
| 19 | + Name: "developers", |
| 20 | + Namespace: "thedoening", |
| 21 | + }, |
| 22 | + Spec: controlv1.TeamSpec{ |
| 23 | + UserRefs: buildUserRefs("johndoe"), |
| 24 | + }, |
| 25 | + Status: controlv1.TeamStatus{ |
| 26 | + ResolvedUserRefs: buildUserRefs("johndoe"), |
| 27 | + }, |
| 28 | + } |
| 29 | + upstreamOM := controlv1.OrganizationMembers{ |
| 30 | + ObjectMeta: metav1.ObjectMeta{ |
| 31 | + Name: OrganizationMembersManifestName, |
| 32 | + Namespace: "thedoening", |
| 33 | + }, |
| 34 | + Spec: controlv1.OrganizationMembersSpec{ |
| 35 | + UserRefs: buildUserRefs("johndoe"), |
| 36 | + }, |
| 37 | + Status: controlv1.OrganizationMembersStatus{ |
| 38 | + ResolvedUserRefs: buildUserRefs("johndoe"), |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + client, scheme, recorder := prepareClient(t) |
| 43 | + foreignClient, _, _ := prepareClient(t, &upstreamTeam, &upstreamOM) |
| 44 | + |
| 45 | + subject := GroupSyncReconciler{ |
| 46 | + Client: client, |
| 47 | + Scheme: scheme, |
| 48 | + Recorder: recorder, |
| 49 | + ForeignClient: foreignClient, |
| 50 | + |
| 51 | + ControlAPIFinalizerZoneName: "lupfig", |
| 52 | + } |
| 53 | + |
| 54 | + t.Run("Team", func(t *testing.T) { |
| 55 | + // Create |
| 56 | + _, err := subject.Reconcile(context.Background(), teamMapper(context.Background(), &upstreamTeam)[0]) |
| 57 | + require.NoError(t, err) |
| 58 | + var group userv1.Group |
| 59 | + require.NoError(t, client.Get(context.Background(), types.NamespacedName{Name: "thedoening+developers"}, &group), "should have created a group from the team") |
| 60 | + require.Equal(t, userv1.OptionalNames{"johndoe"}, group.Users, "should have set the group users") |
| 61 | + // Finalizer |
| 62 | + require.NoError(t, foreignClient.Get(context.Background(), namespacedName(&upstreamTeam), &upstreamTeam)) |
| 63 | + require.Contains(t, upstreamTeam.Finalizers, "agent.appuio.io/group-zone-lupfig", "should have added a finalizer upstream") |
| 64 | + |
| 65 | + // Update |
| 66 | + upstreamTeam.Spec.UserRefs = buildUserRefs("johndoe", "janedoe") |
| 67 | + upstreamTeam.Status.ResolvedUserRefs = buildUserRefs("johndoe", "janedoe") |
| 68 | + require.NoError(t, foreignClient.Update(context.Background(), &upstreamTeam)) |
| 69 | + _, err = subject.Reconcile(context.Background(), teamMapper(context.Background(), &upstreamTeam)[0]) |
| 70 | + require.NoError(t, err) |
| 71 | + require.NoError(t, client.Get(context.Background(), types.NamespacedName{Name: "thedoening+developers"}, &group)) |
| 72 | + require.Equal(t, userv1.OptionalNames{"janedoe", "johndoe"}, group.Users, "should have updated the group from the team") |
| 73 | + |
| 74 | + // Delete upstream team |
| 75 | + require.NoError(t, foreignClient.Delete(context.Background(), &upstreamTeam)) |
| 76 | + require.NoError(t, foreignClient.Get(context.Background(), namespacedName(&upstreamTeam), &upstreamTeam), "should not have deleted the upstream team since it has a finalizer") |
| 77 | + _, err = subject.Reconcile(context.Background(), teamMapper(context.Background(), &upstreamTeam)[0]) |
| 78 | + require.NoError(t, err) |
| 79 | + require.True(t, apierrors.IsNotFound(foreignClient.Get(context.Background(), namespacedName(&upstreamTeam), &upstreamTeam)), "should have deleted the upstream team after removing the finalizer") |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("OrganizationMembers", func(t *testing.T) { |
| 83 | + // Create |
| 84 | + _, err := subject.Reconcile(context.Background(), organizationMembersMapper(context.Background(), &upstreamOM)[0]) |
| 85 | + require.NoError(t, err) |
| 86 | + var group userv1.Group |
| 87 | + require.NoError(t, client.Get(context.Background(), types.NamespacedName{Name: "thedoening"}, &group), "should have created a group from the organization members") |
| 88 | + require.Equal(t, userv1.OptionalNames{"johndoe"}, group.Users, "should have set the group users") |
| 89 | + // Finalizer |
| 90 | + require.NoError(t, foreignClient.Get(context.Background(), namespacedName(&upstreamOM), &upstreamOM)) |
| 91 | + require.Contains(t, upstreamOM.Finalizers, "agent.appuio.io/group-zone-lupfig", "should have added a finalizer upstream") |
| 92 | + |
| 93 | + // Update |
| 94 | + upstreamOM.Spec.UserRefs = buildUserRefs("johndoe", "janedoe") |
| 95 | + upstreamOM.Status.ResolvedUserRefs = buildUserRefs("johndoe", "janedoe") |
| 96 | + require.NoError(t, foreignClient.Update(context.Background(), &upstreamOM)) |
| 97 | + _, err = subject.Reconcile(context.Background(), organizationMembersMapper(context.Background(), &upstreamOM)[0]) |
| 98 | + require.NoError(t, err) |
| 99 | + require.NoError(t, client.Get(context.Background(), types.NamespacedName{Name: "thedoening"}, &group)) |
| 100 | + require.Equal(t, userv1.OptionalNames{"janedoe", "johndoe"}, group.Users, "should have updated the group from the organization members") |
| 101 | + |
| 102 | + // Delete upstream organization members |
| 103 | + require.NoError(t, foreignClient.Delete(context.Background(), &upstreamOM)) |
| 104 | + require.NoError(t, foreignClient.Get(context.Background(), namespacedName(&upstreamOM), &upstreamOM), "should not have deleted the upstream OrganizationMembers since it has a finalizer") |
| 105 | + _, err = subject.Reconcile(context.Background(), organizationMembersMapper(context.Background(), &upstreamOM)[0]) |
| 106 | + require.NoError(t, err) |
| 107 | + require.True(t, apierrors.IsNotFound(foreignClient.Get(context.Background(), namespacedName(&upstreamOM), &upstreamOM)), "should have deleted the upstream OrganizationMembers after removing the finalizer") |
| 108 | + }) |
| 109 | +} |
| 110 | + |
| 111 | +func buildUserRefs(names ...string) []controlv1.UserRef { |
| 112 | + var refs []controlv1.UserRef |
| 113 | + for _, name := range names { |
| 114 | + refs = append(refs, controlv1.UserRef{Name: name}) |
| 115 | + } |
| 116 | + return refs |
| 117 | +} |
| 118 | + |
| 119 | +func namespacedName(o client.Object) types.NamespacedName { |
| 120 | + return types.NamespacedName{Name: o.GetName(), Namespace: o.GetNamespace()} |
| 121 | +} |
0 commit comments