Skip to content

Commit

Permalink
Added few tests Terraform struct
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrst88 committed May 9, 2018
1 parent c940233 commit ac2b870
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
11 changes: 11 additions & 0 deletions configuration.mock
@@ -0,0 +1,11 @@
//Mock Terraform Configuration File
resource "aws_eip" "nat" {
vpc = true
count = "${length(var.public_subnets)}"
}

resource "aws_nat_gateway" "nat" {
allocation_id = "${element(aws_eip.nat.*.id, count.index)}"
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
count = "${length(var.public_subnets)}"
}
5 changes: 2 additions & 3 deletions file_utils.go
Expand Up @@ -12,12 +12,11 @@ func getAllFiles(ext string) ([]string, error) {
checkError(err)
var files []string
log.Infof("Finding files in %q directory", dir)
files, err = filepath.Glob(tfFileExt)
files, err = filepath.Glob(ext)
checkError(err)

if len(files) == 0 {
log.Infof("No files with .tf extensions found in %q", dir)
os.Exit(0)
log.Infof("No files with %q extensions found in %q", ext, dir)
}
return files, nil
}
Expand Down
4 changes: 4 additions & 0 deletions generator.go
Expand Up @@ -42,6 +42,10 @@ func main() {
}

tfFiles, err := getAllFiles(tfFileExt)
if len(tfFiles) == 0 {
log.Warn("No terraform files to proceed, exiting")
os.Exit(0)
}
checkError(err)
var wg sync.WaitGroup
messages := make(chan string)
Expand Down
33 changes: 27 additions & 6 deletions generator_test.go
@@ -1,11 +1,13 @@
package main

import (
"bufio"
"os"
"testing"
)

var mockFile = "mock.tf"
var mockFile = "tf_configuration.mock"
var testExtFile = "*.mock"

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

func TestGetAllFiles(t *testing.T) {
var file, err = os.Create(mockFile)
checkError(err)
defer file.Close()
files, err := getAllFiles(tfFileExt)
files, err := getAllFiles(testExtFile)
checkError(err)
if len(files) == 0 {
t.Error("Should found at least one file")
}
defer os.Remove(mockFile)
}

func TestMatchVariable(t *testing.T) {
ter := &terraformVars{}
var messages []string

file, err := getAllFiles(testExtFile)
checkError(err)

fileHandle, _ := os.Open(file[0])
defer fileHandle.Close()

fileScanner := bufio.NewScanner(fileHandle)
for fileScanner.Scan() {
messages = append(messages, fileScanner.Text())
}
for _, text := range messages {
ter.matchVarPref(text, varPrefix)
}
if len(ter.Variables) != 1 {
t.Errorf("Should return one variable. but returned %d", len(ter.Variables))
}

}

0 comments on commit ac2b870

Please sign in to comment.