gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/runsc/cmd/trace/create_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 trace 16 17 import ( 18 "encoding/json" 19 "os" 20 "reflect" 21 "strings" 22 "testing" 23 24 "gvisor.dev/gvisor/pkg/sentry/seccheck" 25 "gvisor.dev/gvisor/pkg/test/testutil" 26 "gvisor.dev/gvisor/runsc/boot" 27 ) 28 29 func TestConfigFile(t *testing.T) { 30 testCfg := seccheck.SessionConfig{ 31 Name: "Default", 32 Points: []seccheck.PointConfig{ 33 {Name: "point-1"}, 34 }, 35 Sinks: []seccheck.SinkConfig{{Name: "sink-1"}}, 36 } 37 38 for _, tc := range []struct { 39 name string 40 json any 41 want seccheck.SessionConfig 42 err string 43 }{ 44 { 45 name: "SessionConfig", 46 json: testCfg, 47 want: testCfg, 48 }, 49 { 50 name: "InitConfig", 51 json: boot.InitConfig{TraceSession: testCfg}, 52 want: testCfg, 53 }, 54 } { 55 t.Run(tc.name, func(t *testing.T) { 56 tmp, err := os.CreateTemp(testutil.TmpDir(), "trace-create") 57 if err != nil { 58 t.Fatal(err) 59 } 60 defer tmp.Close() 61 encoder := json.NewEncoder(tmp) 62 if err := encoder.Encode(tc.json); err != nil { 63 t.Fatal(err) 64 } 65 66 config, err := decodeTraceConfig(tmp.Name()) 67 if len(tc.err) == 0 { 68 if err != nil { 69 t.Fatal(err) 70 } 71 if !reflect.DeepEqual(&tc.want, config) { 72 t.Errorf("loaded trace session is different, want: %+v, got: %+v", &tc.want, config) 73 } 74 } else if err == nil || !strings.Contains(err.Error(), tc.err) { 75 t.Errorf("unexpected error, want: %q, got: %v", tc.err, err) 76 } 77 }) 78 } 79 } 80 81 func TestMain(m *testing.M) { 82 seccheck.Initialize() 83 os.Exit(m.Run()) 84 }