github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/downloadmismatch/download_test.go (about) 1 // Copyright 2023 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package downloadmismatch 16 17 import ( 18 "bytes" 19 "context" 20 "io/ioutil" 21 "os" 22 "path/filepath" 23 "testing" 24 25 "google.golang.org/protobuf/proto" 26 27 lpb "github.com/bazelbuild/reclient/api/log" 28 spb "github.com/bazelbuild/reclient/api/stats" 29 "github.com/bazelbuild/remote-apis-sdks/go/pkg/digest" 30 "github.com/bazelbuild/remote-apis-sdks/go/pkg/fakes" 31 ) 32 33 func TestReading(t *testing.T) { 34 numRecords := int64(2) 35 invocationIds := []string{"random id"} 36 fileName := "test_read.pb" 37 stats := &spb.Stats{ 38 NumRecords: numRecords, 39 InvocationIds: invocationIds, 40 } 41 f, err := os.Create(fileName) 42 if err != nil { 43 t.Errorf("error creating test file") 44 } 45 blob, _ := proto.Marshal(stats) 46 f.Write(blob) 47 f.Close() 48 49 readStats, err := readMismatchesFromFile(fileName) 50 if proto.Equal(readStats, stats) { 51 return 52 } 53 t.Errorf("protobuf mismatch, expected: %v, received: %v", stats, readStats) 54 } 55 56 func TestDownload(t *testing.T) { 57 server, _ := fakes.NewServer(t) 58 grpcClient, _ := server.NewTestClient(context.Background()) 59 actionDg := &digest.Digest{ 60 Hash: "2ac66acc7116b6555eebfc5f578530dd1d1842b5c18729aedc63776ebf010377", 61 Size: 147, 62 } 63 content1 := []byte("blaa") 64 content2 := []byte("aba") 65 contentHash1 := server.CAS.Put(content1) 66 contentHash2 := server.CAS.Put(content2) 67 stats := &spb.Stats{ 68 Verification: &lpb.Verification{ 69 Mismatches: []*lpb.Verification_Mismatch{&lpb.Verification_Mismatch{ 70 ActionDigest: actionDg.String(), 71 RemoteDigests: []string{contentHash1.String()}, 72 LocalDigest: contentHash2.String(), 73 }}, 74 }, 75 } 76 blob, _ := proto.Marshal(stats) 77 f, _ := os.Create("rbe_metrics.pb") 78 f.Write(blob) 79 f.Close() 80 81 if err := DownloadMismatches(".", ".", grpcClient); err != nil { 82 t.Errorf("DownloadMismatches encountered unexpected error: %v", err) 83 } 84 remoteFp := filepath.Join("reclient_mismatches", actionDg.Hash, "remote", contentHash1.Hash) 85 localFp := filepath.Join("reclient_mismatches", actionDg.Hash, "local", contentHash2.Hash) 86 verifyContent(remoteFp, t, content1) 87 verifyContent(localFp, t, content2) 88 } 89 90 func verifyContent(fp string, t *testing.T, content []byte) { 91 blobs, err := ioutil.ReadFile(fp) 92 if err != nil { 93 t.Errorf("Can not read the wanted file %v, error: %v", fp, err) 94 } 95 if bytes.Compare(blobs, content) != 0 { 96 t.Errorf("downloaded content is different from original. Wanted: %v\n Received: %v\n", content, blobs) 97 } 98 }