1package command
2
3import (
4	"strings"
5	"testing"
6
7	"github.com/hashicorp/nomad/api"
8	"github.com/mitchellh/cli"
9	"github.com/stretchr/testify/require"
10)
11
12func TestQuotaApplyCommand_Implements(t *testing.T) {
13	t.Parallel()
14	var _ cli.Command = &QuotaApplyCommand{}
15}
16
17func TestQuotaApplyCommand_Fails(t *testing.T) {
18	t.Parallel()
19	ui := new(cli.MockUi)
20	cmd := &QuotaApplyCommand{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
40func TestQuotaApplyNetwork(t *testing.T) {
41	t.Parallel()
42
43	mbits := 20
44
45	cases := []struct {
46		hcl string
47		q   *api.QuotaSpec
48		err string
49	}{{
50		hcl: `limit {region = "global", region_limit {network {mbits = 20}}}`,
51		q: &api.QuotaSpec{
52			Limits: []*api.QuotaLimit{{
53				Region: "global",
54				RegionLimit: &api.Resources{
55					Networks: []*api.NetworkResource{{
56						MBits: &mbits,
57					}},
58				},
59			}},
60		},
61		err: "",
62	}, {
63		hcl: `limit {region = "global", region_limit {network { mbits = 20, device = "eth0"}}}`,
64		q:   nil,
65		err: "network -> invalid key: device",
66	}}
67
68	for _, c := range cases {
69		t.Run(c.hcl, func(t *testing.T) {
70			q, err := parseQuotaSpec([]byte(c.hcl))
71			require.Equal(t, c.q, q)
72			if c.err != "" {
73				require.Contains(t, err.Error(), c.err)
74			}
75		})
76	}
77}
78