github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/command/namespace_status_test.go (about)

     1  // +build ent
     2  
     3  package command
     4  
     5  import (
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/nomad/api"
    10  	"github.com/mitchellh/cli"
    11  	"github.com/posener/complete"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestNamespaceStatusCommand_Implements(t *testing.T) {
    16  	t.Parallel()
    17  	var _ cli.Command = &NamespaceStatusCommand{}
    18  }
    19  
    20  func TestNamespaceStatusCommand_Fails(t *testing.T) {
    21  	t.Parallel()
    22  	ui := new(cli.MockUi)
    23  	cmd := &NamespaceStatusCommand{Meta: Meta{Ui: ui}}
    24  
    25  	// Fails on misuse
    26  	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
    27  		t.Fatalf("expected exit code 1, got: %d", code)
    28  	}
    29  	if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) {
    30  		t.Fatalf("expected help output, got: %s", out)
    31  	}
    32  	ui.ErrorWriter.Reset()
    33  
    34  	if code := cmd.Run([]string{"-address=nope", "foo"}); code != 1 {
    35  		t.Fatalf("expected exit code 1, got: %d", code)
    36  	}
    37  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "retrieving namespace") {
    38  		t.Fatalf("connection error, got: %s", out)
    39  	}
    40  	ui.ErrorWriter.Reset()
    41  }
    42  
    43  func TestNamespaceStatusCommand_Good(t *testing.T) {
    44  	t.Parallel()
    45  
    46  	// Create a server
    47  	srv, client, url := testServer(t, true, nil)
    48  	defer srv.Shutdown()
    49  
    50  	ui := new(cli.MockUi)
    51  	cmd := &NamespaceStatusCommand{Meta: Meta{Ui: ui}}
    52  
    53  	// Create a namespace
    54  	ns := &api.Namespace{
    55  		Name: "foo",
    56  	}
    57  	_, err := client.Namespaces().Register(ns, nil)
    58  	assert.Nil(t, err)
    59  
    60  	// Check status on namespace
    61  	if code := cmd.Run([]string{"-address=" + url, ns.Name}); code != 0 {
    62  		t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
    63  	}
    64  
    65  	// Check for basic spec
    66  	out := ui.OutputWriter.String()
    67  	if !strings.Contains(out, "= foo") {
    68  		t.Fatalf("expected quota, got: %s", out)
    69  	}
    70  }
    71  
    72  func TestNamespaceStatusCommand_Good_Quota(t *testing.T) {
    73  	t.Parallel()
    74  
    75  	// Create a server
    76  	srv, client, url := testServer(t, true, nil)
    77  	defer srv.Shutdown()
    78  
    79  	ui := new(cli.MockUi)
    80  	cmd := &NamespaceStatusCommand{Meta: Meta{Ui: ui}}
    81  
    82  	// Create a quota to delete
    83  	qs := testQuotaSpec()
    84  	_, err := client.Quotas().Register(qs, nil)
    85  	assert.Nil(t, err)
    86  
    87  	// Create a namespace
    88  	ns := &api.Namespace{
    89  		Name:  "foo",
    90  		Quota: qs.Name,
    91  	}
    92  	_, err = client.Namespaces().Register(ns, nil)
    93  	assert.Nil(t, err)
    94  
    95  	// Check status on namespace
    96  	if code := cmd.Run([]string{"-address=" + url, ns.Name}); code != 0 {
    97  		t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
    98  	}
    99  
   100  	// Check for basic spec
   101  	out := ui.OutputWriter.String()
   102  	if !strings.Contains(out, "= foo") {
   103  		t.Fatalf("expected quota, got: %s", out)
   104  	}
   105  
   106  	// Check for usage
   107  	if !strings.Contains(out, "0 / 100") {
   108  		t.Fatalf("expected quota, got: %s", out)
   109  	}
   110  }
   111  
   112  func TestNamespaceStatusCommand_AutocompleteArgs(t *testing.T) {
   113  	assert := assert.New(t)
   114  	t.Parallel()
   115  
   116  	srv, client, url := testServer(t, true, nil)
   117  	defer srv.Shutdown()
   118  
   119  	ui := new(cli.MockUi)
   120  	cmd := &NamespaceStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
   121  
   122  	// Create a namespace
   123  	ns := &api.Namespace{
   124  		Name: "foo",
   125  	}
   126  	_, err := client.Namespaces().Register(ns, nil)
   127  	assert.Nil(err)
   128  
   129  	args := complete.Args{Last: "f"}
   130  	predictor := cmd.AutocompleteArgs()
   131  
   132  	res := predictor.Predict(args)
   133  	assert.Equal(1, len(res))
   134  	assert.Equal(ns.Name, res[0])
   135  }
   136  
   137  // This test should demonstrate the behavior of a namespace
   138  // and prefix collision.  In that case, the Namespace status
   139  // command should pull the matching namespace rather than
   140  // displaying the multiple match error
   141  func TestNamespaceStatusCommand_NamespaceMatchesPrefix(t *testing.T) {
   142  	t.Parallel()
   143  
   144  	// Create a server
   145  	srv, client, url := testServer(t, true, nil)
   146  	defer srv.Shutdown()
   147  
   148  	ui := new(cli.MockUi)
   149  	cmd := &NamespaceStatusCommand{Meta: Meta{Ui: ui}}
   150  
   151  	// Create a namespace that uses foo as a prefix
   152  	ns := &api.Namespace{Name: "fooBar"}
   153  	_, err := client.Namespaces().Register(ns, nil)
   154  	assert.Nil(t, err)
   155  
   156  	// Create a foo namespace
   157  	ns2 := &api.Namespace{Name: "foo"}
   158  	_, err = client.Namespaces().Register(ns2, nil)
   159  	assert.Nil(t, err)
   160  
   161  	// Adding a NS after to prevent sort from creating
   162  	// false successes
   163  	ns = &api.Namespace{Name: "fooBaz"}
   164  	_, err = client.Namespaces().Register(ns, nil)
   165  	assert.Nil(t, err)
   166  
   167  	// Check status on namespace
   168  	code := cmd.Run([]string{"-address=" + url, ns2.Name})
   169  	if code != 0 {
   170  		t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
   171  	}
   172  	// Check to ensure we got the proper foo
   173  	out := ui.OutputWriter.String()
   174  	if !strings.Contains(out, "= foo\n") {
   175  		t.Fatalf("expected namespace foo, got: %s", out)
   176  	}
   177  }