github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/container/lxd/connection_test.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package lxd_test
     5  
     6  import (
     7  	"context"
     8  	"os"
     9  	"path/filepath"
    10  
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/container/lxd"
    15  	coretesting "github.com/juju/juju/testing"
    16  )
    17  
    18  type connectionSuite struct {
    19  	coretesting.BaseSuite
    20  }
    21  
    22  var _ = gc.Suite(&connectionSuite{})
    23  
    24  func (s *connectionSuite) TestLxdSocketPathLxdDirSet(c *gc.C) {
    25  	c.Assert(os.Setenv("LXD_DIR", "foobar"), jc.ErrorIsNil)
    26  	isSocket := func(path string) bool {
    27  		return path == filepath.FromSlash("foobar/unix.socket")
    28  	}
    29  	c.Check(lxd.SocketPath(isSocket), gc.Equals, filepath.Join("foobar", "unix.socket"))
    30  }
    31  
    32  func (s *connectionSuite) TestLxdSocketPathSnapSocketAndDebianSocketExists(c *gc.C) {
    33  	c.Assert(os.Setenv("LXD_DIR", ""), jc.ErrorIsNil)
    34  	isSocket := func(path string) bool {
    35  		return path == filepath.FromSlash("/var/snap/lxd/common/lxd/unix.socket") ||
    36  			path == filepath.FromSlash("/var/lib/lxd/unix.socket")
    37  	}
    38  	c.Check(lxd.SocketPath(isSocket), gc.Equals, filepath.FromSlash("/var/snap/lxd/common/lxd/unix.socket"))
    39  }
    40  
    41  func (s *connectionSuite) TestLxdSocketPathNoSnapSocket(c *gc.C) {
    42  	c.Assert(os.Setenv("LXD_DIR", ""), jc.ErrorIsNil)
    43  	isSocket := func(path string) bool {
    44  		return path == filepath.FromSlash("/var/lib/lxd/unix.socket")
    45  	}
    46  	c.Check(lxd.SocketPath(isSocket), gc.Equals, filepath.FromSlash("/var/lib/lxd/unix.socket"))
    47  }
    48  
    49  func (s *connectionSuite) TestLxdSocketPathNoSocket(c *gc.C) {
    50  	c.Assert(os.Setenv("LXD_DIR", ""), jc.ErrorIsNil)
    51  	isSocket := func(path string) bool { return false }
    52  	c.Check(lxd.SocketPath(isSocket), gc.Equals, "")
    53  }
    54  
    55  func (s *connectionSuite) TestConnectRemoteBadProtocol(c *gc.C) {
    56  	svr, err := lxd.ConnectImageRemote(context.Background(), lxd.ServerSpec{Host: "wrong-protocol-server", Protocol: "FOOBAR"})
    57  	c.Check(svr, gc.IsNil)
    58  	c.Check(err, gc.ErrorMatches, "bad protocol supplied for connection: FOOBAR")
    59  }
    60  
    61  func (s *connectionSuite) TestEnsureHTTPSUnchangedWhenCorrect(c *gc.C) {
    62  	addr := "https://somewhere"
    63  	c.Check(lxd.EnsureHTTPS(addr), gc.Equals, addr)
    64  }
    65  
    66  func (s *connectionSuite) TestEnsureHTTPS(c *gc.C) {
    67  	for _, t := range []struct {
    68  		Input  string
    69  		Output string
    70  	}{
    71  		{
    72  			Input:  "http://somewhere",
    73  			Output: "https://somewhere",
    74  		},
    75  		{
    76  			Input:  "https://somewhere",
    77  			Output: "https://somewhere",
    78  		},
    79  		{
    80  			Input:  "somewhere",
    81  			Output: "https://somewhere",
    82  		},
    83  	} {
    84  		got := lxd.EnsureHTTPS(t.Input)
    85  		c.Assert(got, gc.Equals, t.Output)
    86  	}
    87  }
    88  
    89  func (s *connectionSuite) TestEnsureHostPort(c *gc.C) {
    90  	for _, t := range []struct {
    91  		Input  string
    92  		Output string
    93  	}{
    94  		{
    95  			Input:  "https://somewhere",
    96  			Output: "https://somewhere:8443",
    97  		},
    98  		{
    99  			Input:  "somewhere",
   100  			Output: "https://somewhere:8443",
   101  		},
   102  		{
   103  			Input:  "http://somewhere:0",
   104  			Output: "https://somewhere:0",
   105  		},
   106  		{
   107  			Input:  "https://somewhere:123",
   108  			Output: "https://somewhere:123",
   109  		},
   110  		{
   111  			Input:  "https://somewhere:123/",
   112  			Output: "https://somewhere:123",
   113  		},
   114  	} {
   115  		got, err := lxd.EnsureHostPort(t.Input)
   116  		c.Assert(err, gc.IsNil)
   117  		c.Assert(got, gc.Equals, t.Output)
   118  	}
   119  }