Skip to content

Commit

Permalink
update unit tests; use client factory to build client
Browse files Browse the repository at this point in the history
  • Loading branch information
cxbrowne1207 committed Jan 15, 2024
1 parent 7f33908 commit 1400a46
Show file tree
Hide file tree
Showing 17 changed files with 211 additions and 83 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ mocks: ## Generate mocks
${MOCKGEN} -destination=pkg/bootstrapper/mocks/bootstrapper.go -package=mocks "github.com/aws/eks-anywhere/pkg/bootstrapper" ClusterClient
${MOCKGEN} -destination=pkg/git/providers/github/mocks/github.go -package=mocks "github.com/aws/eks-anywhere/pkg/git/providers/github" GithubClient
${MOCKGEN} -destination=pkg/git/mocks/git.go -package=mocks "github.com/aws/eks-anywhere/pkg/git" Client,ProviderClient
${MOCKGEN} -destination=pkg/workflows/interfaces/mocks/clients.go -package=mocks "github.com/aws/eks-anywhere/pkg/workflows/interfaces" Bootstrapper,ClusterManager,GitOpsManager,Validator,CAPIManager,EksdInstaller,EksdUpgrader,PackageInstaller,ClusterUpgrader,ClusterCreator
${MOCKGEN} -destination=pkg/workflows/interfaces/mocks/clients.go -package=mocks "github.com/aws/eks-anywhere/pkg/workflows/interfaces" Bootstrapper,ClusterManager,GitOpsManager,Validator,CAPIManager,EksdInstaller,EksdUpgrader,PackageInstaller,ClusterUpgrader,ClusterCreator,ClientFactory
${MOCKGEN} -destination=pkg/git/gogithub/mocks/client.go -package=mocks "github.com/aws/eks-anywhere/pkg/git/gogithub" Client
${MOCKGEN} -destination=pkg/git/gitclient/mocks/client.go -package=mocks "github.com/aws/eks-anywhere/pkg/git/gitclient" GoGit
${MOCKGEN} -destination=pkg/validations/mocks/docker.go -package=mocks "github.com/aws/eks-anywhere/pkg/validations" DockerExecutable
Expand Down
10 changes: 2 additions & 8 deletions cmd/eksctl-anywhere/cmd/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,8 @@ func (cc *createClusterOptions) createCluster(cmd *cobra.Command, _ []string) er
return err
}

clusterKubeconfig := kubeconfig.FromClusterName(clusterSpec.Cluster.Name)
client, err := deps.UnAuthKubeClient.BuildClientFromKubeconfig(clusterKubeconfig)
if err != nil {
return err
}

createCluster := workflows.NewCreate(
client,
deps.UnAuthKubeClient,

Check warning on line 209 in cmd/eksctl-anywhere/cmd/createcluster.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/createcluster.go#L209

Added line #L209 was not covered by tests
deps.Bootstrapper,
deps.Provider,
deps.ClusterManager,
Expand All @@ -229,7 +223,7 @@ func (cc *createClusterOptions) createCluster(cmd *cobra.Command, _ []string) er
Spec: clusterSpec,
WorkloadCluster: &types.Cluster{
Name: clusterSpec.Cluster.Name,
KubeconfigFile: clusterKubeconfig,
KubeconfigFile: kubeconfig.FromClusterName(clusterSpec.Cluster.Name),
},
ManagementCluster: mgmt,
Provider: deps.Provider,
Expand Down
6 changes: 1 addition & 5 deletions cmd/eksctl-anywhere/cmd/upgradecluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,9 @@ func (uc *upgradeClusterOptions) upgradeCluster(cmd *cobra.Command, args []strin

upgradeValidations := upgradevalidations.New(validationOpts)

client, err := deps.UnAuthKubeClient.BuildClientFromKubeconfig(workloadCluster.KubeconfigFile)
if err != nil {
return err
}
if clusterConfig.IsSelfManaged() {
upgrade := management.NewUpgrade(
client,
deps.UnAuthKubeClient,

Check warning on line 206 in cmd/eksctl-anywhere/cmd/upgradecluster.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/upgradecluster.go#L206

Added line #L206 was not covered by tests
deps.Provider,
deps.CAPIManager,
deps.ClusterManager,
Expand Down
9 changes: 2 additions & 7 deletions cmd/eksctl-anywhere/cmd/upgrademanagementcomponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,8 @@ var upgradeManagementComponentsCmd = &cobra.Command{
}
defer close(cmd.Context(), deps)

clusterKubeconfig := kubeconfig.FromClusterName(clusterSpec.Cluster.Name)
client, err := deps.UnAuthKubeClient.BuildClientFromKubeconfig(clusterKubeconfig)
if err != nil {
return err
}
runner := management.NewUpgradeManagementComponentsRunner(
client,
deps.UnAuthKubeClient,

Check warning on line 74 in cmd/eksctl-anywhere/cmd/upgrademanagementcomponents.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/upgrademanagementcomponents.go#L74

Added line #L74 was not covered by tests
deps.Provider,
deps.CAPIManager,
deps.ClusterManager,
Expand All @@ -88,7 +83,7 @@ var upgradeManagementComponentsCmd = &cobra.Command{

managementCluster := &types.Cluster{
Name: clusterSpec.Cluster.Name,
KubeconfigFile: clusterKubeconfig,
KubeconfigFile: kubeconfig.FromClusterName(clusterSpec.Cluster.Name),
}

validator := management.NewUMCValidator(managementCluster, deps.Kubectl)
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1299,8 +1299,8 @@ func (c *Cluster) ControlPlaneAnnotation() string {
return controlPlaneAnnotation
}

// SetManagmentComponentsVersion sets the `eksa-management-components version` annotation on the Cluster object.
func (c *Cluster) SetManagmentComponentsVersion(version string) {
// SetManagementComponentsVersion sets the `eksa-management-components version` annotation on the Cluster object.
func (c *Cluster) SetManagementComponentsVersion(version string) {
if c.IsManaged() {
return
}
Expand Down
27 changes: 13 additions & 14 deletions pkg/api/v1alpha1/cluster_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3232,34 +3232,33 @@ func TestValidateCluster(t *testing.T) {
}

func TestCluster_SetManagmentComponentsVersion(t *testing.T) {
managmentClusterName := "mgmt-cluster"
expectedManagmentComponentVersion := "v0.0.0-dev+build.0000"

testCases := []struct {
name string
cluster *v1alpha1.Cluster
wantAnnotations map[string]string
name string
cluster *v1alpha1.Cluster
managementComponentsVersion string
wantManagementComponentsVersion string
wantAnnotations map[string]string
}{
{
name: "self-managed cluster",
cluster: baseCluster(func(c *v1alpha1.Cluster) {
c.SetManagmentComponentsVersion(expectedManagmentComponentVersion)
}),
wantAnnotations: map[string]string{"anywhere.eks.amazonaws.com/eksa-management-components-version": expectedManagmentComponentVersion},
name: "self-managed cluster",
cluster: baseCluster(),
managementComponentsVersion: "v0.0.0-dev+build.0000",
wantAnnotations: map[string]string{"anywhere.eks.amazonaws.com/eksa-management-components-version": "v0.0.0-dev+build.0000"},
},
{
name: "managed cluster",
cluster: baseCluster(func(c *v1alpha1.Cluster) {
c.SetManagedBy(managmentClusterName)
c.SetManagmentComponentsVersion(expectedManagmentComponentVersion)
c.SetManagedBy("mgmt-cluster")
}),
wantAnnotations: map[string]string{"anywhere.eks.amazonaws.com/managed-by": managmentClusterName},
managementComponentsVersion: "v0.0.0-dev+build.0000",
wantAnnotations: map[string]string{"anywhere.eks.amazonaws.com/managed-by": "mgmt-cluster"},
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
tt.cluster.SetManagementComponentsVersion(tt.managementComponentsVersion)
g.Expect(tt.cluster.Annotations).To(Equal(tt.wantAnnotations))
})
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,6 @@ const (
Upgrade Operation = 1
Delete Operation = 2
)

// EKSAFieldManager is the owner name for fields applied by the EKS-A CLI.
const EKSAFieldManager = "eks-a-cli"
3 changes: 1 addition & 2 deletions pkg/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"sigs.k8s.io/yaml"

"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/filewriter"
"github.com/aws/eks-anywhere/pkg/logger"
Expand All @@ -28,7 +27,7 @@ type Task interface {

// Command context maintains the mutable and shared entities.
type CommandContext struct {
Client kubernetes.Client
ClientFactory interfaces.ClientFactory
Bootstrapper interfaces.Bootstrapper
Provider providers.Provider
ClusterManager interfaces.ClusterManager
Expand Down
24 changes: 13 additions & 11 deletions pkg/workflows/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ import (
"github.com/aws/eks-anywhere/pkg/workflows/interfaces"
)

const (
eksaFieldManager = "eks-a-cli"
)

type Create struct {
client kubernetes.Client
clientFactory interfaces.ClientFactory
bootstrapper interfaces.Bootstrapper
provider providers.Provider
clusterManager interfaces.ClusterManager
Expand All @@ -33,13 +29,13 @@ type Create struct {
}

// NewCreate returns a Create instance.
func NewCreate(client kubernetes.Client, bootstrapper interfaces.Bootstrapper, provider providers.Provider,
func NewCreate(clientFactory interfaces.ClientFactory, bootstrapper interfaces.Bootstrapper, provider providers.Provider,
clusterManager interfaces.ClusterManager, gitOpsManager interfaces.GitOpsManager,
writer filewriter.FileWriter, eksdInstaller interfaces.EksdInstaller,
packageInstaller interfaces.PackageInstaller,
) *Create {
return &Create{
client: client,
clientFactory: clientFactory,
bootstrapper: bootstrapper,
provider: provider,
clusterManager: clusterManager,
Expand All @@ -59,7 +55,7 @@ func (c *Create) Run(ctx context.Context, clusterSpec *cluster.Spec, validator i
}
}
commandContext := &task.CommandContext{
Client: c.client,
ClientFactory: c.clientFactory,
Bootstrapper: c.bootstrapper,
Provider: c.provider,
ClusterManager: c.clusterManager,
Expand Down Expand Up @@ -382,9 +378,15 @@ func (s *InstallEksaComponentsTask) Run(ctx context.Context, commandContext *tas
return &CollectDiagnosticsTask{}
}

commandContext.ClusterSpec.Cluster.SetManagmentComponentsVersion(commandContext.ClusterSpec.EKSARelease.Spec.Version)
if err := commandContext.Client.ApplyServerSide(ctx,
eksaFieldManager,
client, err := commandContext.ClientFactory.BuildClientFromKubeconfig(targetCluster.KubeconfigFile)
if err != nil {
commandContext.SetError(err)
return &CollectDiagnosticsTask{}
}

commandContext.ClusterSpec.Cluster.SetManagementComponentsVersion(commandContext.ClusterSpec.EKSARelease.Spec.Version)
if err := client.ApplyServerSide(ctx,
constants.EKSAFieldManager,
commandContext.ClusterSpec.Cluster,
kubernetes.ApplyServerSideOptions{ForceOwnership: true},
); err != nil {
Expand Down
58 changes: 53 additions & 5 deletions pkg/workflows/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
clientmocks "github.com/aws/eks-anywhere/pkg/clients/kubernetes/mocks"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/constants"
writermocks "github.com/aws/eks-anywhere/pkg/filewriter/mocks"
"github.com/aws/eks-anywhere/pkg/providers"
providermocks "github.com/aws/eks-anywhere/pkg/providers/mocks"
Expand All @@ -26,6 +27,7 @@ import (
type createTestSetup struct {
t *testing.T
client *clientmocks.MockClient
clientFactory *mocks.MockClientFactory
packageInstaller *mocks.MockPackageInstaller
bootstrapper *mocks.MockBootstrapper
clusterManager *mocks.MockClusterManager
Expand All @@ -47,6 +49,7 @@ type createTestSetup struct {
func newCreateTest(t *testing.T) *createTestSetup {
mockCtrl := gomock.NewController(t)
client := clientmocks.NewMockClient(mockCtrl)
clientFactory := mocks.NewMockClientFactory(mockCtrl)
bootstrapper := mocks.NewMockBootstrapper(mockCtrl)
clusterManager := mocks.NewMockClusterManager(mockCtrl)
gitOpsManager := mocks.NewMockGitOpsManager(mockCtrl)
Expand All @@ -57,12 +60,13 @@ func newCreateTest(t *testing.T) *createTestSetup {

datacenterConfig := &v1alpha1.VSphereDatacenterConfig{}
machineConfigs := []providers.MachineConfig{&v1alpha1.VSphereMachineConfig{}}
workflow := workflows.NewCreate(client, bootstrapper, provider, clusterManager, gitOpsManager, writer, eksd, packageInstaller)
workflow := workflows.NewCreate(clientFactory, bootstrapper, provider, clusterManager, gitOpsManager, writer, eksd, packageInstaller)
validator := mocks.NewMockValidator(mockCtrl)

return &createTestSetup{
t: t,
client: client,
clientFactory: clientFactory,
bootstrapper: bootstrapper,
clusterManager: clusterManager,
gitOpsManager: gitOpsManager,
Expand Down Expand Up @@ -207,9 +211,11 @@ func (c *createTestSetup) expectInstallEksaComponents() {
c.eksd.EXPECT().InstallEksdManifest(
c.ctx, c.clusterSpec, c.workloadCluster),

c.clientFactory.EXPECT().BuildClientFromKubeconfig(c.workloadCluster.KubeconfigFile).Return(c.client, nil),

c.client.EXPECT().ApplyServerSide(
c.ctx,
"eks-a-cli",
constants.EKSAFieldManager,
c.clusterSpec.Cluster,
kubernetes.ApplyServerSideOptions{ForceOwnership: true},
),
Expand All @@ -236,9 +242,11 @@ func (c *createTestSetup) skipInstallEksaComponents() {
c.eksd.EXPECT().InstallEksdManifest(
c.ctx, c.clusterSpec, c.bootstrapCluster),

c.clientFactory.EXPECT().BuildClientFromKubeconfig(c.bootstrapCluster.KubeconfigFile).Return(c.client, nil),

c.client.EXPECT().ApplyServerSide(
c.ctx,
"eks-a-cli",
constants.EKSAFieldManager,
c.clusterSpec.Cluster,
kubernetes.ApplyServerSideOptions{ForceOwnership: true},
),
Expand Down Expand Up @@ -312,7 +320,7 @@ func TestCreateRunSuccess(t *testing.T) {
}
}

func TestCreateRunInstallEksaComponentsApplyFail(t *testing.T) {
func TestCreateRunInstallEksaComponentsBuildClientFailure(t *testing.T) {
wantError := errors.New("test error")
test := newCreateTest(t)

Expand All @@ -339,9 +347,49 @@ func TestCreateRunInstallEksaComponentsApplyFail(t *testing.T) {
test.eksd.EXPECT().InstallEksdManifest(
test.ctx, test.clusterSpec, test.workloadCluster),

test.clientFactory.EXPECT().BuildClientFromKubeconfig(test.workloadCluster.KubeconfigFile).Return(nil, wantError),
)
test.clusterManager.EXPECT().SaveLogsManagementCluster(test.ctx, test.clusterSpec, test.bootstrapCluster)
test.clusterManager.EXPECT().SaveLogsWorkloadCluster(test.ctx, test.provider, test.clusterSpec, test.workloadCluster)
test.writer.EXPECT().Write(fmt.Sprintf("%s-checkpoint.yaml", test.clusterSpec.Cluster.Name), gomock.Any())

if err := test.run(); err == nil {
t.Fatalf("Create.Run() err = %v, want err = %v", err, wantError)
}
}

func TestCreateRunInstallEksaComponentsApplyServerSideFailure(t *testing.T) {
wantError := errors.New("test error")
test := newCreateTest(t)

test.expectSetup()
test.expectPreflightValidationsToPass()
test.expectCreateBootstrap()
test.expectCreateWorkload()
test.expectInstallResourcesOnManagementTask()
test.expectMoveManagement()
gomock.InOrder(
test.clusterManager.EXPECT().InstallCustomComponents(
test.ctx, test.clusterSpec, test.workloadCluster, test.provider),

test.eksd.EXPECT().InstallEksdCRDs(test.ctx, test.clusterSpec, test.workloadCluster),

test.provider.EXPECT().DatacenterConfig(test.clusterSpec).Return(test.datacenterConfig),

test.provider.EXPECT().MachineConfigs(test.clusterSpec).Return(test.machineConfigs),

test.clusterManager.EXPECT().CreateEKSAResources(
test.ctx, test.workloadCluster, test.clusterSpec, test.datacenterConfig, test.machineConfigs,
),

test.eksd.EXPECT().InstallEksdManifest(
test.ctx, test.clusterSpec, test.workloadCluster),

test.clientFactory.EXPECT().BuildClientFromKubeconfig(test.workloadCluster.KubeconfigFile).Return(test.client, nil),

test.client.EXPECT().ApplyServerSide(
test.ctx,
"eks-a-cli",
constants.EKSAFieldManager,
test.clusterSpec.Cluster,
kubernetes.ApplyServerSideOptions{ForceOwnership: true},
).Return(wantError),
Expand Down
7 changes: 7 additions & 0 deletions pkg/workflows/interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ import (
"context"

"github.com/aws/eks-anywhere/pkg/bootstrapper"
"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/constants"
"github.com/aws/eks-anywhere/pkg/providers"
"github.com/aws/eks-anywhere/pkg/types"
"github.com/aws/eks-anywhere/pkg/validations"
)

// ClientFactory builds Kubernetes clients.
type ClientFactory interface {
// BuildClientFromKubeconfig builds a Kubernetes client from a kubeconfig file.
BuildClientFromKubeconfig(kubeconfigPath string) (kubernetes.Client, error)
}

type Bootstrapper interface {
CreateBootstrapCluster(ctx context.Context, clusterSpec *cluster.Spec, opts ...bootstrapper.BootstrapClusterOption) (*types.Cluster, error)
DeleteBootstrapCluster(context.Context, *types.Cluster, constants.Operation, bool) error
Expand Down
Loading

0 comments on commit 1400a46

Please sign in to comment.