@@ -17,12 +17,18 @@ package functional
1717import (
1818 "fmt"
1919 "io/ioutil"
20+ "os"
2021 "strings"
2122 "testing"
2223
2324 "github.com/coreos/fleet/functional/platform"
2425)
2526
27+ const (
28+ tmpHelloService = "/tmp/hello.service"
29+ fxtHelloService = "fixtures/units/hello.service"
30+ )
31+
2632// TestUnitRunnable is the simplest test possible, deplying a single-node
2733// cluster and ensuring a unit can enter an 'active' state
2834func TestUnitRunnable (t * testing.T ) {
@@ -169,6 +175,30 @@ func TestUnitRestart(t *testing.T) {
169175
170176}
171177
178+ // TestUnitSubmitReplace() tests whether a command "fleetctl submit --replace
179+ // hello.service" works or not.
180+ func TestUnitSubmitReplace (t * testing.T ) {
181+ if err := replaceUnitCommon ("submit" ); err != nil {
182+ t .Fatal (err )
183+ }
184+ }
185+
186+ // TestUnitLoadReplace() tests whether a command "fleetctl load --replace
187+ // hello.service" works or not.
188+ func TestUnitLoadReplace (t * testing.T ) {
189+ if err := replaceUnitCommon ("load" ); err != nil {
190+ t .Fatal (err )
191+ }
192+ }
193+
194+ // TestUnitStartReplace() tests whether a command "fleetctl start --replace
195+ // hello.service" works or not.
196+ func TestUnitStartReplace (t * testing.T ) {
197+ if err := replaceUnitCommon ("start" ); err != nil {
198+ t .Fatal (err )
199+ }
200+ }
201+
172202func TestUnitSSHActions (t * testing.T ) {
173203 cluster , err := platform .NewNspawnCluster ("smoke" )
174204 if err != nil {
@@ -227,6 +257,74 @@ func TestUnitSSHActions(t *testing.T) {
227257 }
228258}
229259
260+ // replaceUnitCommon() tests whether a command "fleetctl {submit,load,start}
261+ // --replace hello.service" works or not.
262+ func replaceUnitCommon (cmd string ) error {
263+ // check if cmd is one of the supported commands.
264+ listCmds := []string {"submit" , "load" , "start" }
265+ found := false
266+ for _ , ccmd := range listCmds {
267+ if ccmd == cmd {
268+ found = true
269+ }
270+ }
271+ if ! found {
272+ return fmt .Errorf ("invalid command %s" , cmd )
273+ }
274+
275+ cluster , err := platform .NewNspawnCluster ("smoke" )
276+ if err != nil {
277+ return fmt .Errorf ("%v" , err )
278+ }
279+ defer cluster .Destroy ()
280+
281+ m , err := cluster .CreateMember ()
282+ if err != nil {
283+ return fmt .Errorf ("%v" , err )
284+ }
285+ _ , err = cluster .WaitForNMachines (m , 1 )
286+ if err != nil {
287+ return fmt .Errorf ("%v" , err )
288+ }
289+
290+ // run a command for a unit and assert it shows up
291+ if _ , _ , err := cluster .Fleetctl (m , cmd , fxtHelloService ); err != nil {
292+ return fmt .Errorf ("Unable to %s fleet unit: %v" , cmd , err )
293+ }
294+ stdout , _ , err := cluster .Fleetctl (m , "list-units" , "--no-legend" )
295+ if err != nil {
296+ return fmt .Errorf ("Failed to run list-units: %v" , err )
297+ }
298+ units := strings .Split (strings .TrimSpace (stdout ), "\n " )
299+ if len (units ) != 1 {
300+ return fmt .Errorf ("Did not find 1 unit in cluster: \n %s" , stdout )
301+ }
302+
303+ // replace the unit and assert it shows up
304+ err = genNewFleetService (tmpHelloService , fxtHelloService , "sleep 2" , "sleep 1" )
305+ if err != nil {
306+ return fmt .Errorf ("Failed to generate a temp fleet service: %v" , err )
307+ }
308+ if _ , _ , err := cluster .Fleetctl (m , cmd , "--replace" , tmpHelloService ); err != nil {
309+ return fmt .Errorf ("Unable to replace fleet unit: %v" , err )
310+ }
311+ stdout , _ , err = cluster .Fleetctl (m , "list-units" , "--no-legend" )
312+ if err != nil {
313+ return fmt .Errorf ("Failed to run list-units: %v" , err )
314+ }
315+ units = strings .Split (strings .TrimSpace (stdout ), "\n " )
316+ if len (units ) != 1 {
317+ return fmt .Errorf ("Did not find 1 unit in cluster: \n %s" , stdout )
318+ }
319+ os .Remove (tmpHelloService )
320+
321+ if err := destroyUnitRetrying (cluster , m , fxtHelloService ); err != nil {
322+ return fmt .Errorf ("Cannot destroy unit %v" , fxtHelloService )
323+ }
324+
325+ return nil
326+ }
327+
230328// genNewFleetService() is a helper for generating a temporary fleet service
231329// that reads from oldFile, replaces oldVal with newVal, and stores the result
232330// to newFile.
0 commit comments