github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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(), gc.Equals, "running")
    75  }
    76  
    77  func (s *instanceSuite) TestInstanceRefresh(c *gc.C) {
    78  	c.Check(s.inst.Status(), gc.Equals, "running")
    79  
    80  	mock.SetServerStatus("f4ec5097-121e-44a7-a207-75bc02163260", "stopped")
    81  
    82  	err := s.inst.Refresh()
    83  	c.Check(err, gc.IsNil)
    84  
    85  	c.Check(s.inst.Status(), gc.Equals, "stopped")
    86  }
    87  
    88  func (s *instanceSuite) TestInstanceAddresses(c *gc.C) {
    89  	addrs, err := s.inst.Addresses()
    90  	c.Check(addrs, gc.HasLen, 1)
    91  	c.Check(err, gc.IsNil)
    92  	a := addrs[0]
    93  	c.Check(a.Value, gc.Equals, "178.22.70.33")
    94  	c.Check(a.Type, gc.Equals, network.IPv4Address)
    95  	c.Check(a.Scope, gc.Equals, network.ScopePublic)
    96  	c.Check(a.NetworkName, gc.Equals, "")
    97  
    98  	addrs, err = s.instWithoutIP.Addresses()
    99  	c.Check(err, gc.IsNil)
   100  	c.Check(len(addrs), gc.Equals, 0)
   101  }
   102  
   103  func (s *instanceSuite) TestInstancePorts(c *gc.C) {
   104  	c.Check(s.inst.OpenPorts("", nil), gc.ErrorMatches, "OpenPorts not implemented")
   105  	c.Check(s.inst.ClosePorts("", nil), gc.ErrorMatches, "ClosePorts not implemented")
   106  
   107  	_, err := s.inst.Ports("")
   108  	c.Check(err, gc.ErrorMatches, "Ports not implemented")
   109  }
   110  
   111  func (s *instanceSuite) TestInstanceHardware(c *gc.C) {
   112  	hw, err := s.inst.hardware("64", 1000000)
   113  	c.Assert(err, gc.IsNil)
   114  	c.Assert(hw, gc.NotNil)
   115  
   116  	c.Check(*hw.Arch, gc.Equals, "64")
   117  
   118  	c.Check(hw.Mem, gc.NotNil)
   119  	if hw.Mem != nil {
   120  		c.Check(*hw.Mem, gc.Equals, uint64(2048))
   121  	}
   122  
   123  	c.Check(hw.RootDisk, gc.IsNil)
   124  
   125  	c.Check(hw.CpuCores, gc.NotNil)
   126  	if hw.CpuCores != nil {
   127  		c.Check(*hw.CpuCores, gc.Equals, uint64(1))
   128  	}
   129  
   130  	c.Check(hw.CpuPower, gc.NotNil)
   131  	c.Check(*hw.CpuPower, gc.Equals, uint64(2000))
   132  
   133  	c.Check(hw.Tags, gc.IsNil)
   134  }
   135  
   136  const jsonInstanceData = `{
   137      "context": true,
   138      "cpu": 2000,
   139      "cpu_model": null,
   140      "cpus_instead_of_cores": false,
   141      "drives": [
   142          {
   143              "boot_order": 1,
   144              "dev_channel": "0:0",
   145              "device": "virtio",
   146              "drive": {
   147                  "resource_uri": "/api/2.0/drives/f968dc48-25a0-4d46-8f16-e12e073e1fe6/",
   148                  "uuid": "f968dc48-25a0-4d46-8f16-e12e073e1fe6"
   149              },
   150              "runtime": {
   151                  "io": {
   152                      "bytes_read": 82980352,
   153                      "bytes_written": 189440,
   154                      "count_flush": 0,
   155                      "count_read": 3952,
   156                      "count_written": 19,
   157                      "total_time_ns_flush": 0,
   158                      "total_time_ns_read": 4435322816,
   159                      "total_time_ns_write": 123240430
   160                  }
   161              }
   162          }
   163      ],
   164      "enable_numa": false,
   165      "hv_relaxed": false,
   166      "hv_tsc": false,
   167      "jobs": [],
   168      "mem": 2147483648,
   169      "meta": {
   170          "description": "test-description",
   171          "ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDDiwTGBsmFKBYHcKaVy5IgsYBR4XVYLS6KP/NKClE7gONlIGURE3+/45BX8TfHJHM5WTN8NBqJejKDHqwfyueR1f2VGoPkJxODGt/X/ZDNftLZLYwPd2DfDBs27ahOadZCk4Cl5l7mU0aoE74UnIcQoNPl6w7axkIFTIXr8+0HMk8DFB0iviBSJK118p1RGwhsoA1Hudn1CsgqARGPmNn6mxwvmQfQY7hZxZoOH9WMcvkNZ7rAFrwS/BuvEpEXkoC95K/JDPvmQVVJk7we+WeHfTYSmApkDFcSaypyjL2HOV8pvE+VntcIIhZccHiOubyjsBAx5aoTI+ueCsoz5AL1 maxim.perenesenko@altoros.com"
   172      },
   173      "name": "LiveTest-srv-17-54-51-999999999",
   174      "nics": [
   175          {
   176              "boot_order": null,
   177              "firewall_policy": null,
   178              "ip_v4_conf": {
   179                  "conf": "dhcp",
   180                  "ip": null
   181              },
   182              "ip_v6_conf": null,
   183              "mac": "22:ab:bf:26:e1:be",
   184              "model": "virtio",
   185              "runtime": {
   186                  "interface_type": "public",
   187                  "io": {
   188                      "bytes_recv": 0,
   189                      "bytes_sent": 17540,
   190                      "packets_recv": 0,
   191                      "packets_sent": 256
   192                  },
   193                  "ip_v4": {
   194                      "resource_uri": "/api/2.0/ips/178.22.70.33/",
   195                      "uuid": "178.22.70.33"
   196                  },
   197                  "ip_v6": null
   198              },
   199              "vlan": null
   200          },
   201          {
   202              "boot_order": null,
   203              "firewall_policy": null,
   204              "ip_v4_conf": null,
   205              "ip_v6_conf": null,
   206              "mac": "22:9e:e7:d7:86:94",
   207              "model": "virtio",
   208              "runtime": {
   209                  "interface_type": "private",
   210                  "io": {
   211                      "bytes_recv": 0,
   212                      "bytes_sent": 1046,
   213                      "packets_recv": 0,
   214                      "packets_sent": 13
   215                  },
   216                  "ip_v4": null,
   217                  "ip_v6": null
   218              },
   219              "vlan": {
   220                  "resource_uri": "/api/2.0/vlans/5bc05e7e-6555-4f40-add8-3b8e91447702/",
   221                  "uuid": "5bc05e7e-6555-4f40-add8-3b8e91447702"
   222              }
   223          }
   224      ],
   225      "owner": {
   226          "resource_uri": "/api/2.0/user/c25eb0ed-161f-44f4-ac1d-d584ce3a5312/",
   227          "uuid": "c25eb0ed-161f-44f4-ac1d-d584ce3a5312"
   228      },
   229      "requirements": [],
   230      "resource_uri": "/api/2.0/servers/f4ec5097-121e-44a7-a207-75bc02163260/",
   231      "runtime": {
   232          "active_since": "2014-04-24T14:56:58+00:00",
   233          "nics": [
   234              {
   235                  "interface_type": "public",
   236                  "io": {
   237                      "bytes_recv": 0,
   238                      "bytes_sent": 17540,
   239                      "packets_recv": 0,
   240                      "packets_sent": 256
   241                  },
   242                  "ip_v4": {
   243                      "resource_uri": "/api/2.0/ips/178.22.70.33/",
   244                      "uuid": "178.22.70.33"
   245                  },
   246                  "ip_v6": null,
   247                  "mac": "22:ab:bf:26:e1:be"
   248              },
   249              {
   250                  "interface_type": "private",
   251                  "io": {
   252                      "bytes_recv": 0,
   253                      "bytes_sent": 1046,
   254                      "packets_recv": 0,
   255                      "packets_sent": 13
   256                  },
   257                  "ip_v4": null,
   258                  "ip_v6": null,
   259                  "mac": "22:9e:e7:d7:86:94"
   260              }
   261          ]
   262      },
   263      "smp": 1,
   264      "status": "running",
   265      "tags": [],
   266      "uuid": "f4ec5097-121e-44a7-a207-75bc02163260",
   267      "vnc_password": "test-vnc-password"
   268  }`