github.com/greenboxal/deis@v1.12.1/deisctl/backend/fleet/unit_test.go (about)

     1  package fleet
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path"
     6  	"reflect"
     7  	"strings"
     8  	"sync"
     9  	"testing"
    10  
    11  	"github.com/coreos/fleet/schema"
    12  	"github.com/deis/deis/deisctl/units"
    13  )
    14  
    15  func TestUnits(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	testUnits := []*schema.Unit{
    19  		&schema.Unit{
    20  			Name: "deis-router@1.service",
    21  		},
    22  		&schema.Unit{
    23  			Name: "deis-router@2.service",
    24  		},
    25  		&schema.Unit{
    26  			Name: "deis-router@3.service",
    27  		},
    28  	}
    29  
    30  	c := &FleetClient{Fleet: &stubFleetClient{testUnits: testUnits, unitsMutex: &sync.Mutex{}}}
    31  	expected := []string{"deis-router@1.service", "deis-router@2.service", "deis-router@3.service"}
    32  
    33  	// Test that the correct units are resolved when providing a target string
    34  	// that EXCEPT for lacking the "deis-" prefix matches the beginning of one
    35  	// or more units' name...
    36  	targets, err := c.Units("router")
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	if !reflect.DeepEqual(targets, expected) {
    41  		t.Fatalf("Expected %v, Got %v", expected, targets)
    42  	}
    43  
    44  	// Test that the correct units are resolved when providing a target string
    45  	// INCLUDING the "deis-" prefix that matches the beginning of one or more
    46  	// units' name...
    47  	targets, err = c.Units("deis-router")
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	if !reflect.DeepEqual(targets, expected) {
    52  		t.Fatalf("Expected %v, Got %v", expected, targets)
    53  	}
    54  
    55  	// Test that no units are resolved and an error is returned when providing
    56  	// a target string that does not match the BEGINNING of a service unit name,
    57  	// either with or without the "deis-" prefix. We deliberately test here using
    58  	// a string that IS a substring (albeit in the wrong position) of service
    59  	// units in the stub...
    60  	targets, err = c.Units("outer")
    61  	if err == nil || !strings.HasPrefix(err.Error(), "could not find unit:") {
    62  		t.Fatalf("Expected an error beginning with \"could not find unit:\", but did not get one")
    63  	}
    64  }
    65  
    66  func TestNextUnit(t *testing.T) {
    67  	t.Parallel()
    68  
    69  	testUnits := []*schema.Unit{
    70  		&schema.Unit{
    71  			Name: "deis-router@1.service",
    72  		},
    73  		&schema.Unit{
    74  			Name: "deis-router@3.service",
    75  		},
    76  	}
    77  
    78  	c := &FleetClient{Fleet: &stubFleetClient{testUnits: testUnits, unitsMutex: &sync.Mutex{}}}
    79  
    80  	num, err := c.nextUnit("router")
    81  
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  
    86  	expected := 2
    87  
    88  	if num != expected {
    89  		t.Fatalf("Expected %d, Got %d", expected, num)
    90  	}
    91  }
    92  
    93  func TestLastUnit(t *testing.T) {
    94  	t.Parallel()
    95  
    96  	testUnits := []*schema.Unit{
    97  		&schema.Unit{
    98  			Name: "deis-router@1.service",
    99  		},
   100  		&schema.Unit{
   101  			Name: "deis-router@3.service",
   102  		},
   103  	}
   104  
   105  	c := &FleetClient{Fleet: &stubFleetClient{testUnits: testUnits, unitsMutex: &sync.Mutex{}}}
   106  
   107  	num, err := c.lastUnit("router")
   108  
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  
   113  	expected := 3
   114  
   115  	if num != expected {
   116  		t.Fatalf("Expected %d, Got %d", expected, num)
   117  	}
   118  }
   119  
   120  func TestFormatUnitName(t *testing.T) {
   121  	t.Parallel()
   122  
   123  	unitName, err := formatUnitName("router", 1)
   124  	if err != nil {
   125  		t.Fatal(err)
   126  	}
   127  	if unitName != "deis-router@1.service" {
   128  		t.Fatalf("invalid unit name: %v", unitName)
   129  	}
   130  
   131  }
   132  
   133  func TestNewUnit(t *testing.T) {
   134  	t.Parallel()
   135  
   136  	name, err := ioutil.TempDir("", "deisctl-fleetctl")
   137  	unitFile := `[Unit]
   138  Description=deis-controller`
   139  
   140  	unit := "deis-controller"
   141  
   142  	ioutil.WriteFile(path.Join(name, unit+".service"), []byte(unitFile), 777)
   143  
   144  	uf, err := NewUnit(unit[5:], []string{name}, false)
   145  
   146  	if err != nil {
   147  		t.Fatal(err)
   148  	}
   149  
   150  	result := uf.Contents["Unit"]["Description"][0]
   151  	expected := unitFile[19:]
   152  	if result != expected {
   153  		t.Errorf("Expected: %s, Got %s", expected)
   154  	}
   155  }
   156  
   157  func TestReadTemplate(t *testing.T) {
   158  	t.Parallel()
   159  
   160  	name, err := ioutil.TempDir("", "deisctl-fleetctl")
   161  	expected := []byte("test")
   162  
   163  	if err != nil {
   164  		t.Error(err)
   165  	}
   166  
   167  	for _, unit := range units.Names {
   168  		ioutil.WriteFile(path.Join(name, unit+".service"), expected, 777)
   169  		output, err := readTemplate(unit[5:], []string{name})
   170  
   171  		if err != nil {
   172  			t.Error(err)
   173  		}
   174  
   175  		if string(output) != string(expected) {
   176  			t.Errorf("Unit %s: Expected %s, Got %s", unit, expected, output)
   177  		}
   178  	}
   179  }
   180  
   181  func TestReadTemplateError(t *testing.T) {
   182  	t.Parallel()
   183  
   184  	name, err := ioutil.TempDir("", "deisctl-fleetctl")
   185  
   186  	if err != nil {
   187  		t.Error(err)
   188  	}
   189  
   190  	_, err = readTemplate("foo", []string{name})
   191  	expected := "Could not find unit template for foo"
   192  	errorf := err.Error()
   193  
   194  	if errorf != expected {
   195  		t.Errorf("Expected %s, Got %s", expected, errorf)
   196  	}
   197  }