Merge lp:~fgimenez/snappy/integration-tests-verbosity-flag into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by Federico Gimenez
Status: Needs review
Proposed branch: lp:~fgimenez/snappy/integration-tests-verbosity-flag
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Diff against target: 766 lines (+345/-35)
16 files modified
_integration-tests/main.go (+8/-1)
_integration-tests/tests/base_test.go (+16/-0)
_integration-tests/testutils/autopkgtest/autopkgtest.go (+2/-2)
_integration-tests/testutils/autopkgtest/autopkgtest_test.go (+26/-1)
_integration-tests/testutils/autopkgtest/ssh.go (+14/-2)
_integration-tests/testutils/build/build.go (+4/-4)
_integration-tests/testutils/build/build_test.go (+0/-1)
_integration-tests/testutils/cli/cli.go (+4/-3)
_integration-tests/testutils/common/common.go (+8/-7)
_integration-tests/testutils/config/config.go (+7/-5)
_integration-tests/testutils/config/config_test.go (+5/-3)
_integration-tests/testutils/image/image.go (+2/-2)
_integration-tests/testutils/partition/partition.go (+0/-2)
_integration-tests/testutils/testutils.go (+3/-2)
_integration-tests/testutils/tlog/tlog.go (+101/-0)
_integration-tests/testutils/tlog/tlog_test.go (+145/-0)
To merge this branch: bzr merge lp:~fgimenez/snappy/integration-tests-verbosity-flag
Reviewer Review Type Date Requested Status
Leo Arias (community) Needs Information
Review via email: mp+273670@code.launchpad.net

Commit message

Integration tests verbosity flag

Description of the change

Integration tests verbosity flag.

There are currently only two levels implemented, DebugLevel (the default) and InfoLevel. The level can be set with:

  tlog.SetLevel(tlog.InfoLevel)

There are functions for logging at each level, tlog.Debugf and tlog.Infof. Currently all the previous fmt.Print have been changed into tlog.Debugf, which makes that, being DebugLevel the default, without flags the test run logs the same output as before. To get a less verbose output:

  $ go run _integration-tests/main.go -loglevel info

There are currently no messages sent with tlog.Infof, the previous command only logs gocheck and adt-run related output.

To post a comment you must log in.
Revision history for this message
Leo Arias (elopio) wrote :

Thanks for working on this.

I'm wondering if we should use log.Print instead of fmt.Fprint. I've never done logging in go before, so I think it would be nice to get a review from mvo, sergio or chipaca. Can you please ask one of them tomorrow to take a quick look?

review: Needs Information
Revision history for this message
Michael Vogt (mvo) wrote :

Thanks for this branch! Its a bit unfortunate IMO that the native go logger is so limited, no support for log levels is really limiting and means that everyone has to reimplement a logger:/

We have a logger in snappy/logger already, I wonder if we could reuse that here instead of having a additional one? The one we have is a little more limited right now, i.e. you can suppress log messages right now, but it looks like it would be worthwhile to add there instead of having two versions. What do you think? Or is there something in the tests that makes sharing the logger unsuitable?

Revision history for this message
Federico Gimenez (fgimenez) wrote :

@Michael I totally agree, it would be nice if we could use a single logger.

I had a look at the logger package before beginning with this and it seemed to me that it was oriented to a cli tool, with two "channels" to output messages but without giving the user the option to decide which kind of messages to show. In the case of the test logger there should be just one way of output (it shouldn't use syslog), but with different levels, and the level to be used should be selectable at invocation time. If we can make both things work at once it would be great :)

When we'll execute the tests on CI, for instance, we would like always the more verbose output on stdout, while perhaps a developer executing the suite locally would prefer to have a less verbose output, and enable the verbose one if there's an error.

Leo, what do you think?

Unmerged revisions

746. By Federico Gimenez

boot log also affected by the flag

745. By Federico Gimenez

set level in testbed moved to base_test's init

744. By Federico Gimenez

loglevel flag; passing the loglevel to the testbedd in config

743. By Federico Gimenez

removed calls to fmt.Print; added new line

742. By Federico Gimenez

tlog package

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file '_integration-tests/main.go'
--- _integration-tests/main.go 2015-09-02 03:12:43 +0000
+++ _integration-tests/main.go 2015-10-07 15:29:59 +0000
@@ -31,6 +31,7 @@
31 "launchpad.net/snappy/_integration-tests/testutils/build"31 "launchpad.net/snappy/_integration-tests/testutils/build"
32 "launchpad.net/snappy/_integration-tests/testutils/config"32 "launchpad.net/snappy/_integration-tests/testutils/config"
33 "launchpad.net/snappy/_integration-tests/testutils/image"33 "launchpad.net/snappy/_integration-tests/testutils/image"
34 "launchpad.net/snappy/_integration-tests/testutils/tlog"
34)35)
3536
36const (37const (
@@ -39,6 +40,7 @@
39 defaultChannel = "edge"40 defaultChannel = "edge"
40 defaultSSHPort = 2241 defaultSSHPort = 22
41 dataOutputDir = "_integration-tests/data/output/"42 dataOutputDir = "_integration-tests/data/output/"
43 defaultLogLevel = "debug"
42)44)
4345
44var configFileName = filepath.Join(dataOutputDir, "testconfig.json")46var configFileName = filepath.Join(dataOutputDir, "testconfig.json")
@@ -70,10 +72,15 @@
70 rollback = flag.Bool("rollback", false,72 rollback = flag.Bool("rollback", false,
71 "If this flag is used, the image will be updated and then rolled back before running the tests.")73 "If this flag is used, the image will be updated and then rolled back before running the tests.")
72 outputDir = flag.String("output-dir", defaultOutputDir, "Directory where test artifacts will be stored.")74 outputDir = flag.String("output-dir", defaultOutputDir, "Directory where test artifacts will be stored.")
75 logLevel = flag.String("loglevel", defaultLogLevel, "Log verbosity level, info or debug.")
73 )76 )
7477
75 flag.Parse()78 flag.Parse()
7679
80 if err := tlog.SetTextLevel(*logLevel); err != nil {
81 log.Panic(err.Error())
82 }
83
77 build.Assets(*useSnappyFromBranch, *arch)84 build.Assets(*useSnappyFromBranch, *arch)
7885
79 // TODO: generate the files out of the source tree. --elopio - 2015-07-1586 // TODO: generate the files out of the source tree. --elopio - 2015-07-15
@@ -85,7 +92,7 @@
85 // TODO: pass the config as arguments to the test binaries.92 // TODO: pass the config as arguments to the test binaries.
86 // --elopio - 2015-07-1593 // --elopio - 2015-07-15
87 cfg := config.NewConfig(94 cfg := config.NewConfig(
88 configFileName, *imgRelease, *imgChannel, *targetRelease, *targetChannel,95 configFileName, *imgRelease, *imgChannel, *targetRelease, *targetChannel, *logLevel,
89 remoteTestbed, *update, *rollback)96 remoteTestbed, *update, *rollback)
90 cfg.Write()97 cfg.Write()
9198
9299
=== modified file '_integration-tests/tests/base_test.go'
--- _integration-tests/tests/base_test.go 2015-09-03 11:35:59 +0000
+++ _integration-tests/tests/base_test.go 2015-10-07 15:29:59 +0000
@@ -21,13 +21,29 @@
2121
22import (22import (
23 "io"23 "io"
24 "log"
24 "os"25 "os"
25 "testing"26 "testing"
2627
28 "launchpad.net/snappy/_integration-tests/testutils/config"
27 "launchpad.net/snappy/_integration-tests/testutils/report"29 "launchpad.net/snappy/_integration-tests/testutils/report"
28 "launchpad.net/snappy/_integration-tests/testutils/runner"30 "launchpad.net/snappy/_integration-tests/testutils/runner"
31 "launchpad.net/snappy/_integration-tests/testutils/tlog"
29)32)
3033
34func init() {
35 cfg, err := config.ReadConfig(
36 "_integration-tests/data/output/testconfig.json")
37
38 if err != nil {
39 log.Panic(err.Error())
40 }
41
42 if err = tlog.SetTextLevel(cfg.LogLevel); err != nil {
43 log.Panic(err.Error())
44 }
45}
46
31// Hook up gocheck into the "go test" runner.47// Hook up gocheck into the "go test" runner.
32func Test(t *testing.T) {48func Test(t *testing.T) {
33 output := io.MultiWriter(49 output := io.MultiWriter(
3450
=== modified file '_integration-tests/testutils/autopkgtest/autopkgtest.go'
--- _integration-tests/testutils/autopkgtest/autopkgtest.go 2015-08-24 17:43:59 +0000
+++ _integration-tests/testutils/autopkgtest/autopkgtest.go 2015-10-07 15:29:59 +0000
@@ -20,11 +20,11 @@
20package autopkgtest20package autopkgtest
2121
22import (22import (
23 "fmt"
24 "path/filepath"23 "path/filepath"
25 "strings"24 "strings"
2625
27 "launchpad.net/snappy/_integration-tests/testutils"26 "launchpad.net/snappy/_integration-tests/testutils"
27 "launchpad.net/snappy/_integration-tests/testutils/tlog"
28 "launchpad.net/snappy/_integration-tests/testutils/tpl"28 "launchpad.net/snappy/_integration-tests/testutils/tpl"
29)29)
3030
@@ -75,7 +75,7 @@
75 return75 return
76 }76 }
7777
78 fmt.Println("Calling adt-run...")78 tlog.Debugf("Calling adt-run...")
79 outputDir := filepath.Join(a.testArtifactsPath, "output")79 outputDir := filepath.Join(a.testArtifactsPath, "output")
80 prepareTargetDir(outputDir)80 prepareTargetDir(outputDir)
8181
8282
=== modified file '_integration-tests/testutils/autopkgtest/autopkgtest_test.go'
--- _integration-tests/testutils/autopkgtest/autopkgtest_test.go 2015-09-30 04:39:25 +0000
+++ _integration-tests/testutils/autopkgtest/autopkgtest_test.go 2015-10-07 15:29:59 +0000
@@ -29,6 +29,8 @@
29 "testing"29 "testing"
3030
31 "gopkg.in/check.v1"31 "gopkg.in/check.v1"
32
33 "launchpad.net/snappy/_integration-tests/testutils/tlog"
32)34)
3335
34// Hook up check.v1 into the "go test" runner36// Hook up check.v1 into the "go test" runner
@@ -183,6 +185,22 @@
183 c.Assert(err, check.NotNil, check.Commentf("Expected error from tpl not received!"))185 c.Assert(err, check.NotNil, check.Commentf("Expected error from tpl not received!"))
184}186}
185187
188func (s *AutopkgtestSuite) TestAdtRunLocalHonoursLogLevel(c *check.C) {
189 backTlogGetLevel := tlogGetLevel
190 defer func() { tlogGetLevel = backTlogGetLevel }()
191 tlogGetLevel = func() tlog.Level {
192 return tlog.InfoLevel
193 }
194 s.subject.AdtRunLocal(imgPath)
195
196 outputDir := outputDir(testArtifactsPath)
197 expectedExecCommadCall := adtrunLocalCmd(controlFile, sourceCodePath, outputDir, imgPath)
198
199 c.Assert(s.execCalls[expectedExecCommadCall],
200 check.Equals, 1,
201 check.Commentf("Expected call %s not executed 1 time", expectedExecCommadCall))
202}
203
186func tplExecuteCmd(tplFile, outputFile string, data interface{}) string {204func tplExecuteCmd(tplFile, outputFile string, data interface{}) string {
187 return fmt.Sprint(tplFile, outputFile, data)205 return fmt.Sprint(tplFile, outputFile, data)
188}206}
@@ -192,7 +210,14 @@
192}210}
193211
194func adtrunLocalCmd(controlFile, sourceCodePath, outputDir, imgPath string) string {212func adtrunLocalCmd(controlFile, sourceCodePath, outputDir, imgPath string) string {
195 options := fmt.Sprintf("--- ssh -s /usr/share/autopkgtest/ssh-setup/snappy -- -b -i %s", imgPath)213 var showBoot string
214
215 if tlogGetLevel() == tlog.DebugLevel {
216 showBoot = " -b"
217 }
218
219 options := fmt.Sprintf("--- ssh -s /usr/share/autopkgtest/ssh-setup/snappy --%s -i %s",
220 showBoot, imgPath)
196 return adtrunCommonCmd(controlFile, sourceCodePath, outputDir, options)221 return adtrunCommonCmd(controlFile, sourceCodePath, outputDir, options)
197}222}
198223
199224
=== modified file '_integration-tests/testutils/autopkgtest/ssh.go'
--- _integration-tests/testutils/autopkgtest/ssh.go 2015-09-30 04:39:25 +0000
+++ _integration-tests/testutils/autopkgtest/ssh.go 2015-10-07 15:29:59 +0000
@@ -24,6 +24,8 @@
24 "os"24 "os"
25 "path/filepath"25 "path/filepath"
26 "strconv"26 "strconv"
27
28 "launchpad.net/snappy/_integration-tests/testutils/tlog"
27)29)
2830
29const (31const (
@@ -31,9 +33,19 @@
31 sshTimeout = 60033 sshTimeout = 600
32)34)
3335
36var (
37 // dependency aliasing
38 tlogGetLevel = tlog.GetLevel
39)
40
34func kvmSSHOptions(imagePath string) string {41func kvmSSHOptions(imagePath string) string {
35 return fmt.Sprint(commonSSHOptions,42 var showBoot string
36 "-s /usr/share/autopkgtest/ssh-setup/snappy -- -b -i ", imagePath)43
44 if tlogGetLevel() == tlog.DebugLevel {
45 showBoot = " -b"
46 }
47 return fmt.Sprintf(commonSSHOptions+
48 "-s /usr/share/autopkgtest/ssh-setup/snappy --%s -i "+imagePath, showBoot)
37}49}
3850
39func remoteTestbedSSHOptions(testbedIP string, testbedPort int) string {51func remoteTestbedSSHOptions(testbedIP string, testbedPort int) string {
4052
=== modified file '_integration-tests/testutils/build/build.go'
--- _integration-tests/testutils/build/build.go 2015-09-14 09:38:49 +0000
+++ _integration-tests/testutils/build/build.go 2015-10-07 15:29:59 +0000
@@ -20,12 +20,12 @@
20package build20package build
2121
22import (22import (
23 "fmt"
24 "os"23 "os"
25 "path/filepath"24 "path/filepath"
26 "strings"25 "strings"
2726
28 "launchpad.net/snappy/_integration-tests/testutils"27 "launchpad.net/snappy/_integration-tests/testutils"
28 "launchpad.net/snappy/_integration-tests/testutils/tlog"
29)29)
3030
31const (31const (
@@ -71,17 +71,17 @@
71}71}
7272
73func buildSnappyCLI(arch string) {73func buildSnappyCLI(arch string) {
74 fmt.Println("Building snappy CLI...")74 tlog.Debugf("Building snappy CLI...")
75 goCall(arch, buildSnappyCliCmd)75 goCall(arch, buildSnappyCliCmd)
76}76}
7777
78func buildSnapd(arch string) {78func buildSnapd(arch string) {
79 fmt.Println("Building snapd...")79 tlog.Debugf("Building snapd...")
80 goCall(arch, buildSnapdCmd)80 goCall(arch, buildSnapdCmd)
81}81}
8282
83func buildTests(arch string) {83func buildTests(arch string) {
84 fmt.Println("Building tests...")84 tlog.Debugf("Building tests...")
8585
86 goCall(arch, buildTestCmd)86 goCall(arch, buildTestCmd)
87 // XXX Go test 1.3 does not have the output flag, so we move the87 // XXX Go test 1.3 does not have the output flag, so we move the
8888
=== modified file '_integration-tests/testutils/build/build_test.go'
--- _integration-tests/testutils/build/build_test.go 2015-09-09 13:12:16 +0000
+++ _integration-tests/testutils/build/build_test.go 2015-10-07 15:29:59 +0000
@@ -127,7 +127,6 @@
127127
128 buildCall := s.execCalls[buildTestCmd]128 buildCall := s.execCalls[buildTestCmd]
129129
130 fmt.Println(s.execCalls)
131 c.Assert(buildCall, check.Equals, 1,130 c.Assert(buildCall, check.Equals, 1,
132 check.Commentf("Expected 1 call to execCommand with %s, got %d",131 check.Commentf("Expected 1 call to execCommand with %s, got %d",
133 buildTestCmd, buildCall))132 buildTestCmd, buildCall))
134133
=== modified file '_integration-tests/testutils/cli/cli.go'
--- _integration-tests/testutils/cli/cli.go 2015-10-02 12:02:16 +0000
+++ _integration-tests/testutils/cli/cli.go 2015-10-07 15:29:59 +0000
@@ -20,12 +20,13 @@
20package cli20package cli
2121
22import (22import (
23 "fmt"
24 "os"23 "os"
25 "os/exec"24 "os/exec"
26 "strings"25 "strings"
2726
28 "gopkg.in/check.v1"27 "gopkg.in/check.v1"
28
29 "launchpad.net/snappy/_integration-tests/testutils/tlog"
29)30)
3031
31var execCommand = exec.Command32var execCommand = exec.Command
@@ -55,10 +56,10 @@
55// ExecCommandErr executes a shell command and returns a string with the output56// ExecCommandErr executes a shell command and returns a string with the output
56// of the command and eventually the obtained error57// of the command and eventually the obtained error
57func ExecCommandErr(cmds ...string) (output string, err error) {58func ExecCommandErr(cmds ...string) (output string, err error) {
58 fmt.Println(strings.Join(cmds, " "))59 tlog.Debugf(strings.Join(cmds, " "))
59 cmd := execCommand(cmds[0], cmds[1:]...)60 cmd := execCommand(cmds[0], cmds[1:]...)
60 outputByte, err := cmd.CombinedOutput()61 outputByte, err := cmd.CombinedOutput()
61 output = string(outputByte)62 output = string(outputByte)
62 fmt.Print(output)63 tlog.Debugf(output)
63 return64 return
64}65}
6566
=== modified file '_integration-tests/testutils/common/common.go'
--- _integration-tests/testutils/common/common.go 2015-10-01 09:37:04 +0000
+++ _integration-tests/testutils/common/common.go 2015-10-07 15:29:59 +0000
@@ -33,6 +33,7 @@
33 "launchpad.net/snappy/_integration-tests/testutils/cli"33 "launchpad.net/snappy/_integration-tests/testutils/cli"
34 "launchpad.net/snappy/_integration-tests/testutils/config"34 "launchpad.net/snappy/_integration-tests/testutils/config"
35 "launchpad.net/snappy/_integration-tests/testutils/partition"35 "launchpad.net/snappy/_integration-tests/testutils/partition"
36 "launchpad.net/snappy/_integration-tests/testutils/tlog"
36)37)
3738
38const (39const (
@@ -102,11 +103,11 @@
102 c.TestName(), contents))103 c.TestName(), contents))
103 } else {104 } else {
104 if CheckRebootMark("") {105 if CheckRebootMark("") {
105 c.Logf("****** Running %s", c.TestName())106 tlog.Debugf("****** Running %s", c.TestName())
106 SetSavedVersion(c, GetCurrentUbuntuCoreVersion(c))107 SetSavedVersion(c, GetCurrentUbuntuCoreVersion(c))
107 } else {108 } else {
108 if AfterReboot(c) {109 if AfterReboot(c) {
109 c.Logf("****** Resuming %s after reboot", c.TestName())110 tlog.Debugf("****** Resuming %s after reboot", c.TestName())
110 } else {111 } else {
111 c.Skip(fmt.Sprintf("****** Skipped %s after reboot caused by %s",112 c.Skip(fmt.Sprintf("****** Skipped %s after reboot caused by %s",
112 c.TestName(), os.Getenv("ADT_REBOOT_MARK")))113 c.TestName(), os.Getenv("ADT_REBOOT_MARK")))
@@ -131,7 +132,7 @@
131 partition.MakeWritable(c, target)132 partition.MakeWritable(c, target)
132 defer partition.MakeReadonly(c, target)133 defer partition.MakeReadonly(c, target)
133 original := filepath.Join(target, channelCfgFile)134 original := filepath.Join(target, channelCfgFile)
134 c.Logf("Restoring %s...", original)135 tlog.Debugf("Restoring %s...", original)
135 cli.ExecCommand(c, "sudo", "mv", backup, original)136 cli.ExecCommand(c, "sudo", "mv", backup, original)
136 }137 }
137 }138 }
@@ -162,7 +163,7 @@
162}163}
163164
164func replaceSystemImageValues(c *check.C, file, release, channel, version string) {165func replaceSystemImageValues(c *check.C, file, release, channel, version string) {
165 c.Log("Switching the system image conf...")166 tlog.Debugf("Switching the system image conf...")
166 replaceRegex := map[string]string{167 replaceRegex := map[string]string{
167 release: `s#channel: ubuntu-core/.*/\(.*\)#channel: ubuntu-core/%s/\1#`,168 release: `s#channel: ubuntu-core/.*/\(.*\)#channel: ubuntu-core/%s/\1#`,
168 channel: `s#channel: ubuntu-core/\(.*\)/.*#channel: ubuntu-core/\1/%s#`,169 channel: `s#channel: ubuntu-core/\(.*\)/.*#channel: ubuntu-core/\1/%s#`,
@@ -210,13 +211,13 @@
210211
211// CallFakeUpdate calls snappy update after faking the current version212// CallFakeUpdate calls snappy update after faking the current version
212func CallFakeUpdate(c *check.C) string {213func CallFakeUpdate(c *check.C) string {
213 c.Log("Preparing fake and calling update.")214 tlog.Debugf("Preparing fake and calling update.")
214 fakeAvailableUpdate(c)215 fakeAvailableUpdate(c)
215 return cli.ExecCommand(c, "sudo", "snappy", "update")216 return cli.ExecCommand(c, "sudo", "snappy", "update")
216}217}
217218
218func fakeAvailableUpdate(c *check.C) {219func fakeAvailableUpdate(c *check.C) {
219 c.Log("Faking an available update...")220 tlog.Debugf("Faking an available update...")
220 currentVersion := GetCurrentUbuntuCoreVersion(c)221 currentVersion := GetCurrentUbuntuCoreVersion(c)
221 switchChannelVersionWithBackup(c, currentVersion-1)222 switchChannelVersionWithBackup(c, currentVersion-1)
222 SetSavedVersion(c, currentVersion-1)223 SetSavedVersion(c, currentVersion-1)
@@ -245,7 +246,7 @@
245246
246// RebootWithMark requests a reboot using a specified mark.247// RebootWithMark requests a reboot using a specified mark.
247func RebootWithMark(c *check.C, mark string) {248func RebootWithMark(c *check.C, mark string) {
248 c.Log("Preparing reboot with mark " + mark)249 tlog.Debugf("Preparing reboot with mark " + mark)
249 err := ioutil.WriteFile(needsRebootFile, []byte(mark), 0777)250 err := ioutil.WriteFile(needsRebootFile, []byte(mark), 0777)
250 c.Assert(err, check.IsNil, check.Commentf("Error writing needs-reboot file: %v", err))251 c.Assert(err, check.IsNil, check.Commentf("Error writing needs-reboot file: %v", err))
251}252}
252253
=== modified file '_integration-tests/testutils/config/config.go'
--- _integration-tests/testutils/config/config.go 2015-07-31 07:55:33 +0000
+++ _integration-tests/testutils/config/config.go 2015-10-07 15:29:59 +0000
@@ -21,9 +21,10 @@
2121
22import (22import (
23 "encoding/json"23 "encoding/json"
24 "fmt"
25 "io/ioutil"24 "io/ioutil"
26 "log"25 "log"
26
27 "launchpad.net/snappy/_integration-tests/testutils/tlog"
27)28)
2829
29// Config contains the values to pass to the test bed from the host.30// Config contains the values to pass to the test bed from the host.
@@ -33,24 +34,25 @@
33 Channel string34 Channel string
34 TargetRelease string35 TargetRelease string
35 TargetChannel string36 TargetChannel string
37 LogLevel string
36 RemoteTestbed bool38 RemoteTestbed bool
37 Update bool39 Update bool
38 Rollback bool40 Rollback bool
39}41}
4042
41// NewConfig is the Config constructor43// NewConfig is the Config constructor
42func NewConfig(fileName, release, channel, targetRelease, targetChannel string, remoteTestbed, update, rollback bool) *Config {44func NewConfig(fileName, release, channel, targetRelease, targetChannel, logLevel string, remoteTestbed, update, rollback bool) *Config {
43 return &Config{45 return &Config{
44 FileName: fileName, Release: release, Channel: channel,46 FileName: fileName, Release: release, Channel: channel,
45 TargetRelease: targetRelease, TargetChannel: targetChannel,47 TargetRelease: targetRelease, TargetChannel: targetChannel, LogLevel: logLevel,
46 RemoteTestbed: remoteTestbed, Update: update, Rollback: rollback,48 RemoteTestbed: remoteTestbed, Update: update, Rollback: rollback,
47 }49 }
48}50}
4951
50// Write writes the config to a file that will be copied to the test bed.52// Write writes the config to a file that will be copied to the test bed.
51func (cfg Config) Write() {53func (cfg Config) Write() {
52 fmt.Println("Writing test config...")54 tlog.Debugf("Writing test config...")
53 fmt.Println(cfg)55 tlog.Debugf("Config: %v", cfg)
54 encoded, err := json.Marshal(cfg)56 encoded, err := json.Marshal(cfg)
55 if err != nil {57 if err != nil {
56 log.Panicf("Error encoding the test config: %v", err)58 log.Panicf("Error encoding the test config: %v", err)
5759
=== modified file '_integration-tests/testutils/config/config_test.go'
--- _integration-tests/testutils/config/config_test.go 2015-07-31 07:55:33 +0000
+++ _integration-tests/testutils/config/config_test.go 2015-10-07 15:29:59 +0000
@@ -26,7 +26,7 @@
26 "path/filepath"26 "path/filepath"
27 "testing"27 "testing"
2828
29 check "gopkg.in/check.v1"29 "gopkg.in/check.v1"
30)30)
3131
32// Hook up check.v1 into the "go test" runner32// Hook up check.v1 into the "go test" runner
@@ -46,7 +46,7 @@
46func testConfigStruct(fileName string) *Config {46func testConfigStruct(fileName string) *Config {
47 return NewConfig(47 return NewConfig(
48 fileName,48 fileName,
49 "testrelease", "testchannel", "testtargetrelease", "testtargetchannel",49 "testrelease", "testchannel", "testtargetrelease", "testtargetchannel", "info",
50 true, true, true)50 true, true, true)
51}51}
52func testConfigContents(fileName string) string {52func testConfigContents(fileName string) string {
@@ -56,6 +56,7 @@
56 `"Channel":"testchannel",` +56 `"Channel":"testchannel",` +
57 `"TargetRelease":"testtargetrelease",` +57 `"TargetRelease":"testtargetrelease",` +
58 `"TargetChannel":"testtargetchannel",` +58 `"TargetChannel":"testtargetchannel",` +
59 `"LogLevel":"info",` +
59 `"RemoteTestbed":true,` +60 `"RemoteTestbed":true,` +
60 `"Update":true,` +61 `"Update":true,` +
61 `"Rollback":true` +62 `"Rollback":true` +
@@ -102,6 +103,7 @@
102 `"Channel":"testchannel",` +103 `"Channel":"testchannel",` +
103 `"TargetRelease":"testtargetrelease",` +104 `"TargetRelease":"testtargetrelease",` +
104 `"TargetChannel":"testtargetchannel",` +105 `"TargetChannel":"testtargetchannel",` +
106 `"LogLevel":"info",` +
105 `"RemoteTestbed":false,` +107 `"RemoteTestbed":false,` +
106 `"Update":true,` +108 `"Update":true,` +
107 `"Rollback":true` +109 `"Rollback":true` +
@@ -113,7 +115,7 @@
113115
114 testConfigStruct := NewConfig(116 testConfigStruct := NewConfig(
115 configFileName,117 configFileName,
116 "testrelease", "testchannel", "testtargetrelease", "testtargetchannel",118 "testrelease", "testchannel", "testtargetrelease", "testtargetchannel", "info",
117 false, true, true)119 false, true, true)
118120
119 c.Assert(err, check.IsNil, check.Commentf("Error reading config: %v", err))121 c.Assert(err, check.IsNil, check.Commentf("Error reading config: %v", err))
120122
=== modified file '_integration-tests/testutils/image/image.go'
--- _integration-tests/testutils/image/image.go 2015-09-15 09:33:22 +0000
+++ _integration-tests/testutils/image/image.go 2015-10-07 15:29:59 +0000
@@ -20,11 +20,11 @@
20package image20package image
2121
22import (22import (
23 "fmt"
24 "path/filepath"23 "path/filepath"
25 "strings"24 "strings"
2625
27 "launchpad.net/snappy/_integration-tests/testutils"26 "launchpad.net/snappy/_integration-tests/testutils"
27 "launchpad.net/snappy/_integration-tests/testutils/tlog"
28)28)
2929
30// Image type encapsulates image actions30// Image type encapsulates image actions
@@ -42,7 +42,7 @@
4242
43// UdfCreate forms and executes the UDF command for creating the image43// UdfCreate forms and executes the UDF command for creating the image
44func (img Image) UdfCreate() (string, error) {44func (img Image) UdfCreate() (string, error) {
45 fmt.Println("Creating image...")45 tlog.Debugf("Creating image...")
4646
47 imageDir := filepath.Join(img.baseDir, "image")47 imageDir := filepath.Join(img.baseDir, "image")
4848
4949
=== modified file '_integration-tests/testutils/partition/partition.go'
--- _integration-tests/testutils/partition/partition.go 2015-10-02 12:02:16 +0000
+++ _integration-tests/testutils/partition/partition.go 2015-10-07 15:29:59 +0000
@@ -21,7 +21,6 @@
2121
22import (22import (
23 "bufio"23 "bufio"
24 "fmt"
25 "regexp"24 "regexp"
26 "strings"25 "strings"
2726
@@ -80,7 +79,6 @@
80 for scanner.Scan() {79 for scanner.Scan() {
81 fields := strings.Fields(scanner.Text())80 fields := strings.Fields(scanner.Text())
82 if match, _ := regexp.MatchString("^[0-9]+w$", fields[3]); match {81 if match, _ := regexp.MatchString("^[0-9]+w$", fields[3]); match {
83 fmt.Printf("match! %s", fields[3])
84 return fields[3], nil82 return fields[3], nil
85 }83 }
86 }84 }
8785
=== modified file '_integration-tests/testutils/testutils.go'
--- _integration-tests/testutils/testutils.go 2015-07-28 12:45:05 +0000
+++ _integration-tests/testutils/testutils.go 2015-10-07 15:29:59 +0000
@@ -20,11 +20,12 @@
20package testutils20package testutils
2121
22import (22import (
23 "fmt"
24 "log"23 "log"
25 "os"24 "os"
26 "os/exec"25 "os/exec"
27 "strings"26 "strings"
27
28 "launchpad.net/snappy/_integration-tests/testutils/tlog"
28)29)
2930
30// PrepareTargetDir creates the given target directory, removing it previously if it didn't exist31// PrepareTargetDir creates the given target directory, removing it previously if it didn't exist
@@ -47,7 +48,7 @@
4748
48// ExecCommand executes the given command and pipes the results to os.Stdout and os.Stderr, returning the resulting error49// ExecCommand executes the given command and pipes the results to os.Stdout and os.Stderr, returning the resulting error
49func ExecCommand(cmds ...string) error {50func ExecCommand(cmds ...string) error {
50 fmt.Println(strings.Join(cmds, " "))51 tlog.Debugf(strings.Join(cmds, " "))
5152
52 cmd := exec.Command(cmds[0], cmds[1:]...)53 cmd := exec.Command(cmds[0], cmds[1:]...)
53 cmd.Stdout = os.Stdout54 cmd.Stdout = os.Stdout
5455
=== added directory '_integration-tests/testutils/tlog'
=== added file '_integration-tests/testutils/tlog/tlog.go'
--- _integration-tests/testutils/tlog/tlog.go 1970-01-01 00:00:00 +0000
+++ _integration-tests/testutils/tlog/tlog.go 2015-10-07 15:29:59 +0000
@@ -0,0 +1,101 @@
1// -*- Mode: Go; indent-tabs-mode: t -*-
2
3/*
4 * Copyright (C) 2015 Canonical Ltd
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20package tlog
21
22import (
23 "fmt"
24 "io"
25 "os"
26)
27
28// Level represents the logging level
29type Level uint8
30
31// ErrLogLevel is returned when a not recognized textual log level is passed
32// to SetTextLevel
33type ErrLogLevel struct {
34 s string
35}
36
37func (e *ErrLogLevel) Error() string {
38 return fmt.Sprintf("The level %s is not supported", e.s)
39}
40
41const (
42 // InfoLevel is the less verbose mode
43 InfoLevel Level = iota
44 // DebugLevel is the most verbose mode
45 DebugLevel
46)
47
48// Logger is the type of the internal instance holding the state
49type Logger struct {
50 output io.Writer // the output goes through this writer
51 level Level // log level, functions with a level below this won't write to output
52}
53
54// this variable carries the underlaying state of the logger
55var l = &Logger{output: os.Stdout, level: DebugLevel}
56
57// GetOutput is the getter for the output writter
58func GetOutput() io.Writer {
59 return l.output
60}
61
62// SetOutput is the setter for the output writter
63func SetOutput(w io.Writer) {
64 l.output = w
65}
66
67// GetLevel is the getter for the log level
68func GetLevel() Level {
69 return l.level
70}
71
72// SetLevel is the getter for the log level
73func SetLevel(lvl Level) {
74 l.level = lvl
75}
76
77// SetTextLevel is the getter for the log level
78func SetTextLevel(s string) (err error) {
79 switch s {
80 case "info":
81 SetLevel(InfoLevel)
82 return
83 case "debug":
84 SetLevel(DebugLevel)
85 return
86 default:
87 return &ErrLogLevel{s}
88 }
89}
90
91// Debugf outputs debug messages
92func Debugf(format string, args ...interface{}) {
93 if l.level >= DebugLevel {
94 fmt.Fprintf(l.output, format+"\n", args...)
95 }
96}
97
98// Infof outputs info messages
99func Infof(format string, args ...interface{}) {
100 fmt.Fprintf(l.output, format+"\n", args...)
101}
0102
=== added file '_integration-tests/testutils/tlog/tlog_test.go'
--- _integration-tests/testutils/tlog/tlog_test.go 1970-01-01 00:00:00 +0000
+++ _integration-tests/testutils/tlog/tlog_test.go 2015-10-07 15:29:59 +0000
@@ -0,0 +1,145 @@
1// -*- Mode: Go; indent-tabs-mode: t -*-
2
3/*
4 * Copyright (C) 2015 Canonical Ltd
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20package tlog
21
22import (
23 "bytes"
24 "fmt"
25 "os"
26 "testing"
27
28 "gopkg.in/check.v1"
29)
30
31// Hook up check.v1 into the "go test" runner
32func Test(t *testing.T) { check.TestingT(t) }
33
34// Different suite so that we don't initialize fields
35// of the subject and can do assertions on the default
36// values
37type logTestDefaultValuesSuite struct {
38}
39
40var _ = check.Suite(&logTestDefaultValuesSuite{})
41
42func (s *logTestDefaultValuesSuite) TestDefaultOutput(c *check.C) {
43 output := GetOutput()
44
45 c.Assert(output, check.Equals, os.Stdout)
46}
47
48func (s *logTestDefaultValuesSuite) TestDefaultLevel(c *check.C) {
49 level := GetLevel()
50
51 c.Assert(level, check.Equals, DebugLevel)
52}
53
54type logTestSuite struct {
55 output bytes.Buffer
56}
57
58var _ = check.Suite(&logTestSuite{})
59
60func (s *logTestSuite) SetUpSuite(c *check.C) {
61 SetOutput(&s.output)
62}
63
64func (s *logTestSuite) SetUpTest(c *check.C) {
65 s.output.Reset()
66 SetLevel(DebugLevel)
67}
68
69func (s *logTestSuite) TestLogWritesDebugOutput(c *check.C) {
70 msg := "this is a debug message"
71 Debugf(msg)
72
73 c.Assert(s.output.String(), check.Equals, msg+"\n")
74}
75
76func (s *logTestSuite) TestLogDoesNotWritesDebugOutputWhenLevelIsInfo(c *check.C) {
77 msg := "this is a debug message"
78
79 SetLevel(InfoLevel)
80 Debugf(msg)
81
82 c.Assert(s.output.String(), check.Equals, "")
83}
84
85func (s *logTestSuite) TestLogWritesDebugOutputWithFormat(c *check.C) {
86 msg := "this is a debug message with %d %s"
87 par1 := 2
88 par2 := "parameters"
89 expected := "this is a debug message with 2 parameters\n"
90
91 Debugf(msg, par1, par2)
92
93 c.Assert(s.output.String(), check.Equals, expected)
94}
95
96func (s *logTestSuite) TestLogWritesInfoOutput(c *check.C) {
97 msg := "this is a info message"
98 Infof(msg)
99
100 c.Assert(s.output.String(), check.Equals, msg+"\n")
101}
102
103func (s *logTestSuite) TestLogWritesInfoOutputWithFormat(c *check.C) {
104 msg := "this is a info message with %d %s"
105 par1 := 2
106 par2 := "parameters"
107 expected := "this is a info message with 2 parameters\n"
108
109 Infof(msg, par1, par2)
110
111 c.Assert(s.output.String(), check.Equals, expected)
112}
113
114func (s *logTestSuite) TestSetTextLevel(c *check.C) {
115 currentLvl := GetLevel()
116 defer SetLevel(currentLvl)
117 testCases := []struct {
118 textLvl string
119 level Level
120 }{{"info", InfoLevel},
121 {"debug", DebugLevel},
122 }
123 for _, testCase := range testCases {
124 err := SetTextLevel(testCase.textLvl)
125 lvl := GetLevel()
126
127 c.Check(err, check.IsNil)
128 c.Check(lvl, check.Equals, testCase.level)
129 }
130}
131
132func (s *logTestSuite) TestSetWrongTextLevel(c *check.C) {
133 currentLvl := GetLevel()
134 defer SetLevel(currentLvl)
135
136 wrongLvl := "not-supported-level"
137
138 err := SetTextLevel(wrongLvl)
139 lvl := GetLevel()
140
141 c.Assert(err.Error(), check.Equals,
142 fmt.Sprintf("The level %s is not supported", wrongLvl))
143
144 c.Assert(lvl, check.Equals, currentLvl)
145}

Subscribers

People subscribed via source and target branches