test: execution: replace deprecated protobuf-related modules
`github.com/golang/protobuf` and its sub-modules has been deprecated
and superseded by `google.golang.org/protobuf`[1]. Drop all use of the
old modules and switch to using only the latter.
[1] https://pkg.go.dev/github.com/golang/protobuf
BUG=b:218608945
TEST=staticcheck -checks SA1019 \
"go.chromium.org/chromiumos/test/execution/..."
Change-Id: I6a757f47224e4382a916873c189c854686b016bb
Signed-off-by: Arnaud Ferraris <[email protected]>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/6088130
Reviewed-by: George Burgess <[email protected]>
Reviewed-by: Allen Xie <[email protected]>
diff --git a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/cli/testexecserver.go b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/cli/testexecserver.go
index 95d518e..8296882 100644
--- a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/cli/testexecserver.go
+++ b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/cli/testexecserver.go
@@ -14,12 +14,13 @@
"os/exec"
"path/filepath"
- "github.com/golang/protobuf/jsonpb"
"go.chromium.org/chromiumos/config/go/test/api"
"go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/driver"
statuserrors "go.chromium.org/chromiumos/test/execution/errors"
"go.chromium.org/chromiumos/test/util/finder"
+
+ "google.golang.org/protobuf/encoding/protojson"
)
// driverToTestsMapping builds a map between test and its driver.
@@ -114,15 +115,14 @@
// readInput reads an execution_service json file and returns a pointer to RunTestsRequest.
func readInput(fileName string) (*api.CrosTestRequest, error) {
- f, err := os.Open(fileName)
+ f, err := os.ReadFile(fileName)
if err != nil {
return nil, statuserrors.NewStatusError(statuserrors.IOAccessError,
fmt.Errorf("failed to read file %v: %v", fileName, err))
}
req := api.CrosTestRequest{}
- umrsh := jsonpb.Unmarshaler{}
- umrsh.AllowUnknownFields = true
+ umrsh := protojson.UnmarshalOptions{DiscardUnknown: true}
if err := umrsh.Unmarshal(f, &req); err != nil {
return nil, statuserrors.NewStatusError(statuserrors.UnmarshalError,
fmt.Errorf("failed to unmarshal file %v: %v", fileName, err))
@@ -132,30 +132,28 @@
// writeMetadata writes a TestCaseMetadataList json.
func writeMetadata(outputPath string, mdList *api.TestCaseMetadataList) error {
- f, err := os.Create(outputPath)
+ json, err := protojson.Marshal(mdList)
if err != nil {
- return statuserrors.NewStatusError(statuserrors.IOCreateError,
- fmt.Errorf("failed to create file %v: %v", outputPath, err))
- }
- m := jsonpb.Marshaler{}
- if err := m.Marshal(f, mdList); err != nil {
return statuserrors.NewStatusError(statuserrors.MarshalError,
- fmt.Errorf("failed to marshall metadata list to file %v: %v", outputPath, err))
+ fmt.Errorf("failed to marshall metadata list: %v", err))
+ }
+ if err := os.WriteFile(outputPath, json, 0666); err != nil {
+ return statuserrors.NewStatusError(statuserrors.IOCreateError,
+ fmt.Errorf("failed to write file %v: %v", outputPath, err))
}
return nil
}
// writeOutput writes a RunTestsResponse json.
func writeOutput(output string, resp *api.CrosTestResponse) error {
- f, err := os.Create(output)
+ json, err := protojson.Marshal(resp)
if err != nil {
- return statuserrors.NewStatusError(statuserrors.IOCreateError,
- fmt.Errorf("fail to create file %v: %v", output, err))
- }
- m := jsonpb.Marshaler{}
- if err := m.Marshal(f, resp); err != nil {
return statuserrors.NewStatusError(statuserrors.MarshalError,
- fmt.Errorf("failed to marshall response to file %v: %v", output, err))
+ fmt.Errorf("failed to marshall response: %v", err))
+ }
+ if err := os.WriteFile(output, json, 0666); err != nil {
+ return statuserrors.NewStatusError(statuserrors.IOCreateError,
+ fmt.Errorf("failed to write file %v: %v", output, err))
}
return nil
}
diff --git a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/cli/testexecserver_test.go b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/cli/testexecserver_test.go
index 4faf007..9fd17f5 100644
--- a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/cli/testexecserver_test.go
+++ b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/cli/testexecserver_test.go
@@ -12,10 +12,10 @@
"path/filepath"
"testing"
- "github.com/golang/protobuf/jsonpb"
_go "go.chromium.org/chromiumos/config/go"
"go.chromium.org/chromiumos/config/go/test/api"
labapi "go.chromium.org/chromiumos/config/go/test/lab/api"
+ "google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)
@@ -97,8 +97,7 @@
Metadata: tastMetadata,
}
- m := jsonpb.Marshaler{}
- encodedData, err := m.MarshalToString(expReq)
+ encodedData, err := protojson.Marshal(expReq)
if err != nil {
t.Fatal("Failed to marshall request")
}
@@ -109,10 +108,10 @@
defer os.RemoveAll(td)
fn := filepath.Join(td, "t.json")
- if err := ioutil.WriteFile(fn, []byte(encodedData), 0644); err != nil {
+ if err := ioutil.WriteFile(fn, encodedData, 0644); err != nil {
t.Fatalf("Failed to write file %v: %v", fn, err)
}
- ioutil.WriteFile("/tmp/t.json", []byte(encodedData), 0644)
+ ioutil.WriteFile("/tmp/t.json", encodedData, 0644)
req, err := readInput(fn)
if err != nil {
t.Fatalf("Failed to read input file %v: %v", fn, err)
@@ -167,12 +166,12 @@
if err := writeOutput(fn, &expectedRspn); err != nil {
t.Fatalf("Failed to write file %v: %v", fn, err)
}
- f, err := os.Open(fn)
+ f, err := os.ReadFile(fn)
if err != nil {
t.Fatalf("Failed to read response from file %v: %v", fn, err)
}
rspn := api.CrosTestResponse{}
- if err := jsonpb.Unmarshal(f, &rspn); err != nil {
+ if err := protojson.Unmarshal(f, &rspn); err != nil {
t.Fatalf("Failed to unmarshall data from file %v: %v", fn, err)
}
if !proto.Equal(&rspn, &expectedRspn) {
diff --git a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/common/helpers.go b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/common/helpers.go
index be3f047..7e2adcd 100644
--- a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/common/helpers.go
+++ b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/common/helpers.go
@@ -16,7 +16,6 @@
"strings"
"time"
- "github.com/golang/protobuf/ptypes"
"gopkg.in/yaml.v3"
_go "go.chromium.org/chromiumos/config/go"
@@ -24,6 +23,9 @@
"go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/device"
testerrors "go.chromium.org/chromiumos/test/execution/errors"
+
+ "google.golang.org/protobuf/types/known/durationpb"
+ "google.golang.org/protobuf/types/known/timestamppb"
)
// TestScanner makes a scanner to read from test streams.
@@ -239,11 +241,8 @@
}
duration := endTime.Sub(startTime)
- protoStartTime, err := ptypes.TimestampProto(startTime)
- if err != nil {
- return nil, fmt.Errorf("fail to convert start time to protobuf: %v", err)
- }
- protoDuration := ptypes.DurationProto(duration)
+ protoStartTime := timestamppb.New(startTime)
+ protoDuration := durationpb.New(duration)
r := &api.TestCaseResult{
TestCaseId: &api.TestCase_Id{Value: testID},
diff --git a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/common/helpers_test.go b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/common/helpers_test.go
index 91c4789..5cb8591 100644
--- a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/common/helpers_test.go
+++ b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/common/helpers_test.go
@@ -8,10 +8,11 @@
"testing"
"time"
- "github.com/golang/protobuf/ptypes"
"github.com/google/go-cmp/cmp"
_go "go.chromium.org/chromiumos/config/go"
"google.golang.org/protobuf/proto"
+ "google.golang.org/protobuf/types/known/durationpb"
+ "google.golang.org/protobuf/types/known/timestamppb"
"go.chromium.org/chromiumos/config/go/test/api"
labapi "go.chromium.org/chromiumos/config/go/test/lab/api"
@@ -173,11 +174,11 @@
startTime1 := time.Unix(0, 1726178525229)
endTime1 := time.Unix(0, 1726178525573)
duration1 := endTime1.Sub(startTime1)
- startTimeProto1, _ := ptypes.TimestampProto(startTime1)
+ startTimeProto1 := timestamppb.New(startTime1)
startTime2 := time.Unix(0, 1726178525574)
endTime2 := time.Unix(0, 1726178525575)
- startTimeProto2, _ := ptypes.TimestampProto(startTime2)
+ startTimeProto2 := timestamppb.New(startTime2)
duration2 := endTime2.Sub(startTime2)
resultsRootDir := "resultsRootDir"
expected := []*api.TestCaseResult{
@@ -194,7 +195,7 @@
},
},
StartTime: startTimeProto1,
- Duration: ptypes.DurationProto(duration1),
+ Duration: durationpb.New(duration1),
},
{
TestCaseId: &api.TestCase_Id{Value: "MultiDeviceTest.test_multidevice"},
@@ -209,7 +210,7 @@
},
},
StartTime: startTimeProto2,
- Duration: ptypes.DurationProto(duration2),
+ Duration: durationpb.New(duration2),
},
}
diff --git a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/common/tko_results_test.go b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/common/tko_results_test.go
index edd93f0..d8b3bc3 100644
--- a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/common/tko_results_test.go
+++ b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/common/tko_results_test.go
@@ -10,7 +10,8 @@
"testing"
"time"
- "github.com/golang/protobuf/ptypes"
+ "google.golang.org/protobuf/types/known/durationpb"
+ "google.golang.org/protobuf/types/known/timestamppb"
_go "go.chromium.org/chromiumos/config/go"
"go.chromium.org/chromiumos/config/go/test/api"
@@ -19,11 +20,8 @@
// TestTestsReports verify results can be parsed and returned in the expected fmt.
func TestTestsReports(t *testing.T) {
- EXPECTSTARTTIME, err := ptypes.TimestampProto(time.Unix(1670923418, 0))
- if err != nil {
- fmt.Printf("!!!! ERR %v", err)
- }
- DURATION105 := ptypes.DurationProto(time.Second * time.Duration(105))
+ EXPECTSTARTTIME := timestamppb.New(time.Unix(1670923418, 0))
+ DURATION105 := durationpb.New(time.Second * time.Duration(105))
testResults := []*api.TestCaseResult{
{
diff --git a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/tastrpc/reports.go b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/tastrpc/reports.go
index 6398a72..595fbe0 100644
--- a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/tastrpc/reports.go
+++ b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/tastrpc/reports.go
@@ -14,10 +14,10 @@
"strings"
"sync"
- "github.com/golang/protobuf/ptypes/empty"
_go "go.chromium.org/chromiumos/config/go"
"go.chromium.org/chromiumos/config/go/test/api"
"google.golang.org/grpc"
+ "google.golang.org/protobuf/types/known/emptypb"
"go.chromium.org/chromiumos/test/execution/errors"
@@ -48,7 +48,7 @@
for {
_, err := stream.Recv()
if err == io.EOF {
- return stream.SendAndClose(&empty.Empty{})
+ return stream.SendAndClose(&emptypb.Empty{})
}
if err != nil {
return err
diff --git a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/tastrpc/reports_test.go b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/tastrpc/reports_test.go
index 3da0dbb..f17ec92 100644
--- a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/tastrpc/reports_test.go
+++ b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/tastrpc/reports_test.go
@@ -10,11 +10,12 @@
"testing"
"time"
- "github.com/golang/protobuf/ptypes"
_go "go.chromium.org/chromiumos/config/go"
"go.chromium.org/chromiumos/config/go/test/api"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
+ "google.golang.org/protobuf/types/known/durationpb"
+ "google.golang.org/protobuf/types/known/timestamppb"
"go.chromium.org/tast/core/framework/protocol"
)
@@ -197,29 +198,20 @@
},
},
}
- testTimePassedTest, err := ptypes.TimestampProto(time.Time{})
- if err != nil {
- t.Error("Failed to create start time for PassedTest", err)
- }
- testTimeFailedTest, err := ptypes.TimestampProto(time.Time{}.Add(1))
- if err != nil {
- t.Error("Failed to create start time for FailedTest", err)
- }
- testTimeSkippedTest, err := ptypes.TimestampProto(time.Time{}.Add(2))
- if err != nil {
- t.Error("Failed to create start time for FailedTest", err)
- }
+ testTimePassedTest := timestamppb.New(time.Time{})
+ testTimeFailedTest := timestamppb.New(time.Time{}.Add(1))
+ testTimeSkippedTest := timestamppb.New(time.Time{}.Add(2))
requests := []*protocol.ReportResultRequest{
{
Test: "PassedTest",
StartTime: testTimePassedTest,
- Duration: ptypes.DurationProto(time.Second),
+ Duration: durationpb.New(time.Second),
},
{
Test: "PassedTestWithNoMetadata",
StartTime: testTimePassedTest,
- Duration: ptypes.DurationProto(time.Second),
+ Duration: durationpb.New(time.Second),
},
{
Test: "FailedTest",
@@ -233,13 +225,13 @@
},
},
StartTime: testTimeFailedTest,
- Duration: ptypes.DurationProto(time.Second),
+ Duration: durationpb.New(time.Second),
},
{
Test: "SkippedTest",
SkipReason: "intentionally skipped",
StartTime: testTimeSkippedTest,
- Duration: ptypes.DurationProto(0),
+ Duration: durationpb.New(0),
},
}
@@ -257,7 +249,7 @@
},
},
StartTime: testTimePassedTest,
- Duration: ptypes.DurationProto(time.Second),
+ Duration: durationpb.New(time.Second),
TestCaseMetadata: testNamesToMetadata[tests[0]],
},
{
@@ -273,7 +265,7 @@
},
},
StartTime: testTimePassedTest,
- Duration: ptypes.DurationProto(time.Second),
+ Duration: durationpb.New(time.Second),
TestCaseMetadata: nil,
},
{
@@ -290,7 +282,7 @@
},
},
StartTime: testTimeFailedTest,
- Duration: ptypes.DurationProto(time.Second),
+ Duration: durationpb.New(time.Second),
TestCaseMetadata: testNamesToMetadata[tests[2]],
},
{
@@ -307,7 +299,7 @@
},
},
StartTime: testTimeSkippedTest,
- Duration: ptypes.DurationProto(0),
+ Duration: durationpb.New(0),
TestCaseMetadata: testNamesToMetadata[tests[3]],
},
}
@@ -465,27 +457,15 @@
},
},
}
- testTimePassedTest, err := ptypes.TimestampProto(time.Time{})
- if err != nil {
- t.Error("Failed to create start time for PassedTest", err)
- }
- testTimeFailedTest, err := ptypes.TimestampProto(time.Time{}.Add(1))
- if err != nil {
- t.Error("Failed to create start time for FailededTest", err)
- }
- testTimeSkippedTest, err := ptypes.TimestampProto(time.Time{}.Add(2))
- if err != nil {
- t.Error("Failed to create start time for FailedTest", err)
- }
- testTimeFailedTestRetry, err := ptypes.TimestampProto(time.Time{}.Add(3))
- if err != nil {
- t.Error("Failed to create start time for FailedTestRetry", err)
- }
+ testTimePassedTest := timestamppb.New(time.Time{})
+ testTimeFailedTest := timestamppb.New(time.Time{}.Add(1))
+ testTimeSkippedTest := timestamppb.New(time.Time{}.Add(2))
+ testTimeFailedTestRetry := timestamppb.New(time.Time{}.Add(3))
requests := []*protocol.ReportResultRequest{
{
Test: "PassedTest",
StartTime: testTimePassedTest,
- Duration: ptypes.DurationProto(time.Second),
+ Duration: durationpb.New(time.Second),
},
{
Test: "FailedTest",
@@ -499,18 +479,18 @@
},
},
StartTime: testTimeFailedTest,
- Duration: ptypes.DurationProto(time.Second),
+ Duration: durationpb.New(time.Second),
},
{
Test: "SkippedTest",
SkipReason: "intentionally skipped",
StartTime: testTimeSkippedTest,
- Duration: ptypes.DurationProto(0),
+ Duration: durationpb.New(0),
},
{
Test: "FailedTest",
StartTime: testTimeFailedTestRetry,
- Duration: ptypes.DurationProto(time.Second),
+ Duration: durationpb.New(time.Second),
},
}
@@ -528,7 +508,7 @@
},
},
StartTime: testTimePassedTest,
- Duration: ptypes.DurationProto(time.Second),
+ Duration: durationpb.New(time.Second),
TestCaseMetadata: testNamesToMetadata[tests[0]],
},
{
@@ -545,7 +525,7 @@
},
},
StartTime: testTimeSkippedTest,
- Duration: ptypes.DurationProto(0),
+ Duration: durationpb.New(0),
TestCaseMetadata: testNamesToMetadata[tests[2]],
},
{
@@ -561,7 +541,7 @@
},
},
StartTime: testTimeFailedTestRetry,
- Duration: ptypes.DurationProto(time.Second),
+ Duration: durationpb.New(time.Second),
TestCaseMetadata: testNamesToMetadata[tests[1]],
},
}
diff --git a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/tautoresults/tautoresults.go b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/tautoresults/tautoresults.go
index 0e93e60..ac2022b 100644
--- a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/tautoresults/tautoresults.go
+++ b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/tautoresults/tautoresults.go
@@ -16,7 +16,8 @@
"strings"
"time"
- "github.com/golang/protobuf/ptypes"
+ "google.golang.org/protobuf/types/known/durationpb"
+ "google.golang.org/protobuf/types/known/timestamppb"
_go "go.chromium.org/chromiumos/config/go"
"go.chromium.org/chromiumos/config/go/test/api"
@@ -118,13 +119,10 @@
}
startTime := getTime(test.StartTime)
if startTime != time.Unix(0, 0) {
- startProtoTime, err := ptypes.TimestampProto(startTime)
- // Only add times if there was no err in conversion.
- if err == nil {
- durationProtoTime := ptypes.DurationProto(GetDuration(test))
- testResult.StartTime = startProtoTime
- testResult.Duration = durationProtoTime
- }
+ startProtoTime := timestamppb.New(startTime)
+ durationProtoTime := durationpb.New(GetDuration(test))
+ testResult.StartTime = startProtoTime
+ testResult.Duration = durationProtoTime
}
// Change result to fail/err as needed.
diff --git a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/tautoresults/tautoresults_test.go b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/tautoresults/tautoresults_test.go
index 7ffe133..757cf95 100644
--- a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/tautoresults/tautoresults_test.go
+++ b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/tautoresults/tautoresults_test.go
@@ -5,7 +5,6 @@
package tautoresults
import (
- "fmt"
"io/ioutil"
"os"
"path/filepath"
@@ -13,8 +12,9 @@
"testing"
"time"
- "github.com/golang/protobuf/ptypes"
"google.golang.org/protobuf/proto"
+ "google.golang.org/protobuf/types/known/durationpb"
+ "google.golang.org/protobuf/types/known/timestamppb"
_go "go.chromium.org/chromiumos/config/go"
"go.chromium.org/chromiumos/config/go/test/api"
@@ -80,12 +80,9 @@
f.WriteString(testJSON)
- EXPECTSTARTTIME, err := ptypes.TimestampProto(time.Unix(1650319391, 0))
- if err != nil {
- fmt.Printf("!!!! ERR %v", err)
- }
- DURATION0 := ptypes.DurationProto(time.Second * time.Duration(0))
- DURATION105 := ptypes.DurationProto(time.Second * time.Duration(105))
+ EXPECTSTARTTIME := timestamppb.New(time.Unix(1650319391, 0))
+ DURATION0 := durationpb.New(time.Second * time.Duration(0))
+ DURATION105 := durationpb.New(time.Second * time.Duration(105))
resultsDir := td
expectedResults := []*api.TestCaseResult{
@@ -429,11 +426,8 @@
f.WriteString(testJSON)
- EXPECTSTARTTIME, err := ptypes.TimestampProto(time.Unix(1670062681, 0))
- if err != nil {
- fmt.Printf("!!!! ERR %v", err)
- }
- DURATION105 := ptypes.DurationProto(time.Second * time.Duration(37))
+ EXPECTSTARTTIME := timestamppb.New(time.Unix(1670062681, 0))
+ DURATION105 := durationpb.New(time.Second * time.Duration(37))
resultsDir := td
expectedResults := []*api.TestCaseResult{
@@ -546,11 +540,8 @@
f.WriteString(testJSON)
- EXPECTSTARTTIME, err := ptypes.TimestampProto(time.Unix(1670062681, 0))
- if err != nil {
- fmt.Printf("!!!! ERR %v", err)
- }
- DURATION105 := ptypes.DurationProto(time.Second * time.Duration(37))
+ EXPECTSTARTTIME := timestamppb.New(time.Unix(1670062681, 0))
+ DURATION105 := durationpb.New(time.Second * time.Duration(37))
resultsDir := td
expectedResults := []*api.TestCaseResult{