github.com/craigmonson/colonize@v0.1.1-alpha.0.20170808202020-04bf903fb1ea/prep/prep_test.go (about)

     1  package prep_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  
     7  	"github.com/craigmonson/colonize/config"
     8  	"github.com/craigmonson/colonize/log_mock"
     9  	. "github.com/craigmonson/colonize/prep"
    10  	"github.com/craigmonson/colonize/util_mock"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("Prep", func() {
    17  	var conf *config.Config
    18  	var err error
    19  
    20  	BeforeEach(func() {
    21  		conf, err = config.LoadConfigInTree("../test/vpc", "dev")
    22  	})
    23  	AfterEach(func() {
    24  		// remove files.
    25  		_ = os.Remove(conf.CombinedValsFilePath)
    26  		_ = os.Remove(conf.CombinedVarsFilePath)
    27  		_ = os.Remove(conf.CombinedTfFilePath)
    28  		_ = os.Remove(conf.CombinedDerivedVarsFilePath)
    29  		_ = os.Remove(conf.CombinedDerivedValsFilePath)
    30  		_ = os.Remove(conf.CombinedRemoteFilePath)
    31  	})
    32  
    33  	Describe("Run", func() {
    34  		var mLog *log_mock.MockLog
    35  		var err error
    36  
    37  		BeforeEach(func() {
    38  			mLog = &log_mock.MockLog{}
    39  			util_mock.MCmd = &util_mock.MockCmd{}
    40  			util_mock.MockTheCommand()
    41  
    42  			err = Run(conf, mLog, nil)
    43  		})
    44  
    45  		AfterEach(func() {
    46  			util_mock.ResetTheCommand()
    47  		})
    48  
    49  		It("should not raise an error", func() {
    50  			Ω(err).ToNot(HaveOccurred())
    51  		})
    52  
    53  		It("should log the steps", func() {
    54  			expected := `
    55  Removing .terraform directory...
    56  Building combined variables files...
    57  Building combined terraform files...
    58  Building combined derived files...
    59  Building remote config script...
    60  Fetching terraform modules...`
    61  			Ω(mLog.Output).To(Equal(expected))
    62  		})
    63  
    64  		It("should have terraform get modules", func() {
    65  			Ω(util_mock.MCmd.Cmd).To(Equal("\nterraform get -update"))
    66  		})
    67  
    68  		It("should run the exec.Run command", func() {
    69  			Ω(util_mock.MCmd.CallCount).To(Equal(1))
    70  		})
    71  
    72  	})
    73  
    74  	Describe("BuildCombinedValsFile", func() {
    75  		BeforeEach(func() {
    76  			err = BuildCombinedValsFile(conf)
    77  		})
    78  
    79  		It("should create the combined val file with proper permissions", func() {
    80  			Ω(conf.CombinedValsFilePath).To(BeARegularFile())
    81  			stat, _ := os.Stat(conf.CombinedValsFilePath)
    82  			Ω(int(stat.Mode())).To(Equal(0644))
    83  		})
    84  
    85  		It("should create the combined var file with proper permissions", func() {
    86  			Ω(conf.CombinedVarsFilePath).To(BeARegularFile())
    87  			stat, _ := os.Stat(conf.CombinedVarsFilePath)
    88  			Ω(int(stat.Mode())).To(Equal(0644))
    89  		})
    90  
    91  		It("should have the right variable contents", func() {
    92  			contents, _ := ioutil.ReadFile(conf.CombinedVarsFilePath)
    93  			checks := []string{
    94  				`variable "root_path" {}`,
    95  				`variable "root_var" {}`,
    96  				`variable "vpc_var" {}`,
    97  				`variable "environment" {}`,
    98  				`variable "origin_path" {}`,
    99  				`variable "tmpl_name" {}`,
   100  				`variable "tmpl_path_dashed" {}`,
   101  				`variable "tmpl_path_underscored" {}`,
   102  			}
   103  			for _, expected := range checks {
   104  				Ω(string(contents)).To(MatchRegexp(expected))
   105  			}
   106  		})
   107  		It("should have the right values contents", func() {
   108  			contents, _ := ioutil.ReadFile(conf.CombinedValsFilePath)
   109  			expected := `environment = "dev"
   110  origin_path = "../test/vpc"
   111  tmpl_name = "vpc"
   112  tmpl_path_dashed = "vpc"
   113  tmpl_path_underscored = "vpc"
   114  root_path = "../test"
   115  root_var = "dev_root_var"
   116  vpc_var = "dev_vpc_var"
   117  `
   118  			Ω(string(contents)).To(Equal(expected))
   119  		})
   120  		It("should not have wrong environment values contents", func() {
   121  			contents, _ := ioutil.ReadFile(conf.CombinedValsFilePath)
   122  			Ω(string(contents)).ToNot(MatchRegexp(`dev_foo_vpc_var`))
   123  		})
   124  
   125  	})
   126  
   127  	Describe("BuildCombinedTfFile", func() {
   128  		BeforeEach(func() {
   129  			err = BuildCombinedTfFile(conf)
   130  		})
   131  
   132  		It("should create the combined file with proper permissions", func() {
   133  			Ω(conf.CombinedTfFilePath).To(BeARegularFile())
   134  			stat, _ := os.Stat(conf.CombinedTfFilePath)
   135  			Ω(int(stat.Mode())).To(Equal(0644))
   136  		})
   137  
   138  		It("should have the right contents", func() {
   139  			contents, _ := ioutil.ReadFile(conf.CombinedTfFilePath)
   140  			expected := "provider \"aws\" {}\nvariable \"vpc_tf_test\" {}\n# test env file\n# test base file\n"
   141  			Ω(string(contents)).To(Equal(expected))
   142  		})
   143  
   144  		It("should NOT have the wrong contents", func() {
   145  			contents, _ := ioutil.ReadFile(conf.CombinedTfFilePath)
   146  			Ω(string(contents)).ToNot(Equal(`# test env foo file`))
   147  		})
   148  	})
   149  
   150  	Describe("BuildCombinedDerivedFiles", func() {
   151  		BeforeEach(func() {
   152  			// reads in vals file in order to do substitutions
   153  			err = BuildCombinedValsFile(conf)
   154  			err = BuildCombinedDerivedFiles(conf)
   155  		})
   156  
   157  		It("should create the combined val file with proper permissions", func() {
   158  			Ω(conf.CombinedDerivedValsFilePath).To(BeARegularFile())
   159  			stat, _ := os.Stat(conf.CombinedDerivedValsFilePath)
   160  			Ω(int(stat.Mode())).To(Equal(0644))
   161  		})
   162  
   163  		It("should create the combined var file with proper permissions", func() {
   164  			Ω(conf.CombinedDerivedVarsFilePath).To(BeARegularFile())
   165  			stat, _ := os.Stat(conf.CombinedDerivedVarsFilePath)
   166  			Ω(int(stat.Mode())).To(Equal(0644))
   167  		})
   168  
   169  		It("should have the right val contents", func() {
   170  			contents, _ := ioutil.ReadFile(conf.CombinedDerivedValsFilePath)
   171  			expected := "test_derived = \"foo-dev-bar-vpc\"\n"
   172  			Ω(string(contents)).To(Equal(expected))
   173  		})
   174  
   175  		It("should have the right var contents", func() {
   176  			contents, _ := ioutil.ReadFile(conf.CombinedDerivedVarsFilePath)
   177  			expected := "variable \"test_derived\" {}\n"
   178  			Ω(string(contents)).To(Equal(expected))
   179  		})
   180  	})
   181  
   182  	Describe("BuildRemoteFile", func() {
   183  		BeforeEach(func() {
   184  			// reads in vals and derived files in order to do substitutions
   185  			err = BuildCombinedValsFile(conf)
   186  			err = BuildCombinedDerivedFiles(conf)
   187  			err = BuildRemoteFile(conf)
   188  		})
   189  
   190  		It("should create the executable combined remote shell script", func() {
   191  			Ω(conf.CombinedRemoteFilePath).To(BeARegularFile())
   192  			stat, _ := os.Stat(conf.CombinedRemoteFilePath)
   193  			Ω(int(stat.Mode())).To(Equal(0755))
   194  		})
   195  
   196  		It("should have the right contents", func() {
   197  			contents, _ := ioutil.ReadFile(conf.CombinedRemoteFilePath)
   198  			expected := `terraform remote config \
   199  -backend=s3 \
   200  -backend-config="bucket=dev" \
   201  -backend-config="region=dev_root_var" \
   202  -backend-config="key=foo-dev-bar-vpc"
   203  `
   204  			Ω(string(contents)).To(Equal(expected))
   205  		})
   206  	})
   207  })