github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/provider/cloudsigma/instance_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cloudsigma
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/altoros/gosigma"
    10  	"github.com/altoros/gosigma/data"
    11  	"github.com/altoros/gosigma/mock"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/instance"
    15  	"github.com/juju/juju/network"
    16  	"github.com/juju/juju/testing"
    17  )
    18  
    19  type instanceSuite struct {
    20  	testing.BaseSuite
    21  	inst          *sigmaInstance
    22  	instWithoutIP *sigmaInstance
    23  }
    24  
    25  var _ = gc.Suite(&instanceSuite{})
    26  
    27  func (s *instanceSuite) SetUpSuite(c *gc.C) {
    28  	s.BaseSuite.SetUpSuite(c)
    29  	mock.Start()
    30  }
    31  
    32  func (s *instanceSuite) TearDownSuite(c *gc.C) {
    33  	mock.Stop()
    34  	s.BaseSuite.TearDownSuite(c)
    35  }
    36  
    37  func (s *instanceSuite) SetUpTest(c *gc.C) {
    38  	s.BaseSuite.SetUpTest(c)
    39  
    40  	cli, err := gosigma.NewClient(mock.Endpoint(""), mock.TestUser, mock.TestPassword, nil)
    41  	c.Assert(err, gc.IsNil)
    42  
    43  	mock.ResetServers()
    44  
    45  	ds, err := data.ReadServer(strings.NewReader(jsonInstanceData))
    46  	c.Assert(err, gc.IsNil)
    47  	mock.AddServer(ds)
    48  
    49  	mock.AddServer(&data.Server{
    50  		Resource: data.Resource{URI: "uri", UUID: "uuid-no-ip"},
    51  	})
    52  
    53  	server, err := cli.Server("f4ec5097-121e-44a7-a207-75bc02163260")
    54  	c.Assert(err, gc.IsNil)
    55  	c.Assert(server, gc.NotNil)
    56  	s.inst = &sigmaInstance{server}
    57  
    58  	server, err = cli.Server("uuid-no-ip")
    59  	c.Assert(err, gc.IsNil)
    60  	c.Assert(server, gc.NotNil)
    61  	s.instWithoutIP = &sigmaInstance{server}
    62  }
    63  
    64  func (s *instanceSuite) TearDownTest(c *gc.C) {
    65  	mock.ResetServers()
    66  	s.BaseSuite.TearDownTest(c)
    67  }
    68  
    69  func (s *instanceSuite) TestInstanceId(c *gc.C) {
    70  	c.Check(s.inst.Id(), gc.Equals, instance.Id("f4ec5097-121e-44a7-a207-75bc02163260"))
    71  }
    72  
    73  func (s *instanceSuite) TestInstanceStatus(c *gc.C) {
    74  	c.Check(s.inst.Status().Message, gc.Equals, "running")
    75  }
    76  
    77  func (s *instanceSuite) TestInstanceAddresses(c *gc.C) {
    78  	addrs, err := s.inst.Addresses()
    79  	c.Check(addrs, gc.HasLen, 1)
    80  	c.Check(err, gc.IsNil)
    81  	a := addrs[0]
    82  	c.Check(a.Value, gc.Equals, "178.22.70.33")
    83  	c.Check(a.Type, gc.Equals, network.IPv4Address)
    84  	c.Check(a.Scope, gc.Equals, network.ScopePublic)
    85  
    86  	addrs, err = s.instWithoutIP.Addresses()
    87  	c.Check(err, gc.IsNil)
    88  	c.Check(len(addrs), gc.Equals, 0)
    89  }
    90  
    91  func (s *instanceSuite) TestInstancePorts(c *gc.C) {
    92  	c.Check(s.inst.OpenPorts("", nil), gc.ErrorMatches, "OpenPorts not implemented")
    93  	c.Check(s.inst.ClosePorts("", nil), gc.ErrorMatches, "ClosePorts not implemented")
    94  
    95  	_, err := s.inst.Ports("")
    96  	c.Check(err, gc.ErrorMatches, "Ports not implemented")
    97  }
    98  
    99  func (s *instanceSuite) TestInstanceHardware(c *gc.C) {
   100  	hw, err := s.inst.hardware("64", 1000000)
   101  	c.Assert(err, gc.IsNil)
   102  	c.Assert(hw, gc.NotNil)
   103  
   104  	c.Check(*hw.Arch, gc.Equals, "64")
   105  
   106  	c.Check(hw.Mem, gc.NotNil)
   107  	if hw.Mem != nil {
   108  		c.Check(*hw.Mem, gc.Equals, uint64(2048))
   109  	}
   110  
   111  	c.Check(hw.RootDisk, gc.IsNil)
   112  
   113  	c.Check(hw.CpuCores, gc.NotNil)
   114  	if hw.CpuCores != nil {
   115  		c.Check(*hw.CpuCores, gc.Equals, uint64(1))
   116  	}
   117  
   118  	c.Check(hw.CpuPower, gc.NotNil)
   119  	c.Check(*hw.CpuPower, gc.Equals, uint64(2000))
   120  
   121  	c.Check(hw.Tags, gc.IsNil)
   122  }
   123  
   124  const jsonInstanceData = `{
   125      "context": true,
   126      "cpu": 2000,
   127      "cpu_model": null,
   128      "cpus_instead_of_cores": false,
   129      "drives": [
   130          {
   131              "boot_order": 1,
   132              "dev_channel": "0:0",
   133              "device": "virtio",
   134              "drive": {
   135                  "resource_uri": "/api/2.0/drives/f968dc48-25a0-4d46-8f16-e12e073e1fe6/",
   136                  "uuid": "f968dc48-25a0-4d46-8f16-e12e073e1fe6"
   137              },
   138              "runtime": {
   139                  "io": {
   140                      "bytes_read": 82980352,
   141                      "bytes_written": 189440,
   142                      "count_flush": 0,
   143                      "count_read": 3952,
   144                      "count_written": 19,
   145                      "total_time_ns_flush": 0,
   146                      "total_time_ns_read": 4435322816,
   147                      "total_time_ns_write": 123240430
   148                  }
   149              }
   150          }
   151      ],
   152      "enable_numa": false,
   153      "hv_relaxed": false,
   154      "hv_tsc": false,
   155      "jobs": [],
   156      "mem": 2147483648,
   157      "meta": {
   158          "description": "test-description",
   159          "ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDDiwTGBsmFKBYHcKaVy5IgsYBR4XVYLS6KP/NKClE7gONlIGURE3+/45BX8TfHJHM5WTN8NBqJejKDHqwfyueR1f2VGoPkJxODGt/X/ZDNftLZLYwPd2DfDBs27ahOadZCk4Cl5l7mU0aoE74UnIcQoNPl6w7axkIFTIXr8+0HMk8DFB0iviBSJK118p1RGwhsoA1Hudn1CsgqARGPmNn6mxwvmQfQY7hZxZoOH9WMcvkNZ7rAFrwS/BuvEpEXkoC95K/JDPvmQVVJk7we+WeHfTYSmApkDFcSaypyjL2HOV8pvE+VntcIIhZccHiOubyjsBAx5aoTI+ueCsoz5AL1 maxim.perenesenko@altoros.com"
   160      },
   161      "name": "LiveTest-srv-17-54-51-999999999",
   162      "nics": [
   163          {
   164              "boot_order": null,
   165              "firewall_policy": null,
   166              "ip_v4_conf": {
   167                  "conf": "dhcp",
   168                  "ip": null
   169              },
   170              "ip_v6_conf": null,
   171              "mac": "22:ab:bf:26:e1:be",
   172              "model": "virtio",
   173              "runtime": {
   174                  "interface_type": "public",
   175                  "io": {
   176                      "bytes_recv": 0,
   177                      "bytes_sent": 17540,
   178                      "packets_recv": 0,
   179                      "packets_sent": 256
   180                  },
   181                  "ip_v4": {
   182                      "resource_uri": "/api/2.0/ips/178.22.70.33/",
   183                      "uuid": "178.22.70.33"
   184                  },
   185                  "ip_v6": null
   186              },
   187              "vlan": null
   188          },
   189          {
   190              "boot_order": null,
   191              "firewall_policy": null,
   192              "ip_v4_conf": null,
   193              "ip_v6_conf": null,
   194              "mac": "22:9e:e7:d7:86:94",
   195              "model": "virtio",
   196              "runtime": {
   197                  "interface_type": "private",
   198                  "io": {
   199                      "bytes_recv": 0,
   200                      "bytes_sent": 1046,
   201                      "packets_recv": 0,
   202                      "packets_sent": 13
   203                  },
   204                  "ip_v4": null,
   205                  "ip_v6": null
   206              },
   207              "vlan": {
   208                  "resource_uri": "/api/2.0/vlans/5bc05e7e-6555-4f40-add8-3b8e91447702/",
   209                  "uuid": "5bc05e7e-6555-4f40-add8-3b8e91447702"
   210              }
   211          }
   212      ],
   213      "owner": {
   214          "resource_uri": "/api/2.0/user/c25eb0ed-161f-44f4-ac1d-d584ce3a5312/",
   215          "uuid": "c25eb0ed-161f-44f4-ac1d-d584ce3a5312"
   216      },
   217      "requirements": [],
   218      "resource_uri": "/api/2.0/servers/f4ec5097-121e-44a7-a207-75bc02163260/",
   219      "runtime": {
   220          "active_since": "2014-04-24T14:56:58+00:00",
   221          "nics": [
   222              {
   223                  "interface_type": "public",
   224                  "io": {
   225                      "bytes_recv": 0,
   226                      "bytes_sent": 17540,
   227                      "packets_recv": 0,
   228                      "packets_sent": 256
   229                  },
   230                  "ip_v4": {
   231                      "resource_uri": "/api/2.0/ips/178.22.70.33/",
   232                      "uuid": "178.22.70.33"
   233                  },
   234                  "ip_v6": null,
   235                  "mac": "22:ab:bf:26:e1:be"
   236              },
   237              {
   238                  "interface_type": "private",
   239                  "io": {
   240                      "bytes_recv": 0,
   241                      "bytes_sent": 1046,
   242                      "packets_recv": 0,
   243                      "packets_sent": 13
   244                  },
   245                  "ip_v4": null,
   246                  "ip_v6": null,
   247                  "mac": "22:9e:e7:d7:86:94"
   248              }
   249          ]
   250      },
   251      "smp": 1,
   252      "status": "running",
   253      "tags": [],
   254      "uuid": "f4ec5097-121e-44a7-a207-75bc02163260",
   255      "vnc_password": "test-vnc-password"
   256  }`