Skip to content

Commit

Permalink
fix: remove workaround and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frankyn committed Aug 24, 2022
1 parent d36b741 commit e915f30
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 66 deletions.
46 changes: 11 additions & 35 deletions storage/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -1429,19 +1429,6 @@ func toCORSFromProto(rc []*storagepb.Bucket_Cors) []CORS {
return out
}

// Used to handle breaking change in Autogen Storage client OLM Age field
// from int64 to *int64 gracefully in the manual client
// TODO(#6240): Method should be removed once breaking change is made and introduced to this client
func setAgeCondition(age int64, ageField interface{}) {
c := reflect.ValueOf(ageField).Elem()
switch c.Kind() {
case reflect.Int64:
c.SetInt(age)
case reflect.Ptr:
c.Set(reflect.ValueOf(&age))
}
}

func toRawLifecycle(l Lifecycle) *raw.BucketLifecycle {
var rl raw.BucketLifecycle
if len(l.Rules) == 0 {
Expand All @@ -1462,13 +1449,16 @@ func toRawLifecycle(l Lifecycle) *raw.BucketLifecycle {
NumNewerVersions: r.Condition.NumNewerVersions,
},
}

if r.Condition.AgeInDays > 0 {
rr.Condition.Age = googleapi.Int64(r.Condition.AgeInDays)
}

if r.Condition.AllObjects {
rr.Condition.Age = 0
rr.Condition.Age = googleapi.Int64(0)
rr.Condition.ForceSendFields = []string{"Age"}
}

setAgeCondition(r.Condition.AgeInDays, &rr.Condition.Age)

switch r.Condition.Liveness {
case LiveAndArchived:
rr.Condition.IsLive = nil
Expand Down Expand Up @@ -1544,21 +1534,6 @@ func toProtoLifecycle(l Lifecycle) *storagepb.Bucket_Lifecycle {
return &rl
}

// Used to handle breaking change in Autogen Storage client OLM Age field
// from int64 to *int64 gracefully in the manual client
// TODO(#6240): Method should be removed once breaking change is made and introduced to this client
func getAgeCondition(ageField interface{}) int64 {
v := reflect.ValueOf(ageField)
if v.Kind() == reflect.Int64 {
return v.Interface().(int64)
} else if v.Kind() == reflect.Ptr {
if val, ok := v.Interface().(*int64); ok {
return *val
}
}
return 0
}

func toLifecycle(rl *raw.BucketLifecycle) Lifecycle {
var l Lifecycle
if rl == nil {
Expand All @@ -1579,10 +1554,11 @@ func toLifecycle(rl *raw.BucketLifecycle) Lifecycle {
NumNewerVersions: rr.Condition.NumNewerVersions,
},
}
r.Condition.AgeInDays = getAgeCondition(rr.Condition.Age)

if rr.Condition.Age == 0 {
r.Condition.AllObjects = true
if rr.Condition.Age != nil {
r.Condition.AgeInDays = *rr.Condition.Age
if rr.Condition.Age == googleapi.Int64(0) {
r.Condition.AllObjects = true
}
}

if rr.Condition.IsLive == nil {
Expand Down
41 changes: 10 additions & 31 deletions storage/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
raw "google.golang.org/api/storage/v1"
)

// TODO(#6539): re-enable tests after breaking change is released and AgeInDays is a int64*
func TestBucketAttrsToRawBucket(t *testing.T) {
t.Skip("TestBucketAttrsToRawBucket skipped: https://github.com/googleapis/google-cloud-go/issues/6539")
t.Parallel()
Expand Down Expand Up @@ -170,7 +169,7 @@ func TestBucketAttrsToRawBucket(t *testing.T) {
StorageClass: "NEARLINE",
},
Condition: &raw.BucketLifecycleRuleCondition{
//Age: 10,
Age: googleapi.Int64(10),
IsLive: googleapi.Bool(true),
CreatedBefore: "2017-01-02",
MatchesStorageClass: []string{"STANDARD"},
Expand Down Expand Up @@ -206,7 +205,7 @@ func TestBucketAttrsToRawBucket(t *testing.T) {
Type: DeleteAction,
},
Condition: &raw.BucketLifecycleRuleCondition{
//Age: 10,
Age: googleapi.Int64(10),
MatchesPrefix: []string{"testPrefix"},
MatchesSuffix: []string{"testSuffix"},
NumNewerVersions: 3,
Expand All @@ -217,6 +216,7 @@ func TestBucketAttrsToRawBucket(t *testing.T) {
Type: DeleteAction,
},
Condition: &raw.BucketLifecycleRuleCondition{
Age: googleapi.Int64(0),
IsLive: googleapi.Bool(false),
},
},
Expand All @@ -225,15 +225,15 @@ func TestBucketAttrsToRawBucket(t *testing.T) {
Type: AbortIncompleteMPUAction,
},
Condition: &raw.BucketLifecycleRuleCondition{
//Age: 20,
Age: googleapi.Int64(20),
},
},
{
Action: &raw.BucketLifecycleRuleAction{
Type: DeleteAction,
},
Condition: &raw.BucketLifecycleRuleCondition{
Age: 0,
Age: googleapi.Int64(0),
ForceSendFields: []string{"Age"},
},
},
Expand Down Expand Up @@ -366,9 +366,7 @@ func TestBucketAttrsToRawBucket(t *testing.T) {
}
}

// TODO(#6539): re-enable tests after breaking change is released and AgeInDays is a int64*
func TestBucketAttrsToUpdateToRawBucket(t *testing.T) {
t.Skip("TestBucketAttrsToUpdateToRawBucket skipped: https://github.com/googleapis/google-cloud-go/issues/6539")
t.Parallel()
au := &BucketAttrsToUpdate{
VersioningEnabled: false,
Expand Down Expand Up @@ -424,12 +422,12 @@ func TestBucketAttrsToUpdateToRawBucket(t *testing.T) {
Lifecycle: &raw.BucketLifecycle{
Rule: []*raw.BucketLifecycleRule{
{
Action: &raw.BucketLifecycleRuleAction{Type: "Delete"},
//Condition: &raw.BucketLifecycleRuleCondition{Age: 30},
Action: &raw.BucketLifecycleRuleAction{Type: "Delete"},
Condition: &raw.BucketLifecycleRuleCondition{Age: googleapi.Int64(30)},
},
{
Action: &raw.BucketLifecycleRuleAction{Type: AbortIncompleteMPUAction},
//Condition: &raw.BucketLifecycleRuleCondition{Age: 13},
Action: &raw.BucketLifecycleRuleAction{Type: AbortIncompleteMPUAction},
Condition: &raw.BucketLifecycleRuleCondition{Age: googleapi.Int64(13)},
},
},
},
Expand Down Expand Up @@ -580,26 +578,7 @@ func TestBucketAttrsToUpdateToRawBucket(t *testing.T) {
}
}

func TestAgeConditionBackwardCompat(t *testing.T) {
var ti int64
var want int64 = 100
setAgeCondition(want, &ti)
if getAgeCondition(ti) != want {
t.Fatalf("got %v, want %v", getAgeCondition(ti), want)
}

var tp *int64
want = 10
setAgeCondition(want, &tp)
if getAgeCondition(tp) != want {
t.Fatalf("got %v, want %v", getAgeCondition(tp), want)
}

}

// TODO(#6539): re-enable tests after breaking change is released and AgeInDays is a int64*
func TestNewBucket(t *testing.T) {
t.Skip("TestNewBucket skipped: https://github.com/googleapis/google-cloud-go/issues/6539")
labels := map[string]string{"a": "b"}
matchClasses := []string{"STANDARD"}
aTime := time.Date(2017, 1, 2, 0, 0, 0, 0, time.UTC)
Expand All @@ -621,7 +600,7 @@ func TestNewBucket(t *testing.T) {
StorageClass: "NEARLINE",
},
Condition: &raw.BucketLifecycleRuleCondition{
// Age: 10,
Age: googleapi.Int64(10),
IsLive: googleapi.Bool(true),
CreatedBefore: "2017-01-02",
MatchesStorageClass: matchClasses,
Expand Down

0 comments on commit e915f30

Please sign in to comment.