launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/version/current_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package version_test
     5  
     6  import (
     7  	"io/ioutil"
     8  	"os/exec"
     9  	"path/filepath"
    10  
    11  	gc "launchpad.net/gocheck"
    12  
    13  	"launchpad.net/juju-core/version"
    14  )
    15  
    16  type CurrentSuite struct{}
    17  
    18  var _ = gc.Suite(&CurrentSuite{})
    19  
    20  var readSeriesTests = []struct {
    21  	contents string
    22  	series   string
    23  }{{
    24  	`DISTRIB_ID=Ubuntu
    25  DISTRIB_RELEASE=12.04
    26  DISTRIB_CODENAME=precise
    27  DISTRIB_DESCRIPTION="Ubuntu 12.04 LTS"`,
    28  	"precise",
    29  }, {
    30  	"DISTRIB_CODENAME= \tprecise\t",
    31  	"precise",
    32  }, {
    33  	`DISTRIB_CODENAME="precise"`,
    34  	"precise",
    35  }, {
    36  	"DISTRIB_CODENAME='precise'",
    37  	"precise",
    38  }, {
    39  	`DISTRIB_ID=Ubuntu
    40  DISTRIB_RELEASE=12.10
    41  DISTRIB_CODENAME=quantal
    42  DISTRIB_DESCRIPTION="Ubuntu 12.10"`,
    43  	"quantal",
    44  }, {
    45  	"",
    46  	"unknown",
    47  },
    48  }
    49  
    50  func (*CurrentSuite) TestReadSeries(c *gc.C) {
    51  	d := c.MkDir()
    52  	f := filepath.Join(d, "foo")
    53  	for i, t := range readSeriesTests {
    54  		c.Logf("test %d", i)
    55  		err := ioutil.WriteFile(f, []byte(t.contents), 0666)
    56  		c.Assert(err, gc.IsNil)
    57  		c.Assert(version.ReadSeries(f), gc.Equals, t.series)
    58  	}
    59  }
    60  
    61  func (*CurrentSuite) TestCurrentSeries(c *gc.C) {
    62  	s := version.Current.Series
    63  	if s == "unknown" {
    64  		s = "n/a"
    65  	}
    66  	out, err := exec.Command("lsb_release", "-c").CombinedOutput()
    67  	if err != nil {
    68  		// If the command fails (for instance if we're running on some other
    69  		// platform) then CurrentSeries should be unknown.
    70  		c.Assert(s, gc.Equals, "n/a")
    71  	} else {
    72  		c.Assert(string(out), gc.Equals, "Codename:\t"+s+"\n")
    73  	}
    74  }