github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/helpers/plugin_repo.go (about)

     1  package helpers
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"log"
     9  	"net/http"
    10  	"os"
    11  	"path/filepath"
    12  
    13  	"code.cloudfoundry.org/cli/util"
    14  	"code.cloudfoundry.org/cli/util/generic"
    15  
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/ghttp"
    18  )
    19  
    20  type Binary struct {
    21  	Checksum string `json:"checksum"`
    22  	Platform string `json:"platform"`
    23  	URL      string `json:"url"`
    24  }
    25  
    26  type Plugin struct {
    27  	Name     string   `json:"name"`
    28  	Version  string   `json:"version"`
    29  	Binaries []Binary `json:"binaries"`
    30  }
    31  
    32  type PluginRepository struct {
    33  	Plugins []Plugin `json:"plugins"`
    34  }
    35  
    36  type PluginRepositoryServerWithPlugin struct {
    37  	server     *Server
    38  	pluginPath string
    39  }
    40  
    41  func NewPluginRepositoryServer(pluginRepo PluginRepository) *Server {
    42  	return configurePluginRepositoryServer(NewTLSServer(), pluginRepo)
    43  }
    44  
    45  func NewPluginRepositoryServerWithPlugin(pluginName string, version string, platform string, shouldCalculateChecksum bool) *PluginRepositoryServerWithPlugin {
    46  	pluginRepoServer := PluginRepositoryServerWithPlugin{}
    47  
    48  	pluginRepoServer.Init(pluginName, version, platform, shouldCalculateChecksum)
    49  
    50  	return &pluginRepoServer
    51  }
    52  
    53  func (pluginRepoServer *PluginRepositoryServerWithPlugin) Init(pluginName string, version string, platform string, shouldCalculateChecksum bool) {
    54  	pluginPath := BuildConfigurablePlugin("configurable_plugin", pluginName, version,
    55  		[]PluginCommand{
    56  			{Name: "some-command", Help: "some-command-help"},
    57  		},
    58  	)
    59  
    60  	repoServer := NewServer()
    61  
    62  	pluginRepoServer.server = repoServer
    63  	pluginRepoServer.pluginPath = pluginPath
    64  
    65  	var (
    66  		checksum []byte
    67  		err      error
    68  	)
    69  
    70  	if shouldCalculateChecksum {
    71  		checksum, err = util.NewSha1Checksum(pluginPath).ComputeFileSha1()
    72  		Expect(err).NotTo(HaveOccurred())
    73  	}
    74  
    75  	baseFile := fmt.Sprintf("/%s", generic.ExecutableFilename(filepath.Base(pluginPath)))
    76  	downloadURL := fmt.Sprintf("%s%s", repoServer.URL(), baseFile)
    77  	pluginRepo := PluginRepository{
    78  		Plugins: []Plugin{
    79  			{
    80  				Name:    pluginName,
    81  				Version: version,
    82  				Binaries: []Binary{
    83  					{
    84  						Checksum: fmt.Sprintf("%x", checksum),
    85  						Platform: platform,
    86  						URL:      downloadURL,
    87  					},
    88  				},
    89  			},
    90  		}}
    91  
    92  	// Suppresses ginkgo server logs
    93  	repoServer.HTTPTestServer.Config.ErrorLog = log.New(&bytes.Buffer{}, "", 0)
    94  
    95  	jsonBytes, err := json.Marshal(pluginRepo)
    96  	Expect(err).ToNot(HaveOccurred())
    97  
    98  	pluginData, err := ioutil.ReadFile(pluginPath)
    99  	Expect(err).ToNot(HaveOccurred())
   100  
   101  	repoServer.AppendHandlers(
   102  		CombineHandlers(
   103  			VerifyRequest(http.MethodGet, "/list"),
   104  			RespondWith(http.StatusOK, jsonBytes),
   105  		),
   106  		CombineHandlers(
   107  			VerifyRequest(http.MethodGet, "/list"),
   108  			RespondWith(http.StatusOK, jsonBytes),
   109  		),
   110  		CombineHandlers(
   111  			VerifyRequest(http.MethodGet, baseFile),
   112  			RespondWith(http.StatusOK, pluginData),
   113  		),
   114  	)
   115  }
   116  
   117  func (pluginRepoServer *PluginRepositoryServerWithPlugin) PluginSize() int64 {
   118  	fileinfo, err := os.Stat(pluginRepoServer.pluginPath)
   119  	Expect(err).NotTo(HaveOccurred())
   120  	return fileinfo.Size()
   121  }
   122  
   123  func (pluginRepoServer *PluginRepositoryServerWithPlugin) URL() string {
   124  	return pluginRepoServer.server.URL()
   125  }
   126  
   127  func (pluginRepoServer *PluginRepositoryServerWithPlugin) Cleanup() {
   128  	pluginRepoServer.server.Close()
   129  	Expect(os.RemoveAll(filepath.Dir(pluginRepoServer.pluginPath))).NotTo(HaveOccurred())
   130  }
   131  
   132  func NewPluginRepositoryTLSServer(pluginRepo PluginRepository) *Server {
   133  	return configurePluginRepositoryServer(NewTLSServer(), pluginRepo)
   134  }
   135  
   136  func configurePluginRepositoryServer(server *Server, pluginRepo PluginRepository) *Server {
   137  	// Suppresses ginkgo server logs
   138  	server.HTTPTestServer.Config.ErrorLog = log.New(&bytes.Buffer{}, "", 0)
   139  
   140  	jsonBytes, err := json.Marshal(pluginRepo)
   141  	Expect(err).ToNot(HaveOccurred())
   142  
   143  	server.AppendHandlers(
   144  		RespondWith(http.StatusOK, string(jsonBytes)),
   145  		RespondWith(http.StatusOK, string(jsonBytes)),
   146  		RespondWith(http.StatusOK, string(jsonBytes)),
   147  		RespondWith(http.StatusOK, string(jsonBytes)),
   148  	)
   149  
   150  	return server
   151  }