Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
(GH-183) Update pct new to handle namespaced & versioned templates
Browse files Browse the repository at this point in the history
Prior to this commit, the `pct new` command would only be capable
of processing templates in the old format (`< 0.4.0`) of
`id/(pct-config.yml|content/)`.

This commit adds support for the new namespaced and versioned PCT
template structure of `namespace/id/ver/(pct-config.yml|content/)`
which will become the standard from `0.4.0` onwards.

Also as part of this commit:
- The test resources under `internal/pkg/pct/testdata/examples`
have been updated to the new template format
- Unit and acceptance tests updated
- Re-enabled the tests temporarily backed out until GH-183 complete
  • Loading branch information
sanfrancrisko committed Sep 9, 2021
1 parent 4070f5d commit 401a58c
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 87 deletions.
108 changes: 54 additions & 54 deletions acceptance/new/new_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package new_test

import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -9,6 +10,7 @@ import (
"github.com/puppetlabs/pdkgo/acceptance/testutils"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v2"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -65,72 +67,70 @@ func TestPctNewUnknownTemplate(t *testing.T) {
assert.Equal(t, 1, exitCode)
}

// The following tests will need to be re-enabled after GH-183 has been completed

// func TestPctNewKnownTemplate(t *testing.T) {
// testutils.SkipAcceptanceTest(t)
func TestPctNewKnownTemplate(t *testing.T) {
testutils.SkipAcceptanceTest(t)

// stdout, stderr, exitCode := testutils.RunPctCommand("new full-project --templatepath "+templatePath, os.TempDir())
// assert.Contains(t, stdout, "Deployed:")
// assert.Equal(t, "", stderr)
// assert.Equal(t, 0, exitCode)
// }
stdout, stderr, exitCode := testutils.RunPctCommand("new puppetlabs/full-project --templatepath "+templatePath, os.TempDir())
assert.Contains(t, stdout, "Deployed:")
assert.Equal(t, "", stderr)
assert.Equal(t, 0, exitCode)
}

// func TestPctNewInfo(t *testing.T) {
// testutils.SkipAcceptanceTest(t)
func TestPctNewInfo(t *testing.T) {
testutils.SkipAcceptanceTest(t)

// stdout, stderr, exitCode := testutils.RunPctCommand("new --info full-project --templatepath "+templatePath, os.TempDir())
stdout, stderr, exitCode := testutils.RunPctCommand("new --info puppetlabs/full-project --templatepath "+templatePath, os.TempDir())

// expectedYaml := `puppet_module:
// license: Apache-2.0
// summary: A New Puppet Module
// version: 0.1.0`
expectedYaml := `puppet_module:
license: Apache-2.0
summary: A New Puppet Module
version: 0.1.0`

// var output map[string]interface{}
// var expected map[string]interface{}
var output map[string]interface{}
var expected map[string]interface{}

// err := yaml.Unmarshal([]byte(stdout), &output)
// if err != nil {
// assert.Fail(t, "returned data is not YAML")
// }
err := yaml.Unmarshal([]byte(stdout), &output)
if err != nil {
assert.Fail(t, "returned data is not YAML")
}

// err = yaml.Unmarshal([]byte(expectedYaml), &expected)
// if err != nil {
// assert.Fail(t, "expected data is not YAML")
// }
err = yaml.Unmarshal([]byte(expectedYaml), &expected)
if err != nil {
assert.Fail(t, "expected data is not YAML")
}

// assert.Equal(t, expected, output)
// assert.Equal(t, "", stderr)
// assert.Equal(t, 0, exitCode)
// }
assert.Equal(t, expected, output)
assert.Equal(t, "", stderr)
assert.Equal(t, 0, exitCode)
}

// func TestPctNewInfoJson(t *testing.T) {
// testutils.SkipAcceptanceTest(t)
func TestPctNewInfoJson(t *testing.T) {
testutils.SkipAcceptanceTest(t)

// stdout, stderr, exitCode := testutils.RunPctCommand("new --info full-project --format json --templatepath "+templatePath, os.TempDir())
stdout, stderr, exitCode := testutils.RunPctCommand("new --info puppetlabs/full-project --format json --templatepath "+templatePath, os.TempDir())

// expectedJson := `{
// "puppet_module": {
// "license": "Apache-2.0",
// "version": "0.1.0",
// "summary": "A New Puppet Module"
// }
// }`
expectedJson := `{
"puppet_module": {
"license": "Apache-2.0",
"version": "0.1.0",
"summary": "A New Puppet Module"
}
}`

// var output map[string]interface{}
// var expected map[string]interface{}
var output map[string]interface{}
var expected map[string]interface{}

// err := json.Unmarshal([]byte(stdout), &output)
// if err != nil {
// assert.Fail(t, "returned data is not JSON")
// }
err := json.Unmarshal([]byte(stdout), &output)
if err != nil {
assert.Fail(t, "returned data is not JSON")
}

// err = json.Unmarshal([]byte(expectedJson), &expected)
// if err != nil {
// assert.Fail(t, "expected data is not JSON")
// }
err = json.Unmarshal([]byte(expectedJson), &expected)
if err != nil {
assert.Fail(t, "expected data is not JSON")
}

// assert.Equal(t, expected, output)
// assert.Equal(t, "", stderr)
// assert.Equal(t, 0, exitCode)
// }
assert.Equal(t, expected, output)
assert.Equal(t, "", stderr)
assert.Equal(t, 0, exitCode)
}
29 changes: 15 additions & 14 deletions cmd/new/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ import (
)

var (
localTemplatePath string
format string
selectedTemplate string
selectedTemplateInfo string
listTemplates bool
targetName string
targetOutput string
pctApi *pct.Pct
cachedTemplates []pct.PuppetContentTemplate
localTemplatePath string
format string
selectedTemplate string
selectedTemplateDirPath string
selectedTemplateInfo string
listTemplates bool
targetName string
targetOutput string
pctApi *pct.Pct
cachedTemplates []pct.PuppetContentTemplate
)

func CreateCommand() *cobra.Command {
Expand Down Expand Up @@ -172,8 +173,8 @@ func execute(cmd *cobra.Command, args []string) error {

if len(matchingTemplates) == 1 {
matchingTemplate := matchingTemplates[0]
templateDirPath := filepath.Join(localTemplatePath, matchingTemplate.Author, matchingTemplate.Id, matchingTemplate.Version)
pctData, err := pctApi.GetInfo(templateDirPath)
selectedTemplateDirPath = filepath.Join(localTemplatePath, matchingTemplate.Author, matchingTemplate.Id, matchingTemplate.Version)
pctData, err := pctApi.GetInfo(selectedTemplateDirPath)
if err != nil {
return err
}
Expand All @@ -193,8 +194,8 @@ func execute(cmd *cobra.Command, args []string) error {

if len(matchingTemplates) == 1 {
matchingTemplate := matchingTemplates[0]
templateDirPath := filepath.Join(localTemplatePath, matchingTemplate.Author, matchingTemplate.Id, matchingTemplate.Version)
_, err := pctApi.Get(templateDirPath)
selectedTemplateDirPath = filepath.Join(localTemplatePath, matchingTemplate.Author, matchingTemplate.Id, matchingTemplate.Version)
_, err := pctApi.Get(selectedTemplateDirPath)
if err != nil {
return err
}
Expand All @@ -207,7 +208,7 @@ func execute(cmd *cobra.Command, args []string) error {

deployed := pctApi.Deploy(pct.DeployInfo{
SelectedTemplate: selectedTemplate,
TemplateCache: localTemplatePath,
TemplateDirPath: selectedTemplateDirPath,
TargetOutputDir: targetOutput,
TargetName: targetName,
PdkInfo: pdkInfo,
Expand Down
18 changes: 9 additions & 9 deletions internal/pkg/pct/pct.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type PDKInfo struct {
// DeployInfo represents the final set of information needed to deploy a template
type DeployInfo struct {
SelectedTemplate string
TemplateCache string
TemplateDirPath string
TargetOutputDir string
TargetName string
PdkInfo PDKInfo
Expand Down Expand Up @@ -238,9 +238,8 @@ func (p *Pct) Deploy(info DeployInfo) []string {

log.Trace().Msgf("PDKInfo: %+v", info.PdkInfo)

file := filepath.Join(info.TemplateCache, info.SelectedTemplate, TemplateConfigFileName)
log.Debug().Msgf("Template: %s", file)
tmpl := p.readTemplateConfig(file)
log.Debug().Msgf("Template: %s", info.TemplateDirPath)
tmpl := p.readTemplateConfig(filepath.Join(info.TemplateDirPath, TemplateConfigFileName))
log.Trace().Msgf("Parsed: %+v", tmpl)

if info.TargetName == "" && info.TargetOutputDir == "" { // pdk new foo-foo
Expand All @@ -262,7 +261,7 @@ func (p *Pct) Deploy(info DeployInfo) []string {
}
}

contentDir := filepath.Join(info.TemplateCache, info.SelectedTemplate, "content")
contentDir := filepath.Join(info.TemplateDirPath, "content")
log.Debug().Msgf("Target Name: %s", info.TargetName)
log.Debug().Msgf("Target Output: %s", info.TargetOutputDir)

Expand Down Expand Up @@ -308,7 +307,7 @@ func (p *Pct) Deploy(info DeployInfo) []string {
deployed = append(deployed, templateFile.TargetFilePath)
}
} else {
err := p.createTemplateFile(info, file, templateFile, tmpl.Template)
err := p.createTemplateFile(info, templateFile, tmpl.Template)
if err != nil {
log.Error().Msgf("%s", err)
continue
Expand All @@ -332,11 +331,10 @@ func (p *Pct) createTemplateDirectory(targetDir string) error {
return nil
}

func (p *Pct) createTemplateFile(info DeployInfo, configFile string, templateFile PuppetContentTemplateFileInfo, tmpl PuppetContentTemplate) error {
func (p *Pct) createTemplateFile(info DeployInfo, templateFile PuppetContentTemplateFileInfo, tmpl PuppetContentTemplate) error {
log.Trace().Msgf("Creating: '%s'", templateFile.TargetFilePath)
config := p.processConfiguration(
info,
configFile,
templateFile.TemplatePath,
tmpl,
)
Expand Down Expand Up @@ -379,7 +377,7 @@ func (p *Pct) createTemplateFile(info DeployInfo, configFile string, templateFil
return nil
}

func (p *Pct) processConfiguration(info DeployInfo, configFile string, projectTemplate string, tmpl PuppetContentTemplate) map[string]interface{} {
func (p *Pct) processConfiguration(info DeployInfo, projectTemplate string, tmpl PuppetContentTemplate) map[string]interface{} {
v := viper.New()

log.Trace().Msgf("PDKInfo: %+v", info.PdkInfo)
Expand Down Expand Up @@ -420,6 +418,8 @@ func (p *Pct) processConfiguration(info DeployInfo, configFile string, projectTe
v.SetDefault("pdk.build_date", info.PdkInfo.BuildDate)

// Template specific variables

configFile := filepath.Join(info.TemplateDirPath, TemplateConfigFileName)
log.Trace().Msgf("Adding %v", filepath.Dir(configFile))
// v.SetConfigFile(configFile)
v.SetConfigName(TemplateConfigName)
Expand Down
19 changes: 9 additions & 10 deletions internal/pkg/pct/pct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestDeploy(t *testing.T) {
args: args{
info: pct.DeployInfo{
SelectedTemplate: "full-project",
TemplateCache: "testdata/examples",
TemplateDirPath: "templates/author/id/0.1.0",
TargetOutputDir: filepath.Join(tmp, "foobar"),
TargetName: "woo",
PdkInfo: pct.PDKInfo{
Expand Down Expand Up @@ -71,7 +71,7 @@ template:
args: args{
info: pct.DeployInfo{
SelectedTemplate: "full-project",
TemplateCache: "testdata/examples",
TemplateDirPath: "templates/author/id/0.1.0",
TargetOutputDir: "",
TargetName: "",
PdkInfo: pct.PDKInfo{
Expand Down Expand Up @@ -100,7 +100,7 @@ template:
args: args{
info: pct.DeployInfo{
SelectedTemplate: "full-project",
TemplateCache: "testdata/examples",
TemplateDirPath: "templates/author/id/0.1.0",
TargetOutputDir: "",
TargetName: "wibble",
PdkInfo: pct.PDKInfo{
Expand Down Expand Up @@ -129,7 +129,7 @@ template:
args: args{
info: pct.DeployInfo{
SelectedTemplate: "replace-thing",
TemplateCache: "testdata/examples",
TemplateDirPath: "templates/author/id/0.1.0",
TargetOutputDir: filepath.Join(tmp, "thing"),
TargetName: "woo",
PdkInfo: pct.PDKInfo{
Expand Down Expand Up @@ -160,7 +160,7 @@ Summary: {{.example_replace.summary}}`,
args: args{
info: pct.DeployInfo{
SelectedTemplate: "replace-thing",
TemplateCache: "testdata/examples",
TemplateDirPath: "templates/author/id/0.1.0",
TargetOutputDir: "",
TargetName: "",
PdkInfo: pct.PDKInfo{
Expand Down Expand Up @@ -191,7 +191,7 @@ Summary: {{.example_replace.summary}}`,
args: args{
info: pct.DeployInfo{
SelectedTemplate: "replace-thing",
TemplateCache: "testdata/examples",
TemplateDirPath: "templates/author/id/0.1.0",
TargetOutputDir: "",
TargetName: "wibble",
PdkInfo: pct.PDKInfo{
Expand Down Expand Up @@ -226,11 +226,10 @@ Summary: {{.example_replace.summary}}`,
iofs := &afero.IOFS{Fs: fs}

// Create the template
templateDir := filepath.Join(tt.args.info.TemplateCache, tt.args.info.SelectedTemplate)
contentDir := filepath.Join(templateDir, "content")
contentDir := filepath.Join(tt.args.info.TemplateDirPath, "content")
afs.MkdirAll(contentDir, 0750) //nolint:errcheck
// Create template config
config, _ := afs.Create(filepath.Join(templateDir, "pct-config.yml"))
config, _ := afs.Create(filepath.Join(tt.args.info.TemplateDirPath, "pct-config.yml"))
config.Write([]byte(tt.args.templateConfig)) //nolint:errcheck
// Create the contents
for file, content := range tt.args.templateContent {
Expand Down Expand Up @@ -276,7 +275,7 @@ func TestGet(t *testing.T) {
{
name: "returns tmpl for existent template",
args: args{
templateDirPath: "emplates/author/full-project/0.1.0",
templateDirPath: "templates/author/full-project/0.1.0",
setup: true,
templateConfig: `---
template:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
template:
id: full-project
author: puppetlabs
type: project
display: Full Project
version: 0.1.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
template:
id: good-project
author: puppetlabs
type: project
display: Good Project
version: 0.1.0
Expand Down

0 comments on commit 401a58c

Please sign in to comment.