2016-07-22 04:08:53 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-07-26 21:52:07 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-07-22 04:08:53 +03:00
|
|
|
"testing"
|
|
|
|
|
2016-07-26 21:52:07 +03:00
|
|
|
"github.com/ahmetalpbalkan/go-httpbin"
|
|
|
|
"github.com/go-kit/kit/log"
|
2016-07-22 04:08:53 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_commandsExist(t *testing.T) {
|
|
|
|
// we expect these subcommands to be handled
|
|
|
|
expect := []string{"install", "enable", "disable", "uninstall", "update"}
|
|
|
|
for _, c := range expect {
|
|
|
|
_, ok := cmds[c]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("cmd '%s' is not handled", c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_commands_shouldReportStatus(t *testing.T) {
|
2016-07-26 21:52:07 +03:00
|
|
|
// - certain extension invocations are supposed to write 'N.status' files and some do not.
|
2016-07-22 04:08:53 +03:00
|
|
|
|
|
|
|
// these subcommands should NOT report status
|
|
|
|
require.False(t, cmds["install"].shouldReportStatus, "install should not report status")
|
|
|
|
require.False(t, cmds["uninstall"].shouldReportStatus, "uninstall should not report status")
|
|
|
|
|
|
|
|
// these subcommands SHOULD report status
|
|
|
|
require.True(t, cmds["enable"].shouldReportStatus, "enable should report status")
|
|
|
|
require.True(t, cmds["disable"].shouldReportStatus, "disable should report status")
|
|
|
|
require.True(t, cmds["update"].shouldReportStatus, "update should report status")
|
|
|
|
}
|
2016-07-26 21:52:07 +03:00
|
|
|
|
|
|
|
func Test_checkAndSaveSeqNum_fails(t *testing.T) {
|
|
|
|
// pass in invalid seqnum format
|
2016-08-03 00:55:39 +03:00
|
|
|
_, err := checkAndSaveSeqNum(log.NewNopLogger(), 0, "/non/existing/dir")
|
2016-07-26 22:21:03 +03:00
|
|
|
require.NotNil(t, err)
|
2020-01-08 00:41:01 +03:00
|
|
|
require.Contains(t, err.Error(), `failed to save sequence number`)
|
2016-07-26 21:52:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_checkAndSaveSeqNum(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "")
|
|
|
|
require.Nil(t, err)
|
|
|
|
fp := filepath.Join(dir, "seqnum")
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
nop := log.NewNopLogger()
|
|
|
|
|
|
|
|
// no sequence number, 0 comes in.
|
2016-08-03 00:55:39 +03:00
|
|
|
shouldExit, err := checkAndSaveSeqNum(nop, 0, fp)
|
2016-07-26 21:52:07 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.False(t, shouldExit)
|
|
|
|
|
|
|
|
// file=0, seq=0 comes in. (should exit)
|
2016-08-03 00:55:39 +03:00
|
|
|
shouldExit, err = checkAndSaveSeqNum(nop, 0, fp)
|
2016-07-26 21:52:07 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.True(t, shouldExit)
|
|
|
|
|
|
|
|
// file=0, seq=1 comes in.
|
2016-08-03 00:55:39 +03:00
|
|
|
shouldExit, err = checkAndSaveSeqNum(nop, 1, fp)
|
2016-07-26 21:52:07 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.False(t, shouldExit)
|
|
|
|
|
|
|
|
// file=1, seq=1 comes in. (should exit)
|
2016-08-03 00:55:39 +03:00
|
|
|
shouldExit, err = checkAndSaveSeqNum(nop, 1, fp)
|
2016-07-26 21:52:07 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.True(t, shouldExit)
|
|
|
|
|
|
|
|
// file=1, seq=0 comes in. (should exit)
|
2016-08-03 00:55:39 +03:00
|
|
|
shouldExit, err = checkAndSaveSeqNum(nop, 1, fp)
|
2016-07-26 21:52:07 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.True(t, shouldExit)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_runCmd_success(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "")
|
|
|
|
require.Nil(t, err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
require.Nil(t, runCmd(log.NewNopLogger(), dir, handlerSettings{
|
|
|
|
publicSettings: publicSettings{CommandToExecute: "date"},
|
|
|
|
}), "command should run successfully")
|
|
|
|
|
|
|
|
// check stdout stderr files
|
|
|
|
_, err = os.Stat(filepath.Join(dir, "stdout"))
|
|
|
|
require.Nil(t, err, "stdout should exist")
|
|
|
|
_, err = os.Stat(filepath.Join(dir, "stderr"))
|
|
|
|
require.Nil(t, err, "stderr should exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_runCmd_fail(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "")
|
|
|
|
require.Nil(t, err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
err = runCmd(log.NewNopLogger(), dir, handlerSettings{
|
|
|
|
publicSettings: publicSettings{CommandToExecute: "non-existing-cmd"},
|
|
|
|
})
|
|
|
|
require.NotNil(t, err, "command terminated with exit status")
|
|
|
|
require.Contains(t, err.Error(), "failed to execute command")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_downloadFiles(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "")
|
|
|
|
require.Nil(t, err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
srv := httptest.NewServer(httpbin.GetMux())
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
err = downloadFiles(log.NewContext(log.NewNopLogger()),
|
|
|
|
dir,
|
|
|
|
handlerSettings{
|
|
|
|
publicSettings: publicSettings{
|
|
|
|
FileURLs: []string{
|
|
|
|
srv.URL + "/bytes/10",
|
|
|
|
srv.URL + "/bytes/100",
|
|
|
|
srv.URL + "/bytes/1000",
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// check the files
|
|
|
|
f := []string{"10", "100", "1000"}
|
|
|
|
for _, fn := range f {
|
|
|
|
fp := filepath.Join(dir, fn)
|
|
|
|
_, err := os.Stat(fp)
|
|
|
|
require.Nil(t, err, "%s is missing from download dir", fp)
|
|
|
|
}
|
|
|
|
}
|
2017-10-11 09:14:01 +03:00
|
|
|
|
|
|
|
func Test_decodeScript(t *testing.T) {
|
|
|
|
testSubject := "bHMK"
|
|
|
|
s, info, err := decodeScript(testSubject)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, info, "4;3;gzip=0")
|
|
|
|
require.Equal(t, s, "ls\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_decodeScriptGzip(t *testing.T) {
|
|
|
|
testSubject := "H4sIACD731kAA8sp5gIAfShLWgMAAAA="
|
|
|
|
s, info, err := decodeScript(testSubject)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, info, "32;3;gzip=1")
|
|
|
|
require.Equal(t, s, "ls\n")
|
|
|
|
}
|