1// Copyright (C) 2019 Storj Labs, Inc. 2// See LICENSE for copying information. 3 4package trust_test 5 6import ( 7 "context" 8 "fmt" 9 "io/ioutil" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 15 "storj.io/common/testcontext" 16 "storj.io/storj/storagenode/trust" 17) 18 19func TestFileSourceString(t *testing.T) { 20 source := trust.NewFileSource("/some/path") 21 require.Equal(t, "/some/path", source.String()) 22} 23 24func TestFileSourceIsStatic(t *testing.T) { 25 source := trust.NewFileSource("/some/path") 26 require.True(t, source.Static(), "file source is unexpectedly not static") 27} 28 29func TestFileSourceFetchEntries(t *testing.T) { 30 ctx := testcontext.New(t) 31 defer ctx.Cleanup() 32 33 url1 := makeSatelliteURL("domain1.test") 34 url2 := makeSatelliteURL("domain2.test") 35 36 // Prepare a directory with a couple of lists 37 goodData := fmt.Sprintf(` 38 # Some comment 39 %s 40 %s 41 `, url1.String(), url2.String()) 42 goodPath := ctx.File("good.txt") 43 require.NoError(t, ioutil.WriteFile(goodPath, []byte(goodData), 0644)) 44 45 badData := `BAD` 46 badPath := ctx.File("bad.txt") 47 require.NoError(t, ioutil.WriteFile(badPath, []byte(badData), 0644)) 48 49 missingPath := ctx.File("missing.txt") 50 51 for _, tt := range []struct { 52 name string 53 path string 54 errs []string 55 entries []trust.Entry 56 }{ 57 { 58 name: "bad list", 59 path: badPath, 60 errs: []string{"file source: invalid satellite URL: must contain an ID"}, 61 }, 62 { 63 name: "missing list", 64 path: missingPath, 65 errs: []string{ 66 fmt.Sprintf("file source: open %s: no such file or directory", missingPath), 67 fmt.Sprintf("file source: open %s: The system cannot find the file specified.", missingPath), 68 }, 69 }, 70 { 71 name: "good list", 72 path: goodPath, 73 entries: []trust.Entry{ 74 { 75 SatelliteURL: url1, 76 Authoritative: true, 77 }, 78 { 79 SatelliteURL: url2, 80 Authoritative: true, 81 }, 82 }, 83 }, 84 } { 85 tt := tt // quiet linting 86 t.Run(tt.name, func(t *testing.T) { 87 source := trust.NewFileSource(tt.path) 88 entries, err := source.FetchEntries(context.Background()) 89 if len(tt.errs) > 0 { 90 require.Error(t, err) 91 require.Contains(t, tt.errs, err.Error()) 92 return 93 } 94 require.NoError(t, err) 95 assert.Equal(t, tt.entries, entries) 96 }) 97 } 98} 99