summaryrefslogtreecommitdiff
path: root/logging/telemetryConfig_test.go
blob: 28cfef61369032ac9128c7f6da577f817215d61d (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package logging

import (
	"encoding/json"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	"github.com/algorand/go-algorand/test/partitiontest"
	"github.com/stretchr/testify/require"
)

func Test_loadTelemetryConfig(t *testing.T) {
	partitiontest.PartitionTest(t)

	sample := TelemetryConfig{
		Enable:             true,
		GUID:               "guid",
		URI:                "elastic.algorand.com",
		MinLogLevel:        4,
		ReportHistoryLevel: 4,
		// These credentials are here intentionally. Not a bug.
		UserName: defaultTelemetryUsername,
		Password: defaultTelemetryPassword,
	}

	a := require.New(t)
	ourPath, err := os.Getwd()
	a.NoError(err)
	configsPath := filepath.Join(ourPath, "../test/testdata/configs/logging/logging.config.example")

	config, err := loadTelemetryConfig(configsPath)
	a.NoError(err)

	a.Equal(sample.Enable, config.Enable)
	a.Equal(sample.GUID, config.GUID)
	a.Equal(sample.URI, config.URI)
	a.Equal(sample.MinLogLevel, config.MinLogLevel)
	a.Equal(sample.ReportHistoryLevel, config.ReportHistoryLevel)
	a.Equal(sample.UserName, config.UserName)
	a.Equal(sample.Password, config.Password)

}

func Test_CreateSaveLoadTelemetryConfig(t *testing.T) {
	partitiontest.PartitionTest(t)

	testDir := os.Getenv("TESTDIR")

	if testDir == "" {
		testDir, _ = ioutil.TempDir("", "tmp")
	}

	a := require.New(t)

	configsPath := filepath.Join(testDir, "logging.config")
	config1 := createTelemetryConfig()

	err := config1.Save(configsPath)
	a.NoError(err)

	config2, err := loadTelemetryConfig(configsPath)
	a.NoError(err)

	a.Equal(config1.Enable, config2.Enable)
	a.Equal(config1.URI, config2.URI)
	a.Equal(config1.Name, config2.Name)
	a.Equal(config1.GUID, config2.GUID)
	a.Equal(config1.MinLogLevel, config2.MinLogLevel)
	a.Equal(config1.ReportHistoryLevel, config2.ReportHistoryLevel)
	a.Equal(config1.FilePath, "")
	a.Equal(configsPath, config2.FilePath)
	a.Equal(config1.ChainID, config2.ChainID)
	a.Equal(config1.SessionGUID, config2.SessionGUID)
	a.Equal(config1.UserName, config2.UserName)
	a.Equal(config1.Password, config2.Password)

}

func Test_SanitizeTelemetryString(t *testing.T) {
	partitiontest.PartitionTest(t)
	type testcase struct {
		input    string
		expected string
		parts    int
	}

	tests := []testcase{
		{"2001:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:0db8:85a3:0000:0000:8a2e:0370:7334", 1},
		{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1},
		{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1},
	}

	for _, test := range tests {
		require.Equal(t, test.expected, SanitizeTelemetryString(test.input, test.parts))
	}
}

func TestLoadTelemetryConfig(t *testing.T) {
	partitiontest.PartitionTest(t)
	testLoggingConfigFileName := "../test/testdata/configs/logging/logging.config.test1"
	tc, err := loadTelemetryConfig(testLoggingConfigFileName)
	require.NoError(t, err)
	require.Equal(t, true, tc.Enable)
	// make sure the user name was loaded from the specified file
	require.Equal(t, "test-user-name", tc.UserName)
	// ensure we know how to default correctly if some of the fields in the configuration field aren't specified.
	require.Equal(t, createTelemetryConfig().Password, tc.Password)

}

func TestLoadTelemetryConfigBlankUsernamePassword(t *testing.T) {
	partitiontest.PartitionTest(t)
	testLoggingConfigFileName := "../test/testdata/configs/logging/logging.config.test2"
	tc, err := loadTelemetryConfig(testLoggingConfigFileName)
	require.NoError(t, err)
	// make sure the user name was loaded from the specified file
	require.Equal(t, defaultTelemetryUsername, tc.UserName)
	// ensure we know how to default correctly if some of the fields in the configuration field aren't specified.
	require.Equal(t, defaultTelemetryPassword, tc.Password)
}

func TestSaveTelemetryConfigBlankUsernamePassword(t *testing.T) {
	partitiontest.PartitionTest(t)

	testDir := os.Getenv("TESTDIR")

	if testDir == "" {
		testDir, _ = ioutil.TempDir("", "tmp")
	}

	a := require.New(t)

	configsPath := filepath.Join(testDir, "logging.config")

	config := createTelemetryConfig()

	// Ensure that config has default username and password
	config.UserName = defaultTelemetryUsername
	config.Password = defaultTelemetryPassword

	err := config.Save(configsPath)
	a.NoError(err)

	f, err := os.Open(configsPath)
	a.NoError(err)
	defer f.Close()

	var cfg TelemetryConfig

	var marshaledConfig MarshalingTelemetryConfig
	marshaledConfig.TelemetryConfig = createTelemetryConfig()

	dec := json.NewDecoder(f)
	err = dec.Decode(&marshaledConfig)
	a.NoError(err)

	cfg = marshaledConfig.TelemetryConfig
	a.Equal(cfg.UserName, "")
	a.Equal(cfg.Password, "")

}