Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

template: Create a my alias for the current pack #221

Merged
merged 3 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ BUG FIXES:
IMPROVEMENTS:
* cli: Add flags to configure Nomad API client [[GH-213](https://github.com/hashicorp/nomad-pack/pull/213)]
* template: Add support for custom Spew configurations. [[GH-220](https://github.com/hashicorp/nomad-pack/pull/220)]
* template: Create a `my` alias for the current pack [[GH-221](https://github.com/hashicorp/nomad-pack/pull/221)]

## 0.0.1-techpreview2 (February 07, 2022)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
app {
url = ""
author = "HashiCorp"
}

pack {
name = "child1"
description = "render-only child dependency"
url = "github.com/hashicorp/nomad-pack/fixtures/test_registry/packs/simple-raw-exec"
version = "0.0.1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[- .my.job_name -]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "job_name" {
description = "job name"
type = string
default = "child1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
app {
url = ""
author = "HashiCorp"
}

pack {
name = "child2"
description = "render-only child dependency"
url = "github.com/hashicorp/nomad-pack/fixtures/test_registry/packs/simple-raw-exec"
version = "0.0.1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[- .my.job_name -]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "job_name" {
description = "job name"
type = string
default = "child2"
}
18 changes: 18 additions & 0 deletions fixtures/test_registry/packs/my_alias_test/metadata.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
app {
url = ""
author = "HashiCorp"
}

pack {
name = "deps_test"
description = "This pack tests dependencies"
url = "github.com/hashicorp/nomad-pack/fixtures/test_registry/packs/deps_test"
version = "0.0.1"
}

dependency "child1" {
source = "./deps/child1"
}
dependency "child2" {
source = "./deps/child2"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[- .my.job_name -]]
47 changes: 47 additions & 0 deletions fixtures/test_registry/packs/my_alias_test/variables.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
variable "job_name" {
description = "The name to use as the job name which overrides using the pack name"
type = string
default = "deps_test"
}

variable "test_name" {
description = "This variable allows for configurable test output"
type = string
default = "test"
}

variable "region" {
description = "The region where jobs will be deployed"
type = string
default = ""
}

variable "datacenters" {
description = "A list of datacenters in the region which are eligible for task placement"
type = list(string)
default = ["dc1"]
}

variable "count" {
description = "The number of app instances to deploy"
type = number
default = 1
}

variable "command" {
type = string
description = "bash command to run"
default = "echo \"$(date) - Started.\"; while true; do sleep 300; echo -n .; done"
}

variable "env" {
type = map(string)
description = "environment variable collection"
default = {}
}

variable "test_name" {
type = string
description = "behavior modifying constant"
default = ""
}
27 changes: 27 additions & 0 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path"
"path/filepath"
"regexp"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -375,6 +376,32 @@ func TestStatusFails(t *testing.T) {
})
}

func TestRenderMyAlias(t *testing.T) {
// This test has to do some extra shenanigans because dependent pack template
// output is not guaranteed to be ordered. This requires that the test handle
// either order.
expected := []string{
"child1/child1.nomad=child1",
"child2/child2.nomad=child2",
"deps_test/deps_test.nomad=deps_test",
}

result := runPackCmd(t, []string{
"render",
getTestPackPath("my_alias_test"),
})
require.Empty(t, result.cmdErr.String(), "cmdErr should be empty, but was %q", result.cmdErr.String())

// Performing a little clever string manipulation on the render output to
// prepare it for splitting into a slice of string enables us to use
// require.ElementsMatch to validate goodness.
outStr := strings.TrimSpace(result.cmdOut.String())
outStr = strings.ReplaceAll(outStr, ":\n\n", "=")
elems := strings.Split(outStr, "\n")

require.ElementsMatch(t, expected, elems)
}

type PackCommandResult struct {
exitCode int
cmdOut *bytes.Buffer
Expand Down
11 changes: 8 additions & 3 deletions internal/pkg/renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (r *Renderer) RenderOutput() (string, error) {
return buf.String(), nil
}

// prepareTemplates recurses the pack and it's dependencies to populate to the
// prepareTemplates recurses the pack and its dependencies to populate to the
// passed map with the templates to render along with the variables which
// correspond.
func prepareTemplates(p *pack.Pack, templates map[string]toRender, variables map[string]interface{}) {
Expand All @@ -147,8 +147,9 @@ func prepareTemplates(p *pack.Pack, templates map[string]toRender, variables map
// variables. If the pack is the parent/root pack, then it has access to
// all.
if p.HasParent() {
if vars, ok := variables[p.Name()]; ok {
newVars[p.Name()] = vars
if v, ok := variables[p.Name()]; ok {
newVars["my"] = v
newVars[p.Name()] = v
}
} else {
newVars = variables
Expand All @@ -157,6 +158,10 @@ func prepareTemplates(p *pack.Pack, templates map[string]toRender, variables map
// Add the pack's metadata to the variable mapping.
newVars = p.Metadata.AddToInterfaceMap(newVars)

// Make the `my` alias for the parent pack.
if !p.HasParent() {
newVars["my"] = newVars[p.Name()]
}
// Iterate the dependencies and prepareTemplates for each.
for _, child := range p.Dependencies() {
prepareTemplates(child, templates, newVars)
Expand Down
4 changes: 2 additions & 2 deletions sdk/pack/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ func (md *Metadata) ConvertToMapInterface() map[string]interface{} {
}

// AddToInterfaceMap adds the metadata information to the provided map as a new
// entry under the "nom" key. This is useful for adding this information to the
// template rendering data.
// entry under the "nomad_pack" key. This is useful for adding this information
// to the template rendering data.
func (md *Metadata) AddToInterfaceMap(m map[string]interface{}) map[string]interface{} {
m["nomad_pack"] = map[string]interface{}{
"app": map[string]interface{}{
Expand Down