diff --git a/pkg/engine/engine_chef.go b/pkg/engine/engine_chef.go index 9e20943..814180c 100644 --- a/pkg/engine/engine_chef.go +++ b/pkg/engine/engine_chef.go @@ -12,6 +12,7 @@ import ( "os" "os/exec" "path" + "log" ) type chefMetadata struct { @@ -110,6 +111,7 @@ func (g *engineChef) AssembleStep() error { gitignorePath := path.Join(g.PipelineData.GitLocalPath, ".gitignore") if !utils.FileExists(gitignorePath) { if err := utils.GitGenerateGitIgnore(g.PipelineData.GitLocalPath, "ChefCookbook"); err != nil { + log.Print("Generate error") return err } } diff --git a/pkg/engine/engine_chef_test.go b/pkg/engine/engine_chef_test.go index fc9fe43..d1324bd 100644 --- a/pkg/engine/engine_chef_test.go +++ b/pkg/engine/engine_chef_test.go @@ -9,13 +9,18 @@ import ( "capsulecd/pkg/scm" "capsulecd/pkg/utils" "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + "github.com/golang/mock/gomock" "io/ioutil" - "os" "path" - "path/filepath" + //"path/filepath" "testing" + "capsulecd/pkg/config/mock" + "capsulecd/pkg/scm/mock" + "os" ) + func TestEngineChef_Create(t *testing.T) { //setup testConfig, err := config.Create() @@ -38,224 +43,284 @@ func TestEngineChef_Create(t *testing.T) { require.Equal(t, "Other", testConfig.GetString("chef_supermarket_type"), "should load engine defaults") } -func TestEngineChef_AssembleStep(t *testing.T) { - //setup - parentPath, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(parentPath) - dirPath := path.Join(parentPath, "cookbook_analogj_test") - cerr := utils.CopyDir(path.Join("testdata", "chef", "cookbook_analogj_test"), dirPath) - require.NoError(t, cerr) +// Define the suite, and absorb the built-in basic suite +// functionality from testify - including a T() method which +// returns the current testing context +type EngineChefTestSuite struct { + suite.Suite + MockCtrl *gomock.Controller + Scm *mock_scm.MockInterface + Config *mock_config.MockInterface + PipelineData *pipeline.Data +} - pipelineData := new(pipeline.Data) - pipelineData.GitParentPath = parentPath - pipelineData.GitLocalPath = dirPath - testConfig, _ := config.Create() - testConfig.Set("scm", "github") - testConfig.Set("package_type", "chef") - testConfig.Set("scm_github_access_token","placeholder") +// Make sure that VariableThatShouldStartAtFive is set to five +// before each test +func (suite *EngineChefTestSuite) SetupTest() { + suite.MockCtrl = gomock.NewController(suite.T()) - githubScm, err := scm.Create("github", pipelineData, testConfig, nil) - require.NoError(t, err) + suite.PipelineData = new(pipeline.Data) - chefEngine, err := engine.Create("chef", pipelineData, testConfig, githubScm) - require.NoError(t, err) + suite.Config = mock_config.NewMockInterface(suite.MockCtrl) + suite.Scm = mock_scm.NewMockInterface(suite.MockCtrl) - //test - berr := chefEngine.AssembleStep() - require.NoError(t, berr) +} - //assert - require.True(t, utils.FileExists(path.Join(dirPath, "RakeFile"))) - require.True(t, utils.FileExists(path.Join(dirPath, "Berksfile"))) - require.True(t, utils.FileExists(path.Join(dirPath, ".gitignore"))) - require.True(t, utils.FileExists(path.Join(dirPath, "Gemfile"))) +func (suite *EngineChefTestSuite) TearDownTest() { + suite.MockCtrl.Finish() +} + +// In order for 'go test' to run this suite, we need to create +// a normal test function and pass our suite to suite.Run +func TestEngineChef_TestSuite(t *testing.T) { + suite.Run(t, new(EngineChefTestSuite)) } -func TestEngineChef_AssembleStep_WithMinimalCookbook(t *testing.T) { +func (suite *EngineChefTestSuite)TestEngineChef_AssembleStep() { //setup + suite.Config.EXPECT().SetDefault(gomock.Any(),gomock.Any()).MinTimes(1) + suite.Config.EXPECT().GetString("engine_version_bump_type").Return("patch") + + //copy cookbook fixture into a temp directory. parentPath, err := ioutil.TempDir("", "") - require.NoError(t, err) defer os.RemoveAll(parentPath) - dirPath := path.Join(parentPath, "cookbook_analogj_test") - - cerr := utils.CopyDir(path.Join("testdata", "chef", "minimal_cookbook_analogj_test"), dirPath) - require.NoError(t, cerr) - - pipelineData := new(pipeline.Data) - pipelineData.GitParentPath = parentPath - pipelineData.GitLocalPath = dirPath - testConfig, _ := config.Create() - testConfig.Set("scm", "github") - testConfig.Set("package_type", "chef") - testConfig.Set("scm_github_access_token","placeholder") - - githubScm, err := scm.Create("github", pipelineData, testConfig, nil) - require.NoError(t, err) + suite.PipelineData.GitParentPath = parentPath + suite.PipelineData.GitLocalPath = path.Join(parentPath, "cookbook_analogj_test") + cerr := utils.CopyDir(path.Join("testdata", "chef", "cookbook_analogj_test"), suite.PipelineData.GitLocalPath ) + require.NoError(suite.T(), cerr) - chefEngine, err := engine.Create("chef", pipelineData, testConfig, githubScm) - require.NoError(t, err) + chefEngine, err := engine.Create("chef", suite.PipelineData, suite.Config, suite.Scm) + require.NoError(suite.T(), err) //test berr := chefEngine.AssembleStep() - require.NoError(t, berr) + require.NoError(suite.T(), berr) //assert - require.True(t, utils.FileExists(path.Join(dirPath, "RakeFile")), "should generate recommended files" ) - require.True(t, utils.FileExists(path.Join(dirPath, "Berksfile")), "should generate recommended files" ) - require.True(t, utils.FileExists(path.Join(dirPath, ".gitignore")), "should generate recommended files" ) - require.True(t, utils.FileExists(path.Join(dirPath, "Gemfile")), "should generate recommended files" ) + require.True(suite.T(), utils.FileExists(path.Join(suite.PipelineData.GitLocalPath, "RakeFile"))) + require.True(suite.T(), utils.FileExists(path.Join(suite.PipelineData.GitLocalPath, "Berksfile"))) + require.True(suite.T(), utils.FileExists(path.Join(suite.PipelineData.GitLocalPath, ".gitignore"))) + require.True(suite.T(), utils.FileExists(path.Join(suite.PipelineData.GitLocalPath, "Gemfile"))) } -func TestEngineChef_AssembleStep_WithoutMetadata(t *testing.T) { +func (suite *EngineChefTestSuite)TestEngineChef_AssembleStep_WithMinimalCookbook() { //setup + suite.Config.EXPECT().SetDefault(gomock.Any(),gomock.Any()).MinTimes(1) + suite.Config.EXPECT().GetString("engine_version_bump_type").Return("patch") + + //copy cookbook fixture into a temp directory. parentPath, err := ioutil.TempDir("", "") - require.NoError(t, err) + require.NoError(suite.T(), err) defer os.RemoveAll(parentPath) - dirPath := path.Join(parentPath, "cookbook_analogj_test") + suite.PipelineData.GitParentPath = parentPath + suite.PipelineData.GitLocalPath = path.Join(parentPath, "cookbook_analogj_test") + cerr := utils.CopyDir(path.Join("testdata", "chef", "minimal_cookbook_analogj_test"), suite.PipelineData.GitLocalPath ) + require.NoError(suite.T(), cerr) - cerr := utils.CopyDir(path.Join("testdata", "chef", "cookbook_analogj_test"), dirPath) - require.NoError(t, cerr) + chefEngine, err := engine.Create("chef", suite.PipelineData, suite.Config, suite.Scm) + require.NoError(suite.T(), err) - os.Remove(path.Join(dirPath, "metadata.rb")) + //test + berr := chefEngine.AssembleStep() + require.NoError(suite.T(), berr) - pipelineData := new(pipeline.Data) - absPath, aerr := filepath.Abs(dirPath) - require.NoError(t, aerr) + //assert + require.True(suite.T(), utils.FileExists(path.Join(suite.PipelineData.GitLocalPath, "RakeFile"))) + require.True(suite.T(), utils.FileExists(path.Join(suite.PipelineData.GitLocalPath, "Berksfile"))) + require.True(suite.T(), utils.FileExists(path.Join(suite.PipelineData.GitLocalPath, ".gitignore"))) + require.True(suite.T(), utils.FileExists(path.Join(suite.PipelineData.GitLocalPath, "Gemfile"))) +} - pipelineData.GitLocalPath = absPath - testConfig, _ := config.Create() - testConfig.Set("scm", "github") - testConfig.Set("package_type", "chef") - testConfig.Set("scm_github_access_token","placeholder") +func (suite *EngineChefTestSuite)TestEngineChef_AssembleStep_WithoutMetadata() { + //setup + suite.Config.EXPECT().SetDefault(gomock.Any(),gomock.Any()).MinTimes(1) - githubScm, err := scm.Create("github", pipelineData, testConfig, nil) - require.NoError(t, err) + //copy cookbook fixture into a temp directory. + parentPath, err := ioutil.TempDir("", "") + require.NoError(suite.T(), err) + defer os.RemoveAll(parentPath) + suite.PipelineData.GitParentPath = parentPath + suite.PipelineData.GitLocalPath = path.Join(parentPath, "cookbook_analogj_test") + cerr := utils.CopyDir(path.Join("testdata", "chef", "minimal_cookbook_analogj_test"), suite.PipelineData.GitLocalPath ) + require.NoError(suite.T(), cerr) + os.Remove(path.Join(suite.PipelineData.GitLocalPath, "metadata.rb")) - chefEngine, err := engine.Create("chef", pipelineData, testConfig, githubScm) - require.NoError(t, err) + chefEngine, err := engine.Create("chef", suite.PipelineData, suite.Config, suite.Scm) + require.NoError(suite.T(), err) //test berr := chefEngine.AssembleStep() //assert - require.Error(t, berr, "shoule return an error") - + require.Error(suite.T(), berr, "should return an error") } -func TestEngineChef_DependenciesStep(t *testing.T) { +func (suite *EngineChefTestSuite)TestEngineChef_DependenciesStep() { //setup + suite.Config.EXPECT().SetDefault(gomock.Any(),gomock.Any()).MinTimes(1) + + //copy cookbook fixture into a temp directory. parentPath, err := ioutil.TempDir("", "") - require.NoError(t, err) + require.NoError(suite.T(), err) defer os.RemoveAll(parentPath) - dirPath := path.Join(parentPath, "cookbook_analogj_test") + suite.PipelineData.GitParentPath = parentPath + suite.PipelineData.GitLocalPath = path.Join(parentPath, "cookbook_analogj_test") + cerr := utils.CopyDir(path.Join("testdata", "chef", "cookbook_analogj_test"), suite.PipelineData.GitLocalPath ) + require.NoError(suite.T(), cerr) - cerr := utils.CopyDir(path.Join("testdata", "chef", "cookbook_analogj_test"), dirPath) - require.NoError(t, cerr) + chefEngine, err := engine.Create("chef", suite.PipelineData, suite.Config, suite.Scm) + require.NoError(suite.T(), err) - pipelineData := new(pipeline.Data) - absPath, aerr := filepath.Abs(dirPath) - require.NoError(t, aerr) + //test + berr := chefEngine.DependenciesStep() - pipelineData.GitLocalPath = absPath - testConfig, _ := config.Create() - testConfig.Set("scm", "github") - testConfig.Set("package_type", "chef") - testConfig.Set("scm_github_access_token","placeholder") + //assert + require.NoError(suite.T(), berr) + require.True(suite.T(), utils.FileExists(path.Join(suite.PipelineData.GitLocalPath, "Berksfile.lock"))) + require.True(suite.T(), utils.FileExists(path.Join(suite.PipelineData.GitLocalPath, "Gemfile.lock"))) +} - githubScm, err := scm.Create("github", pipelineData, testConfig, nil) - require.NoError(t, err) +func (suite *EngineChefTestSuite)TestEngineChef_TestStep_AllDisabled() { + //setup + suite.Config.EXPECT().SetDefault(gomock.Any(),gomock.Any()).MinTimes(1) + suite.Config.EXPECT().GetBool(gomock.Any()).MinTimes(1).Return(true) - chefEngine, err := engine.Create("chef", pipelineData, testConfig, githubScm) - require.NoError(t, err) - berr := chefEngine.AssembleStep() - require.NoError(t, berr) + //copy cookbook fixture into a temp directory. + parentPath, err := ioutil.TempDir("", "") + require.NoError(suite.T(), err) + defer os.RemoveAll(parentPath) + suite.PipelineData.GitParentPath = parentPath + suite.PipelineData.GitLocalPath = path.Join(parentPath, "cookbook_analogj_test") + cerr := utils.CopyDir(path.Join("testdata", "chef", "cookbook_analogj_test"), suite.PipelineData.GitLocalPath ) + require.NoError(suite.T(), cerr) + + chefEngine, err := engine.Create("chef", suite.PipelineData, suite.Config, suite.Scm) + require.NoError(suite.T(), err) //test - terr := chefEngine.DependenciesStep() + berr := chefEngine.TestStep() //assert - require.NoError(t, terr) - + require.NoError(suite.T(), berr) } -func TestEngineChef_TestStep(t *testing.T) { +func (suite *EngineChefTestSuite)TestEngineChef_TestStep_LintFailure() { //setup + suite.Config.EXPECT().SetDefault(gomock.Any(),gomock.Any()).MinTimes(1) + suite.Config.EXPECT().GetBool(gomock.Any()).MinTimes(1).Return(false) + suite.Config.EXPECT().GetString("engine_cmd_lint").Return("exit 1") + + //copy cookbook fixture into a temp directory. parentPath, err := ioutil.TempDir("", "") - require.NoError(t, err) + require.NoError(suite.T(), err) defer os.RemoveAll(parentPath) - dirPath := path.Join(parentPath, "cookbook_analogj_test") - - cerr := utils.CopyDir(path.Join("testdata", "chef", "cookbook_analogj_test"), dirPath) - require.NoError(t, cerr) + suite.PipelineData.GitParentPath = parentPath + suite.PipelineData.GitLocalPath = path.Join(parentPath, "cookbook_analogj_test") + cerr := utils.CopyDir(path.Join("testdata", "chef", "cookbook_analogj_test"), suite.PipelineData.GitLocalPath ) + require.NoError(suite.T(), cerr) - pipelineData := new(pipeline.Data) - absPath, aerr := filepath.Abs(dirPath) - require.NoError(t, aerr) + chefEngine, err := engine.Create("chef", suite.PipelineData, suite.Config, suite.Scm) + require.NoError(suite.T(), err) - pipelineData.GitLocalPath = absPath - testConfig, _ := config.Create() - testConfig.Set("scm", "github") - testConfig.Set("package_type", "chef") - testConfig.Set("scm_github_access_token","placeholder") + //test + berr := chefEngine.TestStep() - githubScm, err := scm.Create("github", pipelineData, testConfig, nil) - require.NoError(t, err) + //assert + require.Error(suite.T(), berr) +} - chefEngine, err := engine.Create("chef", pipelineData, testConfig, githubScm) - require.NoError(t, err) +func (suite *EngineChefTestSuite)TestEngineChef_TestStep_TestFailure() { + //setup + suite.Config.EXPECT().SetDefault(gomock.Any(),gomock.Any()).MinTimes(1) + suite.Config.EXPECT().GetBool(gomock.Any()).MinTimes(1).Return(false) + suite.Config.EXPECT().GetString("engine_cmd_lint").Return("exit 0") + suite.Config.EXPECT().GetString("engine_cmd_test").Return("exit 1") - berr := chefEngine.AssembleStep() - require.NoError(t, berr) + //copy cookbook fixture into a temp directory. + parentPath, err := ioutil.TempDir("", "") + require.NoError(suite.T(), err) + defer os.RemoveAll(parentPath) + suite.PipelineData.GitParentPath = parentPath + suite.PipelineData.GitLocalPath = path.Join(parentPath, "cookbook_analogj_test") + cerr := utils.CopyDir(path.Join("testdata", "chef", "cookbook_analogj_test"), suite.PipelineData.GitLocalPath ) + require.NoError(suite.T(), cerr) - derr := chefEngine.DependenciesStep() - require.NoError(t, derr) + chefEngine, err := engine.Create("chef", suite.PipelineData, suite.Config, suite.Scm) + require.NoError(suite.T(), err) //test - terr := chefEngine.TestStep() + berr := chefEngine.TestStep() //assert - require.NoError(t, terr) + require.Error(suite.T(), berr) } - -func TestEngineChef_PackageStep(t *testing.T) { +func (suite *EngineChefTestSuite)TestEngineChef_TestStep_SecurityCheckFailure() { //setup + suite.Config.EXPECT().SetDefault(gomock.Any(),gomock.Any()).MinTimes(1) + suite.Config.EXPECT().GetBool(gomock.Any()).MinTimes(1).Return(false) + suite.Config.EXPECT().GetString("engine_cmd_lint").Return("exit 0") + suite.Config.EXPECT().GetString("engine_cmd_test").Return("exit 0") + suite.Config.EXPECT().GetString("engine_cmd_security_check").Return("exit 1") + + //copy cookbook fixture into a temp directory. parentPath, err := ioutil.TempDir("", "") - require.NoError(t, err) + require.NoError(suite.T(), err) defer os.RemoveAll(parentPath) - dirPath := path.Join(parentPath, "cookbook_analogj_test") + suite.PipelineData.GitParentPath = parentPath + suite.PipelineData.GitLocalPath = path.Join(parentPath, "cookbook_analogj_test") + cerr := utils.CopyDir(path.Join("testdata", "chef", "cookbook_analogj_test"), suite.PipelineData.GitLocalPath ) + require.NoError(suite.T(), cerr) - cerr := utils.CopyDir(path.Join("testdata", "chef", "cookbook_analogj_test"), dirPath) - require.NoError(t, cerr) + chefEngine, err := engine.Create("chef", suite.PipelineData, suite.Config, suite.Scm) + require.NoError(suite.T(), err) - pipelineData := new(pipeline.Data) - absPath, aerr := filepath.Abs(dirPath) - require.NoError(t, aerr) + //test + berr := chefEngine.TestStep() - pipelineData.GitLocalPath = absPath - testConfig, _ := config.Create() - testConfig.Set("scm", "github") - testConfig.Set("package_type", "chef") - testConfig.Set("scm_github_access_token","placeholder") + //assert + require.Error(suite.T(), berr) +} - githubScm, err := scm.Create("github", pipelineData, testConfig, nil) - require.NoError(t, err) +func (suite *EngineChefTestSuite)TestEngineChef_PackageStep_WithoutLockFiles() { + //setup + suite.Config.EXPECT().SetDefault(gomock.Any(),gomock.Any()).MinTimes(1) + suite.Config.EXPECT().GetBool("engine_package_keep_lock_file").MinTimes(1).Return(false) - chefEngine, err := engine.Create("chef", pipelineData, testConfig, githubScm) - require.NoError(t, err) + //copy cookbook fixture into a temp directory. + parentPath, err := ioutil.TempDir("", "") + require.NoError(suite.T(), err) + defer os.RemoveAll(parentPath) + suite.PipelineData.GitParentPath = parentPath + cpath, cerr := utils.GitClone(parentPath, "cookbook_analogj_test", "https://github.com/AnalogJ/cookbook_analogj_test.git") + require.NoError(suite.T(), cerr) + suite.PipelineData.GitLocalPath = cpath - berr := chefEngine.AssembleStep() - require.NoError(t, berr) + chefEngine, err := engine.Create("chef", suite.PipelineData, suite.Config, suite.Scm) + require.NoError(suite.T(), err) + + //test + berr := chefEngine.PackageStep() + + //assert + require.NoError(suite.T(), berr) + require.False(suite.T(), utils.FileExists(path.Join(suite.PipelineData.GitLocalPath, "Berksfile.lock"))) + require.False(suite.T(), utils.FileExists(path.Join(suite.PipelineData.GitLocalPath, "Gemfile.lock"))) +} + +func (suite *EngineChefTestSuite)TestEngineChef_DistStep_WithoutCredentials() { + //setup + suite.Config.EXPECT().SetDefault(gomock.Any(),gomock.Any()).MinTimes(1) + suite.Config.EXPECT().IsSet("chef_supermarket_username").MinTimes(1).Return(false) - derr := chefEngine.DependenciesStep() - require.NoError(t, derr) + chefEngine, err := engine.Create("chef", suite.PipelineData, suite.Config, suite.Scm) + require.NoError(suite.T(), err) //test - perr := chefEngine.PackageStep() + berr := chefEngine.DistStep() //assert - require.NoError(t, perr) + require.Error(suite.T(), berr) } \ No newline at end of file diff --git a/pkg/engine/testdata/chef/cookbook_analogj_test/metadata.rb b/pkg/engine/testdata/chef/cookbook_analogj_test/metadata.rb index 66357ee..fb03d5f 100644 --- a/pkg/engine/testdata/chef/cookbook_analogj_test/metadata.rb +++ b/pkg/engine/testdata/chef/cookbook_analogj_test/metadata.rb @@ -7,4 +7,3 @@ maintainer_email 'test@test.com' issues_url 'http://www.example.com' source_url 'http://www.example.com' -chef_version '>= 2.6.5' \ No newline at end of file