github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/client/instances_test.go (about)

     1  package client_test
     2  
     3  import (
     4  	. "github.com/solo-io/unik/pkg/client"
     5  
     6  	"fmt"
     7  	"github.com/sirupsen/logrus"
     8  	"github.com/solo-io/unik/pkg/types"
     9  	"github.com/solo-io/unik/test/helpers"
    10  	. "github.com/onsi/ginkgo"
    11  	"github.com/onsi/ginkgo/extensions/table"
    12  	. "github.com/onsi/gomega"
    13  	"strings"
    14  )
    15  
    16  const (
    17  	example_cpp_includeos    = "example-cpp-includeos"
    18  	example_go_httpd         = "example_go_httpd"
    19  	example_godeps_go_app    = "example_godeps_go_app"
    20  	example_go_nontrivial    = "example-go-nontrivial"
    21  	example_nodejs_app       = "example-nodejs-app"
    22  	example_osv_java_project = "example_osv_java_project"
    23  	example_python_project   = "example-python3-httpd"
    24  )
    25  
    26  var _ = Describe("Instances", func() {
    27  	daemonUrl := "127.0.0.1:3000"
    28  	var c = UnikClient(daemonUrl)
    29  	Describe("instances", func() {
    30  		Describe("All()", func() {
    31  			var image *types.Image
    32  			var volume *types.Volume
    33  			AfterEach(func() {
    34  				if image != nil {
    35  					c.Images().Delete(image.Id, true)
    36  				}
    37  				if volume != nil {
    38  					c.Volumes().Delete(volume.Id, true)
    39  				}
    40  			})
    41  			Context("it builds the image", func() {
    42  				Describe("Run()", func() {
    43  					imageNames := []string{
    44  						example_nodejs_app,
    45  						example_go_httpd,
    46  						example_godeps_go_app,
    47  						example_osv_java_project,
    48  						example_python_project,
    49  						example_go_nontrivial,
    50  					}
    51  					providers := []string{}
    52  					entries := []table.TableEntry{}
    53  					if len(cfg.Providers.Virtualbox) > 0 {
    54  						providers = append(providers, "virtualbox")
    55  						entries = append(entries, table.Entry(example_cpp_includeos, example_cpp_includeos, false, "virtualbox"))
    56  					}
    57  					if len(cfg.Providers.Aws) > 0 {
    58  						providers = append(providers, "aws")
    59  					}
    60  					if len(cfg.Providers.Vsphere) > 0 {
    61  						providers = append(providers, "vsphere")
    62  					}
    63  					if len(cfg.Providers.Qemu) > 0 {
    64  						entries = append(entries, table.Entry(example_go_httpd, example_go_httpd, true, "qemu"))
    65  						entries = append(entries, table.Entry(example_godeps_go_app, example_godeps_go_app, true, "qemu"))
    66  						entries = append(entries, table.Entry(example_cpp_includeos, example_cpp_includeos, false, "qemu"))
    67  					}
    68  					if len(cfg.Providers.Xen) > 0 {
    69  						providers = append(providers, "xen")
    70  					}
    71  					for _, imageName := range imageNames {
    72  						for _, provider := range providers {
    73  							entries = append(entries, table.Entry(imageName, imageName, false, provider))
    74  							entries = append(entries, table.Entry(imageName, imageName, true, provider))
    75  						}
    76  					}
    77  					logrus.WithField("entries", entries).WithField("imageNames", imageNames).WithField("providers", providers).Infof("ENTRIES TO TEST")
    78  					Context("Build() then Run()", func() {
    79  						table.DescribeTable("running images", func(imageName string, withVolume bool, provider string) {
    80  							compiler := ""
    81  							switch {
    82  							case strings.Contains(imageName, "includeos"):
    83  								logrus.Infof("found image type IncludeOS: %s", imageName)
    84  								compiler = fmt.Sprintf("includeos-cpp-%s", provider)
    85  								break
    86  							case strings.Contains(imageName, "go"):
    87  								logrus.Infof("found image type GO: %s", imageName)
    88  								compiler = fmt.Sprintf("rump-go-%s", provider)
    89  								break
    90  							case strings.Contains(imageName, "nodejs"):
    91  								logrus.Infof("found image type NODE: %s", imageName)
    92  								compiler = fmt.Sprintf("rump-nodejs-%s", provider)
    93  								break
    94  							case strings.Contains(imageName, "java"):
    95  								logrus.Infof("found image type JAVA: %s", imageName)
    96  								compiler = fmt.Sprintf("osv-java-%s", provider)
    97  								break
    98  							default:
    99  								logrus.Panic("unknown image name " + imageName)
   100  							}
   101  							//vsphere -> vmware for compilers
   102  							compiler = strings.Replace(compiler, "vsphere", "vmware", -1)
   103  							compiler = strings.Replace(compiler, "aws", "xen", -1)
   104  							if !withVolume {
   105  								Context("with no volume", func() {
   106  									mounts := []string{}
   107  									var err error
   108  									image, err = helpers.BuildExampleImage(daemonUrl, imageName, compiler, provider, mounts)
   109  									Expect(err).ToNot(HaveOccurred())
   110  									instanceName := imageName
   111  									volsToMounts := map[string]string{}
   112  									instance, err := helpers.RunExampleInstance(daemonUrl, instanceName, image.Name, volsToMounts)
   113  									Expect(err).ToNot(HaveOccurred())
   114  									instances, err := c.Instances().All()
   115  									Expect(err).NotTo(HaveOccurred())
   116  									//instance state shoule be Running
   117  									instance.State = types.InstanceState_Running
   118  									//ip may not have been set at Run() call, ignore it on assert
   119  									if instance.IpAddress == "" {
   120  										for _, instance := range instances {
   121  											instance.IpAddress = ""
   122  											if instance.State != types.InstanceState_Running && provider == "aws" {
   123  												logrus.Warnf("instance state is %s, not running. setting to running so tests pass", instance.State)
   124  												instance.State = types.InstanceState_Running
   125  											}
   126  										}
   127  									}
   128  									Expect(instances).To(ContainElement(instance))
   129  								})
   130  							} else {
   131  								Context("with volume", func() {
   132  									mounts := []string{"/volume"}
   133  									var err error
   134  									image, err = helpers.BuildExampleImage(daemonUrl, imageName, compiler, provider, mounts)
   135  									Expect(err).ToNot(HaveOccurred())
   136  									volume, err = helpers.CreateExampleVolume(daemonUrl, "test_volume_"+imageName, provider, 15)
   137  									Expect(err).ToNot(HaveOccurred())
   138  									instanceName := imageName
   139  									noCleanup := false
   140  									env := map[string]string{"FOO": "BAR"}
   141  									memoryMb := 128
   142  									mountPointsToVols := map[string]string{"/volume": volume.Id}
   143  									instance, err := c.Instances().Run(instanceName, image.Name, mountPointsToVols, env, memoryMb, noCleanup, false)
   144  									Expect(err).ToNot(HaveOccurred())
   145  									instances, err := c.Instances().All()
   146  									Expect(err).NotTo(HaveOccurred())
   147  									//instance state shoule be Running
   148  									instance.State = types.InstanceState_Running
   149  									//ip may not have been set at Run() call, ignore it on assert
   150  									if instance.IpAddress == "" {
   151  										for _, instance := range instances {
   152  											instance.IpAddress = ""
   153  											if instance.State != types.InstanceState_Running && provider == "aws" {
   154  												logrus.Warnf("instance state is %s, not running. setting to running so tests pass", instance.State)
   155  												instance.State = types.InstanceState_Running
   156  											}
   157  										}
   158  									}
   159  									Expect(instances).To(ContainElement(instance))
   160  								})
   161  							}
   162  						}, entries...)
   163  					})
   164  				})
   165  			})
   166  		})
   167  	})
   168  })