Skip to content

Commit

Permalink
Move unit tests to new apply backend
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Weiße <[email protected]>
  • Loading branch information
daniel-weisse committed Oct 16, 2023
1 parent fd121c4 commit 1e834e4
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 979 deletions.
4 changes: 4 additions & 0 deletions cli/internal/cmd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ go_library(
go_test(
name = "cmd_test",
srcs = [
"apply_test.go",
"cloud_test.go",
"configfetchmeasurements_test.go",
"configgenerate_test.go",
Expand Down Expand Up @@ -173,12 +174,15 @@ go_test(
"@com_github_google_go_tpm_tools//proto/tpm",
"@com_github_spf13_afero//:afero",
"@com_github_spf13_cobra//:cobra",
"@com_github_spf13_pflag//:pflag",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//mock",
"@com_github_stretchr_testify//require",
"@io_k8s_api//core/v1:core",
"@io_k8s_apiextensions_apiserver//pkg/apis/apiextensions/v1:apiextensions",
"@io_k8s_apimachinery//pkg/api/errors",
"@io_k8s_apimachinery//pkg/apis/meta/v1:meta",
"@io_k8s_apimachinery//pkg/runtime/schema",
"@io_k8s_client_go//tools/clientcmd",
"@io_k8s_client_go//tools/clientcmd/api",
"@org_golang_google_grpc//:go_default_library",
Expand Down
24 changes: 16 additions & 8 deletions cli/internal/cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/edgelesssys/constellation/v2/internal/api/attestationconfigapi"
"github.com/edgelesssys/constellation/v2/internal/atls"
"github.com/edgelesssys/constellation/v2/internal/attestation/variant"
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
"github.com/edgelesssys/constellation/v2/internal/compatibility"
"github.com/edgelesssys/constellation/v2/internal/config"
"github.com/edgelesssys/constellation/v2/internal/constants"
Expand Down Expand Up @@ -64,8 +65,7 @@ func (f *applyFlags) parse(flags *pflag.FlagSet) error {

rawSkipPhases, err := flags.GetStringSlice("skip-phases")
if err != nil {
rawSkipPhases = []string{}
// return fmt.Errorf("getting 'skip-phases' flag: %w", err)
return fmt.Errorf("getting 'skip-phases' flag: %w", err)
}
var skipPhases []skipPhase
for _, phase := range rawSkipPhases {
Expand All @@ -80,14 +80,12 @@ func (f *applyFlags) parse(flags *pflag.FlagSet) error {

f.yes, err = flags.GetBool("yes")
if err != nil {
f.yes = false
// return fmt.Errorf("getting 'yes' flag: %w", err)
return fmt.Errorf("getting 'yes' flag: %w", err)
}

f.upgradeTimeout, err = flags.GetDuration("timeout")
if err != nil {
f.upgradeTimeout = time.Hour
// return fmt.Errorf("getting 'timeout' flag: %w", err)
return fmt.Errorf("getting 'timeout' flag: %w", err)
}

f.conformance, err = flags.GetBool("conformance")
Expand All @@ -106,8 +104,7 @@ func (f *applyFlags) parse(flags *pflag.FlagSet) error {

f.mergeConfigs, err = flags.GetBool("merge-kubeconfig")
if err != nil {
f.mergeConfigs = false
// return fmt.Errorf("getting 'merge-kubeconfig' flag: %w", err)
return fmt.Errorf("getting 'merge-kubeconfig' flag: %w", err)
}
return nil
}
Expand Down Expand Up @@ -339,6 +336,17 @@ func (a *applyCmd) apply(cmd *cobra.Command, configFetcher attestationconfigapi.
}
}

// Constellation on QEMU or OpenStack don't support upgrades
// If using one of those providers, make sure the command is only used to initialize a cluster
if !(conf.GetProvider() == cloudprovider.AWS || conf.GetProvider() == cloudprovider.Azure || conf.GetProvider() == cloudprovider.GCP) {
if !initRequired {
return fmt.Errorf("upgrades are not supported for provider %s", conf.GetProvider())
}
// Skip Terraform phase
a.log.Debugf("Skipping Infrastructure phase for provider %s", conf.GetProvider())
a.flags.skipPhases = append(a.flags.skipPhases, skipInfrastructurePhase)
}

// Print warning about AWS attestation
// TODO(derpsteb): remove once AWS fixes SEV-SNP attestation provisioning issues
if initRequired && conf.GetAttestationConfig().GetVariant().Equal(variant.AWSSEVSNP{}) {
Expand Down
153 changes: 153 additions & 0 deletions cli/internal/cmd/apply_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/

package cmd

import (
"context"
"fmt"
"testing"

"github.com/edgelesssys/constellation/v2/cli/internal/helm"
"github.com/edgelesssys/constellation/v2/internal/file"
"github.com/edgelesssys/constellation/v2/internal/logger"
"github.com/spf13/afero"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestParseApplyFlags(t *testing.T) {
require := require.New(t)
// TODO: Use flags := applyCmd().Flags() once we have a separate apply command
defaultFlags := func() *pflag.FlagSet {
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
flags.String("workspace", "", "")
flags.String("tf-log", "NONE", "")
flags.Bool("force", false, "")
flags.Bool("debug", false, "")
flags.Bool("merge-kubeconfig", false, "")
flags.Bool("conformance", false, "")
flags.Bool("skip-helm-wait", false, "")
flags.Bool("yes", false, "")
flags.StringSlice("skip-phases", []string{}, "")
flags.Duration("timeout", 0, "")
return flags
}

testCases := map[string]struct {
flags *pflag.FlagSet
wantFlags applyFlags
wantErr bool
}{
"default flags": {
flags: defaultFlags(),
wantFlags: applyFlags{
helmWaitMode: helm.WaitModeAtomic,
},
},
"skip phases": {
flags: func() *pflag.FlagSet {
flags := defaultFlags()
require.NoError(flags.Set("skip-phases", fmt.Sprintf("%s,%s", skipHelmPhase, skipK8sPhase)))
return flags
}(),
wantFlags: applyFlags{
skipPhases: []skipPhase{skipHelmPhase, skipK8sPhase},
helmWaitMode: helm.WaitModeAtomic,
},
},
"skip helm wait": {
flags: func() *pflag.FlagSet {
flags := defaultFlags()
require.NoError(flags.Set("skip-helm-wait", "true"))
return flags
}(),
wantFlags: applyFlags{
helmWaitMode: helm.WaitModeNone,
},
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
var flags applyFlags

err := flags.parse(tc.flags)
if tc.wantErr {
assert.Error(err)
return
}
assert.NoError(err)
assert.Equal(tc.wantFlags, flags)
})
}
}

func TestBackupHelmCharts(t *testing.T) {
testCases := map[string]struct {
helmApplier helm.Applier
backupClient *stubKubernetesUpgrader
includesUpgrades bool
wantErr bool
}{
"success, no upgrades": {
helmApplier: &stubRunner{},
backupClient: &stubKubernetesUpgrader{},
},
"success with upgrades": {
helmApplier: &stubRunner{},
backupClient: &stubKubernetesUpgrader{},
includesUpgrades: true,
},
"saving charts fails": {
helmApplier: &stubRunner{
saveChartsErr: assert.AnError,
},
backupClient: &stubKubernetesUpgrader{},
wantErr: true,
},
"backup CRDs fails": {
helmApplier: &stubRunner{},
backupClient: &stubKubernetesUpgrader{
backupCRDsErr: assert.AnError,
},
includesUpgrades: true,
wantErr: true,
},
"backup CRs fails": {
helmApplier: &stubRunner{},
backupClient: &stubKubernetesUpgrader{
backupCRsErr: assert.AnError,
},
includesUpgrades: true,
wantErr: true,
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)

a := applyCmd{
fileHandler: file.NewHandler(afero.NewMemMapFs()),
log: logger.NewTest(t),
}

err := a.backupHelmCharts(context.Background(), tc.backupClient, tc.helmApplier, tc.includesUpgrades, "")
if tc.wantErr {
assert.Error(err)
return
}
assert.NoError(err)
if tc.includesUpgrades {
assert.True(tc.backupClient.backupCRDsCalled)
assert.True(tc.backupClient.backupCRsCalled)
}
})
}
}
Loading

0 comments on commit 1e834e4

Please sign in to comment.