github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/plugin/plugintest/test_utils.go (about)

     1  package plugintest
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/evergreen-ci/evergreen"
    10  	"github.com/evergreen-ci/evergreen/agent/comm"
    11  	"github.com/evergreen-ci/evergreen/model/patch"
    12  	modelutil "github.com/evergreen-ci/evergreen/model/testutil"
    13  	"github.com/evergreen-ci/evergreen/testutil"
    14  	"github.com/mongodb/grip/slogger"
    15  )
    16  
    17  type MockLogger struct{}
    18  
    19  func (_ *MockLogger) Flush()                                                                   {}
    20  func (_ *MockLogger) LogLocal(level slogger.Level, messageFmt string, args ...interface{})     {}
    21  func (_ *MockLogger) LogExecution(level slogger.Level, messageFmt string, args ...interface{}) {}
    22  func (_ *MockLogger) LogTask(level slogger.Level, messageFmt string, args ...interface{})      {}
    23  func (_ *MockLogger) LogSystem(level slogger.Level, messageFmt string, args ...interface{})    {}
    24  func (_ *MockLogger) GetTaskLogWriter(level slogger.Level) io.Writer                           { return ioutil.Discard }
    25  func (_ *MockLogger) GetSystemLogWriter(level slogger.Level) io.Writer                         { return ioutil.Discard }
    26  
    27  func TestAgentCommunicator(testData *modelutil.TestModelData, apiRootUrl string) *comm.HTTPCommunicator {
    28  	hostId := ""
    29  	hostSecret := ""
    30  	if testData.Host != nil {
    31  		hostId = testData.Host.Id
    32  		hostSecret = testData.Host.Secret
    33  	}
    34  	agentCommunicator, err := comm.NewHTTPCommunicator(apiRootUrl, hostId, hostSecret, "")
    35  	if err != nil {
    36  		panic(err)
    37  	}
    38  	agentCommunicator.MaxAttempts = 3
    39  	agentCommunicator.RetrySleep = 100 * time.Millisecond
    40  
    41  	if testData.Task != nil {
    42  		agentCommunicator.TaskId = testData.Task.Id
    43  		agentCommunicator.TaskSecret = testData.Task.Secret
    44  	}
    45  	return agentCommunicator
    46  }
    47  
    48  func SetupPatchData(apiData *modelutil.TestModelData, patchPath string, t *testing.T) error {
    49  
    50  	if patchPath != "" {
    51  		modulePatchContent, err := ioutil.ReadFile(patchPath)
    52  		testutil.HandleTestingErr(err, t, "failed to read test module patch file %v")
    53  
    54  		patch := &patch.Patch{
    55  			Status:  evergreen.PatchCreated,
    56  			Version: apiData.TaskConfig.Version.Id,
    57  			Patches: []patch.ModulePatch{
    58  				{
    59  					ModuleName: "enterprise",
    60  					Githash:    "c2d7ce942a96d7dacd27c55b257e3f2774e04abf",
    61  					PatchSet:   patch.PatchSet{Patch: string(modulePatchContent)},
    62  				},
    63  			},
    64  		}
    65  
    66  		testutil.HandleTestingErr(patch.Insert(), t, "failed to insert patch %v")
    67  
    68  	}
    69  
    70  	return nil
    71  }