Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring resreader test coverage above 80% #13

Merged
merged 1 commit into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ repos:
- id: go-cyclo
args: [-over=15]
- id: go-critic
- id: go-unit-tests
- id: go-build
- id: go-mod-tidy
5 changes: 2 additions & 3 deletions pkg/resreader/metareader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package resreader

import (
"log"
"fmt"
)

// MetaReader implements ResReader for meta resources
Expand All @@ -26,6 +26,5 @@ type MetaReader struct {

// GetInfo reades ResourceInfo for a meta resource
func (r MetaReader) GetInfo(source string) (ResourceInfo, error) {
log.Fatal("Meta GetInfo not implemented")
return ResourceInfo{}, nil
return ResourceInfo{}, fmt.Errorf("Meta GetInfo not implemented: %s", source)
}
83 changes: 75 additions & 8 deletions pkg/resreader/resreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ output "test_output" {
`
)

var tmpResourceDir string
var (
tmpResourceDir string
terraformDir string
packerDir string
)

// Setup GoCheck
type MySuite struct{}
Expand Down Expand Up @@ -86,13 +90,13 @@ func (s *MySuite) TestGetHCLInfo(c *C) {
expectedErr := "Source to resource does not exist: .*"
c.Assert(err, ErrorMatches, expectedErr)
// Invalid source path - points to a file
pathToFile := path.Join(tmpResourceDir, "main.tf")
pathToFile := path.Join(terraformDir, "main.tf")
_, err = getHCLInfo(pathToFile)
expectedErr = "Source of resource must be a directory: .*"
c.Assert(err, ErrorMatches, expectedErr)

// Invalid source path - points to directory with no .tf files
pathToEmptyDir := path.Join(tmpResourceDir, "emptyDir")
pathToEmptyDir := path.Join(packerDir, "emptyDir")
err = os.Mkdir(pathToEmptyDir, 0755)
if err != nil {
log.Fatal("TestGetHCLInfo: Failed to create test directory.")
Expand All @@ -111,7 +115,16 @@ func createTmpResource() {
log.Fatalf(
"Failed to create temp dir for resource in resreader_test, %v", err)
}
mainFile, err := os.Create(path.Join(tmpResourceDir, "main.tf"))

// Create terraform resource dir
terraformDir = path.Join(tmpResourceDir, "terraformResource")
err = os.Mkdir(terraformDir, 0755)
if err != nil {
log.Fatalf("error creating test terraform resource dir: %e", err)
}

// main.tf file
mainFile, err := os.Create(path.Join(terraformDir, "main.tf"))
if err != nil {
log.Fatalf("Failed to create main.tf: %v", err)
}
Expand All @@ -120,7 +133,8 @@ func createTmpResource() {
log.Fatalf("resreader_test: Failed to write main.tf test file. %v", err)
}

varFile, err := os.Create(path.Join(tmpResourceDir, "variables.tf"))
// variables.tf file
varFile, err := os.Create(path.Join(terraformDir, "variables.tf"))
if err != nil {
log.Fatalf("Failed to create variables.tf: %v", err)
}
Expand All @@ -130,14 +144,43 @@ func createTmpResource() {
"resreader_test: Failed to write variables.tf test file. %v", err)
}

outFile, err := os.Create(path.Join(tmpResourceDir, "outputs.tf"))
// outputs.tf file
outFile, err := os.Create(path.Join(terraformDir, "outputs.tf"))
if err != nil {
log.Fatalf("Failed to create outputs.tf: %v", err)
}
_, err = outFile.WriteString(testOutputsTf)
if err != nil {
log.Fatalf("resreader_test: Failed to write outputs.tf test file. %v", err)
}

// Create packer resource dir
packerDir = path.Join(tmpResourceDir, "packerResource")
err = os.Mkdir(packerDir, 0755)
if err != nil {
log.Fatalf("error creating test packer resource dir: %e", err)
}

// main.pkr.hcl file
mainFile, err = os.Create(path.Join(packerDir, "main.pkr.hcl"))
if err != nil {
log.Fatalf("Failed to create main.pkr.hcl: %v", err)
}
_, err = mainFile.WriteString(testMainTf)
if err != nil {
log.Fatalf("resreader_test: Failed to write main.tf test file. %v", err)
}

// variables.pkr.hcl file
varFile, err = os.Create(path.Join(packerDir, "variables.pkr.hcl"))
if err != nil {
log.Fatalf("Failed to create variables.tf: %v", err)
}
_, err = varFile.WriteString(testVariablesTf)
if err != nil {
log.Fatalf(
"resreader_test: Failed to write variables.pkr.hcl test file. %v", err)
}
}

func teardownTmpResource() {
Expand All @@ -149,14 +192,38 @@ func teardownTmpResource() {
}
}

func (s *MySuite) TestTFGetInfo(c *C) {
func (s *MySuite) TestGetInfo_TFWriter(c *C) {
reader := TFReader{allResInfo: make(map[string]ResourceInfo)}
resourceInfo, err := reader.GetInfo(tmpResourceDir)
resourceInfo, err := reader.GetInfo(terraformDir)
c.Assert(err, IsNil)
c.Assert(resourceInfo.Inputs[0].Name, Equals, "test_variable")
c.Assert(resourceInfo.Outputs[0].Name, Equals, "test_output")
}

// packerreader.go
func (s *MySuite) TestGetInfo_PackerReader(c *C) {
// Didn't already exist, succeeds
reader := PackerReader{allResInfo: make(map[string]ResourceInfo)}
resourceInfo, err := reader.GetInfo(packerDir)
c.Assert(err, IsNil)
c.Assert(resourceInfo.Inputs[0].Name, Equals, "test_variable")

// Already exists, succeeds
existingResourceInfo, err := reader.GetInfo(packerDir)
c.Assert(err, IsNil)
c.Assert(
existingResourceInfo.Inputs[0].Name, Equals, resourceInfo.Inputs[0].Name)
}

// metareader.go
func (s *MySuite) TestGetInfo_MetaReader(c *C) {
// Not implemented, expect that error
reader := MetaReader{}
_, err := reader.GetInfo("")
expErr := "Meta GetInfo not implemented: .*"
c.Assert(err, ErrorMatches, expErr)
}

func TestMain(m *testing.M) {
createTmpResource()
code := m.Run()
Expand Down