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

     1  package fleet
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"sync"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/coreos/fleet/machine"
    11  	"github.com/coreos/fleet/schema"
    12  )
    13  
    14  type mockJournalCommandRunner struct {
    15  	validUnits []string
    16  }
    17  
    18  func (mockJournalCommandRunner) LocalCommand(string) (int, error) {
    19  	return 0, nil
    20  }
    21  
    22  func (m mockJournalCommandRunner) RemoteCommand(cmd string, addr string, timeout time.Duration) (int, error) {
    23  	if addr != "1.1.1.1" || timeout != 0 {
    24  		return -1, fmt.Errorf("Got %s %d, which is unexpected", cmd, addr, timeout)
    25  	}
    26  
    27  	for _, unit := range m.validUnits {
    28  		if fmt.Sprintf("journalctl --unit %s --no-pager -n 40 -f", unit) == cmd {
    29  			return 0, nil
    30  		}
    31  	}
    32  
    33  	return -1, fmt.Errorf("Didn't find command %s to match with units %v", cmd, m.validUnits)
    34  }
    35  
    36  func TestJournal(t *testing.T) {
    37  	t.Parallel()
    38  
    39  	testMachines := []machine.MachineState{
    40  		machine.MachineState{
    41  			ID:       "test-1",
    42  			PublicIP: "1.1.1.1",
    43  			Metadata: nil,
    44  			Version:  "",
    45  		},
    46  	}
    47  
    48  	testUnits := []*schema.Unit{
    49  		&schema.Unit{
    50  			Name:         "deis-router@1.service",
    51  			CurrentState: "loaded",
    52  			MachineID:    "test-1",
    53  		},
    54  		&schema.Unit{
    55  			Name:         "deis-router@2.service",
    56  			CurrentState: "loaded",
    57  			MachineID:    "test-1",
    58  		},
    59  	}
    60  
    61  	testWriter := bytes.Buffer{}
    62  
    63  	c := &FleetClient{Fleet: &stubFleetClient{testUnits: testUnits, testMachineStates: testMachines,
    64  		unitsMutex: &sync.Mutex{}}, errWriter: &testWriter, runner: mockJournalCommandRunner{
    65  		validUnits: []string{"deis-router@1.service", "deis-router@2.service"}}}
    66  
    67  	err := c.Journal("router")
    68  
    69  	if err != nil {
    70  		t.Error(err)
    71  	}
    72  
    73  	commandErr := testWriter.String()
    74  
    75  	if commandErr != "" {
    76  		t.Error(commandErr)
    77  	}
    78  }