github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cloudconfig/containerinit/container_userdata_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Copyright 2015 Cloudbase Solutions SRL
     3  // Licensed under the AGPLv3, see LICENCE file for details.
     4  
     5  package containerinit_test
     6  
     7  import (
     8  	"runtime"
     9  	"strings"
    10  	stdtesting "testing"
    11  
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/cloudconfig/containerinit"
    16  	"github.com/juju/juju/container"
    17  	containertesting "github.com/juju/juju/container/testing"
    18  	"github.com/juju/juju/testing"
    19  )
    20  
    21  func Test(t *stdtesting.T) {
    22  	gc.TestingT(t)
    23  }
    24  
    25  type UserDataSuite struct {
    26  	testing.BaseSuite
    27  }
    28  
    29  var _ = gc.Suite(&UserDataSuite{})
    30  
    31  func (s *UserDataSuite) SetUpTest(c *gc.C) {
    32  	s.BaseSuite.SetUpTest(c)
    33  
    34  	if runtime.GOOS == "windows" {
    35  		c.Skip("This test is for Linux only")
    36  	}
    37  }
    38  
    39  func CloudInitDataExcludingOutputSection(data string) []string {
    40  	// Extract the "#cloud-config" header and all lines between
    41  	// from the "bootcmd" section up to (but not including) the
    42  	// "output" sections to match against expected. But we cannot
    43  	// possibly handle all the /other/ output that may be added by
    44  	// CloudInitUserData() in the future, so we also truncate at
    45  	// the first runcmd which now happens to include the runcmd's
    46  	// added for raising the network interfaces captured in
    47  	// expectedFallbackUserData. However, the other tests above do
    48  	// check for that output.
    49  
    50  	var linesToMatch []string
    51  	seenBootcmd := false
    52  	for _, line := range strings.Split(string(data), "\n") {
    53  		if strings.HasPrefix(line, "#cloud-config") {
    54  			linesToMatch = append(linesToMatch, line)
    55  			continue
    56  		}
    57  
    58  		if strings.HasPrefix(line, "bootcmd:") {
    59  			seenBootcmd = true
    60  		}
    61  
    62  		if strings.HasPrefix(line, "output:") && seenBootcmd {
    63  			break
    64  		}
    65  
    66  		if seenBootcmd {
    67  			linesToMatch = append(linesToMatch, line)
    68  		}
    69  	}
    70  
    71  	return linesToMatch
    72  }
    73  
    74  // TestCloudInitUserDataNoNetworkConfig tests that no network-interfaces, or
    75  // related data, appear in user-data when no networkConfig is passed to
    76  // CloudInitUserData.
    77  func (s *UserDataSuite) TestCloudInitUserDataNoNetworkConfig(c *gc.C) {
    78  	instanceConfig, err := containertesting.MockMachineConfig("1/lxd/0")
    79  	c.Assert(err, jc.ErrorIsNil)
    80  	data, err := containerinit.CloudInitUserData(instanceConfig, nil)
    81  	c.Assert(err, jc.ErrorIsNil)
    82  	c.Assert(data, gc.NotNil)
    83  
    84  	linesToMatch := CloudInitDataExcludingOutputSection(string(data))
    85  
    86  	c.Assert(strings.Join(linesToMatch, "\n"), gc.Equals, "#cloud-config")
    87  }
    88  
    89  // TestCloudInitUserDataSomeNetworkConfig tests that the data generated by
    90  // cloudinit.AddNetworkConfig is applied properly.
    91  func (s *UserDataSuite) TestCloudInitUserDataSomeNetworkConfig(c *gc.C) {
    92  	instanceConfig, err := containertesting.MockMachineConfig("1/lxd/0")
    93  	c.Assert(err, jc.ErrorIsNil)
    94  	netConfig := container.BridgeNetworkConfig("foo", 0, nil)
    95  	data, err := containerinit.CloudInitUserData(instanceConfig, netConfig)
    96  	c.Assert(err, jc.ErrorIsNil)
    97  	c.Assert(data, gc.NotNil)
    98  
    99  	linesToMatch := CloudInitDataExcludingOutputSection(string(data))
   100  
   101  	// We just check first two lines, rest is tested deeper.
   102  	c.Assert(linesToMatch[0], gc.Equals, "#cloud-config")
   103  	c.Assert(linesToMatch[1], gc.Equals, "bootcmd:")
   104  
   105  }