File: control_cmd_conf_test.go

package info (click to toggle)
golang-github-cretz-bine 0.2.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, sid, trixie
  • size: 652 kB
  • sloc: makefile: 3
file content (71 lines) | stat: -rw-r--r-- 2,181 bytes parent folder | download
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
package tests

import (
	"io/ioutil"
	"testing"

	"github.com/cretz/bine/control"
)

func TestGetSetAndResetConf(t *testing.T) {
	ctx := NewTestContext(t, nil)
	defer ctx.Close()
	// Simple get conf
	assertConfVals := func(val string) {
		entries, err := ctx.Control.GetConf("LogMessageDomains", "ProtocolWarnings")
		ctx.Require.NoError(err)
		ctx.Require.Len(entries, 2)
		ctx.Require.Contains(entries, control.NewKeyVal("LogMessageDomains", val))
		ctx.Require.Contains(entries, control.NewKeyVal("ProtocolWarnings", val))
	}
	assertConfVals("0")
	// Change em both to 1
	one := "1"
	err := ctx.Control.SetConf(control.KeyVals("LogMessageDomains", "1", "ProtocolWarnings", "1")...)
	ctx.Require.NoError(err)
	// Check again
	assertConfVals(one)
	// Reset em both
	err = ctx.Control.ResetConf(control.KeyVals("LogMessageDomains", "", "ProtocolWarnings", "")...)
	ctx.Require.NoError(err)
	// Make sure both back to zero
	assertConfVals("0")
}

func TestLoadConf(t *testing.T) {
	ctx := NewTestContext(t, nil)
	defer ctx.Close()
	// Get entire conf text
	vals, err := ctx.Control.GetInfo("config-text")
	ctx.Require.NoError(err)
	ctx.Require.Len(vals, 1)
	ctx.Require.Equal("config-text", vals[0].Key)
	confText := vals[0].Val
	// Append new conf val and load
	ctx.Require.NotContains(confText, "LogMessageDomains")
	confText += "\r\nLogMessageDomains 1"
	ctx.Require.NoError(ctx.Control.LoadConf(confText))
	// Check the new val
	vals, err = ctx.Control.GetInfo("config-text")
	ctx.Require.NoError(err)
	ctx.Require.Len(vals, 1)
	ctx.Require.Equal("config-text", vals[0].Key)
	ctx.Require.Contains(vals[0].Val, "LogMessageDomains 1")
}

func TestSaveConf(t *testing.T) {
	ctx := NewTestContext(t, nil)
	defer ctx.Close()
	// Get conf filename
	vals, err := ctx.Control.GetInfo("config-file")
	ctx.Require.NoError(err)
	ctx.Require.Len(vals, 1)
	ctx.Require.Equal("config-file", vals[0].Key)
	confFile := vals[0].Val
	// Save it
	ctx.Require.NoError(ctx.Control.SaveConf(false))
	// Read and make sure, say, the DataDirectory is accurate
	confText, err := ioutil.ReadFile(confFile)
	ctx.Require.NoError(err)
	ctx.Require.Contains(string(confText), "DataDirectory "+ctx.DataDir)
}