github.com/hernad/nomad@v1.6.112/command/namespace_apply_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package command
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/hernad/nomad/api"
    11  	"github.com/hernad/nomad/ci"
    12  	"github.com/mitchellh/cli"
    13  	"github.com/shoenig/test/must"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestNamespaceApplyCommand_Implements(t *testing.T) {
    18  	ci.Parallel(t)
    19  	var _ cli.Command = &NamespaceApplyCommand{}
    20  }
    21  
    22  func TestNamespaceApplyCommand_Fails(t *testing.T) {
    23  	ci.Parallel(t)
    24  	ui := cli.NewMockUi()
    25  	cmd := &NamespaceApplyCommand{Meta: Meta{Ui: ui}}
    26  
    27  	// Fails on misuse
    28  	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
    29  		t.Fatalf("expected exit code 1, got: %d", code)
    30  	}
    31  	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
    32  		t.Fatalf("expected help output, got: %s", out)
    33  	}
    34  	ui.ErrorWriter.Reset()
    35  
    36  	if code := cmd.Run([]string{"-address=nope"}); code != 1 {
    37  		t.Fatalf("expected exit code 1, got: %d", code)
    38  	}
    39  	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
    40  		t.Fatalf("name required error, got: %s", out)
    41  	}
    42  	ui.ErrorWriter.Reset()
    43  }
    44  
    45  func TestNamespaceApplyCommand_Good(t *testing.T) {
    46  	ci.Parallel(t)
    47  
    48  	// Create a server
    49  	srv, client, url := testServer(t, true, nil)
    50  	defer srv.Shutdown()
    51  
    52  	ui := cli.NewMockUi()
    53  	cmd := &NamespaceApplyCommand{Meta: Meta{Ui: ui}}
    54  
    55  	// Create a namespace
    56  	name, desc := "foo", "bar"
    57  	if code := cmd.Run([]string{"-address=" + url, "-description=" + desc, name}); code != 0 {
    58  		t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
    59  	}
    60  
    61  	namespaces, _, err := client.Namespaces().List(nil)
    62  	assert.Nil(t, err)
    63  	assert.Len(t, namespaces, 2)
    64  }
    65  
    66  func TestNamespaceApplyCommand_parseNamesapceSpec(t *testing.T) {
    67  	ci.Parallel(t)
    68  
    69  	testCases := []struct {
    70  		name     string
    71  		input    string
    72  		expected *api.Namespace
    73  	}{
    74  		{
    75  			name: "valid namespace",
    76  			input: `
    77  name        = "test-namespace"
    78  description = "Test namespace"
    79  quota       = "test"
    80  
    81  capabilities {
    82    enabled_task_drivers  = ["exec", "docker"]
    83    disabled_task_drivers = ["raw_exec"]
    84  }
    85  
    86  node_pool_config {
    87    default = "dev"
    88    allowed = ["prod*"]
    89  }
    90  
    91  meta {
    92    dept = "eng"
    93  }`,
    94  			expected: &api.Namespace{
    95  				Name:        "test-namespace",
    96  				Description: "Test namespace",
    97  				Quota:       "test",
    98  				Capabilities: &api.NamespaceCapabilities{
    99  					EnabledTaskDrivers:  []string{"exec", "docker"},
   100  					DisabledTaskDrivers: []string{"raw_exec"},
   101  				},
   102  				NodePoolConfiguration: &api.NamespaceNodePoolConfiguration{
   103  					Default: "dev",
   104  					Allowed: []string{"prod*"},
   105  				},
   106  				Meta: map[string]string{
   107  					"dept": "eng",
   108  				},
   109  			},
   110  		},
   111  		{
   112  			name:  "minimal",
   113  			input: `name = "test-small"`,
   114  			expected: &api.Namespace{
   115  				Name: "test-small",
   116  			},
   117  		},
   118  		{
   119  			name:     "empty",
   120  			input:    "",
   121  			expected: &api.Namespace{},
   122  		},
   123  		{
   124  			name: "lists in node pool config are nil if not provided",
   125  			input: `
   126  name = "nil-lists"
   127  
   128  node_pool_config {
   129    default = "default"
   130  }
   131  `,
   132  			expected: &api.Namespace{
   133  				Name: "nil-lists",
   134  				NodePoolConfiguration: &api.NamespaceNodePoolConfiguration{
   135  					Default: "default",
   136  					Allowed: nil,
   137  					Denied:  nil,
   138  				},
   139  			},
   140  		},
   141  	}
   142  
   143  	for _, tc := range testCases {
   144  		t.Run(tc.name, func(t *testing.T) {
   145  			got, err := parseNamespaceSpec([]byte(tc.input))
   146  			must.NoError(t, err)
   147  			must.Eq(t, tc.expected, got)
   148  		})
   149  	}
   150  }