Skip to content

Commit

Permalink
Merge branch 'main' into i4k-fix-linelength-limit
Browse files Browse the repository at this point in the history
  • Loading branch information
i4ki committed Oct 20, 2023
2 parents ce601de + 7104be0 commit 7d7462a
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 90 deletions.
20 changes: 20 additions & 0 deletions cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"fmt"
"io"
"net/http"
"net/http/httputil"
"os"
"path"
"strconv"

Expand Down Expand Up @@ -68,6 +70,14 @@ type (
}
)

var debugAPIRequests bool

func init() {
if d := os.Getenv("TMC_API_DEBUG"); d == "1" || d == "true" {
debugAPIRequests = true
}
}

// Users retrieves the user details for the signed in user.
func (c *Client) Users(ctx context.Context) (user User, err error) {
return Get[User](ctx, c, UsersPath)
Expand Down Expand Up @@ -218,12 +228,22 @@ func Request[T Resource](ctx context.Context, c *Client, method string, resource
return entity, err
}

if debugAPIRequests {
data, _ := httputil.DumpRequestOut(req, true)
fmt.Printf(">>> %s\n\n", data)
}

client := c.httpClient()
resp, err := client.Do(req)
if err != nil {
return entity, err
}

if debugAPIRequests {
data, _ := httputil.DumpResponse(resp, true)
fmt.Printf("<<< %s\n\n", data)
}

defer func() {
err = errors.L(err, resp.Body.Close()).AsError()
}()
Expand Down
13 changes: 8 additions & 5 deletions cloud/log_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,18 @@ func readLine(r io.Reader) (line []byte, err error) {
}
}

// dropCRLN drops a terminal \r from the data.
// dropCRLN drops a terminating \n and \r from the data.
func dropCRLN(data []byte) []byte {
data = dropByte(data, '\n')
data = dropByte(data, '\r')
return data
}

func dropByte(data []byte, b byte) []byte {
if len(data) == 0 {
return data
}
if data[len(data)-1] == '\n' {
data = data[0 : len(data)-1]
}
if data[len(data)-1] == '\r' {
if data[len(data)-1] == b {
data = data[0 : len(data)-1]
}
return data
Expand Down
20 changes: 20 additions & 0 deletions cloud/log_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ func TestCloudLogSyncer(t *testing.T) {
},
},
},
{
name: "empty line -- regression check",
writes: []write{
{channel: cloud.StdoutLogChannel, data: []byte("\n")},
},
want: want{
output: map[cloud.LogChannel][]byte{
cloud.StdoutLogChannel: []byte("\n"),
},
batches: []cloud.DeploymentLogs{
{
{
Line: 1,
Channel: cloud.StdoutLogChannel,
Message: "",
},
},
},
},
},
{
name: "multiple writes with CRLN",
writes: []write{
Expand Down
4 changes: 4 additions & 0 deletions cloud/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type (
// Stack represents the stack as defined by the user HCL code.
Stack struct {
Repository string `json:"repository"`
DefaultBranch string `json:"default_branch"`
Path string `json:"path"`
MetaID string `json:"meta_id"`
MetaName string `json:"meta_name,omitempty"`
Expand Down Expand Up @@ -336,6 +337,9 @@ func (s Stack) Validate() error {
if s.Repository == "" {
return errors.E(`missing "repository" field`)
}
if s.DefaultBranch == "" {
return errors.E(`missing "default_branch" field`)
}
if s.Path == "" {
return errors.E(`missing "path" field`)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/terramate/cli/cloud_sync_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (c *cli) createCloudDeployment(runStacks []ExecContext) {
MetaDescription: run.Stack.Description,
MetaTags: tags,
Repository: c.prj.prettyRepo(),
DefaultBranch: c.prj.gitcfg().DefaultBranch,
Path: run.Stack.Dir.String(),
},
CommitSHA: deploymentCommitSHA,
Expand Down
1 change: 1 addition & 0 deletions cmd/terramate/cli/cloud_sync_drift.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (c *cli) cloudSyncDriftStatus(runContext ExecContext, exitCode int, err err
_, err = c.cloud.client.CreateStackDrift(ctx, c.cloud.run.orgUUID, cloud.DriftStackPayloadRequest{
Stack: cloud.Stack{
Repository: c.prj.prettyRepo(),
DefaultBranch: c.prj.gitcfg().DefaultBranch,
Path: st.Dir.String(),
MetaID: st.ID,
MetaName: st.Name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"format_version":"1.2","terraform_version":"1.5.0","variables":{"content":{"value":"REDACTED_SENSITIVE"}},"planned_values":{"root_module":{"resources":[{"address":"local_file.foo","mode":"managed","type":"local_file","name":"foo","provider_name":"registry.terraform.io/hashicorp/local","schema_version":0,"values":{"content":"REDACTED_SENSITIVE","content_base64":null,"directory_permission":"0777","file_permission":"0777","filename":"./foo.bar","sensitive_content":"REDACTED_SENSITIVE","source":null},"sensitive_values":{"content":true}}]}},"resource_changes":[{"address":"local_file.foo","mode":"managed","type":"local_file","name":"foo","provider_name":"registry.terraform.io/hashicorp/local","change":{"actions":["create"],"before":null,"after":{"content":"REDACTED_SENSITIVE","content_base64":null,"directory_permission":"0777","file_permission":"0777","filename":"./foo.bar","sensitive_content":"REDACTED_SENSITIVE","source":null},"after_unknown":{"content_base64sha256":true,"content_base64sha512":true,"content_md5":true,"content_sha1":true,"content_sha256":true,"content_sha512":true,"id":true},"before_sensitive":false,"after_sensitive":{"content":true,"sensitive_content":true}}}],"configuration":{"provider_config":{"local":{"name":"local","full_name":"registry.terraform.io/hashicorp/local"}},"root_module":{"resources":[{"address":"local_file.foo","mode":"managed","type":"local_file","name":"foo","provider_config_key":"local","expressions":{"content":{"references":["var.content"]},"filename":{"references":["path.module"]}},"schema_version":0}],"variables":{"content":{"sensitive":true}}}},"timestamp":"2023-10-12T14:03:39Z"}
{"format_version":"1.2","terraform_version":"__terraform_version__","variables":{"content":{"value":"REDACTED_SENSITIVE"}},"planned_values":{"root_module":{"resources":[{"address":"local_file.foo","mode":"managed","type":"local_file","name":"foo","provider_name":"registry.terraform.io/hashicorp/local","schema_version":0,"values":{"content":"REDACTED_SENSITIVE","content_base64":null,"directory_permission":"0777","file_permission":"0777","filename":"./foo.bar","sensitive_content":"REDACTED_SENSITIVE","source":null},"sensitive_values":{"content":true}}]}},"resource_changes":[{"address":"local_file.foo","mode":"managed","type":"local_file","name":"foo","provider_name":"registry.terraform.io/hashicorp/local","change":{"actions":["create"],"before":null,"after":{"content":"REDACTED_SENSITIVE","content_base64":null,"directory_permission":"0777","file_permission":"0777","filename":"./foo.bar","sensitive_content":"REDACTED_SENSITIVE","source":null},"after_unknown":{"content_base64sha256":true,"content_base64sha512":true,"content_md5":true,"content_sha1":true,"content_sha256":true,"content_sha512":true,"id":true},"before_sensitive":false,"after_sensitive":{"content":true,"sensitive_content":true}}}],"configuration":{"provider_config":{"local":{"name":"local","full_name":"registry.terraform.io/hashicorp/local"}},"root_module":{"resources":[{"address":"local_file.foo","mode":"managed","type":"local_file","name":"foo","provider_config_key":"local","expressions":{"content":{"references":["var.content"]},"filename":{"references":["path.module"]}},"schema_version":0}],"variables":{"content":{"sensitive":true}}}},"timestamp":"2023-10-12T14:03:39Z"}
51 changes: 38 additions & 13 deletions cmd/terramate/e2etests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"

Expand All @@ -22,14 +23,17 @@ import (
"github.com/terramate-io/terramate/errors"
)

const terraformVersion = "1.5.0"
const terraformInstallVersion = "1.5.0"

// terramateTestBin is the path to the terramate binary we compiled for test purposes
var terramateTestBin string

// terraformTestBin is the path to the installed terraform binary.
var terraformTestBin string

// terraformVersion is the detected or installed Terraform version.
var terraformVersion string

// testHelperBin is the path to the test binary we compiled for test purposes
var testHelperBin string

Expand Down Expand Up @@ -89,21 +93,14 @@ func setupAndRunTests(m *testing.M) (status int) {
EOF
)}`, testHelperBin)

tfExecPath, installer, err := installTerraform()
tfExecPath, cleanup, err := installTerraform()
if err != nil {
log.Printf("failed to setup Terraform binary")
return 1
}

defer cleanup()
terraformTestBin = tfExecPath

defer func() {
err := installer.Remove(context.Background())
if err != nil {
log.Printf("failed to remove terraform installation")
}
}()

return m.Run()
}

Expand Down Expand Up @@ -146,12 +143,33 @@ func buildTerramate(goBin, projectRoot, binDir string) (string, error) {
return outBinPath, nil
}

func installTerraform() (string, *install.Installer, error) {
func installTerraform() (string, func(), error) {
requireVersion := os.Getenv("TM_TEST_TERRAFORM_REQUIRED_VERSION")
tfExecPath, err := exec.LookPath("terraform")
if err == nil {
cmd := exec.Command("terraform", "version")
output, err := cmd.Output()
if err == nil && len(output) > 0 {
lines := strings.Split(string(output), "\n")
terraformVersion = strings.TrimPrefix(strings.TrimSpace(lines[0]), "Terraform v")

if requireVersion == "" || terraformVersion == requireVersion {
log.Printf("Terraform detected version: %s", terraformVersion)
return tfExecPath, func() {}, nil
}
}
}

installVersion := terraformInstallVersion
if requireVersion != "" {
installVersion = requireVersion
}

ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()

installer := install.NewInstaller()
version := version.Must(version.NewVersion(terraformVersion))
version := version.Must(version.NewVersion(installVersion))

execPath, err := installer.Install(ctx, []src.Installable{
&releases.ExactVersion{
Expand All @@ -162,7 +180,14 @@ func installTerraform() (string, *install.Installer, error) {
if err != nil {
return "", nil, errors.E(err, "installing Terraform")
}
return execPath, installer, nil
terraformVersion = installVersion
log.Printf("Terraform installed version: %s", terraformVersion)
return execPath, func() {
err := installer.Remove(context.Background())
if err != nil {
log.Printf("failed to remove terraform installation")
}
}, nil
}

func lookupGoBin() (string, error) {
Expand Down

0 comments on commit 7d7462a

Please sign in to comment.