-
Notifications
You must be signed in to change notification settings - Fork 43
/
crashd_config_test.go
124 lines (115 loc) · 3.27 KB
/
crashd_config_test.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
// Copyright (c) 2020 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package starlark
import (
"strings"
"testing"
"go.starlark.net/starlarkstruct"
)
func TestCrashdConfigNew(t *testing.T) {
e := New()
if e.thread == nil {
t.Error("thread is nil")
}
cfg := e.thread.Local(identifiers.crashdCfg)
if cfg == nil {
t.Error("crashd_config dict not found in thread")
}
}
func TestCrashdConfigFunc(t *testing.T) {
tests := []struct {
name string
script string
eval func(t *testing.T, script string)
}{
{
name: "crash_config saved in thread",
script: `crashd_config(foo="fooval", bar="barval")`,
eval: func(t *testing.T, script string) {
exe := New()
if err := exe.Exec("test.star", strings.NewReader(script)); err != nil {
t.Fatal(err)
}
data := exe.thread.Local(identifiers.crashdCfg)
if data == nil {
t.Fatal("crashd_config not saved in thread local")
}
cfg, ok := data.(*starlarkstruct.Struct)
if !ok {
t.Fatalf("unexpected type for thread local key configs.crashd: %T", data)
}
if len(cfg.AttrNames()) != 2 {
t.Fatalf("unexpected item count in configs.crashd: %d", len(cfg.AttrNames()))
}
val, err := cfg.Attr("foo")
if err != nil {
t.Fatalf("key 'foo' not found in crashd_config: %s", err)
}
if trimQuotes(val.String()) != "fooval" {
t.Fatalf("unexpected value for key 'foo': %s", val.String())
}
},
},
{
name: "crash_config returned value",
script: `cfg = crashd_config(foo="fooval", bar="barval")`,
eval: func(t *testing.T, script string) {
exe := New()
if err := exe.Exec("test.star", strings.NewReader(script)); err != nil {
t.Fatal(err)
}
data := exe.result["cfg"]
if data == nil {
t.Fatal("crashd_config function not returning value")
}
cfg, ok := data.(*starlarkstruct.Struct)
if !ok {
t.Fatalf("unexpected type for thread local key configs.crashd: %T", data)
}
if len(cfg.AttrNames()) != 2 {
t.Fatalf("unexpected item count in configs.crashd: %d", len(cfg.AttrNames()))
}
val, err := cfg.Attr("foo")
if err != nil {
t.Fatal(err)
}
if trimQuotes(val.String()) != "fooval" {
t.Fatalf("unexpected value for key %s in configs.crashd", val.String())
}
},
},
{
name: "crash_config default",
script: `one = 1`,
eval: func(t *testing.T, script string) {
exe := New()
if err := exe.Exec("test.star", strings.NewReader(script)); err != nil {
t.Fatal(err)
}
data := exe.thread.Local(identifiers.crashdCfg)
if data == nil {
t.Fatal("default crashd_config not saved in thread local")
}
cfg, ok := data.(*starlarkstruct.Struct)
if !ok {
t.Fatalf("unexpected type for thread local key crashd_config: %T", data)
}
if len(cfg.AttrNames()) != 4 {
t.Fatalf("unexpected item count in configs.crashd: %d", len(cfg.AttrNames()))
}
val, err := cfg.Attr("uid")
if err != nil {
t.Fatalf("key 'foo' not found in configs.crashd: %s", err)
}
if trimQuotes(val.String()) != getUid() {
t.Fatalf("unexpected value for key %s in configs.crashd", val.String())
}
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.eval(t, test.script)
})
}
}