Skip to content

Commit

Permalink
many: introduce snapdenv.UseStagingStore
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronis committed Mar 15, 2020
1 parent c30b437 commit ff75820
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 45 deletions.
8 changes: 4 additions & 4 deletions asserts/sysdb/generic.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 @@ -23,7 +23,7 @@ import (
"fmt"

"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/snapdenv"
)

const (
Expand Down Expand Up @@ -152,7 +152,7 @@ func init() {
// Generic returns a copy of the current set of predefined assertions for the 'generic' authority as used by Open.
func Generic() []asserts.Assertion {
generic := []asserts.Assertion(nil)
if !osutil.GetenvBool("SNAPPY_USE_STAGING_STORE") {
if !snapdenv.UseStagingStore() {
generic = append(generic, genericAssertions...)
} else {
generic = append(generic, genericStagingAssertions...)
Expand All @@ -179,7 +179,7 @@ func GenericClassicModel() *asserts.Model {
if genericClassicModelOverride != nil {
return genericClassicModelOverride
}
if !osutil.GetenvBool("SNAPPY_USE_STAGING_STORE") {
if !snapdenv.UseStagingStore() {
return genericClassicModel
} else {
return genericStagingClassicModel
Expand Down
6 changes: 3 additions & 3 deletions asserts/sysdb/trusted.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2016 Canonical Ltd
* Copyright (C) 2016-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 @@ -23,7 +23,7 @@ import (
"fmt"

"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/snapdenv"
)

const (
Expand Down Expand Up @@ -130,7 +130,7 @@ func init() {
// Trusted returns a copy of the current set of trusted assertions as used by Open.
func Trusted() []asserts.Assertion {
trusted := []asserts.Assertion(nil)
if !osutil.GetenvBool("SNAPPY_USE_STAGING_STORE") {
if !snapdenv.UseStagingStore() {
trusted = append(trusted, trustedAssertions...)
} else {
if len(trustedStagingAssertions) == 0 {
Expand Down
5 changes: 3 additions & 2 deletions cmd/snap-repair/cmd_run.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,6 +27,7 @@ import (

"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/snapdenv"
)

func init() {
Expand All @@ -47,7 +48,7 @@ var baseURL *url.URL

func init() {
var baseurl string
if osutil.GetenvBool("SNAPPY_USE_STAGING_STORE") {
if snapdenv.UseStagingStore() {
baseurl = "https://api.staging.snapcraft.io/v2/"
} else {
baseurl = "https://api.snapcraft.io/v2/"
Expand Down
6 changes: 3 additions & 3 deletions cmd/snap-repair/staging.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// +build withtestkeys withstagingkeys

/*
* 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 @@ -24,7 +24,7 @@ import (
"fmt"

"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/snapdenv"
)

const (
Expand Down Expand Up @@ -75,7 +75,7 @@ func init() {
if err != nil {
panic(fmt.Sprintf("cannot decode trusted account-key: %v", err))
}
if osutil.GetenvBool("SNAPPY_USE_STAGING_STORE") {
if snapdenv.UseStagingStore() {
trustedRepairRootKeys = append(trustedRepairRootKeys, repairRootAccountKey.(*asserts.AccountKey))
}
}
6 changes: 3 additions & 3 deletions cmd/snap-repair/trusted.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 @@ -23,7 +23,7 @@ import (
"fmt"

"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/snapdenv"
)

const (
Expand Down Expand Up @@ -83,7 +83,7 @@ func init() {
if err != nil {
panic(fmt.Sprintf("cannot decode trusted account-key: %v", err))
}
if !osutil.GetenvBool("SNAPPY_USE_STAGING_STORE") {
if !snapdenv.UseStagingStore() {
trustedRepairRootKeys = append(trustedRepairRootKeys, repairRootAccountKey.(*asserts.AccountKey))
}
}
3 changes: 2 additions & 1 deletion image/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/snapcore/snapd/progress"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snapdenv"
"github.com/snapcore/snapd/store"
"github.com/snapcore/snapd/strutil"
)
Expand Down Expand Up @@ -127,7 +128,7 @@ func parseAuthFile(authFn string, data []byte) (*authData, error) {
}

func snapcraftLoginSection() string {
if osutil.GetenvBool("SNAPPY_USE_STAGING_STORE") {
if snapdenv.UseStagingStore() {
return "login.staging.ubuntu.com"
}
return "login.ubuntu.com"
Expand Down
7 changes: 1 addition & 6 deletions overlord/devicestate/handlers_serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/httputil"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/configstate/config"
Expand All @@ -47,12 +46,8 @@ import (
"github.com/snapcore/snapd/timings"
)

func useStaging() bool {
return osutil.GetenvBool("SNAPPY_USE_STAGING_STORE")
}

func baseURL() *url.URL {
if useStaging() {
if snapdenv.UseStagingStore() {
return mustParse("https://api.staging.snapcraft.io/")
}
return mustParse("https://api.snapcraft.io/")
Expand Down
18 changes: 18 additions & 0 deletions snapdenv/snapdenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,21 @@ func MockTesting(testing bool) (restore func()) {
mockTesting = old
}
}

var mockUseStagingStore *bool

// UseStagingStore returns whether snapd compontents should use the staging store.
func UseStagingStore() bool {
if mockUseStagingStore != nil {
return *mockUseStagingStore
}
return osutil.GetenvBool("SNAPPY_USE_STAGING_STORE")
}

func MockUseStagingStore(testing bool) (restore func()) {
old := mockUseStagingStore
mockUseStagingStore = &testing
return func() {
mockUseStagingStore = old
}
}
37 changes: 37 additions & 0 deletions snapdenv/snapdenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,40 @@ func (s *snapdenvSuite) TestMockTesting(c *C) {
snapdenv.MockTesting(false)
c.Check(snapdenv.Testing(), Equals, false)
}

func (s *snapdenvSuite) TestUseStagingStore(c *C) {
oldUseStagingStore := os.Getenv("SNAPPY_USE_STAGING_STORE")
defer func() {
if oldUseStagingStore == "" {
os.Unsetenv("SNAPPY_USE_STAGING_STORE")
} else {
os.Setenv("SNAPPY_USE_STAGING_STORE", oldUseStagingStore)
}
}()

os.Setenv("SNAPPY_USE_STAGING_STORE", "1")
c.Check(snapdenv.UseStagingStore(), Equals, true)

os.Unsetenv("SNAPPY_USE_STAGING_STORE")
c.Check(snapdenv.UseStagingStore(), Equals, false)
}

func (s *snapdenvSuite) TestMockUseStagingStore(c *C) {
oldUseStagingStore := os.Getenv("SNAPPY_USE_STAGING_STORE")
defer func() {
if oldUseStagingStore == "" {
os.Unsetenv("SNAPPY_USE_STAGING_STORE")
} else {
os.Setenv("SNAPPY_USE_STAGING_STORE", oldUseStagingStore)
}
}()
os.Unsetenv("SNAPPY_USE_STAGING_STORE")

r := snapdenv.MockUseStagingStore(true)
defer r()

c.Check(snapdenv.UseStagingStore(), Equals, true)

snapdenv.MockUseStagingStore(false)
c.Check(snapdenv.UseStagingStore(), Equals, false)
}
10 changes: 3 additions & 7 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,6 @@ func useDeltas() bool {
return osutil.GetenvBool("SNAPD_USE_DELTAS_EXPERIMENTAL", true)
}

func useStaging() bool {
return osutil.GetenvBool("SNAPPY_USE_STAGING_STORE")
}

// endpointURL clones a base URL and updates it with optional path and query.
func endpointURL(base *url.URL, path string, query url.Values) *url.URL {
u := *base
Expand All @@ -227,7 +223,7 @@ func endpointURL(base *url.URL, path string, query url.Values) *url.URL {
// apiURL returns the system default base API URL.
func apiURL() *url.URL {
s := "https://api.snapcraft.io/"
if useStaging() {
if snapdenv.UseStagingStore() {
s = "https://api.staging.snapcraft.io/"
}
u, _ := url.Parse(s)
Expand Down Expand Up @@ -274,7 +270,7 @@ func assertsURL() (*url.URL, error) {
}

func authLocation() string {
if useStaging() {
if snapdenv.UseStagingStore() {
return "login.staging.ubuntu.com"
}
return "login.ubuntu.com"
Expand All @@ -290,7 +286,7 @@ func authURL() string {
var defaultStoreDeveloperURL = "https://dashboard.snapcraft.io/"

func storeDeveloperURL() string {
if useStaging() {
if snapdenv.UseStagingStore() {
return "https://dashboard.staging.snapcraft.io/"
}
return defaultStoreDeveloperURL
Expand Down
20 changes: 8 additions & 12 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3458,33 +3458,30 @@ func (s *storeTestSuite) TestFindClientUserAgent(c *C) {
}

func (s *storeTestSuite) TestAuthLocationDependsOnEnviron(c *C) {
c.Assert(os.Setenv("SNAPPY_USE_STAGING_STORE", ""), IsNil)
defer snapdenv.MockUseStagingStore(false)()
before := store.AuthLocation()

c.Assert(os.Setenv("SNAPPY_USE_STAGING_STORE", "1"), IsNil)
defer os.Setenv("SNAPPY_USE_STAGING_STORE", "")
snapdenv.MockUseStagingStore(true)
after := store.AuthLocation()

c.Check(before, Not(Equals), after)
}

func (s *storeTestSuite) TestAuthURLDependsOnEnviron(c *C) {
c.Assert(os.Setenv("SNAPPY_USE_STAGING_STORE", ""), IsNil)
defer snapdenv.MockUseStagingStore(false)()
before := store.AuthURL()

c.Assert(os.Setenv("SNAPPY_USE_STAGING_STORE", "1"), IsNil)
defer os.Setenv("SNAPPY_USE_STAGING_STORE", "")
snapdenv.MockUseStagingStore(true)
after := store.AuthURL()

c.Check(before, Not(Equals), after)
}

func (s *storeTestSuite) TestApiURLDependsOnEnviron(c *C) {
c.Assert(os.Setenv("SNAPPY_USE_STAGING_STORE", ""), IsNil)
defer snapdenv.MockUseStagingStore(false)()
before := store.ApiURL()

c.Assert(os.Setenv("SNAPPY_USE_STAGING_STORE", "1"), IsNil)
defer os.Setenv("SNAPPY_USE_STAGING_STORE", "")
snapdenv.MockUseStagingStore(true)
after := store.ApiURL()

c.Check(before, Not(Equals), after)
Expand Down Expand Up @@ -3532,11 +3529,10 @@ func (s *storeTestSuite) TestStoreURLBadEnvironCPI(c *C) {
}

func (s *storeTestSuite) TestStoreDeveloperURLDependsOnEnviron(c *C) {
c.Assert(os.Setenv("SNAPPY_USE_STAGING_STORE", ""), IsNil)
defer snapdenv.MockUseStagingStore(false)()
before := store.StoreDeveloperURL()

c.Assert(os.Setenv("SNAPPY_USE_STAGING_STORE", "1"), IsNil)
defer os.Setenv("SNAPPY_USE_STAGING_STORE", "")
snapdenv.MockUseStagingStore(true)
after := store.StoreDeveloperURL()

c.Check(before, Not(Equals), after)
Expand Down
7 changes: 3 additions & 4 deletions tests/lib/fakestore/store/store.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2016-2018 Canonical Ltd
* Copyright (C) 2016-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 @@ -36,7 +36,6 @@ import (
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/asserts/sysdb"
"github.com/snapcore/snapd/asserts/systestkeys"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snapdenv"
"github.com/snapcore/snapd/store"
Expand Down Expand Up @@ -391,7 +390,7 @@ func (s *Store) bulkEndpoint(w http.ResponseWriter, req *http.Request) {
}

var remoteStore string
if osutil.GetenvBool("SNAPPY_USE_STAGING_STORE") {
if snapdenv.UseStagingStore() {
remoteStore = "staging"
} else {
remoteStore = "production"
Expand Down Expand Up @@ -556,7 +555,7 @@ func (s *Store) snapActionEndpoint(w http.ResponseWriter, req *http.Request) {
}

var remoteStore string
if osutil.GetenvBool("SNAPPY_USE_STAGING_STORE") {
if snapdenv.UseStagingStore() {
remoteStore = "staging"
} else {
remoteStore = "production"
Expand Down

0 comments on commit ff75820

Please sign in to comment.