-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
149 lines (130 loc) · 3.61 KB
/
example_test.go
File metadata and controls
149 lines (130 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//go:build linux
package crun_test
import (
"errors"
"fmt"
"os"
crun "github.com/danielealbano/libcrun-go"
)
// ExampleNewRuntimeContext demonstrates creating a runtime context for container operations.
func ExampleNewRuntimeContext() {
rc, err := crun.NewRuntimeContext(crun.RuntimeConfig{
StateRoot: "/run/crun", // where container state is stored
})
if err != nil {
fmt.Println("Error:", err)
return
}
defer rc.Close()
fmt.Println("RuntimeContext created successfully")
// Output: RuntimeContext created successfully
}
// ExampleNewSpec demonstrates creating a container spec with functional options.
func ExampleNewSpec() {
// Create a rootless container spec with various options
spec, err := crun.NewSpec(true, // rootless mode
crun.WithRootPath("/path/to/rootfs"),
crun.WithArgs("/bin/echo", "hello", "world"),
crun.WithEnv("MY_VAR", "my_value"),
crun.WithEnv("PATH", "/usr/bin:/bin"),
crun.WithMemoryLimit(256*1024*1024), // 256MB
crun.WithCPUShares(1024),
crun.WithPidsLimit(100),
crun.WithHostname("my-container"),
crun.WithCwd("/"),
)
if err != nil {
fmt.Println("Error:", err)
return
}
defer spec.Close()
fmt.Println("ContainerSpec created successfully")
// Output: ContainerSpec created successfully
}
// ExampleRuntimeContext_RunWithIO demonstrates running a container with I/O streams.
func ExampleRuntimeContext_RunWithIO() {
rc, err := crun.NewRuntimeContext(crun.RuntimeConfig{
StateRoot: "/run/crun",
})
if err != nil {
fmt.Println("Error creating context:", err)
return
}
defer rc.Close()
spec, err := crun.NewSpec(true,
crun.WithRootPath("/path/to/rootfs"),
crun.WithArgs("/bin/echo", "hello from container"),
)
if err != nil {
fmt.Println("Error creating spec:", err)
return
}
defer spec.Close()
// Run with stdout/stderr captured
result, err := rc.RunWithIO("example-container", spec, &crun.IOConfig{
Stdout: os.Stdout,
Stderr: os.Stderr,
})
if err != nil {
fmt.Println("Error running container:", err)
return
}
// Wait for container to finish
exitCode, err := result.Wait()
if err != nil {
fmt.Println("Error waiting:", err)
return
}
fmt.Printf("Container exited with code: %d\n", exitCode)
}
// ExampleContainer_State demonstrates getting container state information.
func ExampleContainer_State() {
rc, err := crun.NewRuntimeContext(crun.RuntimeConfig{
StateRoot: "/run/crun",
})
if err != nil {
fmt.Println("Error:", err)
return
}
defer rc.Close()
// Get handle to an existing container
container := rc.Get("my-container-id")
// Get container state
state, err := container.State()
if err != nil {
if errors.Is(err, crun.ErrContainerNotFound) {
fmt.Println("Container not found")
return
}
fmt.Println("Error:", err)
return
}
fmt.Printf("Container %s is %s (PID: %d)\n", state.ID, state.Status, state.Pid)
}
// Example_errorHandling demonstrates using errors.Is() for error classification.
func Example_errorHandling() {
rc, err := crun.NewRuntimeContext(crun.RuntimeConfig{
StateRoot: "/run/crun",
})
if err != nil {
fmt.Println("Error:", err)
return
}
defer rc.Close()
// Try to get state of non-existent container
container := rc.Get("nonexistent-container")
_, err = container.State()
// Use errors.Is() to check error types
switch {
case errors.Is(err, crun.ErrContainerNotFound):
fmt.Println("Container does not exist")
case errors.Is(err, crun.ErrContainerExists):
fmt.Println("Container already exists")
case errors.Is(err, crun.ErrInvalidContainerSpec):
fmt.Println("Invalid container specification")
case err != nil:
fmt.Println("Other error:", err)
default:
fmt.Println("Success")
}
}