forked from kata-containers/tests
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
173 lines (142 loc) · 4.74 KB
/
config.go
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package tests
import (
"flag"
"io/ioutil"
"log"
"os"
"strings"
"github.com/BurntSushi/toml"
)
// Runtime is the path of a Kata Containers Runtime
var Runtime string
// Timeout specifies the time limit in seconds for each test
var Timeout int
// Hypervisor is the hypervisor currently being used with Kata
var Hypervisor string
// KataConfiguration is the runtime configuration
type KataConfiguration struct {
Hypervisor map[string]hypervisor
Proxy map[string]proxy
Shim map[string]shim
Agent map[string]agent
Runtime runtime
}
type hypervisor struct {
Path string `toml:"path"`
Kernel string `toml:"kernel"`
Initrd string `toml:"initrd"`
Image string `toml:"image"`
Firmware string `toml:"firmware"`
MachineAccelerators string `toml:"machine_accelerators"`
KernelParams string `toml:"kernel_params"`
MachineType string `toml:"machine_type"`
SharedFS string `toml:"shared_fs"`
BlockDeviceDriver string `toml:"block_device_driver"`
DefaultVCPUs int32 `toml:"default_vcpus"`
DefaultMaxVCPUs uint32 `toml:"default_maxvcpus"`
DefaultMemSz uint32 `toml:"default_memory"`
DefaultBridges uint32 `toml:"default_bridges"`
Msize9p uint32 `toml:"msize_9p"`
DisableBlockDeviceUse bool `toml:"disable_block_device_use"`
MemPrealloc bool `toml:"enable_mem_prealloc"`
HugePages bool `toml:"enable_hugepages"`
Swap bool `toml:"enable_swap"`
Debug bool `toml:"enable_debug"`
DisableNestingChecks bool `toml:"disable_nesting_checks"`
EnableIOThreads bool `toml:"enable_iothreads"`
Vsock bool `toml:"use_vsock"`
}
type proxy struct {
Path string `toml:"path"`
Debug bool `toml:"enable_debug"`
}
type runtime struct {
InterNetworkModel string `toml:"internetworking_model"`
Debug bool `toml:"enable_debug"`
}
type shim struct {
Path string `toml:"path"`
Debug bool `toml:"enable_debug"`
}
type agent struct {
}
const (
// DefaultHypervisor default hypervisor
DefaultHypervisor = "qemu"
// FirecrackerHypervisor is firecracker
FirecrackerHypervisor = "firecracker"
// CloudHypervisor is cloud-hypervisor
CloudHypervisor = "clh"
// DefaultProxy default proxy
DefaultProxy = "kata"
// DefaultAgent default agent
DefaultAgent = "kata"
// DefaultShim default shim
DefaultShim = "kata"
// DefaultKataConfigPath is the default path to the kata configuration file
DefaultKataConfigPath = "/usr/share/defaults/kata-containers/configuration.toml"
)
// KataConfig is the runtime configuration
var KataConfig KataConfiguration
var KataHypervisor string
func init() {
flag.StringVar(&Runtime, "runtime", "", "Path of the desired Kata Runtime")
flag.IntVar(&Timeout, "timeout", 5, "Time limit in seconds for each test")
flag.StringVar(&Hypervisor, "hypervisor", "", "The hypervisor currently being used with Kata")
}
// KataInit initializes the kata test suite.
// This function should be called as soon as possible
// preferably from `TestMain`
func KataInit() {
var err error
// Since golang 1.13 packages that call flag.Parse during package initialization
// may cause tests to fail. https://golang.org/doc/go1.13#testing
flag.Parse()
kataConfigPath := DefaultKataConfigPath
args := []string{"--kata-show-default-config-paths"}
cmd := NewCommand(Runtime, args...)
stdout, _, exitCode := cmd.Run()
if exitCode == 0 && stdout != "" {
for _, c := range strings.Split(stdout, "\n") {
if _, err = os.Stat(c); err == nil {
kataConfigPath = c
break
}
}
}
KataConfig, err = loadKataConfiguration(kataConfigPath)
if err != nil {
log.Fatalf("failed to load kata configuration: %v\n", err)
}
switch Hypervisor {
case "cloud-hypervisor":
KataHypervisor = CloudHypervisor
case "firecracker":
KataHypervisor = FirecrackerHypervisor
case "":
log.Printf("'-hypervisor' to ginkgo is not set, using 'DefaultHypervisor': '%v'\n", DefaultHypervisor)
KataHypervisor = DefaultHypervisor
default:
log.Fatalf("Invalid '-hypervisor' passed to ginkgo: '%v'\n", Hypervisor)
}
if _, ok := KataConfig.Hypervisor[KataHypervisor]; !ok {
log.Fatalf("No configuration found from 'KataConfig' for 'KataHypervisor': '%v'\n", KataHypervisor)
}
}
// loadKataConfiguration loads kata configuration
func loadKataConfiguration(configPath string) (KataConfiguration, error) {
var config KataConfiguration
configData, err := ioutil.ReadFile(configPath)
if err != nil {
return config, err
}
_, err = toml.Decode(string(configData), &config)
if err != nil {
return config, err
}
return config, err
}