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

     1  package fleet
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path"
     6  	"strings"
     7  	"sync"
     8  	"testing"
     9  
    10  	"github.com/deis/deis/deisctl/config/model"
    11  	"github.com/deis/deis/deisctl/test/mock"
    12  
    13  	"github.com/coreos/fleet/schema"
    14  )
    15  
    16  func TestScaleUp(t *testing.T) {
    17  	t.Parallel()
    18  
    19  	name, err := ioutil.TempDir("", "deisctl-fleetctl")
    20  
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  
    25  	ioutil.WriteFile(path.Join(name, "deis-router.service"), []byte("[Unit]"), 777)
    26  
    27  	testUnits := []*schema.Unit{
    28  		&schema.Unit{
    29  			Name:         "deis-router@1.service",
    30  			DesiredState: "launched",
    31  		},
    32  	}
    33  
    34  	testFleetClient := stubFleetClient{testUnits: testUnits, testUnitStates: []*schema.UnitState{}, unitsMutex: &sync.Mutex{}, unitStatesMutex: &sync.Mutex{}}
    35  
    36  	testConfigBackend := mock.ConfigBackend{Expected: []*model.ConfigNode{{Key: "/deis/platform/enablePlacementOptions", Value: "true"}}}
    37  
    38  	c := &FleetClient{templatePaths: []string{name}, Fleet: &testFleetClient, configBackend: testConfigBackend}
    39  
    40  	var errOutput string
    41  	var wg sync.WaitGroup
    42  
    43  	logMutex := sync.Mutex{}
    44  
    45  	se := newOutErr()
    46  
    47  	c.Scale("router", 3, &wg, se.out, se.ew)
    48  
    49  	wg.Wait()
    50  
    51  	logMutex.Lock()
    52  	if errOutput != "" {
    53  		t.Fatal(errOutput)
    54  	}
    55  	logMutex.Unlock()
    56  
    57  	expectedUnits := []string{"deis-router@1.service", "deis-router@2.service",
    58  		"deis-router@3.service"}
    59  
    60  	for _, expectedUnit := range expectedUnits {
    61  		found := false
    62  
    63  		for _, unit := range testFleetClient.testUnits {
    64  			if unit.Name == expectedUnit {
    65  				found = true
    66  				break
    67  			}
    68  		}
    69  
    70  		if !found {
    71  			t.Errorf("Expected Unit %s not found in Unit States", expectedUnit)
    72  		}
    73  	}
    74  }
    75  
    76  func TestScaleDown(t *testing.T) {
    77  	t.Parallel()
    78  
    79  	testUnits := []*schema.Unit{
    80  		&schema.Unit{
    81  			Name:         "deis-router@1.service",
    82  			DesiredState: "launched",
    83  		},
    84  		&schema.Unit{
    85  			Name:         "deis-router@2.service",
    86  			DesiredState: "launched",
    87  		},
    88  		&schema.Unit{
    89  			Name:         "deis-router@3.service",
    90  			DesiredState: "launched",
    91  		},
    92  	}
    93  
    94  	testFleetClient := stubFleetClient{testUnits: testUnits, testUnitStates: []*schema.UnitState{}, unitsMutex: &sync.Mutex{}, unitStatesMutex: &sync.Mutex{}}
    95  
    96  	c := &FleetClient{Fleet: &testFleetClient}
    97  
    98  	var errOutput string
    99  	var wg sync.WaitGroup
   100  
   101  	logMutex := sync.Mutex{}
   102  
   103  	se := newOutErr()
   104  	c.Scale("router", 1, &wg, se.out, se.ew)
   105  
   106  	wg.Wait()
   107  
   108  	logMutex.Lock()
   109  	if errOutput != "" {
   110  		t.Fatal(errOutput)
   111  	}
   112  	logMutex.Unlock()
   113  
   114  	expectedUnits := []string{"deis-router@1.service"}
   115  
   116  	for _, expectedUnit := range expectedUnits {
   117  		found := false
   118  
   119  		for _, unit := range testFleetClient.testUnits {
   120  			if unit.Name == expectedUnit {
   121  				found = true
   122  				break
   123  			}
   124  		}
   125  
   126  		if !found {
   127  			t.Errorf("Expected Unit %s not found in Unit States", expectedUnit)
   128  		}
   129  	}
   130  }
   131  
   132  func TestScaleError(t *testing.T) {
   133  	t.Parallel()
   134  
   135  	c := &FleetClient{Fleet: &stubFleetClient{}}
   136  
   137  	var errOutput string
   138  	var wg sync.WaitGroup
   139  
   140  	logMutex := sync.Mutex{}
   141  
   142  	se := newOutErr()
   143  	c.Scale("router", -1, &wg, se.out, se.ew)
   144  
   145  	wg.Wait()
   146  
   147  	expected := "cannot scale below 0"
   148  	errOutput = strings.TrimSpace(se.ew.String())
   149  
   150  	logMutex.Lock()
   151  	if errOutput != expected {
   152  		t.Errorf("Expected '%s', Got '%s'", expected, errOutput)
   153  	}
   154  	logMutex.Unlock()
   155  }