-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
102 lines (84 loc) · 2.44 KB
/
main_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
// Copyright 2023 The Backup_remote_files Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package main
import (
"backup_remote_files/config"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
)
func createConfigFile(key, url string) (configurationFilename, outputFilename string, err error) {
configurationFilename = "config." + key + ".yaml"
outputFilename = key + ".out"
// Remove the configuration file if it already exists
if _, err := os.Stat(configurationFilename); !errors.Is(err, os.ErrNotExist) {
os.Remove(configurationFilename)
}
f, err := os.Create(configurationFilename)
if err != nil {
return "", "", err
}
defer f.Close()
fmt.Fprintf(f, ""+
"backups:\n"+
" %s:\n"+
" url: '%s'\n"+
" username: ''\n"+
" password: ''\n"+
" outputFile: '%s'\n"+
"\n"+
"interval: '1m'\n"+
"retryInterval: '10s'\n"+
"metricsPrefix: 'backuprf'\n",
key,
url,
outputFilename,
)
return configurationFilename, outputFilename, nil
}
func TestRetrieveUrlsSimple(t *testing.T) {
wantedMsg := "voh0ahch3E"
// Create a web server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintln(w, wantedMsg)
}))
defer ts.Close()
// Generate the configuration file
configurationFilename, outputFilename, err := createConfigFile(wantedMsg, ts.URL)
assert.Nil(t, err)
defer os.Remove(configurationFilename)
// Remove the output file if it already exists
if _, err := os.Stat(outputFilename); !errors.Is(err, os.ErrNotExist) {
os.Remove(outputFilename)
}
// Change args to be able to run like main()
oldArgs := os.Args
defer func() { os.Args = oldArgs }() // os.Args is a "global variable", so keep the state from before the test, and restore it after.
os.Args = []string{"./backup_remote_riles", "-c", configurationFilename}
// from main.go
cfg, err := config.New("0.0.0")
assert.Nil(t, err)
reg := prometheus.NewRegistry()
m := NewMetrics(reg, cfg.MetricsPrefix)
// Now start the tests
// Test retrieveUrls
r := retrieveUrls(cfg, m, true)
if !assert.FileExists(t, outputFilename) {
return
}
defer os.Remove(outputFilename)
assert.True(t, r)
outputFile, err := os.Open(outputFilename)
if !assert.NoError(t, err) {
return
}
defer outputFile.Close()
byteValue, _ := io.ReadAll(outputFile)
assert.Equal(t, string(byteValue), wantedMsg+"\n")
}