-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13d121b
commit 6c87a0e
Showing
3 changed files
with
276 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package aws_efs | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/efs" | ||
configv1 "github.com/openshift/api/config/v1" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestNewAndUpdatedTags(t *testing.T) { | ||
resourceTags := []configv1.AWSResourceTag{ | ||
{Key: "env", Value: "prod"}, | ||
{Key: "app", Value: "myapp"}, | ||
} | ||
|
||
expectedTags := []*efs.Tag{ | ||
{Key: aws.String("env"), Value: aws.String("prod")}, | ||
{Key: aws.String("app"), Value: aws.String("myapp")}, | ||
} | ||
|
||
tags := newAndUpdatedTags(resourceTags) | ||
if !reflect.DeepEqual(tags, expectedTags) { | ||
t.Errorf("Expected %v, but got %v", expectedTags, tags) | ||
} | ||
} | ||
|
||
func TestComputeTagsHash(t *testing.T) { | ||
resourceTags := []configv1.AWSResourceTag{ | ||
{Key: "env", Value: "prod"}, | ||
{Key: "app", Value: "myapp"}, | ||
} | ||
|
||
// Expected hash is deterministic and sorted | ||
concatenated := "app=myapp;env=prod;" | ||
hash := sha256.Sum256([]byte(concatenated)) | ||
expectedHash := hex.EncodeToString(hash[:]) | ||
|
||
computedHash := computeTagsHash(resourceTags) | ||
if computedHash != expectedHash { | ||
t.Errorf("Expected hash %v, but got %v", expectedHash, computedHash) | ||
} | ||
} | ||
|
||
func TestFilterUpdatableVolumes(t *testing.T) { | ||
pvs := []*v1.PersistentVolume{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{ | ||
tagHashAnnotationKey: "oldHash", | ||
}, | ||
}, | ||
Spec: v1.PersistentVolumeSpec{ | ||
PersistentVolumeSource: v1.PersistentVolumeSource{ | ||
CSI: &v1.CSIPersistentVolumeSource{ | ||
Driver: efsDriverName, | ||
VolumeHandle: "fs-abcd::fsap-abcd", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{ | ||
tagHashAnnotationKey: "newHash", | ||
}, | ||
}, | ||
Spec: v1.PersistentVolumeSpec{ | ||
PersistentVolumeSource: v1.PersistentVolumeSource{ | ||
CSI: &v1.CSIPersistentVolumeSource{ | ||
Driver: efsDriverName, | ||
VolumeHandle: "fs-abcd::fsap-abcd", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
newTagsHash := "newHash" | ||
updatable := filterUpdatableVolumes(pvs, newTagsHash) | ||
|
||
if len(updatable) != 1 { | ||
t.Errorf("Expected 1 updatable volume, but got %v", len(updatable)) | ||
} | ||
if updatable[0].Annotations[tagHashAnnotationKey] != "oldHash" { | ||
t.Errorf("Expected annotation 'oldHash', but got %v", updatable[0].Annotations[tagHashAnnotationKey]) | ||
} | ||
} | ||
|
||
func TestParseAccessPointID(t *testing.T) { | ||
volumeHandle := "fs-0123456789abcdef/12345678" | ||
expectedID := "12345678" | ||
accessPointID := parseAccessPointID(volumeHandle) | ||
if accessPointID != expectedID { | ||
t.Errorf("Expected access point ID %v, but got %v", expectedID, accessPointID) | ||
} | ||
} | ||
|
||
func TestSetPVTagHash(t *testing.T) { | ||
pv := &v1.PersistentVolume{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{}, | ||
}, | ||
} | ||
|
||
newHash := "newHash" | ||
updatedPV := setPVTagHash(pv, newHash) | ||
|
||
if updatedPV.Annotations[tagHashAnnotationKey] != newHash { | ||
t.Errorf("Expected annotation %v, but got %v", newHash, updatedPV.Annotations[tagHashAnnotationKey]) | ||
} | ||
} |
Oops, something went wrong.