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

     1  package fleet
     2  
     3  import (
     4  	"bytes"
     5  	"sync"
     6  	"testing"
     7  	"text/tabwriter"
     8  
     9  	"github.com/coreos/fleet/machine"
    10  	"github.com/coreos/fleet/schema"
    11  )
    12  
    13  func TestListUnits(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	testUnitStates := []*schema.UnitState{
    17  		&schema.UnitState{
    18  			Name:               "deis-controller.service",
    19  			MachineID:          "123456",
    20  			SystemdLoadState:   "loaded",
    21  			SystemdActiveState: "active",
    22  			SystemdSubState:    "running",
    23  			Hash:               "abcd",
    24  		},
    25  		&schema.UnitState{
    26  			Name:               "deis-router@1.service",
    27  			MachineID:          "654321",
    28  			SystemdLoadState:   "loaded",
    29  			SystemdActiveState: "active",
    30  			SystemdSubState:    "running",
    31  			Hash:               "dcba",
    32  		},
    33  	}
    34  
    35  	testMachines := []machine.MachineState{
    36  		machine.MachineState{
    37  			ID:       "123456",
    38  			PublicIP: "1.1.1.1",
    39  			Metadata: nil,
    40  			Version:  "",
    41  		},
    42  		machine.MachineState{
    43  			ID:       "654321",
    44  			PublicIP: "2.2.2.2",
    45  			Metadata: nil,
    46  			Version:  "",
    47  		},
    48  	}
    49  
    50  	testWriter := bytes.Buffer{}
    51  	testTabWriter := new(tabwriter.Writer)
    52  	testTabWriter.Init(&testWriter, 0, 8, 1, '\t', 0)
    53  
    54  	c := &FleetClient{Fleet: &stubFleetClient{testUnitStates: testUnitStates,
    55  		testMachineStates: testMachines, unitStatesMutex: &sync.Mutex{}},
    56  		out: testTabWriter}
    57  
    58  	err := c.ListUnits()
    59  
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	expected := `UNIT			MACHINE			LOAD	ACTIVE	SUB
    65  deis-controller.service	123456.../1.1.1.1	loaded	active	running
    66  deis-router@1.service	654321.../2.2.2.2	loaded	active	running
    67  `
    68  
    69  	actual := testWriter.String()
    70  
    71  	if expected != actual {
    72  		t.Errorf("Expected '%s', Got '%s'", expected, actual)
    73  	}
    74  }