github.com/trigonella/mattermost-server@v5.11.1+incompatible/plugin/supervisor_test.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package plugin
     5  
     6  import (
     7  	"io/ioutil"
     8  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  	"testing"
    12  
    13  	"github.com/mattermost/mattermost-server/mlog"
    14  	"github.com/mattermost/mattermost-server/model"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestSupervisor(t *testing.T) {
    20  	for name, f := range map[string]func(*testing.T){
    21  		"Supervisor_InvalidExecutablePath":     testSupervisor_InvalidExecutablePath,
    22  		"Supervisor_NonExistentExecutablePath": testSupervisor_NonExistentExecutablePath,
    23  		"Supervisor_StartTimeout":              testSupervisor_StartTimeout,
    24  	} {
    25  		t.Run(name, f)
    26  	}
    27  }
    28  
    29  func compileGo(t *testing.T, sourceCode, outputPath string) {
    30  	dir, err := ioutil.TempDir(".", "")
    31  	require.NoError(t, err)
    32  	defer os.RemoveAll(dir)
    33  	require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "main.go"), []byte(sourceCode), 0600))
    34  	cmd := exec.Command("go", "build", "-o", outputPath, "main.go")
    35  	cmd.Dir = dir
    36  	cmd.Stdout = os.Stdout
    37  	cmd.Stderr = os.Stderr
    38  	require.NoError(t, cmd.Run())
    39  }
    40  
    41  func testSupervisor_InvalidExecutablePath(t *testing.T) {
    42  	dir, err := ioutil.TempDir("", "")
    43  	require.NoError(t, err)
    44  	defer os.RemoveAll(dir)
    45  
    46  	ioutil.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "backend": {"executable": "/foo/../../backend.exe"}}`), 0600)
    47  
    48  	bundle := model.BundleInfoForPath(dir)
    49  	log := mlog.NewLogger(&mlog.LoggerConfiguration{
    50  		EnableConsole: true,
    51  		ConsoleJson:   true,
    52  		ConsoleLevel:  "error",
    53  		EnableFile:    false,
    54  	})
    55  	supervisor, err := newSupervisor(bundle, log, nil)
    56  	assert.Nil(t, supervisor)
    57  	assert.Error(t, err)
    58  }
    59  
    60  func testSupervisor_NonExistentExecutablePath(t *testing.T) {
    61  	dir, err := ioutil.TempDir("", "")
    62  	require.NoError(t, err)
    63  	defer os.RemoveAll(dir)
    64  
    65  	ioutil.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "backend": {"executable": "thisfileshouldnotexist"}}`), 0600)
    66  
    67  	bundle := model.BundleInfoForPath(dir)
    68  	log := mlog.NewLogger(&mlog.LoggerConfiguration{
    69  		EnableConsole: true,
    70  		ConsoleJson:   true,
    71  		ConsoleLevel:  "error",
    72  		EnableFile:    false,
    73  	})
    74  	supervisor, err := newSupervisor(bundle, log, nil)
    75  	require.Error(t, err)
    76  	require.Nil(t, supervisor)
    77  }
    78  
    79  // If plugin development goes really wrong, let's make sure plugin activation won't block forever.
    80  func testSupervisor_StartTimeout(t *testing.T) {
    81  	dir, err := ioutil.TempDir("", "")
    82  	require.NoError(t, err)
    83  	defer os.RemoveAll(dir)
    84  
    85  	backend := filepath.Join(dir, "backend.exe")
    86  	compileGo(t, `
    87  		package main
    88  
    89  		func main() {
    90  			for {
    91  			}
    92  		}
    93  	`, backend)
    94  
    95  	ioutil.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "backend": {"executable": "backend.exe"}}`), 0600)
    96  
    97  	bundle := model.BundleInfoForPath(dir)
    98  	log := mlog.NewLogger(&mlog.LoggerConfiguration{
    99  		EnableConsole: true,
   100  		ConsoleJson:   true,
   101  		ConsoleLevel:  "error",
   102  		EnableFile:    false,
   103  	})
   104  	supervisor, err := newSupervisor(bundle, log, nil)
   105  	require.Error(t, err)
   106  	require.Nil(t, supervisor)
   107  }