mirror of
https://github.com/kemko/nomad.git
synced 2026-01-01 16:05:42 +03:00
In anticipation of having quotas for dynamic host volumes, we want the user experience of the storage limits to feel integrated with the other resource limits. This is currently prevented by reusing the `Resources` type instead of having a specific type for `QuotaResources`. Update the quota limit/usage types to use a `QuotaResources` that includes a new storage resources quota block. The wire format for the two types are compatible such that we can migrate the existing variables limit in the FSM. Also fixes improper parallelism in the quota init test where we change working directory to avoid file write conflicts but this breaks when multiple tests are executed in the same process. Ref: https://github.com/hashicorp/nomad-enterprise/pull/2096
111 lines
2.7 KiB
Go
111 lines
2.7 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package command
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/cli"
|
|
"github.com/hashicorp/nomad/ci"
|
|
"github.com/shoenig/test/must"
|
|
)
|
|
|
|
func TestQuotaInitCommand_Implements(t *testing.T) {
|
|
ci.Parallel(t)
|
|
var _ cli.Command = &QuotaInitCommand{}
|
|
}
|
|
|
|
func TestQuotaInitCommand_Run_HCL(t *testing.T) {
|
|
ui := cli.NewMockUi()
|
|
cmd := &QuotaInitCommand{Meta: Meta{Ui: ui}}
|
|
|
|
// Fails on misuse
|
|
code := cmd.Run([]string{"some", "bad", "args"})
|
|
must.One(t, code)
|
|
must.StrContains(t, ui.ErrorWriter.String(), commandErrorText(cmd))
|
|
ui.ErrorWriter.Reset()
|
|
|
|
// Ensure we change the cwd back
|
|
origDir, err := os.Getwd()
|
|
must.NoError(t, err)
|
|
t.Cleanup(func() { os.Chdir(origDir) })
|
|
|
|
// Create a temp dir and change into it
|
|
dir := t.TempDir()
|
|
|
|
err = os.Chdir(dir)
|
|
must.NoError(t, err)
|
|
|
|
// Works if the file doesn't exist
|
|
code = cmd.Run([]string{})
|
|
must.Eq(t, "", ui.ErrorWriter.String())
|
|
must.Zero(t, code)
|
|
|
|
content, err := os.ReadFile(DefaultHclQuotaInitName)
|
|
must.NoError(t, err)
|
|
must.Eq(t, defaultHclQuotaSpec, string(content))
|
|
|
|
// Fails if the file exists
|
|
code = cmd.Run([]string{})
|
|
must.StrContains(t, ui.ErrorWriter.String(), "exists")
|
|
must.One(t, code)
|
|
ui.ErrorWriter.Reset()
|
|
|
|
// Works if file is passed
|
|
code = cmd.Run([]string{"mytest.hcl"})
|
|
must.Eq(t, "", ui.ErrorWriter.String())
|
|
must.Zero(t, code)
|
|
|
|
content, err = os.ReadFile("mytest.hcl")
|
|
must.NoError(t, err)
|
|
must.Eq(t, defaultHclQuotaSpec, string(content))
|
|
}
|
|
|
|
func TestQuotaInitCommand_Run_JSON(t *testing.T) {
|
|
ui := cli.NewMockUi()
|
|
cmd := &QuotaInitCommand{Meta: Meta{Ui: ui}}
|
|
|
|
// Fails on misuse
|
|
code := cmd.Run([]string{"some", "bad", "args"})
|
|
must.One(t, code)
|
|
must.StrContains(t, ui.ErrorWriter.String(), commandErrorText(cmd))
|
|
ui.ErrorWriter.Reset()
|
|
|
|
// Ensure we change the cwd back
|
|
origDir, err := os.Getwd()
|
|
must.NoError(t, err)
|
|
t.Cleanup(func() { os.Chdir(origDir) })
|
|
|
|
// Create a temp dir and change into it
|
|
dir := t.TempDir()
|
|
|
|
err = os.Chdir(dir)
|
|
must.NoError(t, err)
|
|
|
|
// Works if the file doesn't exist
|
|
code = cmd.Run([]string{"-json"})
|
|
must.Eq(t, "", ui.ErrorWriter.String())
|
|
must.Zero(t, code)
|
|
|
|
content, err := os.ReadFile(DefaultJsonQuotaInitName)
|
|
must.NoError(t, err)
|
|
must.Eq(t, defaultJsonQuotaSpec, string(content))
|
|
|
|
// Fails if the file exists
|
|
code = cmd.Run([]string{"-json"})
|
|
must.StrContains(t, ui.ErrorWriter.String(), "exists")
|
|
must.One(t, code)
|
|
ui.ErrorWriter.Reset()
|
|
|
|
// Works if file is passed
|
|
code = cmd.Run([]string{"-json", "mytest.json"})
|
|
must.Eq(t, "", ui.ErrorWriter.String())
|
|
must.Zero(t, code)
|
|
|
|
content, err = os.ReadFile("mytest.json")
|
|
must.NoError(t, err)
|
|
must.Eq(t, defaultJsonQuotaSpec, string(content))
|
|
}
|