Skip to content

Commit ac2b870

Browse files
committed
Added few tests Terraform struct
1 parent c940233 commit ac2b870

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

configuration.mock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//Mock Terraform Configuration File
2+
resource "aws_eip" "nat" {
3+
vpc = true
4+
count = "${length(var.public_subnets)}"
5+
}
6+
7+
resource "aws_nat_gateway" "nat" {
8+
allocation_id = "${element(aws_eip.nat.*.id, count.index)}"
9+
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
10+
count = "${length(var.public_subnets)}"
11+
}

file_utils.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ func getAllFiles(ext string) ([]string, error) {
1212
checkError(err)
1313
var files []string
1414
log.Infof("Finding files in %q directory", dir)
15-
files, err = filepath.Glob(tfFileExt)
15+
files, err = filepath.Glob(ext)
1616
checkError(err)
1717

1818
if len(files) == 0 {
19-
log.Infof("No files with .tf extensions found in %q", dir)
20-
os.Exit(0)
19+
log.Infof("No files with %q extensions found in %q", ext, dir)
2120
}
2221
return files, nil
2322
}

generator.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func main() {
4242
}
4343

4444
tfFiles, err := getAllFiles(tfFileExt)
45+
if len(tfFiles) == 0 {
46+
log.Warn("No terraform files to proceed, exiting")
47+
os.Exit(0)
48+
}
4549
checkError(err)
4650
var wg sync.WaitGroup
4751
messages := make(chan string)

generator_test.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package main
22

33
import (
4+
"bufio"
45
"os"
56
"testing"
67
)
78

8-
var mockFile = "mock.tf"
9+
var mockFile = "tf_configuration.mock"
10+
var testExtFile = "*.mock"
911

1012
func TestContainsElement(t *testing.T) {
1113
testSlice := []string{"Terraform", "Puppet", "Ansible"}
@@ -15,13 +17,32 @@ func TestContainsElement(t *testing.T) {
1517
}
1618

1719
func TestGetAllFiles(t *testing.T) {
18-
var file, err = os.Create(mockFile)
19-
checkError(err)
20-
defer file.Close()
21-
files, err := getAllFiles(tfFileExt)
20+
files, err := getAllFiles(testExtFile)
2221
checkError(err)
2322
if len(files) == 0 {
2423
t.Error("Should found at least one file")
2524
}
26-
defer os.Remove(mockFile)
25+
}
26+
27+
func TestMatchVariable(t *testing.T) {
28+
ter := &terraformVars{}
29+
var messages []string
30+
31+
file, err := getAllFiles(testExtFile)
32+
checkError(err)
33+
34+
fileHandle, _ := os.Open(file[0])
35+
defer fileHandle.Close()
36+
37+
fileScanner := bufio.NewScanner(fileHandle)
38+
for fileScanner.Scan() {
39+
messages = append(messages, fileScanner.Text())
40+
}
41+
for _, text := range messages {
42+
ter.matchVarPref(text, varPrefix)
43+
}
44+
if len(ter.Variables) != 1 {
45+
t.Errorf("Should return one variable. but returned %d", len(ter.Variables))
46+
}
47+
2748
}

0 commit comments

Comments
 (0)