Skip to content

Commit bce271a

Browse files
milantracygvisor-bot
authored andcommitted
Use pointer receiver to load shim state.
Using value recerver reads information from the path, but it doesn't put the information to the state variables. PiperOrigin-RevId: 900431932
1 parent 671e95b commit bce271a

3 files changed

Lines changed: 57 additions & 4 deletions

File tree

pkg/shim/v1/runsc/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ go_test(
6363
srcs = [
6464
"oom_v2_test.go",
6565
"service_test.go",
66+
"state_test.go",
6667
],
6768
library = ":runsc",
6869
deps = [
6970
"//pkg/shim/v1/utils",
7071
"@com_github_containerd_cgroups//v2:go_default_library",
7172
"@com_github_containerd_containerd//events:go_default_library",
7273
"@com_github_containerd_containerd//runtime:go_default_library",
74+
"@com_github_google_go_cmp//cmp:go_default_library",
7375
"@com_github_opencontainers_runtime_spec//specs-go:go_default_library",
7476
],
7577
)

pkg/shim/v1/runsc/state.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ type State struct {
3232
}
3333

3434
// Load loads the state from the given path.
35-
func (s State) Load(path string) error {
35+
func (s *State) Load(path string) error {
3636
data, err := os.ReadFile(filepath.Join(path, filename))
3737
if err != nil {
3838
return err
3939
}
40-
return json.Unmarshal(data, &s)
40+
return json.Unmarshal(data, s)
4141
}
4242

4343
// Save saves the state to the given path.
44-
func (s State) Save(path string) error {
45-
data, err := json.Marshal(&s)
44+
func (s *State) Save(path string) error {
45+
data, err := json.Marshal(s)
4646
if err != nil {
4747
return err
4848
}

pkg/shim/v1/runsc/state_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2026 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+
// https://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 runsc
16+
17+
import (
18+
"testing"
19+
20+
"github.com/google/go-cmp/cmp"
21+
)
22+
23+
func TestState(t *testing.T) {
24+
tmpdir := t.TempDir()
25+
s := State{
26+
Rootfs: "rootfs_path",
27+
Options: Options{
28+
ShimCgroup: "shim_cgroup",
29+
IoUID: 123,
30+
IoGID: 456,
31+
BinaryName: "runsc",
32+
Root: "runsc_root",
33+
LogLevel: "info",
34+
LogPath: "logpath",
35+
RunscConfig: map[string]string{
36+
"flag1": "value1",
37+
"flag2": "value2",
38+
},
39+
},
40+
}
41+
if err := s.Save(tmpdir); err != nil {
42+
t.Fatalf("Save failed: %v", err)
43+
}
44+
var s2 State
45+
if err := s2.Load(tmpdir); err != nil {
46+
t.Fatalf("Load failed: %v", err)
47+
}
48+
if diff := cmp.Diff(s, s2); diff != "" {
49+
t.Errorf("State is not equal, wanted:\n%v\ngot:\n%v\n", s, s2)
50+
}
51+
}

0 commit comments

Comments
 (0)