Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: normalize status.observedState.etag in golden tests #2064

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 7 additions & 43 deletions mockgcp/common/fields/etag.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,17 @@ import (
"encoding/base64"
"fmt"

"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
)

var mustFields = []string{
"displayName",
"state",
}

// ComputeEtag computes the etag of the proto object with weak indicator.
func ComputeEtag(obj proto.Message) string {
pb := proto.Clone(obj)

// ignore dynamic fields like timestampe or uniqueId.
descriptor := pb.ProtoReflect().Descriptor()
fieldDescs := descriptor.Fields()
for i := 0; i < fieldDescs.Len(); i++ {
fieldDesc := fieldDescs.Get(i)
must := false
for _, mustField := range mustFields {
if fieldDesc.JSONName() == mustField {
must = true
}
}
if !must {
pb.ProtoReflect().Clear(fieldDesc)
}
}

m, err := prototext.Marshal(pb)
// ComputeWeakEtag computes the etag of the proto object with weak indicator.
func ComputeWeakEtag(obj proto.Message) string {
b, err := proto.Marshal(obj)
if err != nil {
panic(fmt.Sprintf("converting to prototext: %v", err))
panic(fmt.Sprintf("converting to proto: %v", err))
}
h := sha256.Sum256([]byte(m))
str := base64.StdEncoding.EncodeToString(h[:])
strong := fmt.Sprintf(`"%s"`, str) // ETag must be quoted.
return "W/" + strong
}

func ComputeEtagBytes(obj proto.Message) []byte {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the current in-use ComputeEtag. Some expects a []byte form and some needs a pointer. So I'm trying to make this a generic lib for other contributors to use out of the box, so they don't need to handle the dereference pointer badly (like what I did previously).

return []byte(ComputeEtag(obj))
}

func ComputeEtagPtr(obj proto.Message) *string {
return ptrTo(ComputeEtag(obj))
}

func ptrTo[T any](t T) *T {
return &t
h := sha256.Sum256(b)
str := base64.StdEncoding.EncodeToString(h[:])
return fmt.Sprintf(`W/"%s"`, str) // ETag must be quoted.
}
4 changes: 2 additions & 2 deletions mockgcp/mockcloudbuild/workerpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *CloudBuildV1) CreateWorkerPool(ctx context.Context, req *pb.CreateWorke
result.CreateTime = now
result.UpdateTime = now
result.State = pb.WorkerPool_RUNNING
result.Etag = fields.ComputeEtag(result)
result.Etag = fields.ComputeWeakEtag(result)
return result, nil
})
}
Expand Down Expand Up @@ -124,7 +124,7 @@ func (s *CloudBuildV1) UpdateWorkerPool(ctx context.Context, req *pb.UpdateWorke
result := proto.Clone(obj).(*pb.WorkerPool)
result.UpdateTime = now
result.State = pb.WorkerPool_RUNNING
result.Etag = fields.ComputeEtag(result)
result.Etag = fields.ComputeWeakEtag(result)
return result, nil
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ status:
observedGeneration: 2
observedState:
createTime: "1970-01-01T00:00:00Z"
etag: W/"pwzCjvRptOAz64ZilGUmgNZjYVh0LzD3Oh+zEbMPULw="
etag: abcdef123456
networkConfig:
egressOption: NO_PUBLIC_EGRESS
peeredNetwork: projects/${projectId}/global/networks/computenetwork-${uniqueId}
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func normalizeKRMObject(u *unstructured.Unstructured, project testgcp.GCPProject
visitor.replacePaths[".status.updateTime"] = "1970-01-01T00:00:00Z"
visitor.replacePaths[".status.lastModifiedTime"] = "1970-01-01T00:00:00Z"
visitor.replacePaths[".status.etag"] = "abcdef123456"
visitor.replacePaths[".status.observedState.etag"] = "abcdef123456"

// Specific to AlloyDB
visitor.replacePaths[".status.continuousBackupInfo[].enabledTime"] = "1970-01-01T00:00:00Z"
Expand Down
Loading