github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/testutil/patch.go (about)

     1  package testutil
     2  
     3  import (
     4  	"io/ioutil"
     5  	"strings"
     6  
     7  	"github.com/evergreen-ci/evergreen"
     8  	"github.com/evergreen-ci/evergreen/db"
     9  	"github.com/evergreen-ci/evergreen/model/build"
    10  	"github.com/evergreen-ci/evergreen/model/patch"
    11  	"github.com/pkg/errors"
    12  )
    13  
    14  // the revision we'll assume is the current one for the agent. this is the
    15  // same as appears in testdata/executables/version
    16  var agentRevision = "xxx"
    17  
    18  const PatchId = "58d156352cfeb61064cf08b3"
    19  
    20  type PatchTestMode int
    21  
    22  const (
    23  	NoPatch PatchTestMode = iota
    24  	InlinePatch
    25  	ExternalPatch
    26  )
    27  
    28  func (m PatchTestMode) String() string {
    29  	switch m {
    30  	case NoPatch:
    31  		return "none"
    32  	case InlinePatch:
    33  		return "inline"
    34  	case ExternalPatch:
    35  		return "external"
    36  	}
    37  
    38  	return "unknown"
    39  }
    40  
    41  type PatchRequest struct {
    42  	ModuleName string
    43  	FilePath   string
    44  	Githash    string
    45  }
    46  
    47  func SetupPatches(patchMode PatchTestMode, b *build.Build, patches ...PatchRequest) (*patch.Patch, error) {
    48  	if patchMode == NoPatch {
    49  		return nil, errors.New("no patch defined")
    50  	}
    51  
    52  	ptch := &patch.Patch{
    53  		Id:      patch.NewId(PatchId),
    54  		Status:  evergreen.PatchCreated,
    55  		Version: b.Version,
    56  		Patches: []patch.ModulePatch{},
    57  	}
    58  
    59  	for _, p := range patches {
    60  		patchContent, err := ioutil.ReadFile(p.FilePath)
    61  		if err != nil {
    62  			return nil, err
    63  		}
    64  
    65  		if patchMode == InlinePatch {
    66  			ptch.Patches = append(ptch.Patches, patch.ModulePatch{
    67  				ModuleName: p.ModuleName,
    68  				Githash:    p.Githash,
    69  				PatchSet:   patch.PatchSet{Patch: string(patchContent)},
    70  			})
    71  		} else {
    72  			if err := db.WriteGridFile(patch.GridFSPrefix, string(ptch.Id), strings.NewReader(string(patchContent))); err != nil {
    73  				return nil, err
    74  			}
    75  
    76  			ptch.Patches = append(ptch.Patches, patch.ModulePatch{
    77  				ModuleName: p.ModuleName,
    78  				Githash:    p.Githash,
    79  				PatchSet:   patch.PatchSet{PatchFileId: string(ptch.Id)},
    80  			})
    81  		}
    82  	}
    83  	return ptch, ptch.Insert()
    84  }