aboutsummaryrefslogtreecommitdiff
path: root/config/config_test.go
blob: 1fcc57c3d0904288f8645a1c2cb20e220356ece7 (plain)
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
package config

import (
	"os"
	"reflect"
	"testing"
)

func TestLoadFile(t *testing.T) {
	// Non existant file
	_, err := LoadFile("test_data/non-existant")
	if err == nil {
		t.Fatal("non-existant config file failed without error")
	}

	// Invalid yaml file
	_, err = LoadFile("test_data/invalid_yaml")
	if err == nil {
		t.Fatal("invalid_yaml config file failed without error")
	}

	// TODO test non existant menu in action menu entries, and duplicate, and that anonymous and logged_in exist
	// TODO test non existant game in play actions, and duplicate
	//menuEntry = MenuEntry{
	//Key:    "p",
	//Label:  "play non existant game",
	//Action: "play nonexistant",
	//}
	//if err := menuEntry.validate(); err == nil {
	//t.Fatal("An inexistant game cannot be played")
	//}

	t.Cleanup(func() { os.RemoveAll("var/") })
	// Invalid App example
	if _, err := LoadFile("test_data/invalid_app.yaml"); err == nil {
		t.Fatal("Invalid App entry should fail to load")
	}
	// Not enough menus example
	if _, err := LoadFile("test_data/not_enough_menus.yaml"); err == nil {
		t.Fatal("not enough menu entries should fail to load")
	}
	// Invalid Menus example
	if _, err := LoadFile("test_data/invalid_menus.yaml"); err == nil {
		t.Fatal("Invalid menu entry should fail to load")
	}
	// no anonymous Menu example
	if _, err := LoadFile("test_data/no_anonymous_menu.yaml"); err == nil {
		t.Fatal("Invalid menu entry should fail to load")
	}
	// no logged_in Menu example
	if _, err := LoadFile("test_data/no_logged_in_menu.yaml"); err == nil {
		t.Fatal("Invalid menu entry should fail to load")
	}

	// Complexe example
	config, err := LoadFile("../example/complete.yaml")
	want := Config{
		App: App{
			WorkingDirectory:  "var/",
			MaxUsers:          512,
			AllowRegistration: true,
			MaxNickLen:        15,
			MenuMaxIdleTime:   600,
			PostLoginCommands: []string{
				"mkdir %w/userdata/%u",
				"mkdir %w/userdata/%u/dumplog",
				"mkdir %w/userdata/%u/ttyrec",
			},
		},
		Menus: map[string]Menu{
			"anonymous": Menu{
				Banner:  "Shell Game Launcher - Anonymous access%n======================================",
				XOffset: 5,
				YOffset: 2,
				MenuEntries: []MenuEntry{
					MenuEntry{
						Key:    "l",
						Label:  "login",
						Action: "login",
					},
					MenuEntry{
						Key:    "r",
						Label:  "register",
						Action: "register",
					},
					MenuEntry{
						Key:    "w",
						Label:  "watch",
						Action: "watch_menu",
					},
					MenuEntry{
						Key:    "q",
						Label:  "quit",
						Action: "quit",
					},
				},
			},
			"logged_in": Menu{
				Banner:  "Shell Game Launcher%n===================",
				XOffset: 5,
				YOffset: 2,
				MenuEntries: []MenuEntry{
					MenuEntry{
						Key:    "p",
						Label:  "play Nethack 3.7",
						Action: "play nethack3.7",
					},
					MenuEntry{
						Key:    "o",
						Label:  "edit game options",
						Action: "menu options",
					},
					MenuEntry{
						Key:    "w",
						Label:  "watch",
						Action: "watch",
					},
					MenuEntry{
						Key:    "r",
						Label:  "replay",
						Action: "replay",
					},
					MenuEntry{
						Key:    "c",
						Label:  "change password",
						Action: "passwd",
					},
					MenuEntry{
						Key:    "m",
						Label:  "change email",
						Action: "chmail",
					},
					MenuEntry{
						Key:    "q",
						Label:  "quit",
						Action: "quit",
					},
				},
			},
		},
		Games: map[string]Game{
			"nethack3.7": Game{
				ChrootPath: "/opt/nethack",
				FileMode:   "0666",
				ScoreCommands: []string{
					"exec /games/nethack -s all",
					"wait",
				},
				Commands: []string{
					"cp /games/var/save/%u%n.gz /games/var/save/%u%n.gz.bak",
					"exec /games/nethack -u %n",
				},
				Env: map[string]string{
					"NETHACKOPTIONS": "@%ruserdata/%n/%n.nhrc",
				},
			},
		},
	}
	if err != nil || !reflect.DeepEqual(want, config) {
		t.Fatalf("complete example failed:\nerror %v\nwant:%+v\ngot: %+v", err, want, config)
	}
}