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