@@ -8,8 +8,10 @@ import (
88"net/http/httptest"
99"os"
1010"testing"
11+ "time"
1112
1213"github.com/gaia-pipeline/gaia"
14+ "github.com/gaia-pipeline/gaia/pipeline"
1315"github.com/gaia-pipeline/gaia/store"
1416hclog"github.com/hashicorp/go-hclog"
1517"github.com/labstack/echo"
@@ -93,3 +95,177 @@ func TestPipelineGitLSRemote(t *testing.T) {
9395}
9496})
9597}
98+
99+ func TestPipelineUpdate (t * testing.T ) {
100+ dataDir ,err := ioutil .TempDir ("" ,"temp" )
101+ if err != nil {
102+ t .Fatalf ("error creating data dir %v" ,err .Error ())
103+ }
104+ defer os .RemoveAll (dataDir )
105+
106+ gaia .Cfg = & gaia.Config {
107+ Logger :hclog .NewNullLogger (),
108+ DataPath :dataDir ,
109+ }
110+
111+ // Initialize store
112+ dataStore := store .NewStore ()
113+ err = dataStore .Init ()
114+ if err != nil {
115+ t .Fatalf ("cannot initialize store: %v" ,err .Error ())
116+ }
117+
118+ // Initialize global active pipelines
119+ ap := pipeline .NewActivePipelines ()
120+ pipeline .GlobalActivePipelines = ap
121+
122+ // Initialize echo
123+ e := echo .New ()
124+ InitHandlers (e ,dataStore ,nil )
125+
126+ pipeline1 := gaia.Pipeline {
127+ ID :1 ,
128+ Name :"Pipeline A" ,
129+ Type :gaia .PTypeGolang ,
130+ Created :time .Now (),
131+ }
132+
133+ pipeline2 := gaia.Pipeline {
134+ ID :2 ,
135+ Name :"Pipeline B" ,
136+ Type :gaia .PTypeGolang ,
137+ Created :time .Now (),
138+ }
139+
140+ // Add to store
141+ err = dataStore .PipelinePut (& pipeline1 )
142+ if err != nil {
143+ t .Fatal (err )
144+ }
145+ // Add to active pipelines
146+ ap .Append (pipeline1 )
147+ // Create binary
148+ src := pipeline .GetExecPath (pipeline1 )
149+ f ,_ := os .Create (src )
150+ defer f .Close ()
151+ defer os .Remove (src )
152+
153+ t .Run ("fails for non-existent pipeline" ,func (t * testing.T ) {
154+ bodyBytes ,_ := json .Marshal (pipeline2 )
155+ req := httptest .NewRequest (echo .PUT ,"/" ,bytes .NewBuffer (bodyBytes ))
156+ req .Header .Set ("Content-Type" ,"application/json" )
157+ rec := httptest .NewRecorder ()
158+ c := e .NewContext (req ,rec )
159+ c .SetPath ("/api/" + apiVersion + "/pipeline/:pipelineid" )
160+ c .SetParamNames ("pipelineid" )
161+ c .SetParamValues ("2" )
162+
163+ PipelineUpdate (c )
164+
165+ if rec .Code != http .StatusNotFound {
166+ t .Fatalf ("expected response code %v got %v" ,http .StatusNotFound ,rec .Code )
167+ }
168+ })
169+
170+ t .Run ("works for existing pipeline" ,func (t * testing.T ) {
171+ bodyBytes ,_ := json .Marshal (pipeline1 )
172+ req := httptest .NewRequest (echo .PUT ,"/" ,bytes .NewBuffer (bodyBytes ))
173+ req .Header .Set ("Content-Type" ,"application/json" )
174+ rec := httptest .NewRecorder ()
175+ c := e .NewContext (req ,rec )
176+ c .SetPath ("/api/" + apiVersion + "/pipeline/:pipelineid" )
177+ c .SetParamNames ("pipelineid" )
178+ c .SetParamValues ("1" )
179+
180+ PipelineUpdate (c )
181+
182+ if rec .Code != http .StatusOK {
183+ t .Fatalf ("expected response code %v got %v" ,http .StatusNotFound ,rec .Code )
184+ }
185+ })
186+
187+ }
188+
189+ func TestPipelineDelete (t * testing.T ) {
190+ dataDir ,err := ioutil .TempDir ("" ,"temp" )
191+ if err != nil {
192+ t .Fatalf ("error creating data dir %v" ,err .Error ())
193+ }
194+ defer os .RemoveAll (dataDir )
195+
196+ gaia .Cfg = & gaia.Config {
197+ Logger :hclog .NewNullLogger (),
198+ DataPath :dataDir ,
199+ PipelinePath :dataDir ,
200+ }
201+
202+ // Initialize store
203+ dataStore := store .NewStore ()
204+ err = dataStore .Init ()
205+ if err != nil {
206+ t .Fatalf ("cannot initialize store: %v" ,err .Error ())
207+ }
208+
209+ // Initialize global active pipelines
210+ ap := pipeline .NewActivePipelines ()
211+ pipeline .GlobalActivePipelines = ap
212+
213+ // Initialize echo
214+ e := echo .New ()
215+ InitHandlers (e ,dataStore ,nil )
216+
217+ p := gaia.Pipeline {
218+ ID :1 ,
219+ Name :"Pipeline A" ,
220+ Type :gaia .PTypeGolang ,
221+ Created :time .Now (),
222+ }
223+
224+ // Add to store
225+ err = dataStore .PipelinePut (& p )
226+ if err != nil {
227+ t .Fatal (err )
228+ }
229+ // Add to active pipelines
230+ ap .Append (p )
231+ // Create binary
232+ src := pipeline .GetExecPath (p )
233+ f ,_ := os .Create (src )
234+ defer f .Close ()
235+ defer os .Remove (src )
236+
237+ ioutil .WriteFile (src , []byte ("testcontent" ),0666 )
238+
239+ t .Run ("fails for non-existent pipeline" ,func (t * testing.T ) {
240+ req := httptest .NewRequest (echo .DELETE ,"/" ,nil )
241+ req .Header .Set ("Content-Type" ,"application/json" )
242+ rec := httptest .NewRecorder ()
243+ c := e .NewContext (req ,rec )
244+ c .SetPath ("/api/" + apiVersion + "/pipeline/:pipelineid" )
245+ c .SetParamNames ("pipelineid" )
246+ c .SetParamValues ("2" )
247+
248+ PipelineDelete (c )
249+
250+ if rec .Code != http .StatusNotFound {
251+ t .Fatalf ("expected response code %v got %v" ,http .StatusNotFound ,rec .Code )
252+ }
253+ })
254+
255+ t .Run ("works for existing pipeline" ,func (t * testing.T ) {
256+ req := httptest .NewRequest (echo .DELETE ,"/" ,nil )
257+ req .Header .Set ("Content-Type" ,"application/json" )
258+ rec := httptest .NewRecorder ()
259+ c := e .NewContext (req ,rec )
260+ c .SetPath ("/api/" + apiVersion + "/pipeline/:pipelineid" )
261+ c .SetParamNames ("pipelineid" )
262+ c .SetParamValues ("1" )
263+
264+ PipelineDelete (c )
265+
266+ if rec .Code != http .StatusOK {
267+ t .Fatalf ("expected response code %v got %v" ,http .StatusNotFound ,rec .Code )
268+ }
269+ })
270+
271+ }