
This commit adds the utility testing function TempDir, which provides a tester with a temporary directory as well as a means of cleaning up that directory. The new function is implemented everywhere that makes sense throughout the code base. This also cleans up the directories left behind by go-git's testing fixtures. Some light refactoring was also performed in this change. Change-Id: I754484934660487140f57671bacb5463cf669e3e
116 lines
3.1 KiB
Go
116 lines
3.1 KiB
Go
package pull
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
fixtures "gopkg.in/src-d/go-git-fixtures.v3"
|
|
|
|
repo2 "opendev.org/airship/airshipctl/pkg/document/repo"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"opendev.org/airship/airshipctl/pkg/config"
|
|
"opendev.org/airship/airshipctl/pkg/environment"
|
|
"opendev.org/airship/airshipctl/testutil"
|
|
)
|
|
|
|
func getDummyPullSettings() *Settings {
|
|
mockPullSettings := &Settings{
|
|
AirshipCTLSettings: new(environment.AirshipCTLSettings),
|
|
}
|
|
mockConf := config.DummyConfig()
|
|
mockPullSettings.AirshipCTLSettings.SetConfig(mockConf)
|
|
return mockPullSettings
|
|
}
|
|
|
|
func TestPull(t *testing.T) {
|
|
require := require.New(t)
|
|
assert := assert.New(t)
|
|
|
|
t.Run("cloneRepositories", func(t *testing.T) {
|
|
dummyPullSettings := getDummyPullSettings()
|
|
currentManifest, err := dummyPullSettings.Config().CurrentContextManifest()
|
|
require.NoError(err)
|
|
|
|
err = fixtures.Init()
|
|
require.NoError(err)
|
|
fx := fixtures.Basic().One()
|
|
|
|
dummyGitDir := fx.DotGit().Root()
|
|
currentManifest.Repository = &config.Repository{
|
|
URLString: dummyGitDir,
|
|
CheckoutOptions: &config.RepoCheckout{
|
|
Branch: "master",
|
|
ForceCheckout: false,
|
|
},
|
|
Auth: &config.RepoAuth{
|
|
Type: "http-basic",
|
|
},
|
|
}
|
|
|
|
tmpDir, cleanup := testutil.TempDir(t, "airshipctlPullTest-")
|
|
defer cleanup(t)
|
|
|
|
currentManifest.TargetPath = tmpDir
|
|
|
|
_, err = repo2.NewRepository(".", currentManifest.Repository)
|
|
require.NoError(err)
|
|
|
|
err = dummyPullSettings.cloneRepositories()
|
|
|
|
require.NoError(err)
|
|
dummyRepoDirName := filepath.Base(dummyGitDir)
|
|
assert.FileExists(path.Join(tmpDir, dummyRepoDirName, "go/example.go"))
|
|
assert.FileExists(path.Join(tmpDir, dummyRepoDirName, ".git/HEAD"))
|
|
contents, err := ioutil.ReadFile(path.Join(tmpDir, dummyRepoDirName, ".git/HEAD"))
|
|
require.NoError(err)
|
|
assert.Equal("ref: refs/heads/master", strings.TrimRight(string(contents), "\t \n"))
|
|
})
|
|
|
|
t.Run("Pull", func(t *testing.T) {
|
|
dummyPullSettings := getDummyPullSettings()
|
|
conf := dummyPullSettings.AirshipCTLSettings.Config()
|
|
|
|
err := fixtures.Init()
|
|
require.NoError(err)
|
|
fx := fixtures.Basic().One()
|
|
|
|
mfst := conf.Manifests["dummy_manifest"]
|
|
dummyGitDir := fx.DotGit().Root()
|
|
mfst.Repository = &config.Repository{
|
|
URLString: dummyGitDir,
|
|
CheckoutOptions: &config.RepoCheckout{
|
|
Branch: "master",
|
|
ForceCheckout: false,
|
|
},
|
|
Auth: &config.RepoAuth{
|
|
Type: "http-basic",
|
|
},
|
|
}
|
|
dummyPullSettings.SetConfig(conf)
|
|
|
|
tmpDir, cleanup := testutil.TempDir(t, "airshipctlPullTest-")
|
|
defer cleanup(t)
|
|
|
|
mfst.TargetPath = tmpDir
|
|
require.NoError(err)
|
|
|
|
err = dummyPullSettings.Pull()
|
|
require.NoError(err)
|
|
|
|
dummyRepoDirName := filepath.Base(dummyGitDir)
|
|
assert.FileExists(path.Join(tmpDir, dummyRepoDirName, "go/example.go"))
|
|
assert.FileExists(path.Join(tmpDir, dummyRepoDirName, ".git/HEAD"))
|
|
contents, err := ioutil.ReadFile(path.Join(tmpDir, dummyRepoDirName, ".git/HEAD"))
|
|
require.NoError(err)
|
|
assert.Equal("ref: refs/heads/master", strings.TrimRight(string(contents), "\t \n"))
|
|
})
|
|
|
|
testutil.CleanUpGitFixtures(t)
|
|
}
|