1
1
mirror of https://github.com/mcuadros/ascode synced 2024-11-23 01:11:59 +01:00

starlark/types: State, documentation and examples

This commit is contained in:
Máximo Cuadros 2020-03-22 13:48:15 +01:00
parent 27ef6956a4
commit 10a78e0a1d
No known key found for this signature in database
GPG Key ID: 17A5DFEDC735AE4B
4 changed files with 107 additions and 1 deletions

@ -0,0 +1,77 @@
package types
import (
"bufio"
"os"
stdos "os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"go.starlark.net/starlark"
)
func TestExamples(t *testing.T) {
pwd, _ := stdos.Getwd()
defer func() {
stdos.Chdir(pwd)
}()
stdos.Chdir(filepath.Join(pwd, "testdata/examples"))
tests, err := filepath.Glob("*.star")
assert.NoError(t, err)
for _, test := range tests {
doTestExample(t, test)
}
}
func doTestExample(t *testing.T, filename string) {
var output string
printer := func(_ *starlark.Thread, msg string) {
if output != "" {
output += "\n"
}
output += msg
}
doTestPrint(t, filename, printer)
expected := getExpectedFromExample(t, filename)
assert.Equal(t, output, expected)
}
func getExpectedFromExample(t *testing.T, filename string) string {
f, err := os.Open(filename)
assert.NoError(t, err)
defer f.Close()
var expected []string
scanner := bufio.NewScanner(f)
var capture bool
for scanner.Scan() {
line := scanner.Text()
if line == "# Output:" {
capture = true
continue
}
if !capture {
continue
}
if len(line) >= 2 {
line = line[2:]
} else {
line = ""
}
expected = append(expected, line)
}
return strings.Join(expected, "\n")
}

@ -53,10 +53,14 @@ func TestHCLIntegration(t *testing.T) {
}
func doTest(t *testing.T, filename string) {
doTestPrint(t, filename, nil)
}
func doTestPrint(t *testing.T, filename string, print func(*starlark.Thread, string)) {
id = 0
log.SetOutput(ioutil.Discard)
thread := &starlark.Thread{Load: load}
thread := &starlark.Thread{Load: load, Print: print}
thread.SetLocal("base_path", "testdata")
test.SetReporter(thread, t)

@ -0,0 +1,24 @@
# new instance of a local backend
# https://www.terraform.io/docs/backends/types/local.html
b = backend("local")
b.path = "terraform.tfstate"
# it reads the state
s = b.state()
for provider in sorted(list(s)):
print("%s:" % provider)
for resource in sorted(list(s[provider]["resource"])):
count = len(s[provider]["resource"][resource])
print(" %s (%d)" % (resource, count))
# Output:
# google:
# container_cluster (1)
# container_node_pool (1)
# helm:
# release (5)
# kubernetes:
# cluster_role_binding (1)
# namespace (1)
# secret (1)

@ -0,0 +1 @@
../../fixtures/state/terraform.tfstate