2023-01-31 06:12:19 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2023-08-23 04:35:42 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
|
2023-06-07 10:16:32 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/api/versionsapi"
|
2023-01-31 06:12:19 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
2023-06-09 09:41:02 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/variant"
|
2023-01-31 06:12:19 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2023-07-25 08:20:25 -04:00
|
|
|
consemver "github.com/edgelesssys/constellation/v2/internal/semver"
|
2023-01-31 06:12:19 -05:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestBuildString checks that the resulting user output is as expected. Slow part is the Sscanf in parseCanonicalSemver().
|
|
|
|
func TestBuildString(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
upgrade versionUpgrade
|
|
|
|
expected string
|
|
|
|
wantError bool
|
|
|
|
}{
|
|
|
|
"update everything": {
|
|
|
|
upgrade: versionUpgrade{
|
2023-07-25 08:20:25 -04:00
|
|
|
newServices: consemver.NewFromInt(2, 5, 0, ""),
|
2023-01-31 06:12:19 -05:00
|
|
|
newImages: map[string]measurements.M{
|
2023-05-04 04:51:28 -04:00
|
|
|
"v2.5.0": measurements.DefaultsFor(cloudprovider.QEMU, variant.QEMUVTPM{}),
|
2023-01-31 06:12:19 -05:00
|
|
|
},
|
|
|
|
newKubernetes: []string{"v1.24.12", "v1.25.6"},
|
2023-07-25 08:20:25 -04:00
|
|
|
newCLI: []consemver.Semver{consemver.NewFromInt(2, 5, 0, ""), consemver.NewFromInt(2, 6, 0, "")},
|
|
|
|
currentServices: consemver.NewFromInt(2, 4, 0, ""),
|
2023-09-14 05:51:20 -04:00
|
|
|
currentImage: consemver.NewFromInt(2, 4, 0, ""),
|
|
|
|
currentKubernetes: consemver.NewFromInt(1, 24, 5, ""),
|
2023-07-25 08:20:25 -04:00
|
|
|
currentCLI: consemver.NewFromInt(2, 4, 0, ""),
|
2023-01-31 06:12:19 -05:00
|
|
|
},
|
|
|
|
expected: "The following updates are available with this CLI:\n Kubernetes: v1.24.5 --> v1.24.12 v1.25.6\n Images:\n v2.4.0 --> v2.5.0\n Includes these measurements:\n 4:\n expected: \"1234123412341234123412341234123412341234123412341234123412341234\"\n warnOnly: false\n 8:\n expected: \"0000000000000000000000000000000000000000000000000000000000000000\"\n warnOnly: false\n 9:\n expected: \"1234123412341234123412341234123412341234123412341234123412341234\"\n warnOnly: false\n 11:\n expected: \"0000000000000000000000000000000000000000000000000000000000000000\"\n warnOnly: false\n 12:\n expected: \"1234123412341234123412341234123412341234123412341234123412341234\"\n warnOnly: false\n 13:\n expected: \"0000000000000000000000000000000000000000000000000000000000000000\"\n warnOnly: false\n 15:\n expected: \"0000000000000000000000000000000000000000000000000000000000000000\"\n warnOnly: false\n \n Services: v2.4.0 --> v2.5.0\n",
|
|
|
|
},
|
2023-04-03 08:31:17 -04:00
|
|
|
"cli incompatible with K8s": {
|
|
|
|
upgrade: versionUpgrade{
|
2023-07-25 08:20:25 -04:00
|
|
|
newCLI: []consemver.Semver{consemver.NewFromInt(2, 5, 0, ""), consemver.NewFromInt(2, 6, 0, "")},
|
|
|
|
currentCLI: consemver.NewFromInt(2, 4, 0, ""),
|
2023-04-03 08:31:17 -04:00
|
|
|
},
|
|
|
|
expected: "There are newer CLIs available (v2.5.0 v2.6.0), however, you need to upgrade your cluster's Kubernetes version first.\n",
|
|
|
|
},
|
|
|
|
"cli compatible with K8s": {
|
|
|
|
upgrade: versionUpgrade{
|
2023-07-25 08:20:25 -04:00
|
|
|
newCompatibleCLI: []consemver.Semver{consemver.NewFromInt(2, 5, 0, ""), consemver.NewFromInt(2, 6, 0, "")},
|
|
|
|
currentCLI: consemver.NewFromInt(2, 4, 0, ""),
|
2023-04-03 08:31:17 -04:00
|
|
|
},
|
|
|
|
expected: "Newer CLI versions that are compatible with your cluster are: v2.5.0 v2.6.0\n",
|
|
|
|
},
|
|
|
|
"k8s only": {
|
|
|
|
upgrade: versionUpgrade{
|
|
|
|
newKubernetes: []string{"v1.24.12", "v1.25.6"},
|
2023-09-14 05:51:20 -04:00
|
|
|
currentKubernetes: consemver.NewFromInt(1, 24, 5, ""),
|
2023-04-03 08:31:17 -04:00
|
|
|
},
|
|
|
|
expected: "The following updates are available with this CLI:\n Kubernetes: v1.24.5 --> v1.24.12 v1.25.6\n",
|
|
|
|
},
|
2023-01-31 06:12:19 -05:00
|
|
|
"no upgrades": {
|
|
|
|
upgrade: versionUpgrade{
|
2023-07-25 08:20:25 -04:00
|
|
|
newServices: consemver.Semver{},
|
2023-01-31 06:12:19 -05:00
|
|
|
newImages: map[string]measurements.M{},
|
|
|
|
newKubernetes: []string{},
|
2023-07-25 08:20:25 -04:00
|
|
|
newCLI: []consemver.Semver{},
|
|
|
|
currentServices: consemver.NewFromInt(2, 5, 0, ""),
|
2023-09-14 05:51:20 -04:00
|
|
|
currentImage: consemver.NewFromInt(2, 5, 0, ""),
|
|
|
|
currentKubernetes: consemver.NewFromInt(1, 25, 6, ""),
|
2023-07-25 08:20:25 -04:00
|
|
|
currentCLI: consemver.NewFromInt(2, 5, 0, ""),
|
2023-01-31 06:12:19 -05:00
|
|
|
},
|
2023-04-03 08:31:17 -04:00
|
|
|
expected: "You are up to date.\n",
|
2023-01-31 06:12:19 -05:00
|
|
|
},
|
|
|
|
"no upgrades #2": {
|
|
|
|
upgrade: versionUpgrade{},
|
2023-04-03 08:31:17 -04:00
|
|
|
expected: "You are up to date.\n",
|
2023-01-31 06:12:19 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
result, err := tc.upgrade.buildString()
|
|
|
|
if tc.wantError {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.expected, result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetCompatibleImageMeasurements(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2023-08-01 10:48:13 -04:00
|
|
|
require := require.New(t)
|
2023-01-31 06:12:19 -05:00
|
|
|
|
|
|
|
csp := cloudprovider.Azure
|
2023-05-22 08:59:28 -04:00
|
|
|
attestationVariant := variant.AzureSEVSNP{}
|
2023-08-01 10:48:13 -04:00
|
|
|
|
|
|
|
versionZero, err := versionsapi.NewVersion("-", "stable", "v0.0.0", versionsapi.VersionKindImage)
|
|
|
|
require.NoError(err)
|
2023-01-31 06:12:19 -05:00
|
|
|
|
|
|
|
client := newTestClient(func(req *http.Request) *http.Response {
|
2023-08-01 10:48:13 -04:00
|
|
|
if strings.HasSuffix(req.URL.String(), "v0.0.0/image/measurements.json") {
|
2023-01-31 06:12:19 -05:00
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2023-08-01 10:48:13 -04:00
|
|
|
Body: io.NopCloser(strings.NewReader(`{"version": "v0.0.0","ref": "-","stream": "stable","list": [{"csp": "Azure","attestationVariant": "azure-sev-snp","measurements": {"0": {"expected": "0000000000000000000000000000000000000000000000000000000000000000","warnOnly": false}}}]}`)),
|
2023-01-31 06:12:19 -05:00
|
|
|
Header: make(http.Header),
|
|
|
|
}
|
|
|
|
}
|
2023-08-01 10:48:13 -04:00
|
|
|
if strings.HasSuffix(req.URL.String(), "v0.0.0/image/measurements.json.sig") {
|
2023-01-31 06:12:19 -05:00
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: io.NopCloser(strings.NewReader("MEQCIGRR7RaSMs892Ta06/Tz7LqPUxI05X4wQcP+nFFmZtmaAiBNl9X8mUKmUBfxg13LQBfmmpw6JwYQor5hOwM3NFVPAg==")),
|
|
|
|
Header: make(http.Header),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusNotFound,
|
|
|
|
Body: io.NopCloser(strings.NewReader("Not found.")),
|
|
|
|
Header: make(http.Header),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-08-01 10:48:13 -04:00
|
|
|
upgrades, err := getCompatibleImageMeasurements(context.Background(), &bytes.Buffer{}, client, &stubCosignVerifier{}, singleUUIDVerifier(), csp, attestationVariant, versionZero, logger.NewTest(t))
|
2023-01-31 06:12:19 -05:00
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
for _, measurement := range upgrades {
|
|
|
|
assert.NotEmpty(measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpgradeCheck(t *testing.T) {
|
2023-08-01 10:48:13 -04:00
|
|
|
require := require.New(t)
|
|
|
|
v2_3, err := versionsapi.NewVersion("-", "stable", "v2.3.0", versionsapi.VersionKindImage)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
v2_5, err := versionsapi.NewVersion("-", "stable", "v2.5.0", versionsapi.VersionKindImage)
|
|
|
|
require.NoError(err)
|
|
|
|
|
2023-06-21 03:22:32 -04:00
|
|
|
collector := stubVersionCollector{
|
2023-07-25 08:20:25 -04:00
|
|
|
supportedServicesVersions: consemver.NewFromInt(2, 5, 0, ""),
|
2023-06-21 03:22:32 -04:00
|
|
|
supportedImages: []versionsapi.Version{v2_3},
|
|
|
|
supportedImageVersions: map[string]measurements.M{
|
|
|
|
"v2.3.0": measurements.DefaultsFor(cloudprovider.GCP, variant.GCPSEVES{}),
|
|
|
|
},
|
|
|
|
supportedK8sVersions: []string{"v1.24.5", "v1.24.12", "v1.25.6"},
|
2023-07-25 08:20:25 -04:00
|
|
|
currentServicesVersions: consemver.NewFromInt(2, 4, 0, ""),
|
2023-09-14 05:51:20 -04:00
|
|
|
currentImageVersion: consemver.NewFromInt(2, 4, 0, ""),
|
|
|
|
currentK8sVersion: consemver.NewFromInt(1, 24, 5, ""),
|
2023-07-25 08:20:25 -04:00
|
|
|
currentCLIVersion: consemver.NewFromInt(2, 4, 0, ""),
|
2023-06-21 03:22:32 -04:00
|
|
|
images: []versionsapi.Version{v2_5},
|
2023-07-25 08:20:25 -04:00
|
|
|
newCLIVersionsList: []consemver.Semver{consemver.NewFromInt(2, 5, 0, ""), consemver.NewFromInt(2, 6, 0, "")},
|
2023-06-21 03:22:32 -04:00
|
|
|
}
|
|
|
|
|
2023-01-31 06:12:19 -05:00
|
|
|
testCases := map[string]struct {
|
2023-08-14 09:16:07 -04:00
|
|
|
collector stubVersionCollector
|
|
|
|
csp cloudprovider.Provider
|
2023-08-16 03:59:32 -04:00
|
|
|
checker stubTerraformChecker
|
2023-08-14 09:16:07 -04:00
|
|
|
cliVersion string
|
|
|
|
wantError bool
|
2023-01-31 06:12:19 -05:00
|
|
|
}{
|
|
|
|
"upgrades gcp": {
|
2023-08-14 09:16:07 -04:00
|
|
|
collector: collector,
|
2023-08-16 03:59:32 -04:00
|
|
|
checker: stubTerraformChecker{},
|
2023-08-14 09:16:07 -04:00
|
|
|
csp: cloudprovider.GCP,
|
|
|
|
cliVersion: "v1.0.0",
|
2023-06-21 03:22:32 -04:00
|
|
|
},
|
2023-09-14 05:51:20 -04:00
|
|
|
"terraform plan err": {
|
2023-06-21 03:22:32 -04:00
|
|
|
collector: collector,
|
2023-08-16 03:59:32 -04:00
|
|
|
checker: stubTerraformChecker{
|
2023-09-14 05:51:20 -04:00
|
|
|
planErr: assert.AnError,
|
2023-01-31 06:12:19 -05:00
|
|
|
},
|
2023-08-14 09:16:07 -04:00
|
|
|
csp: cloudprovider.GCP,
|
|
|
|
cliVersion: "v1.0.0",
|
|
|
|
wantError: true,
|
2023-01-31 06:12:19 -05:00
|
|
|
},
|
2023-09-14 05:51:20 -04:00
|
|
|
"terraform rollback err, log only": {
|
|
|
|
collector: collector,
|
|
|
|
checker: stubTerraformChecker{
|
|
|
|
rollbackErr: assert.AnError,
|
|
|
|
},
|
|
|
|
csp: cloudprovider.GCP,
|
|
|
|
cliVersion: "v1.0.0",
|
|
|
|
},
|
2023-01-31 06:12:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
fileHandler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
cfg := defaultConfigWithExpectedMeasurements(t, config.Default(), tc.csp)
|
2023-08-04 07:53:51 -04:00
|
|
|
require.NoError(fileHandler.WriteYAML(constants.ConfigFilename, cfg))
|
2023-01-31 06:12:19 -05:00
|
|
|
|
|
|
|
checkCmd := upgradeCheckCmd{
|
2023-08-16 03:59:32 -04:00
|
|
|
canUpgradeCheck: true,
|
|
|
|
collect: &tc.collector,
|
|
|
|
terraformChecker: tc.checker,
|
2023-08-23 04:35:42 -04:00
|
|
|
fileHandler: fileHandler,
|
2023-08-16 03:59:32 -04:00
|
|
|
log: logger.NewTest(t),
|
2023-01-31 06:12:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := newUpgradeCheckCmd()
|
|
|
|
|
2023-09-14 05:51:20 -04:00
|
|
|
err := checkCmd.upgradeCheck(cmd, stubAttestationFetcher{}, upgradeCheckFlags{})
|
2023-01-31 06:12:19 -05:00
|
|
|
if tc.wantError {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubVersionCollector struct {
|
2023-07-25 08:20:25 -04:00
|
|
|
supportedServicesVersions consemver.Semver
|
2023-04-03 08:31:17 -04:00
|
|
|
supportedImages []versionsapi.Version
|
|
|
|
supportedImageVersions map[string]measurements.M
|
|
|
|
supportedK8sVersions []string
|
2023-07-25 08:20:25 -04:00
|
|
|
supportedCLIVersions []consemver.Semver
|
|
|
|
currentServicesVersions consemver.Semver
|
2023-08-23 08:37:53 -04:00
|
|
|
currentImageVersion consemver.Semver
|
|
|
|
currentK8sVersion consemver.Semver
|
2023-07-25 08:20:25 -04:00
|
|
|
currentCLIVersion consemver.Semver
|
2023-04-03 08:31:17 -04:00
|
|
|
images []versionsapi.Version
|
2023-07-25 08:20:25 -04:00
|
|
|
newCLIVersionsList []consemver.Semver
|
|
|
|
newCompatibleCLIVersionsList []consemver.Semver
|
2023-04-03 08:31:17 -04:00
|
|
|
someErr error
|
2023-01-31 06:12:19 -05:00
|
|
|
}
|
|
|
|
|
2023-05-22 08:59:28 -04:00
|
|
|
func (s *stubVersionCollector) newMeasurements(_ context.Context, _ cloudprovider.Provider, _ variant.Variant, _ []versionsapi.Version) (map[string]measurements.M, error) {
|
2023-01-31 06:12:19 -05:00
|
|
|
return s.supportedImageVersions, nil
|
|
|
|
}
|
|
|
|
|
2023-04-05 12:40:35 -04:00
|
|
|
func (s *stubVersionCollector) currentVersions(_ context.Context) (currentVersionInfo, error) {
|
2023-04-03 08:31:17 -04:00
|
|
|
return currentVersionInfo{
|
|
|
|
service: s.currentServicesVersions,
|
|
|
|
image: s.currentImageVersion,
|
|
|
|
k8s: s.currentK8sVersion,
|
|
|
|
cli: s.currentCLIVersion,
|
|
|
|
}, s.someErr
|
2023-01-31 06:12:19 -05:00
|
|
|
}
|
|
|
|
|
2023-08-23 08:37:53 -04:00
|
|
|
func (s *stubVersionCollector) supportedVersions(_ context.Context, _, _ consemver.Semver) (supportedVersionInfo, error) {
|
2023-04-03 08:31:17 -04:00
|
|
|
return supportedVersionInfo{
|
|
|
|
service: s.supportedServicesVersions,
|
|
|
|
image: s.supportedImages,
|
|
|
|
k8s: s.supportedK8sVersions,
|
|
|
|
cli: s.supportedCLIVersions,
|
|
|
|
}, s.someErr
|
2023-01-31 06:12:19 -05:00
|
|
|
}
|
|
|
|
|
2023-08-23 08:37:53 -04:00
|
|
|
func (s *stubVersionCollector) newImages(_ context.Context, _ consemver.Semver) ([]versionsapi.Version, error) {
|
2023-01-31 06:12:19 -05:00
|
|
|
return s.images, nil
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubVersionCollector) newerVersions(_ context.Context, _ []string) ([]versionsapi.Version, error) {
|
2023-01-31 06:12:19 -05:00
|
|
|
return s.images, nil
|
|
|
|
}
|
|
|
|
|
2023-07-25 08:20:25 -04:00
|
|
|
func (s *stubVersionCollector) newCLIVersions(_ context.Context) ([]consemver.Semver, error) {
|
2023-04-03 08:31:17 -04:00
|
|
|
return s.newCLIVersionsList, nil
|
|
|
|
}
|
|
|
|
|
2023-08-23 08:37:53 -04:00
|
|
|
func (s *stubVersionCollector) filterCompatibleCLIVersions(_ context.Context, _ []consemver.Semver, _ consemver.Semver) ([]consemver.Semver, error) {
|
2023-04-03 08:31:17 -04:00
|
|
|
return s.newCompatibleCLIVersionsList, nil
|
|
|
|
}
|
|
|
|
|
2023-08-16 03:59:32 -04:00
|
|
|
type stubTerraformChecker struct {
|
2023-09-14 05:51:20 -04:00
|
|
|
tfDiff bool
|
|
|
|
planErr error
|
|
|
|
rollbackErr error
|
2023-06-21 03:22:32 -04:00
|
|
|
}
|
|
|
|
|
2023-08-23 04:35:42 -04:00
|
|
|
func (s stubTerraformChecker) PlanClusterUpgrade(_ context.Context, _ io.Writer, _ terraform.Variables, _ cloudprovider.Provider) (bool, error) {
|
2023-09-14 05:51:20 -04:00
|
|
|
return s.tfDiff, s.planErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubTerraformChecker) RestoreClusterWorkspace() error {
|
|
|
|
return s.rollbackErr
|
2023-06-21 03:22:32 -04:00
|
|
|
}
|
|
|
|
|
2023-04-03 08:31:17 -04:00
|
|
|
func TestNewCLIVersions(t *testing.T) {
|
|
|
|
someErr := errors.New("some error")
|
|
|
|
minorList := func() versionsapi.List {
|
|
|
|
return versionsapi.List{
|
|
|
|
Versions: []string{"v0.2.0"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
patchList := func() versionsapi.List {
|
|
|
|
return versionsapi.List{
|
|
|
|
Versions: []string{"v0.2.1"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emptyVerList := func() versionsapi.List {
|
|
|
|
return versionsapi.List{}
|
|
|
|
}
|
|
|
|
verCollector := func(minorList, patchList versionsapi.List, verListErr error) versionCollector {
|
|
|
|
return versionCollector{
|
2023-07-25 08:20:25 -04:00
|
|
|
cliVersion: consemver.NewFromInt(0, 1, 0, ""),
|
2023-04-03 08:31:17 -04:00
|
|
|
versionsapi: stubVersionFetcher{
|
|
|
|
minorList: minorList,
|
|
|
|
patchList: patchList,
|
|
|
|
versionListErr: verListErr,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
verCollector versionCollector
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"works": {
|
|
|
|
verCollector: verCollector(minorList(), patchList(), nil),
|
|
|
|
},
|
|
|
|
"empty versions list": {
|
|
|
|
verCollector: verCollector(emptyVerList(), emptyVerList(), nil),
|
|
|
|
},
|
|
|
|
"version list error": {
|
|
|
|
verCollector: verCollector(minorList(), patchList(), someErr),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
_, err := tc.verCollector.newCLIVersions(context.Background())
|
|
|
|
if tc.wantErr {
|
|
|
|
require.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilterCompatibleCLIVersions(t *testing.T) {
|
|
|
|
someErr := errors.New("some error")
|
|
|
|
verCollector := func(cliInfoErr error) versionCollector {
|
|
|
|
return versionCollector{
|
2023-07-25 08:20:25 -04:00
|
|
|
cliVersion: consemver.NewFromInt(0, 1, 0, ""),
|
2023-04-03 08:31:17 -04:00
|
|
|
versionsapi: stubVersionFetcher{
|
|
|
|
cliInfoErr: cliInfoErr,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
verCollector versionCollector
|
2023-07-25 08:20:25 -04:00
|
|
|
cliPatchVersions []consemver.Semver
|
2023-04-03 08:31:17 -04:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"works": {
|
|
|
|
verCollector: verCollector(nil),
|
2023-07-25 08:20:25 -04:00
|
|
|
cliPatchVersions: []consemver.Semver{consemver.NewFromInt(0, 1, 1, "")},
|
2023-04-03 08:31:17 -04:00
|
|
|
},
|
|
|
|
"cli info error": {
|
|
|
|
verCollector: verCollector(someErr),
|
2023-07-25 08:20:25 -04:00
|
|
|
cliPatchVersions: []consemver.Semver{consemver.NewFromInt(0, 1, 1, "")},
|
2023-04-03 08:31:17 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
2023-09-14 05:51:20 -04:00
|
|
|
_, err := tc.verCollector.filterCompatibleCLIVersions(context.Background(), tc.cliPatchVersions, consemver.NewFromInt(1, 24, 5, ""))
|
2023-04-03 08:31:17 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
require.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubVersionFetcher struct {
|
|
|
|
minorList versionsapi.List
|
|
|
|
patchList versionsapi.List
|
|
|
|
versionListErr error
|
|
|
|
cliInfoErr error
|
|
|
|
}
|
|
|
|
|
2023-04-05 12:40:35 -04:00
|
|
|
func (f stubVersionFetcher) FetchVersionList(_ context.Context, list versionsapi.List) (versionsapi.List, error) {
|
2023-04-03 08:31:17 -04:00
|
|
|
switch list.Granularity {
|
|
|
|
case versionsapi.GranularityMajor:
|
|
|
|
return f.minorList, f.versionListErr
|
|
|
|
case versionsapi.GranularityMinor:
|
|
|
|
return f.patchList, f.versionListErr
|
|
|
|
}
|
|
|
|
return versionsapi.List{}, f.versionListErr
|
|
|
|
}
|
|
|
|
|
2023-04-05 12:40:35 -04:00
|
|
|
func (f stubVersionFetcher) FetchCLIInfo(_ context.Context, _ versionsapi.CLIInfo) (versionsapi.CLIInfo, error) {
|
2023-04-03 08:31:17 -04:00
|
|
|
return versionsapi.CLIInfo{}, f.cliInfoErr
|
|
|
|
}
|