github.com/diggerhq/digger/libs@v0.0.0-20240604170430-9d61cdf01cc5/locking/gcp/gcp_lock_test.go (about) 1 package gcp 2 3 import ( 4 "cloud.google.com/go/storage" 5 "github.com/stretchr/testify/assert" 6 "log" 7 "math/rand" 8 "os" 9 "testing" 10 "time" 11 ) 12 13 func randomString(length int) string { 14 const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 15 16 var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano())) 17 18 b := make([]byte, length) 19 for i := range b { 20 b[i] = charset[seededRand.Intn(len(charset))] 21 } 22 return string(b) 23 } 24 25 func SkipCI(t *testing.T) { 26 if os.Getenv("CI") != "" { 27 t.Skip("Skipping testing in CI environment") 28 } 29 } 30 31 func TestGoogleStorageLock_NewLock(t *testing.T) { 32 SkipCI(t) 33 ctx, client := GetGoogleStorageClient() 34 defer func(client *storage.Client) { 35 err := client.Close() 36 if err != nil { 37 log.Fatalf("Failed to close Google Storage client: %v", err) 38 } 39 }(client) 40 41 bucketName := "digger-lock-test" 42 fileName := "digger-lock-" + randomString(16) 43 44 bucket := client.Bucket(bucketName) 45 lock := GoogleStorageLock{client, bucket, ctx} 46 47 locked, err := lock.Lock(100, fileName) 48 assert.NoError(t, err) 49 assert.True(t, locked) 50 } 51 52 func TestGoogleStorageLock_LockLocked(t *testing.T) { 53 SkipCI(t) 54 ctx, client := GetGoogleStorageClient() 55 defer func(client *storage.Client) { 56 err := client.Close() 57 if err != nil { 58 log.Fatalf("Failed to close Google Storage client: %v", err) 59 } 60 }(client) 61 62 bucketName := "digger-lock-test" 63 fileName := "digger-lock-" + randomString(16) 64 65 bucket := client.Bucket(bucketName) 66 lock := GoogleStorageLock{client, bucket, ctx} 67 68 locked, err := lock.Lock(100, fileName) 69 assert.NoError(t, err) 70 assert.True(t, locked) 71 72 locked, err = lock.Lock(100, fileName) 73 assert.NoError(t, err) 74 assert.False(t, locked) 75 } 76 77 func TestGoogleStorageLock_UnlockLocked(t *testing.T) { 78 SkipCI(t) 79 ctx, client := GetGoogleStorageClient() 80 defer func(client *storage.Client) { 81 err := client.Close() 82 if err != nil { 83 log.Fatalf("Failed to close Google Storage client: %v", err) 84 } 85 }(client) 86 87 bucketName := "digger-lock-test" 88 fileName := "digger-lock-" + randomString(16) 89 transactionId := 100 90 91 bucket := client.Bucket(bucketName) 92 lock := GoogleStorageLock{client, bucket, ctx} 93 94 locked, err := lock.Lock(transactionId, fileName) 95 assert.NoError(t, err) 96 assert.True(t, locked) 97 98 unlocked, err := lock.Unlock(fileName) 99 assert.NoError(t, err) 100 assert.True(t, unlocked) 101 102 lockTransactionId, err := lock.GetLock(fileName) 103 assert.NoError(t, err) 104 assert.Nil(t, lockTransactionId) 105 } 106 107 func TestGoogleStorageLock_UnlockLockedWithDifferentId(t *testing.T) { 108 SkipCI(t) 109 ctx, client := GetGoogleStorageClient() 110 defer func(client *storage.Client) { 111 err := client.Close() 112 if err != nil { 113 log.Fatalf("Failed to close Google Storage client: %v", err) 114 } 115 }(client) 116 117 bucketName := "digger-lock-test" 118 fileName := "digger-lock-" + randomString(16) 119 transactionId := 100 120 //anotherTransactionId := 200 121 122 bucket := client.Bucket(bucketName) 123 lock := GoogleStorageLock{client, bucket, ctx} 124 125 locked, err := lock.Lock(transactionId, fileName) 126 assert.NoError(t, err) 127 assert.True(t, locked) 128 129 unlocked, err := lock.Unlock(fileName) 130 assert.NoError(t, err) 131 assert.True(t, unlocked) 132 } 133 134 func TestGoogleStorageLock_GetExistingLock(t *testing.T) { 135 SkipCI(t) 136 ctx, client := GetGoogleStorageClient() 137 defer func(client *storage.Client) { 138 err := client.Close() 139 if err != nil { 140 log.Fatalf("Failed to close Google Storage client: %v", err) 141 } 142 }(client) 143 144 bucketName := "digger-lock-test" 145 fileName := "digger-lock-" + randomString(16) 146 transactionId := 100 147 148 bucket := client.Bucket(bucketName) 149 lock := GoogleStorageLock{client, bucket, ctx} 150 151 locked, err := lock.Lock(transactionId, fileName) 152 assert.NoError(t, err) 153 assert.True(t, locked) 154 155 lockTransactionId, err := lock.GetLock(fileName) 156 assert.NoError(t, err) 157 assert.Equal(t, transactionId, *lockTransactionId) 158 } 159 160 func TestGoogleStorageLock_GetNotExistingLock(t *testing.T) { 161 SkipCI(t) 162 ctx, client := GetGoogleStorageClient() 163 defer func(client *storage.Client) { 164 err := client.Close() 165 if err != nil { 166 log.Fatalf("Failed to close Google Storage client: %v", err) 167 } 168 }(client) 169 170 bucketName := "digger-lock-test" 171 fileName := "digger-lock-" + randomString(16) 172 173 bucket := client.Bucket(bucketName) 174 lock := GoogleStorageLock{client, bucket, ctx} 175 176 lockTransactionId, err := lock.GetLock(fileName) 177 assert.NoError(t, err) 178 assert.Nil(t, lockTransactionId) 179 }