gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/tools/tracereplay/tracereplay_test.go (about) 1 // Copyright 2022 The gVisor Authors. 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 tracereplay 16 17 import ( 18 "bytes" 19 "os" 20 "path/filepath" 21 "testing" 22 23 "gvisor.dev/gvisor/pkg/test/testutil" 24 ) 25 26 // TestBasic uses a pre-generated file that is replayed into a save process. 27 // Then verifies that the generated file looks exactly the same as the original. 28 // In other words, it is doing `replay original | save new`, then checking if 29 // `original == new`. 30 func TestBasic(t *testing.T) { 31 dir, err := os.MkdirTemp(testutil.TmpDir(), "tracereplay") 32 if err != nil { 33 t.Fatal(err) 34 } 35 endpoint := filepath.Join(dir, "tracereplay.sock") 36 37 // Start a new save server to store the replayed file. This tests that save 38 // communicates with clients correctly and generates a valid file. 39 s := NewSave(endpoint, filepath.Join(dir, "out"), "test-") 40 defer s.Close() 41 42 if err := s.Start(); err != nil { 43 t.Fatal(err) 44 } 45 46 // Then replay the re-generated file. This tests that replay can connect to 47 // a server and process the generated file. 48 r := Replay{} 49 r.Endpoint = endpoint 50 51 const testdata = "tools/tracereplay/testdata/client-0001" 52 r.In, err = testutil.FindFile(testdata) 53 if err != nil { 54 t.Fatalf("FindFile(%q): %v", testdata, err) 55 } 56 57 if err := r.Execute(); err != nil { 58 t.Fatal(err) 59 } 60 61 // Wait until all messages are processed and client disconnects. 62 s.WaitForNoClients() 63 64 // The generated file must be an exact copy of the original file. 65 want, err := os.ReadFile(r.In) 66 if err != nil { 67 t.Fatal(err) 68 } 69 got, err := os.ReadFile(filepath.Join(dir, "out", "test-0001")) 70 if err != nil { 71 t.Fatal(err) 72 } 73 if !bytes.Equal(want, got) { 74 t.Errorf("files don't match\nwant: %s\ngot: %s", want, got) 75 } 76 }