2019-11-15 23:57:40 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package trust_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-10-11 12:39:08 +01:00
|
|
|
"os"
|
2019-11-15 23:57:40 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/testcontext"
|
2019-11-15 23:57:40 +00:00
|
|
|
"storj.io/storj/storagenode/trust"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFileSourceString(t *testing.T) {
|
|
|
|
source := trust.NewFileSource("/some/path")
|
|
|
|
require.Equal(t, "/some/path", source.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileSourceIsStatic(t *testing.T) {
|
|
|
|
source := trust.NewFileSource("/some/path")
|
|
|
|
require.True(t, source.Static(), "file source is unexpectedly not static")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileSourceFetchEntries(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
url1 := makeSatelliteURL("domain1.test")
|
|
|
|
url2 := makeSatelliteURL("domain2.test")
|
|
|
|
|
|
|
|
// Prepare a directory with a couple of lists
|
|
|
|
goodData := fmt.Sprintf(`
|
|
|
|
# Some comment
|
|
|
|
%s
|
|
|
|
%s
|
|
|
|
`, url1.String(), url2.String())
|
|
|
|
goodPath := ctx.File("good.txt")
|
2022-10-11 12:39:08 +01:00
|
|
|
require.NoError(t, os.WriteFile(goodPath, []byte(goodData), 0644))
|
2019-11-15 23:57:40 +00:00
|
|
|
|
|
|
|
badData := `BAD`
|
|
|
|
badPath := ctx.File("bad.txt")
|
2022-10-11 12:39:08 +01:00
|
|
|
require.NoError(t, os.WriteFile(badPath, []byte(badData), 0644))
|
2019-11-15 23:57:40 +00:00
|
|
|
|
|
|
|
missingPath := ctx.File("missing.txt")
|
|
|
|
|
|
|
|
for _, tt := range []struct {
|
|
|
|
name string
|
|
|
|
path string
|
2020-03-24 10:57:14 +00:00
|
|
|
errs []string
|
2019-11-15 23:57:40 +00:00
|
|
|
entries []trust.Entry
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "bad list",
|
|
|
|
path: badPath,
|
2020-03-24 10:57:14 +00:00
|
|
|
errs: []string{"file source: invalid satellite URL: must contain an ID"},
|
2019-11-15 23:57:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing list",
|
|
|
|
path: missingPath,
|
2020-03-24 10:57:14 +00:00
|
|
|
errs: []string{
|
|
|
|
fmt.Sprintf("file source: open %s: no such file or directory", missingPath),
|
|
|
|
fmt.Sprintf("file source: open %s: The system cannot find the file specified.", missingPath),
|
|
|
|
},
|
2019-11-15 23:57:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "good list",
|
|
|
|
path: goodPath,
|
|
|
|
entries: []trust.Entry{
|
|
|
|
{
|
|
|
|
SatelliteURL: url1,
|
|
|
|
Authoritative: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SatelliteURL: url2,
|
|
|
|
Authoritative: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
tt := tt // quiet linting
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
source := trust.NewFileSource(tt.path)
|
|
|
|
entries, err := source.FetchEntries(context.Background())
|
2020-03-24 10:57:14 +00:00
|
|
|
if len(tt.errs) > 0 {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, tt.errs, err.Error())
|
2019-11-15 23:57:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tt.entries, entries)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|