55 "fmt"
66 "io"
77 "net/http"
8+ "net/url"
89 "os"
10+ "path"
911 "strings"
1012 "time"
1113
@@ -15,25 +17,32 @@ import (
1517)
1618
1719type Descriptor struct {
18- id string
19- source string
20- config * types.TestConfig
21- vars types.Variables
22- err error
20+ id string
21+ source string
22+ basePath string
23+ config * types.TestConfig
24+ vars types.Variables
25+ err error
2326}
2427
25- func NewDescriptor (testID , testSrc string , config * types.TestConfig , variables types.Variables ) * Descriptor {
28+ func NewDescriptor (testID , testSrc , basePath string , config * types.TestConfig , variables types.Variables ) * Descriptor {
2629 return & Descriptor {
27- id : testID ,
28- source : testSrc ,
29- config : config ,
30- vars : variables ,
30+ id : testID ,
31+ source : testSrc ,
32+ basePath : basePath ,
33+ config : config ,
34+ vars : variables ,
3135 }
3236}
3337
3438func LoadTestDescriptors (ctx context.Context , globalVars types.Variables , localTests []* types.TestConfig , externalTests []* types.ExternalTestConfig ) []types.TestDescriptor {
3539 descriptors := []types.TestDescriptor {}
3640
41+ workingDir , err := os .Getwd ()
42+ if err != nil {
43+ logrus .WithError (err ).Warn ("failed to get working directory" )
44+ }
45+
3746 // load local tests
3847 for testIdx , testCfg := range localTests {
3948 testID := testCfg .ID
@@ -52,11 +61,12 @@ func LoadTestDescriptors(ctx context.Context, globalVars types.Variables, localT
5261 err := testVars .CopyVars (globalVars , testCfg .ConfigVars )
5362
5463 descriptors = append (descriptors , & Descriptor {
55- id : testID ,
56- source : testSrc ,
57- vars : testVars ,
58- config : localTests [testIdx ],
59- err : err ,
64+ id : testID ,
65+ source : testSrc ,
66+ basePath : workingDir ,
67+ vars : testVars ,
68+ config : localTests [testIdx ],
69+ err : err ,
6070 })
6171 }
6272
@@ -65,7 +75,7 @@ func LoadTestDescriptors(ctx context.Context, globalVars types.Variables, localT
6575 testSrc := fmt .Sprintf ("external:%v" , extTestCfg .File )
6676 testID := ""
6777
68- testConfig , testVars , err := LoadExternalTestConfig (ctx , globalVars , extTestCfg )
78+ testConfig , testVars , basePath , err := LoadExternalTestConfig (ctx , globalVars , extTestCfg )
6979
7080 if testConfig != nil && testConfig .ID != "" {
7181 testID = testConfig .ID
@@ -76,31 +86,46 @@ func LoadTestDescriptors(ctx context.Context, globalVars types.Variables, localT
7686 }
7787
7888 descriptors = append (descriptors , & Descriptor {
79- id : testID ,
80- source : testSrc ,
81- config : testConfig ,
82- vars : testVars ,
83- err : err ,
89+ id : testID ,
90+ source : testSrc ,
91+ basePath : basePath ,
92+ config : testConfig ,
93+ vars : testVars ,
94+ err : err ,
8495 })
8596 }
8697
8798 return descriptors
8899}
89100
90- func LoadExternalTestConfig (ctx context.Context , globalVars types.Variables , extTestCfg * types.ExternalTestConfig ) (* types.TestConfig , types.Variables , error ) {
101+ func LoadExternalTestConfig (ctx context.Context , globalVars types.Variables , extTestCfg * types.ExternalTestConfig ) (* types.TestConfig , types.Variables , string , error ) {
91102 var reader io.Reader
92103
104+ var basePath string
105+
93106 if strings .HasPrefix (extTestCfg .File , "http://" ) || strings .HasPrefix (extTestCfg .File , "https://" ) {
107+ parsedURL , err := url .Parse (extTestCfg .File )
108+ if err != nil {
109+ return nil , nil , "" , err
110+ }
111+
112+ // Remove the filename from the path
113+ parsedURL .Path = path .Dir (parsedURL .Path )
114+ parsedURL .RawQuery = ""
115+ parsedURL .Fragment = ""
116+
117+ basePath = parsedURL .String ()
118+
94119 client := & http.Client {Timeout : time .Second * 120 }
95120
96121 req , err := http .NewRequestWithContext (ctx , "GET" , extTestCfg .File , http .NoBody )
97122 if err != nil {
98- return nil , nil , err
123+ return nil , nil , basePath , err
99124 }
100125
101126 resp , err := client .Do (req )
102127 if err != nil {
103- return nil , nil , err
128+ return nil , nil , basePath , err
104129 }
105130
106131 defer func () {
@@ -110,14 +135,16 @@ func LoadExternalTestConfig(ctx context.Context, globalVars types.Variables, ext
110135 }()
111136
112137 if resp .StatusCode != http .StatusOK {
113- return nil , nil , fmt .Errorf ("error loading test config from url: %v, result: %v %v" , extTestCfg .File , resp .StatusCode , resp .Status )
138+ return nil , nil , basePath , fmt .Errorf ("error loading test config from url: %v, result: %v %v" , extTestCfg .File , resp .StatusCode , resp .Status )
114139 }
115140
116141 reader = resp .Body
117142 } else {
143+ basePath = path .Dir (extTestCfg .File )
144+
118145 f , err := os .Open (extTestCfg .File )
119146 if err != nil {
120- return nil , nil , fmt .Errorf ("error loading test config from file %v: %w" , extTestCfg .File , err )
147+ return nil , nil , basePath , fmt .Errorf ("error loading test config from file %v: %w" , extTestCfg .File , err )
121148 }
122149
123150 defer func () {
@@ -135,7 +162,7 @@ func LoadExternalTestConfig(ctx context.Context, globalVars types.Variables, ext
135162
136163 err := decoder .Decode (testConfig )
137164 if err != nil {
138- return nil , nil , fmt .Errorf ("error decoding external test config %v: %v" , extTestCfg .File , err )
165+ return nil , nil , basePath , fmt .Errorf ("error decoding external test config %v: %v" , extTestCfg .File , err )
139166 }
140167
141168 if testConfig .Config == nil {
@@ -177,10 +204,10 @@ func LoadExternalTestConfig(ctx context.Context, globalVars types.Variables, ext
177204
178205 err = testVars .CopyVars (testVars , testConfig .ConfigVars )
179206 if err != nil {
180- return nil , nil , fmt .Errorf ("error decoding external test configVars %v: %v" , extTestCfg .File , err )
207+ return nil , nil , basePath , fmt .Errorf ("error decoding external test configVars %v: %v" , extTestCfg .File , err )
181208 }
182209
183- return testConfig , testVars , nil
210+ return testConfig , testVars , basePath , nil
184211}
185212
186213func (d * Descriptor ) ID () string {
@@ -191,6 +218,10 @@ func (d *Descriptor) Source() string {
191218 return d .source
192219}
193220
221+ func (d * Descriptor ) BasePath () string {
222+ return d .basePath
223+ }
224+
194225func (d * Descriptor ) Config () * types.TestConfig {
195226 return d .config
196227}
0 commit comments