chore: Convert assets from bindatafs to go embeds (#16066)
* Convert assets from bindatafs to go embeds * Add command/asset to "uninteresting" list for missing test check * Remove generate-examples target * Update paths in tests
This commit is contained in:
parent
65c7e149d3
commit
0b9ba1ae92
|
@ -201,7 +201,7 @@ checkproto: ## Lint protobuf files
|
|||
@buf check breaking --config tools/buf/buf.yaml --against-config tools/buf/buf.yaml --against .git#tag=$(PROTO_COMPARE_TAG)
|
||||
|
||||
.PHONY: generate-all
|
||||
generate-all: generate-structs proto generate-examples ## Generate structs, protobufs, examples
|
||||
generate-all: generate-structs proto ## Generate structs, protobufs
|
||||
|
||||
.PHONY: generate-structs
|
||||
generate-structs: LOCAL_PACKAGES = $(shell go list ./...)
|
||||
|
@ -214,12 +214,6 @@ proto: ## Generate protobuf bindings
|
|||
@echo "==> Generating proto bindings..."
|
||||
@buf --config tools/buf/buf.yaml --template tools/buf/buf.gen.yaml generate
|
||||
|
||||
.PHONY: generate-examples
|
||||
generate-examples: command/job_init.bindata_assetfs.go
|
||||
|
||||
command/job_init.bindata_assetfs.go: command/assets/*
|
||||
go-bindata-assetfs -pkg command -o command/job_init.bindata_assetfs.go ./command/assets/...
|
||||
|
||||
changelog: ## Generate changelog from entries
|
||||
@changelog-build -last-release $(LAST_RELEASE) -this-release HEAD \
|
||||
-entries-dir .changelog/ -changelog-template ./.changelog/changelog.tmpl -note-template ./.changelog/note.tmpl
|
||||
|
@ -414,4 +408,3 @@ ec2info: ## Generate AWS EC2 CPU specification table
|
|||
.PHONY: cl
|
||||
cl: ## Create a new Changelog entry
|
||||
@go run -modfile tools/go.mod tools/cl-entry/main.go
|
||||
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
package asset
|
||||
|
||||
import _ "embed"
|
||||
|
||||
//go:embed example.nomad.hcl
|
||||
var JobExample []byte
|
||||
|
||||
//go:embed example-short.nomad.hcl
|
||||
var JobExampleShort []byte
|
||||
|
||||
//go:embed connect.nomad.hcl
|
||||
var JobConnect []byte
|
||||
|
||||
//go:embed connect-short.nomad.hcl
|
||||
var JobConnectShort []byte
|
File diff suppressed because one or more lines are too long
|
@ -7,6 +7,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/hashicorp/nomad/api"
|
||||
"github.com/hashicorp/nomad/command/asset"
|
||||
"github.com/posener/complete"
|
||||
)
|
||||
|
||||
|
@ -170,19 +171,13 @@ func (c *JobInitCommand) Run(args []string) int {
|
|||
} else {
|
||||
switch {
|
||||
case connect && !short:
|
||||
jobSpec, err = Asset("command/assets/connect.nomad.hcl")
|
||||
jobSpec = asset.JobConnect
|
||||
case connect && short:
|
||||
jobSpec, err = Asset("command/assets/connect-short.nomad.hcl")
|
||||
jobSpec = asset.JobConnectShort
|
||||
case !connect && short:
|
||||
jobSpec, err = Asset("command/assets/example-short.nomad.hcl")
|
||||
jobSpec = asset.JobExampleShort
|
||||
default:
|
||||
jobSpec, err = Asset("command/assets/example.nomad.hcl")
|
||||
}
|
||||
if err != nil {
|
||||
// should never see this because we've precompiled the assets
|
||||
// as part of `make generate-examples`
|
||||
c.Ui.Error(fmt.Sprintf("Accessed non-existent asset: %s", err))
|
||||
return 1
|
||||
jobSpec = asset.JobExample
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/hashicorp/nomad/ci"
|
||||
"github.com/hashicorp/nomad/command/asset"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/shoenig/test/must"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
@ -53,7 +54,7 @@ func TestInitCommand_Run(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
defaultJob, _ := Asset("command/assets/example.nomad.hcl")
|
||||
defaultJob := asset.JobExample
|
||||
if string(content) != string(defaultJob) {
|
||||
t.Fatalf("unexpected file content\n\n%s", string(content))
|
||||
}
|
||||
|
@ -65,7 +66,7 @@ func TestInitCommand_Run(t *testing.T) {
|
|||
}
|
||||
content, err = ioutil.ReadFile(DefaultInitName)
|
||||
require.NoError(t, err)
|
||||
shortJob, _ := Asset("command/assets/example-short.nomad.hcl")
|
||||
shortJob := asset.JobExampleShort
|
||||
require.Equal(t, string(content), string(shortJob))
|
||||
|
||||
// Fails if the file exists
|
||||
|
@ -82,7 +83,7 @@ func TestInitCommand_defaultJob(t *testing.T) {
|
|||
// Ensure the job file is always written with spaces instead of tabs. Since
|
||||
// the default job file is embedded in the go file, it's easy for tabs to
|
||||
// slip in.
|
||||
defaultJob, _ := Asset("command/assets/example.nomad.hcl")
|
||||
defaultJob := asset.JobExample
|
||||
if strings.Contains(string(defaultJob), "\t") {
|
||||
t.Error("default job contains tab character - please convert to spaces")
|
||||
}
|
||||
|
@ -151,7 +152,7 @@ func TestInitCommand_fromJobTemplate(t *testing.T) {
|
|||
|
||||
jobCmd := &JobInitCommand{Meta: Meta{Ui: ui}}
|
||||
|
||||
// Doesnt work if our var lacks a template key
|
||||
// Doesn't work if our var lacks a template key
|
||||
must.Eq(t, 1, jobCmd.Run([]string{"-address=" + url, "-template=invalid-template"}))
|
||||
|
||||
// Works if the file doesn't exist
|
||||
|
@ -193,7 +194,7 @@ func TestInitCommand_customFilename(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
defaultJob, _ := Asset("command/assets/example.nomad.hcl")
|
||||
defaultJob := asset.JobExample
|
||||
if string(content) != string(defaultJob) {
|
||||
t.Fatalf("unexpected file content\n\n%s", string(content))
|
||||
}
|
||||
|
@ -205,7 +206,7 @@ func TestInitCommand_customFilename(t *testing.T) {
|
|||
}
|
||||
content, err = ioutil.ReadFile(filename)
|
||||
require.NoError(t, err)
|
||||
shortJob, _ := Asset("command/assets/example-short.nomad.hcl")
|
||||
shortJob := asset.JobExampleShort
|
||||
require.Equal(t, string(content), string(shortJob))
|
||||
|
||||
// Fails if the file exists
|
||||
|
|
|
@ -125,7 +125,7 @@ func TestPlanCommand_hcl1_hcl2_strict(t *testing.T) {
|
|||
got := cmd.Run([]string{
|
||||
"-hcl1", "-hcl2-strict",
|
||||
"-address", addr,
|
||||
"assets/example-short.nomad.hcl",
|
||||
"asset/example-short.nomad.hcl",
|
||||
})
|
||||
// Exit code 1 here means that an alloc will be created, which is
|
||||
// expected.
|
||||
|
@ -134,6 +134,8 @@ func TestPlanCommand_hcl1_hcl2_strict(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestPlanCommand_From_STDIN(t *testing.T) {
|
||||
_, _, addr := testServer(t, false, nil)
|
||||
|
||||
ci.Parallel(t)
|
||||
stdinR, stdinW, err := os.Pipe()
|
||||
if err != nil {
|
||||
|
@ -142,38 +144,36 @@ func TestPlanCommand_From_STDIN(t *testing.T) {
|
|||
|
||||
ui := cli.NewMockUi()
|
||||
cmd := &JobPlanCommand{
|
||||
Meta: Meta{Ui: ui},
|
||||
Meta: Meta{
|
||||
Ui: ui,
|
||||
flagAddress: addr,
|
||||
},
|
||||
JobGetter: JobGetter{testStdin: stdinR},
|
||||
}
|
||||
|
||||
go func() {
|
||||
stdinW.WriteString(`
|
||||
job "job1" {
|
||||
datacenters = ["dc1"]
|
||||
type = "service"
|
||||
datacenters = [ "dc1" ]
|
||||
group "group1" {
|
||||
count = 1
|
||||
task "task1" {
|
||||
driver = "exec"
|
||||
resources {
|
||||
cpu = 1000
|
||||
memory = 512
|
||||
}
|
||||
}
|
||||
}
|
||||
group "group1" {
|
||||
count = 1
|
||||
task "task1" {
|
||||
driver = "exec"
|
||||
resources {
|
||||
cpu = 100
|
||||
memory = 100
|
||||
}
|
||||
}
|
||||
}
|
||||
}`)
|
||||
stdinW.Close()
|
||||
}()
|
||||
|
||||
args := []string{"-"}
|
||||
if code := cmd.Run(args); code != 255 {
|
||||
t.Fatalf("expected exit code 255, got %d: %q", code, ui.ErrorWriter.String())
|
||||
}
|
||||
|
||||
if out := ui.ErrorWriter.String(); !strings.Contains(out, "connection refused") {
|
||||
t.Fatalf("expected connection refused error, got: %s", out)
|
||||
}
|
||||
ui.ErrorWriter.Reset()
|
||||
args := []string{"-address", addr, "-"}
|
||||
code := cmd.Run(args)
|
||||
must.Eq(t, 1, code, must.Sprintf("expected exit code 1, got %d: %q", code, ui.ErrorWriter.String()))
|
||||
must.Eq(t, "", ui.ErrorWriter.String(), must.Sprintf("expected no stderr output, got:\n%s", ui.ErrorWriter.String()))
|
||||
}
|
||||
|
||||
func TestPlanCommand_From_Files(t *testing.T) {
|
||||
|
@ -246,7 +246,7 @@ func TestPlanCommand_From_URL(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestPlanCommad_Preemptions(t *testing.T) {
|
||||
func TestPlanCommand_Preemptions(t *testing.T) {
|
||||
ci.Parallel(t)
|
||||
ui := cli.NewMockUi()
|
||||
cmd := &JobPlanCommand{Meta: Meta{Ui: ui}}
|
||||
|
@ -329,7 +329,7 @@ func TestPlanCommad_Preemptions(t *testing.T) {
|
|||
require.Contains(out, "service")
|
||||
}
|
||||
|
||||
func TestPlanCommad_JSON(t *testing.T) {
|
||||
func TestPlanCommand_JSON(t *testing.T) {
|
||||
ui := cli.NewMockUi()
|
||||
cmd := &JobPlanCommand{
|
||||
Meta: Meta{Ui: ui},
|
||||
|
|
|
@ -66,7 +66,7 @@ func TestRunCommand_hcl1_hcl2_strict(t *testing.T) {
|
|||
"-hcl1", "-hcl2-strict",
|
||||
"-address", addr,
|
||||
"-detach",
|
||||
"assets/example-short.nomad.hcl",
|
||||
"asset/example-short.nomad.hcl",
|
||||
})
|
||||
require.Equal(t, 0, got, ui.ErrorWriter.String())
|
||||
})
|
||||
|
@ -258,7 +258,7 @@ func TestRunCommand_JSON(t *testing.T) {
|
|||
}()
|
||||
|
||||
// First convert HCL -> JSON with -output
|
||||
stdout, stderr, code := run("-output", "assets/example-short.nomad.hcl")
|
||||
stdout, stderr, code := run("-output", "asset/example-short.nomad.hcl")
|
||||
require.Zero(t, code, stderr)
|
||||
require.Empty(t, stderr)
|
||||
require.NotEmpty(t, stdout)
|
||||
|
|
|
@ -79,7 +79,7 @@ func TestValidateCommand_hcl1_hcl2_strict(t *testing.T) {
|
|||
got := cmd.Run([]string{
|
||||
"-hcl1", "-hcl2-strict",
|
||||
"-address", addr,
|
||||
"assets/example-short.nomad.hcl",
|
||||
"asset/example-short.nomad.hcl",
|
||||
})
|
||||
require.Equal(t, 0, got, ui.ErrorWriter.String())
|
||||
})
|
||||
|
|
|
@ -147,6 +147,9 @@ var uninteresting = []string{
|
|||
// main
|
||||
".",
|
||||
|
||||
// go embed assets
|
||||
"command/asset",
|
||||
|
||||
// testing helpers
|
||||
"ci",
|
||||
"client/testutil",
|
||||
|
|
Loading…
Reference in New Issue