github.com/technosophos/deis@v1.7.1-0.20150915173815-f9005256004b/deisctl/backend/fleet/stop_test.go (about)

     1  package fleet
     2  
     3  import (
     4  	"strings"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/coreos/fleet/schema"
     9  )
    10  
    11  func TestStop(t *testing.T) {
    12  	t.Parallel()
    13  
    14  	testUnits := []*schema.Unit{
    15  		&schema.Unit{
    16  			Name:         "deis-controller.service",
    17  			DesiredState: "launched",
    18  		},
    19  		&schema.Unit{
    20  			Name:         "deis-builder.service",
    21  			DesiredState: "launched",
    22  		},
    23  		&schema.Unit{
    24  			Name:         "deis-publisher.service",
    25  			DesiredState: "launch",
    26  		},
    27  	}
    28  
    29  	testFleetClient := stubFleetClient{testUnits: testUnits, unitsMutex: &sync.Mutex{},
    30  		unitStatesMutex: &sync.Mutex{}}
    31  
    32  	c := &FleetClient{Fleet: &testFleetClient}
    33  
    34  	var errOutput string
    35  	var wg sync.WaitGroup
    36  
    37  	logMutex := sync.Mutex{}
    38  
    39  	se := newOutErr()
    40  	c.Stop([]string{"controller", "builder", "publisher"}, &wg, se.out, se.ew)
    41  
    42  	wg.Wait()
    43  
    44  	logMutex.Lock()
    45  	if errOutput != "" {
    46  		t.Fatal(errOutput)
    47  	}
    48  	logMutex.Unlock()
    49  
    50  	expected := []string{"deis-controller.service", "deis-builder.service", "deis-publisher.service"}
    51  
    52  	for _, expectedUnit := range expected {
    53  		found := false
    54  
    55  		for _, unit := range testFleetClient.testUnitStates {
    56  			if unit.Name == expectedUnit {
    57  				found = true
    58  
    59  				if unit.SystemdSubState != "dead" {
    60  					t.Errorf("Unit %s is %s, expected dead", unit.Name, unit.SystemdSubState)
    61  				}
    62  
    63  				break
    64  			}
    65  		}
    66  
    67  		if !found {
    68  			t.Errorf("Expected Unit %s not found in Unit States", expectedUnit)
    69  		}
    70  	}
    71  }
    72  
    73  var stopTestUnits = []*schema.Unit{
    74  	&schema.Unit{
    75  		Name:         "deis-controller.service",
    76  		DesiredState: "launched",
    77  	},
    78  	&schema.Unit{
    79  		Name:         "deis-builder.service",
    80  		DesiredState: "launched",
    81  	},
    82  	&schema.Unit{
    83  		Name:         "deis-publisher.service",
    84  		DesiredState: "launch",
    85  	},
    86  }
    87  
    88  func TestStopFail(t *testing.T) {
    89  	fc := &failingFleetClient{stubFleetClient{
    90  		testUnits:       stopTestUnits,
    91  		unitStatesMutex: &sync.Mutex{},
    92  		unitsMutex:      &sync.Mutex{},
    93  	}}
    94  	var wg sync.WaitGroup
    95  	c := &FleetClient{Fleet: fc}
    96  
    97  	var b syncBuffer
    98  	c.Stop([]string{"deis-builder.service"}, &wg, &b, &b)
    99  	wg.Wait()
   100  
   101  	if !strings.Contains(b.String(), "failed while stopping") {
   102  		t.Errorf("Expected 'failed while stopping'. Got '%s'", b.String())
   103  	}
   104  
   105  }