feat: early access

This commit is contained in:
James Houlahan
2020-12-03 15:36:02 +01:00
parent eccad4bbfd
commit d2066173f0
22 changed files with 235 additions and 72 deletions

View File

@ -1,24 +0,0 @@
// Copyright (c) 2021 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
// +build !beta
package updater
// Channel is the channel of updates users are subscribed to.
// For now it is hardcoded in the build. In future, it might be selectable in settings.
const Channel = "live"

View File

@ -15,8 +15,15 @@
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
// +build beta
package updater
const Channel = "beta"
// UpdateChannel represents an update channel users can be subscribed to.
type UpdateChannel string
const (
// LiveChannel is the channel all users are subscribed to by default.
LiveChannel UpdateChannel = "live"
// BetaChannel is the channel users subscribe to when they enable "Early Access".
BetaChannel UpdateChannel = "beta"
)

View File

@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
// +build !pmapi_qa
// +build !build_qa
package updater

View File

@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
// +build pmapi_qa
// +build build_qa
package updater

View File

@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
// +build !pmapi_qa
// +build !build_qa
package updater

View File

@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
// +build pmapi_qa
// +build build_qa
package updater

View File

@ -23,6 +23,7 @@ import (
"github.com/Masterminds/semver/v3"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/ProtonMail/proton-bridge/internal/config/settings"
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
@ -38,15 +39,21 @@ type Installer interface {
InstallUpdate(*semver.Version, io.Reader) error
}
type Settings interface {
Get(string) string
Set(string, string)
GetFloat64(string) float64
}
type Updater struct {
cm ClientProvider
installer Installer
settings Settings
kr *crypto.KeyRing
curVer *semver.Version
updateURLName string
platform string
rollout float64
locker *locker
}
@ -54,19 +61,25 @@ type Updater struct {
func New(
cm ClientProvider,
installer Installer,
s Settings,
kr *crypto.KeyRing,
curVer *semver.Version,
updateURLName, platform string,
rollout float64,
) *Updater {
// If there's some unexpected value in the preferences, we force it back onto the live channel.
// This prevents users from screwing up silent updates by modifying their prefs.json file.
if channel := UpdateChannel(s.Get(settings.UpdateChannelKey)); !(channel == LiveChannel || channel == BetaChannel) {
s.Set(settings.UpdateChannelKey, string(LiveChannel))
}
return &Updater{
cm: cm,
installer: installer,
settings: s,
kr: kr,
curVer: curVer,
updateURLName: updateURLName,
platform: platform,
rollout: rollout,
locker: newLocker(),
}
}
@ -92,7 +105,12 @@ func (u *Updater) Check() (VersionInfo, error) {
return VersionInfo{}, err
}
return versionMap[Channel], nil
version, ok := versionMap[u.settings.Get(settings.UpdateChannelKey)]
if !ok {
return VersionInfo{}, errors.New("no updates available for this channel")
}
return version, nil
}
func (u *Updater) IsUpdateApplicable(version VersionInfo) bool {
@ -100,7 +118,7 @@ func (u *Updater) IsUpdateApplicable(version VersionInfo) bool {
return false
}
if u.rollout > version.Rollout {
if u.settings.GetFloat64(settings.RolloutKey) > version.Rollout {
return false
}
@ -108,6 +126,10 @@ func (u *Updater) IsUpdateApplicable(version VersionInfo) bool {
}
func (u *Updater) CanInstall(version VersionInfo) bool {
if version.MinAuto == nil {
return true
}
return !u.curVer.LessThan(version.MinAuto)
}

View File

@ -22,11 +22,13 @@ import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"sync"
"testing"
"time"
"github.com/Masterminds/semver/v3"
"github.com/ProtonMail/proton-bridge/internal/config/settings"
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
"github.com/ProtonMail/proton-bridge/pkg/pmapi/mocks"
"github.com/golang/mock/gomock"
@ -40,7 +42,7 @@ func TestCheck(t *testing.T) {
client := mocks.NewMockClient(c)
updater := newTestUpdater(client, "1.1.0")
updater := newTestUpdater(client, "1.1.0", false)
versionMap := VersionMap{
"live": VersionInfo{
@ -65,13 +67,50 @@ func TestCheck(t *testing.T) {
assert.NoError(t, err)
}
func TestCheckEarlyAccess(t *testing.T) {
c := gomock.NewController(t)
defer c.Finish()
client := mocks.NewMockClient(c)
updater := newTestUpdater(client, "1.1.0", true)
versionMap := VersionMap{
"live": VersionInfo{
Version: semver.MustParse("1.5.0"),
MinAuto: semver.MustParse("1.0.0"),
Package: "https://protonmail.com/download/bridge/update_1.5.0_linux.tgz",
Rollout: 1.0,
},
"beta": VersionInfo{
Version: semver.MustParse("1.6.0"),
MinAuto: semver.MustParse("1.0.0"),
Package: "https://protonmail.com/download/bridge/update_1.6.0_linux.tgz",
Rollout: 1.0,
},
}
client.EXPECT().DownloadAndVerify(
updater.getVersionFileURL(),
updater.getVersionFileURL()+".sig",
gomock.Any(),
).Return(bytes.NewReader(mustMarshal(t, versionMap)), nil)
client.EXPECT().Logout()
version, err := updater.Check()
assert.Equal(t, semver.MustParse("1.6.0"), version.Version)
assert.NoError(t, err)
}
func TestCheckBadSignature(t *testing.T) {
c := gomock.NewController(t)
defer c.Finish()
client := mocks.NewMockClient(c)
updater := newTestUpdater(client, "1.2.0")
updater := newTestUpdater(client, "1.2.0", false)
client.EXPECT().DownloadAndVerify(
updater.getVersionFileURL(),
@ -92,7 +131,7 @@ func TestIsUpdateApplicable(t *testing.T) {
client := mocks.NewMockClient(c)
updater := newTestUpdater(client, "1.4.0")
updater := newTestUpdater(client, "1.4.0", false)
versionOld := VersionInfo{
Version: semver.MustParse("1.3.0"),
@ -128,7 +167,7 @@ func TestCanInstall(t *testing.T) {
client := mocks.NewMockClient(c)
updater := newTestUpdater(client, "1.4.0")
updater := newTestUpdater(client, "1.4.0", false)
versionManual := VersionInfo{
Version: semver.MustParse("1.5.0"),
@ -155,7 +194,7 @@ func TestInstallUpdate(t *testing.T) {
client := mocks.NewMockClient(c)
updater := newTestUpdater(client, "1.4.0")
updater := newTestUpdater(client, "1.4.0", false)
latestVersion := VersionInfo{
Version: semver.MustParse("1.5.0"),
@ -183,7 +222,7 @@ func TestInstallUpdateBadSignature(t *testing.T) {
client := mocks.NewMockClient(c)
updater := newTestUpdater(client, "1.4.0")
updater := newTestUpdater(client, "1.4.0", false)
latestVersion := VersionInfo{
Version: semver.MustParse("1.5.0"),
@ -211,7 +250,7 @@ func TestInstallUpdateAlreadyOngoing(t *testing.T) {
client := mocks.NewMockClient(c)
updater := newTestUpdater(client, "1.4.0")
updater := newTestUpdater(client, "1.4.0", false)
updater.installer = &fakeInstaller{delay: 2 * time.Second}
@ -249,14 +288,14 @@ func TestInstallUpdateAlreadyOngoing(t *testing.T) {
wg.Wait()
}
func newTestUpdater(client *mocks.MockClient, curVer string) *Updater {
func newTestUpdater(client *mocks.MockClient, curVer string, earlyAccess bool) *Updater {
return New(
&fakeClientProvider{client: client},
&fakeInstaller{},
newFakeSettings(0.5, earlyAccess),
nil,
semver.MustParse(curVer),
"bridge", "linux",
0.5,
)
}
@ -289,3 +328,31 @@ func mustMarshal(t *testing.T, v interface{}) []byte {
return b
}
type fakeSettings struct {
*settings.Settings
dir string
}
// newFakeSettings creates a temporary folder for files.
func newFakeSettings(rollout float64, earlyAccess bool) *fakeSettings {
dir, err := ioutil.TempDir("", "test-settings")
if err != nil {
panic(err)
}
s := &fakeSettings{
Settings: settings.New(dir),
dir: dir,
}
s.SetFloat64(settings.RolloutKey, rollout)
if earlyAccess {
s.Set(settings.UpdateChannelKey, string(BetaChannel))
} else {
s.Set(settings.UpdateChannelKey, string(LiveChannel))
}
return s
}