sigs.k8s.io/cluster-api@v1.7.1/controllers/remote/keyedmutex_test.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package remote
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"sigs.k8s.io/controller-runtime/pkg/client"
    25  )
    26  
    27  func TestKeyedMutex(t *testing.T) {
    28  	t.Run("Lock a Cluster and ensures the second Lock on the same Cluster returns false", func(t *testing.T) {
    29  		t.Parallel()
    30  		g := NewWithT(t)
    31  
    32  		cluster1 := client.ObjectKey{Namespace: metav1.NamespaceDefault, Name: "Cluster1"}
    33  		km := newKeyedMutex()
    34  
    35  		// Try to lock cluster1.
    36  		// Should work as nobody currently holds the lock for cluster1.
    37  		g.Expect(km.TryLock(cluster1)).To(BeTrue())
    38  
    39  		// Try to lock cluster1 again.
    40  		// Shouldn't work as cluster1 is already locked.
    41  		g.Expect(km.TryLock(cluster1)).To(BeFalse())
    42  
    43  		// Unlock cluster1.
    44  		km.Unlock(cluster1)
    45  
    46  		// Ensure that the lock was cleaned up from the internal map.
    47  		g.Expect(km.locks).To(BeEmpty())
    48  	})
    49  
    50  	t.Run("Can lock different Clusters in parallel but each one only once", func(t *testing.T) {
    51  		g := NewWithT(t)
    52  		km := newKeyedMutex()
    53  		clusters := []client.ObjectKey{
    54  			{Namespace: metav1.NamespaceDefault, Name: "Cluster1"},
    55  			{Namespace: metav1.NamespaceDefault, Name: "Cluster2"},
    56  			{Namespace: metav1.NamespaceDefault, Name: "Cluster3"},
    57  			{Namespace: metav1.NamespaceDefault, Name: "Cluster4"},
    58  		}
    59  
    60  		// Run this twice to ensure Clusters can be locked again
    61  		// after they have been unlocked.
    62  		for i := 0; i < 2; i++ {
    63  			// Lock all Clusters (should work).
    64  			for _, key := range clusters {
    65  				g.Expect(km.TryLock(key)).To(BeTrue())
    66  			}
    67  
    68  			// Ensure Clusters can't be locked again.
    69  			for _, key := range clusters {
    70  				g.Expect(km.TryLock(key)).To(BeFalse())
    71  			}
    72  
    73  			// Unlock all Clusters.
    74  			for _, key := range clusters {
    75  				km.Unlock(key)
    76  			}
    77  		}
    78  
    79  		// Ensure that the lock was cleaned up from the internal map.
    80  		g.Expect(km.locks).To(BeEmpty())
    81  	})
    82  }