2022-09-05 03:06:08 -04:00
/ *
Copyright ( c ) Edgeless Systems GmbH
SPDX - License - Identifier : AGPL - 3.0 - only
* /
2022-08-31 05:59:07 -04:00
package cmd
import (
"bytes"
"context"
"errors"
"io"
"net/http"
"strings"
"testing"
2022-09-21 07:47:57 -04: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"
2023-01-04 04:46:29 -05:00
"github.com/edgelesssys/constellation/v2/internal/logger"
2022-12-29 11:27:15 -05:00
"github.com/edgelesssys/constellation/v2/internal/versionsapi"
2022-08-31 05:59:07 -04:00
"github.com/spf13/afero"
2022-11-10 04:27:24 -05:00
"github.com/spf13/cobra"
2022-08-31 05:59:07 -04:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/mod/semver"
2022-11-24 04:57:58 -05:00
"gopkg.in/yaml.v3"
2022-08-31 05:59:07 -04:00
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
func TestGetCurrentImageVersion ( t * testing . T ) {
testCases := map [ string ] struct {
stubUpgradePlanner stubUpgradePlanner
wantErr bool
} {
2022-11-29 05:39:07 -05:00
"valid version" : {
2022-08-31 05:59:07 -04:00
stubUpgradePlanner : stubUpgradePlanner {
2022-11-29 05:39:07 -05:00
image : "v1.0.0" ,
2022-08-31 05:59:07 -04:00
} ,
} ,
2022-11-29 05:39:07 -05:00
"invalid version" : {
2022-08-31 05:59:07 -04:00
stubUpgradePlanner : stubUpgradePlanner {
2022-11-29 05:39:07 -05:00
image : "invalid" ,
2022-08-31 05:59:07 -04:00
} ,
wantErr : true ,
} ,
"GetCurrentImage error" : {
stubUpgradePlanner : stubUpgradePlanner {
err : errors . New ( "error" ) ,
} ,
wantErr : true ,
} ,
}
for name , tc := range testCases {
t . Run ( name , func ( t * testing . T ) {
assert := assert . New ( t )
2022-11-29 05:39:07 -05:00
version , err := getCurrentImageVersion ( context . Background ( ) , tc . stubUpgradePlanner )
2022-08-31 05:59:07 -04:00
if tc . wantErr {
assert . Error ( err )
return
}
assert . NoError ( err )
assert . True ( semver . IsValid ( version ) )
} )
}
}
func TestGetCompatibleImages ( t * testing . T ) {
2022-11-29 05:39:07 -05:00
imageList := [ ] string {
"v0.0.0" ,
"v1.0.0" ,
"v1.0.1" ,
"v1.0.2" ,
"v1.1.0" ,
2022-08-31 05:59:07 -04:00
}
testCases := map [ string ] struct {
2022-11-29 05:39:07 -05:00
images [ ] string
2022-08-31 05:59:07 -04:00
version string
2022-11-29 05:39:07 -05:00
wantImages [ ] string
2022-08-31 05:59:07 -04:00
} {
2022-11-29 05:39:07 -05:00
"filters <= v1.0.0" : {
2022-08-31 05:59:07 -04:00
images : imageList ,
version : "v1.0.0" ,
2022-11-29 05:39:07 -05:00
wantImages : [ ] string {
"v1.0.1" ,
"v1.0.2" ,
"v1.1.0" ,
2022-08-31 05:59:07 -04:00
} ,
} ,
"no compatible images" : {
2022-11-29 05:39:07 -05:00
images : imageList ,
version : "v999.999.999" ,
2022-08-31 05:59:07 -04:00
} ,
}
for name , tc := range testCases {
t . Run ( name , func ( t * testing . T ) {
assert := assert . New ( t )
2022-11-29 05:39:07 -05:00
compatibleImages := getCompatibleImages ( tc . version , tc . images )
assert . EqualValues ( tc . wantImages , compatibleImages )
2022-08-31 05:59:07 -04:00
} )
}
}
func TestGetCompatibleImageMeasurements ( t * testing . T ) {
assert := assert . New ( t )
2022-11-29 05:39:07 -05:00
csp := cloudprovider . Azure
images := [ ] string { "v0.0.0" , "v1.0.0" }
2022-08-31 05:59:07 -04:00
client := newTestClient ( func ( req * http . Request ) * http . Response {
2022-11-28 04:27:33 -05:00
if strings . HasSuffix ( req . URL . String ( ) , "v0.0.0/azure/measurements.json" ) {
2022-08-31 05:59:07 -04:00
return & http . Response {
StatusCode : http . StatusOK ,
2022-11-28 04:27:33 -05:00
Body : io . NopCloser ( strings . NewReader ( ` { "csp":"azure","image":"v0.0.0","measurements": { "0": { "expected":"0000000000000000000000000000000000000000000000000000000000000000","warnOnly":false}}} ` ) ) ,
2022-08-31 05:59:07 -04:00
Header : make ( http . Header ) ,
}
}
2022-11-28 04:27:33 -05:00
if strings . HasSuffix ( req . URL . String ( ) , "v0.0.0/azure/measurements.json.sig" ) {
2022-08-31 05:59:07 -04:00
return & http . Response {
StatusCode : http . StatusOK ,
2022-11-28 04:27:33 -05:00
Body : io . NopCloser ( strings . NewReader ( "MEQCIGRR7RaSMs892Ta06/Tz7LqPUxI05X4wQcP+nFFmZtmaAiBNl9X8mUKmUBfxg13LQBfmmpw6JwYQor5hOwM3NFVPAg==" ) ) ,
Header : make ( http . Header ) ,
}
}
if strings . HasSuffix ( req . URL . String ( ) , "v1.0.0/azure/measurements.json" ) {
return & http . Response {
StatusCode : http . StatusOK ,
Body : io . NopCloser ( strings . NewReader ( ` { "csp":"azure","image":"v1.0.0","measurements": { "0": { "expected":"0000000000000000000000000000000000000000000000000000000000000000","warnOnly":false}}} ` ) ) ,
Header : make ( http . Header ) ,
}
}
if strings . HasSuffix ( req . URL . String ( ) , "v1.0.0/azure/measurements.json.sig" ) {
return & http . Response {
StatusCode : http . StatusOK ,
Body : io . NopCloser ( strings . NewReader ( "MEQCIFh8CVELp/Da2U2Jt404OXsUeDfqtrf3pqGRuvxnxhI8AiBTHF9tHEPwFedYG3Jgn2ELOxss+Ybc6135vEtClBrbpg==" ) ) ,
2022-08-31 05:59:07 -04:00
Header : make ( http . Header ) ,
}
}
return & http . Response {
StatusCode : http . StatusNotFound ,
Body : io . NopCloser ( strings . NewReader ( "Not found." ) ) ,
Header : make ( http . Header ) ,
}
} )
2022-11-28 04:27:33 -05:00
pubK := [ ] byte ( "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEu78QgxOOcao6U91CSzEXxrKhvFTt\nJHNy+eX6EMePtDm8CnDF9HSwnTlD0itGJ/XHPQA5YX10fJAqI1y+ehlFMw==\n-----END PUBLIC KEY-----" )
2022-08-31 05:59:07 -04:00
2022-11-29 05:39:07 -05:00
upgrades , err := getCompatibleImageMeasurements ( context . Background ( ) , & cobra . Command { } , client , singleUUIDVerifier ( ) , pubK , csp , images )
2022-08-31 05:59:07 -04:00
assert . NoError ( err )
2022-11-29 05:39:07 -05:00
for _ , image := range upgrades {
2022-08-31 05:59:07 -04:00
assert . NotEmpty ( image . Measurements )
}
}
func TestUpgradePlan ( t * testing . T ) {
2022-12-01 04:26:35 -05:00
availablePatches := versionsapi . List {
2022-11-29 05:39:07 -05:00
Versions : [ ] string { "v1.0.0" , "v1.0.1" } ,
2022-08-31 05:59:07 -04:00
}
2022-11-28 04:27:33 -05:00
// Cosign private key used to sign the measurements.
// Generated with: cosign generate-key-pair
// Password left empty.
//
// -----BEGIN ENCRYPTED COSIGN PRIVATE KEY-----
// eyJrZGYiOnsibmFtZSI6InNjcnlwdCIsInBhcmFtcyI6eyJOIjozMjc2OCwiciI6
// OCwicCI6MX0sInNhbHQiOiJlRHVYMWRQMGtIWVRnK0xkbjcxM0tjbFVJaU92eFVX
// VXgvNi9BbitFVk5BPSJ9LCJjaXBoZXIiOnsibmFtZSI6Im5hY2wvc2VjcmV0Ym94
// Iiwibm9uY2UiOiJwaWhLL2txNmFXa2hqSVVHR3RVUzhTVkdHTDNIWWp4TCJ9LCJj
// aXBoZXJ0ZXh0Ijoidm81SHVWRVFWcUZ2WFlQTTVPaTVaWHM5a255bndZU2dvcyth
// VklIeHcrOGFPamNZNEtvVjVmL3lHRHR0K3BHV2toanJPR1FLOWdBbmtsazFpQ0c5
// a2czUXpPQTZsU2JRaHgvZlowRVRZQ0hLeElncEdPRVRyTDlDenZDemhPZXVSOXJ6
// TDcvRjBBVy9vUDVqZXR3dmJMNmQxOEhjck9kWE8yVmYxY2w0YzNLZjVRcnFSZzlN
// dlRxQWFsNXJCNHNpY1JaMVhpUUJjb0YwNHc9PSJ9
// -----END ENCRYPTED COSIGN PRIVATE KEY-----
pubK := "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEu78QgxOOcao6U91CSzEXxrKhvFTt\nJHNy+eX6EMePtDm8CnDF9HSwnTlD0itGJ/XHPQA5YX10fJAqI1y+ehlFMw==\n-----END PUBLIC KEY-----"
2022-08-31 05:59:07 -04:00
testCases := map [ string ] struct {
2022-12-29 11:27:15 -05:00
patchLister stubVersionListFetcher
2022-08-31 05:59:07 -04:00
planner stubUpgradePlanner
flags upgradePlanFlags
2022-11-29 05:39:07 -05:00
cliVersion string
2022-08-31 05:59:07 -04:00
csp cloudprovider . Provider
2022-10-11 07:57:52 -04:00
verifier rekorVerifier
2022-08-31 05:59:07 -04:00
measurementsFetchStatus int
wantUpgrade bool
wantErr bool
} {
2022-11-29 05:39:07 -05:00
"upgrades gcp" : {
2022-12-29 11:27:15 -05:00
patchLister : stubVersionListFetcher { list : availablePatches } ,
2022-08-31 05:59:07 -04:00
planner : stubUpgradePlanner {
2022-11-29 05:39:07 -05:00
image : "v1.0.0" ,
2022-08-31 05:59:07 -04:00
} ,
measurementsFetchStatus : http . StatusOK ,
flags : upgradePlanFlags {
configPath : constants . ConfigFilename ,
filePath : "upgrade-plan.yaml" ,
2022-11-28 04:27:33 -05:00
cosignPubKey : pubK ,
2022-08-31 05:59:07 -04:00
} ,
2022-11-29 05:39:07 -05:00
cliVersion : "v1.0.0" ,
2022-08-31 05:59:07 -04:00
csp : cloudprovider . GCP ,
2022-10-11 07:57:52 -04:00
verifier : singleUUIDVerifier ( ) ,
2022-11-29 05:39:07 -05:00
wantUpgrade : true ,
2022-08-31 05:59:07 -04:00
} ,
2022-11-29 05:39:07 -05:00
"upgrades azure" : {
2022-12-29 11:27:15 -05:00
patchLister : stubVersionListFetcher { list : availablePatches } ,
2022-08-31 05:59:07 -04:00
planner : stubUpgradePlanner {
2022-11-29 05:39:07 -05:00
image : "v1.0.0" ,
2022-08-31 05:59:07 -04:00
} ,
measurementsFetchStatus : http . StatusOK ,
flags : upgradePlanFlags {
configPath : constants . ConfigFilename ,
filePath : "upgrade-plan.yaml" ,
2022-11-28 04:27:33 -05:00
cosignPubKey : pubK ,
2022-08-31 05:59:07 -04:00
} ,
2022-11-29 05:39:07 -05:00
csp : cloudprovider . Azure ,
cliVersion : "v999.999.999" ,
2022-10-11 07:57:52 -04:00
verifier : singleUUIDVerifier ( ) ,
2022-08-31 05:59:07 -04:00
wantUpgrade : true ,
} ,
2022-11-29 05:39:07 -05:00
"current image newer than updates" : {
2022-12-29 11:27:15 -05:00
patchLister : stubVersionListFetcher { list : availablePatches } ,
2022-08-31 05:59:07 -04:00
planner : stubUpgradePlanner {
2022-11-29 05:39:07 -05:00
image : "v999.999.999" ,
2022-08-31 05:59:07 -04:00
} ,
measurementsFetchStatus : http . StatusOK ,
flags : upgradePlanFlags {
configPath : constants . ConfigFilename ,
filePath : "upgrade-plan.yaml" ,
2022-11-28 04:27:33 -05:00
cosignPubKey : pubK ,
2022-08-31 05:59:07 -04:00
} ,
2022-11-29 05:39:07 -05:00
csp : cloudprovider . GCP ,
2022-10-11 07:57:52 -04:00
verifier : singleUUIDVerifier ( ) ,
2022-11-29 05:39:07 -05:00
wantUpgrade : false ,
} ,
"current image newer than cli" : {
2022-12-29 11:27:15 -05:00
patchLister : stubVersionListFetcher { list : availablePatches } ,
2022-11-29 05:39:07 -05:00
planner : stubUpgradePlanner {
image : "v999.999.999" ,
} ,
measurementsFetchStatus : http . StatusOK ,
flags : upgradePlanFlags {
configPath : constants . ConfigFilename ,
filePath : "upgrade-plan.yaml" ,
cosignPubKey : pubK ,
} ,
csp : cloudprovider . GCP ,
cliVersion : "v1.0.0" ,
verifier : singleUUIDVerifier ( ) ,
wantUpgrade : false ,
2022-08-31 05:59:07 -04:00
} ,
"upgrade to stdout" : {
2022-12-29 11:27:15 -05:00
patchLister : stubVersionListFetcher { list : availablePatches } ,
2022-08-31 05:59:07 -04:00
planner : stubUpgradePlanner {
2022-11-29 05:39:07 -05:00
image : "v1.0.0" ,
2022-08-31 05:59:07 -04:00
} ,
measurementsFetchStatus : http . StatusOK ,
flags : upgradePlanFlags {
configPath : constants . ConfigFilename ,
filePath : "-" ,
2022-11-28 04:27:33 -05:00
cosignPubKey : pubK ,
2022-08-31 05:59:07 -04:00
} ,
csp : cloudprovider . GCP ,
2022-11-29 05:39:07 -05:00
cliVersion : "v1.0.0" ,
2022-10-11 07:57:52 -04:00
verifier : singleUUIDVerifier ( ) ,
2022-08-31 05:59:07 -04:00
wantUpgrade : true ,
} ,
"current image not valid" : {
2022-12-29 11:27:15 -05:00
patchLister : stubVersionListFetcher { list : availablePatches } ,
2022-08-31 05:59:07 -04:00
planner : stubUpgradePlanner {
image : "not-valid" ,
} ,
measurementsFetchStatus : http . StatusOK ,
flags : upgradePlanFlags {
configPath : constants . ConfigFilename ,
filePath : "upgrade-plan.yaml" ,
2022-11-28 04:27:33 -05:00
cosignPubKey : pubK ,
2022-08-31 05:59:07 -04:00
} ,
2022-11-29 05:39:07 -05:00
csp : cloudprovider . GCP ,
cliVersion : "v1.0.0" ,
verifier : singleUUIDVerifier ( ) ,
wantErr : true ,
2022-08-31 05:59:07 -04:00
} ,
"image fetch error" : {
2022-12-29 11:27:15 -05:00
patchLister : stubVersionListFetcher { err : errors . New ( "error" ) } ,
2022-08-31 05:59:07 -04:00
planner : stubUpgradePlanner {
2022-11-29 05:39:07 -05:00
image : "v1.0.0" ,
2022-08-31 05:59:07 -04:00
} ,
measurementsFetchStatus : http . StatusOK ,
flags : upgradePlanFlags {
configPath : constants . ConfigFilename ,
filePath : "upgrade-plan.yaml" ,
2022-11-28 04:27:33 -05:00
cosignPubKey : pubK ,
2022-08-31 05:59:07 -04:00
} ,
2022-11-29 05:39:07 -05:00
csp : cloudprovider . GCP ,
cliVersion : "v1.0.0" ,
verifier : singleUUIDVerifier ( ) ,
2022-08-31 05:59:07 -04:00
} ,
"measurements fetch error" : {
2022-12-29 11:27:15 -05:00
patchLister : stubVersionListFetcher { list : availablePatches } ,
2022-08-31 05:59:07 -04:00
planner : stubUpgradePlanner {
2022-11-29 05:39:07 -05:00
image : "v1.0.0" ,
2022-08-31 05:59:07 -04:00
} ,
measurementsFetchStatus : http . StatusInternalServerError ,
flags : upgradePlanFlags {
configPath : constants . ConfigFilename ,
filePath : "upgrade-plan.yaml" ,
2022-11-28 04:27:33 -05:00
cosignPubKey : pubK ,
2022-08-31 05:59:07 -04:00
} ,
2022-11-29 05:39:07 -05:00
csp : cloudprovider . GCP ,
cliVersion : "v1.0.0" ,
verifier : singleUUIDVerifier ( ) ,
2022-10-11 07:57:52 -04:00
} ,
"failing search should not result in error" : {
2022-12-29 11:27:15 -05:00
patchLister : stubVersionListFetcher { list : availablePatches } ,
2022-10-11 07:57:52 -04:00
planner : stubUpgradePlanner {
2022-11-29 05:39:07 -05:00
image : "v1.0.0" ,
2022-10-11 07:57:52 -04:00
} ,
measurementsFetchStatus : http . StatusOK ,
flags : upgradePlanFlags {
configPath : constants . ConfigFilename ,
filePath : "upgrade-plan.yaml" ,
2022-11-28 04:27:33 -05:00
cosignPubKey : pubK ,
2022-10-11 07:57:52 -04:00
} ,
2022-11-29 05:39:07 -05:00
csp : cloudprovider . GCP ,
cliVersion : "v1.0.0" ,
2022-10-11 07:57:52 -04:00
verifier : & stubRekorVerifier {
SearchByHashUUIDs : [ ] string { } ,
SearchByHashError : errors . New ( "some error" ) ,
} ,
wantUpgrade : true ,
} ,
"failing verify should not result in error" : {
2022-12-29 11:27:15 -05:00
patchLister : stubVersionListFetcher { list : availablePatches } ,
2022-10-11 07:57:52 -04:00
planner : stubUpgradePlanner {
2022-11-29 05:39:07 -05:00
image : "v1.0.0" ,
2022-10-11 07:57:52 -04:00
} ,
measurementsFetchStatus : http . StatusOK ,
flags : upgradePlanFlags {
configPath : constants . ConfigFilename ,
filePath : "upgrade-plan.yaml" ,
2022-11-28 04:27:33 -05:00
cosignPubKey : pubK ,
2022-10-11 07:57:52 -04:00
} ,
2022-11-29 05:39:07 -05:00
csp : cloudprovider . GCP ,
cliVersion : "v1.0.0" ,
2022-10-11 07:57:52 -04:00
verifier : & stubRekorVerifier {
SearchByHashUUIDs : [ ] string { "11111111111111111111111111111111111111111111111111111111111111111111111111111111" } ,
VerifyEntryError : errors . New ( "some error" ) ,
} ,
wantUpgrade : true ,
2022-08-31 05:59:07 -04:00
} ,
}
for name , tc := range testCases {
t . Run ( name , func ( t * testing . T ) {
assert := assert . New ( t )
require := require . New ( t )
fileHandler := file . NewHandler ( afero . NewMemMapFs ( ) )
2022-11-15 09:40:49 -05:00
cfg := defaultConfigWithExpectedMeasurements ( t , config . Default ( ) , tc . csp )
2022-08-31 05:59:07 -04:00
require . NoError ( fileHandler . WriteYAML ( tc . flags . configPath , cfg ) )
cmd := newUpgradePlanCmd ( )
cmd . SetContext ( context . Background ( ) )
2022-11-10 04:27:24 -05:00
var outTarget bytes . Buffer
cmd . SetOut ( & outTarget )
var errTarget bytes . Buffer
cmd . SetErr ( & errTarget )
2022-08-31 05:59:07 -04:00
client := newTestClient ( func ( req * http . Request ) * http . Response {
2022-11-28 04:27:33 -05:00
if strings . HasSuffix ( req . URL . String ( ) , "azure/measurements.json" ) {
return & http . Response {
StatusCode : tc . measurementsFetchStatus ,
2022-11-29 05:39:07 -05:00
Body : io . NopCloser ( strings . NewReader ( ` { "csp":"azure","image":"v1.0.1","measurements": { "0": { "expected":"0000000000000000000000000000000000000000000000000000000000000000","warnOnly":false}}} ` ) ) ,
2022-11-28 04:27:33 -05:00
Header : make ( http . Header ) ,
}
}
if strings . HasSuffix ( req . URL . String ( ) , "azure/measurements.json.sig" ) {
return & http . Response {
StatusCode : tc . measurementsFetchStatus ,
2022-11-29 05:39:07 -05:00
Body : io . NopCloser ( strings . NewReader ( "MEYCIQDu2Sft91FjN278uP+r/HFMms6IH/tRtaHzYvIN0xPgdwIhAJhiFxVsHCa0NK6bZOGLE9c4miZHIqFTKvgpTf3rJ9dW" ) ) ,
2022-11-28 04:27:33 -05:00
Header : make ( http . Header ) ,
}
}
if strings . HasSuffix ( req . URL . String ( ) , "gcp/measurements.json" ) {
2022-08-31 05:59:07 -04:00
return & http . Response {
StatusCode : tc . measurementsFetchStatus ,
2022-11-29 05:39:07 -05:00
Body : io . NopCloser ( strings . NewReader ( ` { "csp":"gcp","image":"v1.0.1","measurements": { "0": { "expected":"0000000000000000000000000000000000000000000000000000000000000000","warnOnly":false}}} ` ) ) ,
2022-08-31 05:59:07 -04:00
Header : make ( http . Header ) ,
}
}
2022-11-28 04:27:33 -05:00
if strings . HasSuffix ( req . URL . String ( ) , "gcp/measurements.json.sig" ) {
2022-08-31 05:59:07 -04:00
return & http . Response {
StatusCode : tc . measurementsFetchStatus ,
2022-11-29 05:39:07 -05:00
Body : io . NopCloser ( strings . NewReader ( "MEQCIBUssv92LpSMiXE1UAVf2fW8J9pZHiLseo2tdZjxv2OMAiB6K8e8yL0768jWjlFnRe3Rc2x/dX34uzX3h0XUrlYt1A==" ) ) ,
2022-08-31 05:59:07 -04:00
Header : make ( http . Header ) ,
}
}
return & http . Response {
StatusCode : http . StatusNotFound ,
Body : io . NopCloser ( strings . NewReader ( "Not found." ) ) ,
Header : make ( http . Header ) ,
}
} )
2023-01-04 04:46:29 -05:00
up := & upgradePlanCmd { log : logger . NewTest ( t ) }
err := up . upgradePlan ( cmd , tc . planner , tc . patchLister , fileHandler , client , tc . verifier , tc . flags , tc . cliVersion )
2022-08-31 05:59:07 -04:00
if tc . wantErr {
assert . Error ( err )
return
}
assert . NoError ( err )
if ! tc . wantUpgrade {
2022-11-10 04:27:24 -05:00
assert . Contains ( errTarget . String ( ) , "No compatible images" )
2022-08-31 05:59:07 -04:00
return
}
var availableUpgrades map [ string ] config . UpgradeConfig
if tc . flags . filePath == "-" {
2022-11-10 04:27:24 -05:00
require . NoError ( yaml . Unmarshal ( outTarget . Bytes ( ) , & availableUpgrades ) )
2022-08-31 05:59:07 -04:00
} else {
require . NoError ( fileHandler . ReadYAMLStrict ( tc . flags . filePath , & availableUpgrades ) )
}
assert . GreaterOrEqual ( len ( availableUpgrades ) , 1 )
for _ , upgrade := range availableUpgrades {
assert . NotEmpty ( upgrade . Image )
assert . NotEmpty ( upgrade . Measurements )
}
} )
}
}
2022-11-29 05:39:07 -05:00
func TestNextMinorVersion ( t * testing . T ) {
testCases := map [ string ] struct {
version string
wantNextMinorVersion string
wantErr bool
} {
"gets next" : {
version : "v1.0.0" ,
wantNextMinorVersion : "v1.1" ,
} ,
"gets next from minor version" : {
version : "v1.0" ,
wantNextMinorVersion : "v1.1" ,
} ,
"empty version" : {
wantErr : true ,
} ,
2022-08-31 05:59:07 -04:00
}
2022-11-29 05:39:07 -05:00
for name , tc := range testCases {
t . Run ( name , func ( t * testing . T ) {
assert := assert . New ( t )
gotNext , err := nextMinorVersion ( tc . version )
if tc . wantErr {
assert . Error ( err )
return
}
assert . NoError ( err )
assert . Equal ( tc . wantNextMinorVersion , gotNext )
} )
}
}
type stubUpgradePlanner struct {
image string
err error
}
func ( u stubUpgradePlanner ) GetCurrentImage ( context . Context ) ( * unstructured . Unstructured , string , error ) {
return nil , u . image , u . err
}
2022-12-29 11:27:15 -05:00
type stubVersionListFetcher struct {
2022-12-01 04:26:35 -05:00
list versionsapi . List
2022-11-29 05:39:07 -05:00
err error
}
2022-12-29 11:27:15 -05:00
func ( s stubVersionListFetcher ) FetchVersionList ( context . Context , versionsapi . List ) ( versionsapi . List , error ) {
return s . list , s . err
2022-08-31 05:59:07 -04:00
}