github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/namespace_apply_test.go (about)

     1  package command
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/nomad/ci"
     8  	"github.com/mitchellh/cli"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestNamespaceApplyCommand_Implements(t *testing.T) {
    13  	ci.Parallel(t)
    14  	var _ cli.Command = &NamespaceApplyCommand{}
    15  }
    16  
    17  func TestNamespaceApplyCommand_Fails(t *testing.T) {
    18  	ci.Parallel(t)
    19  	ui := cli.NewMockUi()
    20  	cmd := &NamespaceApplyCommand{Meta: Meta{Ui: ui}}
    21  
    22  	// Fails on misuse
    23  	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
    24  		t.Fatalf("expected exit code 1, got: %d", code)
    25  	}
    26  	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
    27  		t.Fatalf("expected help output, got: %s", out)
    28  	}
    29  	ui.ErrorWriter.Reset()
    30  
    31  	if code := cmd.Run([]string{"-address=nope"}); code != 1 {
    32  		t.Fatalf("expected exit code 1, got: %d", code)
    33  	}
    34  	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
    35  		t.Fatalf("name required error, got: %s", out)
    36  	}
    37  	ui.ErrorWriter.Reset()
    38  }
    39  
    40  func TestNamespaceApplyCommand_Good(t *testing.T) {
    41  	ci.Parallel(t)
    42  
    43  	// Create a server
    44  	srv, client, url := testServer(t, true, nil)
    45  	defer srv.Shutdown()
    46  
    47  	ui := cli.NewMockUi()
    48  	cmd := &NamespaceApplyCommand{Meta: Meta{Ui: ui}}
    49  
    50  	// Create a namespace
    51  	name, desc := "foo", "bar"
    52  	if code := cmd.Run([]string{"-address=" + url, "-description=" + desc, name}); code != 0 {
    53  		t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
    54  	}
    55  
    56  	namespaces, _, err := client.Namespaces().List(nil)
    57  	assert.Nil(t, err)
    58  	assert.Len(t, namespaces, 2)
    59  }