This commit is contained in:
Christopher Boumenot 2017-10-13 10:28:10 -07:00
Родитель eb7d88741a
Коммит e2fe70a2b2
5 изменённых файлов: 1 добавлений и 134 удалений

Просмотреть файл

@ -117,22 +117,6 @@ teardown(){
[[ "$diff" == *"A /b.txt"* ]] # created by script.sh [[ "$diff" == *"A /b.txt"* ]] # created by script.sh
} }
@test "handler command: enable - migrates the old data directory" {
mk_container sh -c "OLD=/var/lib/azure/custom-script; \
mkdir -p \$OLD && \
touch \$OLD/sentinel.txt && \
fake-waagent install && \
fake-waagent enable && wait-for-enable"
# download an external script and run it
push_settings '{"commandToExecute":"date"}'
run start_container
echo "$output"
diff="$(container_diff)"; echo "$diff"
[[ "$diff" != *"A /var/lib/azure/custom-script"* ]] # should have gone
[[ "$diff" == *"A /var/lib/waagent/custom-script/sentinel.txt"* ]] # should be migrated
}
@test "handler command: enable - download files from storage account" { @test "handler command: enable - download files from storage account" {
if [[ -z "$AZURE_STORAGE_ACCOUNT" ]] || [[ -z "$AZURE_STORAGE_ACCESS_KEY" ]]; then if [[ -z "$AZURE_STORAGE_ACCOUNT" ]] || [[ -z "$AZURE_STORAGE_ACCESS_KEY" ]]; then
skip "AZURE_STORAGE_ACCOUNT or AZURE_STORAGE_ACCESS_KEY not specified" skip "AZURE_STORAGE_ACCOUNT or AZURE_STORAGE_ACCESS_KEY not specified"

Просмотреть файл

@ -68,13 +68,6 @@ func uninstall(ctx *log.Context, h vmextension.HandlerEnvironment, seqNum int) (
} }
func enablePre(ctx *log.Context, seqNum int) error { func enablePre(ctx *log.Context, seqNum int) error {
// for a few versions we need to migrate dataDirOld (introduced in v2.0.0) to
// dataDir (introduced in v2.0.1).
ctx.Log("message", "checking for state migration")
if err := migrateDataDir(ctx, dataDirOld, dataDir); err != nil {
return errors.Wrap(err, "state directory could not be migrated")
}
// exit if this sequence number (a snapshot of the configuration) is alrady // exit if this sequence number (a snapshot of the configuration) is alrady
// processed. if not, save this sequence number before proceeding. // processed. if not, save this sequence number before proceeding.
seqNumPath := filepath.Join(dataDir, seqNumFile) seqNumPath := filepath.Join(dataDir, seqNumFile)

Просмотреть файл

@ -13,8 +13,7 @@ import (
var ( var (
// dataDir is where we store the downloaded files, logs and state for // dataDir is where we store the downloaded files, logs and state for
// the extension handler // the extension handler
dataDir = "/var/lib/waagent/custom-script" dataDir = "/var/lib/waagent/custom-script"
dataDirOld = "/var/lib/azure/custom-script" // used for migration, if present
// seqNumFile holds the processed highest sequence number to make // seqNumFile holds the processed highest sequence number to make
// sure we do not run the command more than once for the same sequence // sure we do not run the command more than once for the same sequence

Просмотреть файл

@ -1,54 +0,0 @@
package main
import (
"bytes"
"fmt"
"os"
"github.com/go-kit/kit/log"
"github.com/pkg/errors"
)
// migrateDataDir moves oldDir to newDir, if oldDir exists by shelling out to
// 'mv -f'.
func migrateDataDir(ctx log.Logger, oldDir, newDir string) error {
ok, err := dirExists(oldDir)
if err != nil {
return errors.Wrap(err, "could not check old directory")
}
if !ok { // no need for migration
ctx.Log("message", "no old state found to migrate")
return nil
}
ctx.Log("message", "migrating old state")
var b bytes.Buffer
var bc = bufferCloser{&b}
stdout, stderr := bc, bc
if exitCode, err := Exec(fmt.Sprintf(`mv -f '%s'/* '%s'`, oldDir, newDir), "", stdout, stderr); err != nil {
output := string(b.Bytes())
return errors.Wrapf(err, "failed to migrate with mv, exit status: %d, output: %q", exitCode, output)
}
if err := os.RemoveAll(oldDir); err != nil {
return errors.Wrapf(err, "failed to delete old state directory %q", oldDir)
}
ctx.Log("message", "migrated old state with mv")
return nil
}
func dirExists(path string) (bool, error) {
s, err := os.Stat(path)
if err == nil {
return s.IsDir(), nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, errors.Wrap(err, "cannot check if directory exists")
}
type bufferCloser struct {
*bytes.Buffer
}
func (b bufferCloser) Close() error { return nil }

Просмотреть файл

@ -1,55 +0,0 @@
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/go-kit/kit/log"
"github.com/stretchr/testify/require"
)
func Test_dirExists(t *testing.T) {
ok, err := dirExists("/non-existing")
require.Nil(t, err)
require.False(t, ok)
d := tempDir(t)
defer os.RemoveAll(d)
ok, err = dirExists(d)
require.Nil(t, err)
require.True(t, ok)
}
func tempDir(t *testing.T) string {
d, err := ioutil.TempDir("", "")
require.Nil(t, err, "failed to create test dir")
return d
}
func Test_migrateDataDir_noPriorData(t *testing.T) {
err := migrateDataDir(log.NewNopLogger(), "/non-existing", "/tmp/foo")
require.Nil(t, err)
}
func Test_migrateDataDir(t *testing.T) {
d1, err := ioutil.TempDir("", "old")
defer os.RemoveAll(d1)
d2, err := ioutil.TempDir("", "new")
defer os.RemoveAll(d2)
require.Nil(t, ioutil.WriteFile(filepath.Join(d1, "hello.txt"), []byte("hello"), 0644))
require.Nil(t, migrateDataDir(log.NewNopLogger(), d1, d2))
f, err := os.Stat(filepath.Join(d1, "hello.txt"))
require.NotNil(t, err, "old file should have moved: %#v", f)
_, err = os.Stat(filepath.Join(d2, "hello.txt"))
require.Nil(t, err, "new file should be there")
ok, err := dirExists(d1)
require.Nil(t, err)
require.False(t, ok, "old directory must be gone")
}