bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/csconfig/hub_test.go (about) 1 package csconfig 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "strings" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestLoadHub(t *testing.T) { 13 hubFullPath, err := filepath.Abs("./hub") 14 if err != nil { 15 t.Fatalf(err.Error()) 16 } 17 18 dataFullPath, err := filepath.Abs("./data") 19 if err != nil { 20 t.Fatalf(err.Error()) 21 } 22 23 configDirFullPath, err := filepath.Abs("./tests") 24 if err != nil { 25 t.Fatalf(err.Error()) 26 } 27 28 hubIndexFileFullPath, err := filepath.Abs("./hub/.index.json") 29 if err != nil { 30 t.Fatalf(err.Error()) 31 } 32 33 tests := []struct { 34 name string 35 Input *Config 36 expectedResult *Hub 37 err string 38 }{ 39 { 40 name: "basic valid configuration", 41 Input: &Config{ 42 ConfigPaths: &ConfigurationPaths{ 43 ConfigDir: "./tests", 44 DataDir: "./data", 45 HubDir: "./hub", 46 HubIndexFile: "./hub/.index.json", 47 }, 48 }, 49 expectedResult: &Hub{ 50 ConfigDir: configDirFullPath, 51 DataDir: dataFullPath, 52 HubDir: hubFullPath, 53 HubIndexFile: hubIndexFileFullPath, 54 }, 55 }, 56 { 57 name: "no data dir", 58 Input: &Config{ 59 ConfigPaths: &ConfigurationPaths{ 60 ConfigDir: "./tests", 61 HubDir: "./hub", 62 HubIndexFile: "./hub/.index.json", 63 }, 64 }, 65 expectedResult: nil, 66 }, 67 { 68 name: "no configuration path", 69 Input: &Config{}, 70 expectedResult: nil, 71 }, 72 } 73 74 for idx, test := range tests { 75 err := test.Input.LoadHub() 76 if err == nil && test.err != "" { 77 fmt.Printf("TEST '%s': NOK\n", test.name) 78 t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests)) 79 } else if test.err != "" { 80 if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) { 81 fmt.Printf("TEST '%s': NOK\n", test.name) 82 t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests), 83 test.err, 84 fmt.Sprintf("%s", err)) 85 } 86 } 87 isOk := assert.Equal(t, test.expectedResult, test.Input.Hub) 88 if !isOk { 89 t.Fatalf("TEST '%s': NOK", test.name) 90 } else { 91 fmt.Printf("TEST '%s': OK\n", test.name) 92 } 93 } 94 }