Skip to content

Commit

Permalink
many: introduce snapdenv and snapdenv.Testing
Browse files Browse the repository at this point in the history
introduce a new package snapdenv to present the common env options
for snapd components

start with snadpenv.Testing exposing SNAPPY_TESTING

it doesn't make sense for *util packages to use snapdenv directly,
as a consequence move (Set)UserAgent from httputil to snapdenv
  • Loading branch information
pedronis committed Mar 15, 2020
1 parent 5762767 commit c30b437
Show file tree
Hide file tree
Showing 27 changed files with 296 additions and 137 deletions.
6 changes: 3 additions & 3 deletions cmd/snap-repair/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2017 Canonical Ltd
* Copyright (C) 2017-2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
Expand All @@ -28,9 +28,9 @@ import (
"github.com/jessevdk/go-flags"

"github.com/snapcore/snapd/cmd"
"github.com/snapcore/snapd/httputil"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snapdenv"
)

var (
Expand Down Expand Up @@ -76,7 +76,7 @@ func run() error {
if osGetuid() != 0 {
return fmt.Errorf("must be run as root")
}
httputil.SetUserAgentFromVersion(cmd.Version, "snap-repair")
snapdenv.SetUserAgentFromVersion(cmd.Version, "snap-repair")

if err := parseArgs(os.Args[1:]); err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions cmd/snap-repair/main_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2017 Canonical Ltd
* Copyright (C) 2017-2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
Expand All @@ -27,8 +27,8 @@ import (

repair "github.com/snapcore/snapd/cmd/snap-repair"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/httputil"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snapdenv"
"github.com/snapcore/snapd/testutil"
)

Expand All @@ -49,7 +49,7 @@ type repairSuite struct {

func (r *repairSuite) SetUpSuite(c *C) {
r.baseRunnerSuite.SetUpSuite(c)
r.restore = httputil.SetUserAgentFromVersion("", "")
r.restore = snapdenv.SetUserAgentFromVersion("", "")
}

func (r *repairSuite) TearDownSuite(c *C) {
Expand Down
10 changes: 6 additions & 4 deletions cmd/snap-repair/runner.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2017 Canonical Ltd
* Copyright (C) 2017-2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
Expand Down Expand Up @@ -49,6 +49,7 @@ import (
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snapdenv"
"github.com/snapcore/snapd/strutil"
)

Expand Down Expand Up @@ -282,7 +283,8 @@ func NewRunner() *Runner {
sequenceNext: make(map[string]int),
}
opts := httputil.ClientOptions{
MayLogBody: false,
MayLogBody: false,
ProxyConnectHeader: http.Header{"User-Agent": []string{snapdenv.UserAgent()}},
TLSConfig: &tls.Config{
Time: run.now,
},
Expand Down Expand Up @@ -335,7 +337,7 @@ func (run *Runner) Fetch(brandID string, repairID int, revision int) (*asserts.R
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", httputil.UserAgent())
req.Header.Set("User-Agent", snapdenv.UserAgent())
req.Header.Set("Accept", "application/x.ubuntu.assertion")
if revision >= 0 {
req.Header.Set("If-None-Match", fmt.Sprintf(`"%d"`, revision))
Expand Down Expand Up @@ -440,7 +442,7 @@ func (run *Runner) Peek(brandID string, repairID int) (headers map[string]interf
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", httputil.UserAgent())
req.Header.Set("User-Agent", snapdenv.UserAgent())
req.Header.Set("Accept", "application/json")
return run.cli.Do(req)
}, func(resp *http.Response) error {
Expand Down
6 changes: 3 additions & 3 deletions cmd/snap-repair/runner_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2017 Canonical Ltd
* Copyright (C) 2017-2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
Expand Down Expand Up @@ -43,9 +43,9 @@ import (
"github.com/snapcore/snapd/asserts/sysdb"
repair "github.com/snapcore/snapd/cmd/snap-repair"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/httputil"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/snapdenv"
"github.com/snapcore/snapd/testutil"
)

Expand Down Expand Up @@ -170,7 +170,7 @@ type runnerSuite struct {

func (s *runnerSuite) SetUpSuite(c *C) {
s.baseRunnerSuite.SetUpSuite(c)
s.restore = httputil.SetUserAgentFromVersion("1", "snap-repair")
s.restore = snapdenv.SetUserAgentFromVersion("1", "snap-repair")
}

func (s *runnerSuite) TearDownSuite(c *C) {
Expand Down
7 changes: 5 additions & 2 deletions cmd/snap/cmd_auto_import.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2014-2016 Canonical Ltd
* Copyright (C) 2014-2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
Expand Down Expand Up @@ -40,6 +40,7 @@ import (
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snapdenv"
)

const autoImportsName = "auto-import.assert"
Expand All @@ -57,6 +58,8 @@ func autoImportCandidates() ([]string, error) {
}
defer f.Close()

isTesting := snapdenv.Testing()

scanner := bufio.NewScanner(f)
for scanner.Scan() {
l := strings.Fields(scanner.Text())
Expand Down Expand Up @@ -86,7 +89,7 @@ func autoImportCandidates() ([]string, error) {
continue
}
// skip all ram disks (unless in tests)
if !osutil.GetenvBool("SNAPPY_TESTING") && strings.HasPrefix(mountSrc, "/dev/ram") {
if !isTesting && strings.HasPrefix(mountSrc, "/dev/ram") {
continue
}

Expand Down
10 changes: 5 additions & 5 deletions cmd/snap/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2014-2015 Canonical Ltd
* Copyright (C) 2014-2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
Expand Down Expand Up @@ -37,19 +37,19 @@ import (
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/cmd"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/httputil"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snapdenv"
)

func init() {
// set User-Agent for when 'snap' talks to the store directly (snap download etc...)
httputil.SetUserAgentFromVersion(cmd.Version, "snap")
snapdenv.SetUserAgentFromVersion(cmd.Version, "snap")

if osutil.GetenvBool("SNAPD_DEBUG") || osutil.GetenvBool("SNAPPY_TESTING") {
if osutil.GetenvBool("SNAPD_DEBUG") || snapdenv.Testing() {
// in tests or when debugging, enforce the "tidy" lint checks
noticef = logger.Panicf
}
Expand Down Expand Up @@ -387,7 +387,7 @@ func mkClient() *client.Client {
cfg := &ClientConfig
// Set client user-agent when talking to the snapd daemon to the
// same value as when talking to the store.
cfg.UserAgent = httputil.UserAgent()
cfg.UserAgent = snapdenv.UserAgent()

cli := client.New(cfg)
goos := runtime.GOOS
Expand Down
6 changes: 3 additions & 3 deletions cmd/snapd/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2015 Canonical Ltd
* Copyright (C) 2015-2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
Expand Down Expand Up @@ -29,11 +29,11 @@ import (
"github.com/snapcore/snapd/cmd"
"github.com/snapcore/snapd/daemon"
"github.com/snapcore/snapd/errtracker"
"github.com/snapcore/snapd/httputil"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/sanity"
"github.com/snapcore/snapd/snapdenv"
"github.com/snapcore/snapd/systemd"
)

Expand Down Expand Up @@ -109,7 +109,7 @@ var checkRunningConditionsRetryDelay = 300 * time.Second

func run(ch chan os.Signal) error {
t0 := time.Now().Truncate(time.Millisecond)
httputil.SetUserAgentFromVersion(cmd.Version)
snapdenv.SetUserAgentFromVersion(cmd.Version)

d, err := daemon.New()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2015-2016 Canonical Ltd
* Copyright (C) 2015-2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
Expand Down Expand Up @@ -37,7 +37,6 @@ import (

"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/httputil"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/netutil"
Expand All @@ -47,6 +46,7 @@ import (
"github.com/snapcore/snapd/overlord/standby"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/polkit"
"github.com/snapcore/snapd/snapdenv"
"github.com/snapcore/snapd/store"
"github.com/snapcore/snapd/systemd"
)
Expand Down Expand Up @@ -340,7 +340,7 @@ func (d *Daemon) Init() error {

d.addRoutes()

logger.Noticef("started %v.", httputil.UserAgent())
logger.Noticef("started %v.", snapdenv.UserAgent())

return nil
}
Expand Down
6 changes: 3 additions & 3 deletions errtracker/errtracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ import (

"github.com/snapcore/snapd/arch"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/httputil"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snapdenv"
)

var (
Expand Down Expand Up @@ -481,7 +481,7 @@ func report(errMsg, dupSig string, extra map[string]string) (string, error) {
}

// see if we run in testing mode
if osutil.GetenvBool("SNAPPY_TESTING") {
if snapdenv.Testing() {
logger.Noticef("errtracker.Report is *not* sent because SNAPPY_TESTING is set")
logger.Noticef("report: %v", report)
return "oops-not-sent", nil
Expand All @@ -498,7 +498,7 @@ func report(errMsg, dupSig string, extra map[string]string) (string, error) {
return "", err
}
req.Header.Add("Content-Type", "application/octet-stream")
req.Header.Add("X-Whoopsie-Version", httputil.UserAgent())
req.Header.Add("X-Whoopsie-Version", snapdenv.UserAgent())
resp, err := client.Do(req)
if err != nil {
return "", err
Expand Down
4 changes: 2 additions & 2 deletions errtracker/errtracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/snapcore/snapd/errtracker"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snapdenv"
"github.com/snapcore/snapd/testutil"
)

Expand Down Expand Up @@ -232,8 +233,7 @@ func (s *ErrtrackerTestSuite) TestReport(c *C) {
}

func (s *ErrtrackerTestSuite) TestReportUnderTesting(c *C) {
os.Setenv("SNAPPY_TESTING", "1")
defer os.Unsetenv("SNAPPY_TESTING")
defer snapdenv.MockTesting(true)()

n := 0
prev := errtracker.SnapdVersion
Expand Down
15 changes: 9 additions & 6 deletions httputil/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2018 Canonical Ltd
* Copyright (C) 2018-2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
Expand Down Expand Up @@ -141,10 +141,13 @@ func (d *dialTLS) addLocalSSLCertificates() (err error) {
}

type ClientOptions struct {
Timeout time.Duration
TLSConfig *tls.Config
MayLogBody bool
Proxy func(*http.Request) (*url.URL, error)
Timeout time.Duration
TLSConfig *tls.Config
MayLogBody bool

Proxy func(*http.Request) (*url.URL, error)
ProxyConnectHeader http.Header

ExtraSSLCerts ExtraSSLCerts
}

Expand All @@ -159,7 +162,7 @@ func NewHTTPClient(opts *ClientOptions) *http.Client {
if opts.Proxy != nil {
transport.Proxy = opts.Proxy
}
transport.ProxyConnectHeader = http.Header{"User-Agent": []string{UserAgent()}}
transport.ProxyConnectHeader = opts.ProxyConnectHeader
// Remember the original ClientOptions.TLSConfig when making
// tls connection.
// Note that we only set TLSClientConfig here because it's extracted
Expand Down
7 changes: 3 additions & 4 deletions httputil/client_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2018 Canonical Ltd
* Copyright (C) 2018-2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
Expand Down Expand Up @@ -79,11 +79,9 @@ func (s *clientSuite) TestClientOptionsWithProxy(c *check.C) {
c.Check(url.String(), check.Equals, "http://some-proxy:3128")
}

func (s *clientSuite) TestClientProxySetsUserAgent(c *check.C) {
func (s *clientSuite) TestClientProxyTakesUserAgent(c *check.C) {
myUserAgent := "snapd yadda yadda"

defer httputil.MockUserAgent(myUserAgent)()

called := false
proxyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.Check(r.UserAgent(), check.Equals, myUserAgent)
Expand All @@ -94,6 +92,7 @@ func (s *clientSuite) TestClientProxySetsUserAgent(c *check.C) {
Proxy: func(*http.Request) (*url.URL, error) {
return mustParse(c, proxyServer.URL), nil
},
ProxyConnectHeader: http.Header{"User-Agent": []string{myUserAgent}},
})
_, err := cli.Get("https://localhost:9999")
c.Check(err, check.NotNil) // because we didn't do anything in the handler
Expand Down
Loading

0 comments on commit c30b437

Please sign in to comment.