-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:mineiros-io/terramate into i4k-add-…
…stack-watch
- Loading branch information
Showing
5 changed files
with
199 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2022 Mineiros GmbH | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package e2etest | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/madlambda/spells/assert" | ||
"github.com/mineiros-io/terramate/test" | ||
"github.com/mineiros-io/terramate/test/sandbox" | ||
) | ||
|
||
func TestCreateStack(t *testing.T) { | ||
s := sandbox.New(t) | ||
cli := newCLI(t, s.RootDir()) | ||
|
||
const ( | ||
stackID = "stack-id" | ||
stackName = "stack name" | ||
stackDescription = "stack description" | ||
stackImport1 = "/core/file1.tm.hcl" | ||
stackImport2 = "/core/file2.tm.hcl" | ||
) | ||
|
||
createFile := func(path string) { | ||
abspath := filepath.Join(s.RootDir(), path) | ||
test.WriteFile(t, filepath.Dir(abspath), filepath.Base(abspath), "") | ||
} | ||
|
||
createFile(stackImport1) | ||
createFile(stackImport2) | ||
|
||
stackPaths := []string{ | ||
"stack-1", | ||
"/stack-2", | ||
"/stacks/stack-a", | ||
"stacks/stack-b", | ||
} | ||
|
||
for _, stackPath := range stackPaths { | ||
res := cli.run("create", stackPath, | ||
"--id", stackID, | ||
"--name", stackName, | ||
"--description", stackDescription, | ||
"--import", stackImport1, | ||
"--import", stackImport2, | ||
) | ||
|
||
assertRunResult(t, res, runExpected{ | ||
Stdout: fmt.Sprintf("Created stack %s with success\n", stackPath), | ||
}) | ||
|
||
got := s.LoadStack(stackPath) | ||
|
||
gotID, _ := got.ID() | ||
assert.EqualStrings(t, stackID, gotID) | ||
assert.EqualStrings(t, stackName, got.Name(), "checking stack name") | ||
assert.EqualStrings(t, stackDescription, got.Desc(), "checking stack description") | ||
|
||
test.AssertStackImports(t, s.RootDir(), got, []string{stackImport1, stackImport2}) | ||
} | ||
} | ||
|
||
func TestCreateStackDefaults(t *testing.T) { | ||
s := sandbox.New(t) | ||
cli := newCLI(t, s.RootDir()) | ||
cli.run("create", "stack") | ||
|
||
got := s.LoadStack("stack") | ||
|
||
assert.EqualStrings(t, "stack", got.Name(), "checking stack name") | ||
assert.EqualStrings(t, "stack", got.Desc(), "checking stack description") | ||
|
||
test.AssertStackImports(t, s.RootDir(), got, []string{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2022 Mineiros GmbH | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/madlambda/spells/assert" | ||
"github.com/mineiros-io/terramate/hcl" | ||
"github.com/mineiros-io/terramate/stack" | ||
) | ||
|
||
// AssertStackImports checks that the given stack has all the wanted import | ||
// definitions. The wanted imports is a slice of the sources that are imported | ||
// on each block. | ||
func AssertStackImports(t *testing.T, rootdir string, got stack.S, want []string) { | ||
t.Helper() | ||
|
||
parser, err := hcl.NewTerramateParser(rootdir, got.HostPath()) | ||
assert.NoError(t, err) | ||
|
||
err = parser.AddDir(got.HostPath()) | ||
assert.NoError(t, err) | ||
|
||
err = parser.MinimalParse() | ||
assert.NoError(t, err) | ||
|
||
imports, err := parser.Imports() | ||
assert.NoError(t, err) | ||
|
||
if len(imports) != len(want) { | ||
t.Fatalf("got %d imports, wanted %v", len(imports), want) | ||
} | ||
|
||
checkImports: | ||
for _, wantImport := range want { | ||
for _, gotImportBlock := range imports { | ||
sourceVal, diags := gotImportBlock.Attributes["source"].Expr.Value(nil) | ||
if diags.HasErrors() { | ||
t.Fatalf("error %v evaluating import source attribute", diags) | ||
} | ||
if sourceVal.AsString() == wantImport { | ||
continue checkImports | ||
} | ||
} | ||
t.Errorf("wanted import %s not found", wantImport) | ||
} | ||
} |