Skip to content

Commit

Permalink
WiP: gcp/tests
Browse files Browse the repository at this point in the history
Signed-off-by: Beraldo Leal <bleal@redhat.com>
  • Loading branch information
beraldoleal committed Jul 3, 2024
1 parent b9b9242 commit 82e9de3
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 20 deletions.
13 changes: 13 additions & 0 deletions src/cloud-api-adaptor/pkg/userdata/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/confidential-containers/cloud-api-adaptor/src/cloud-providers/aws"
"github.com/confidential-containers/cloud-api-adaptor/src/cloud-providers/azure"
"github.com/confidential-containers/cloud-api-adaptor/src/cloud-providers/docker"
"github.com/confidential-containers/cloud-api-adaptor/src/cloud-providers/gcp"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -71,6 +72,14 @@ func (a AWSUserDataProvider) GetUserData(ctx context.Context) ([]byte, error) {
return aws.GetUserData(ctx, url)
}

type GCPUserDataProvider struct{ DefaultRetry }

func (g GCPUserDataProvider) GetUserData(ctx context.Context) ([]byte, error) {
url := gcp.GcpUserDataImdsUrl
logger.Printf("provider: GCP, userDataUrl: %s\n", url)
return aws.GetUserData(ctx, url)
}

type DockerUserDataProvider struct{ DefaultRetry }

func (a DockerUserDataProvider) GetUserData(ctx context.Context) ([]byte, error) {
Expand All @@ -94,6 +103,10 @@ func newProvider(ctx context.Context) (UserDataProvider, error) {
return AWSUserDataProvider{}, nil
}

if gcp.IsGCP(ctx) {
return GCPUserDataProvider{}, nil
}

return nil, fmt.Errorf("unsupported user data provider")
}

Expand Down
71 changes: 71 additions & 0 deletions src/cloud-api-adaptor/test/e2e/gcp_common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// (C) Copyright Confidential Containers Contributors
// SPDX-License-Identifier: Apache-2.0

package e2e

import (
"testing"
"time"

pv "github.com/confidential-containers/cloud-api-adaptor/src/cloud-api-adaptor/test/provisioner/gcp"
)

// GCPAssert implements the CloudAssert interface.
type GCPAssert struct {
Vpc *pv.GCPVPC
}

func NewGCPAssert() GCPAssert {
return GCPAssert{
Vpc: pv.GCPProps.GcpVPC,
}
}

func (aa GCPAssert) DefaultTimeout() time.Duration {
return 1 * time.Minute
}

func (aa GCPAssert) HasPodVM(t *testing.T, id string) {
// // The `id` parameter is not the instance ID but rather the pod's name, so
// // it will need to scan all running pods on the subnet to find one that
// // starts with the prefix.
// podvmPrefix := "podvm-" + id

// describeInstances, err := aa.Vpc.Client.DescribeInstances(context.TODO(),
// &ec2.DescribeInstancesInput{
// Filters: []ec2types.Filter{
// {
// Name: aws.String("subnet-id"),
// Values: []string{aa.Vpc.SubnetId},
// },
// },
// })
// if err != nil {
// t.Errorf("Podvm name=%s not found: %v", id, err)
// }

// found := false
// for _, reservation := range describeInstances.Reservations {
// for _, instance := range reservation.Instances {
// // Code == 48 (terminated)
// // Some podvm from previous tests might be on terminated stage
// // so let's ignore them.
// if instance.State.Code != aws.Int32(48) {
// for _, tag := range instance.Tags {
// if *tag.Key == "Name" &&
// strings.HasPrefix(*tag.Value, podvmPrefix) {
// found = true
// }
// }
// }
// }
// }

// if !found {
// t.Errorf("Podvm name=%s not found", id)
// }
}

func (aa GCPAssert) GetInstanceType(t *testing.T, podName string) (string, error) {
return "", nil
}
105 changes: 105 additions & 0 deletions src/cloud-api-adaptor/test/e2e/gcp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//go:build gcp

// (C) Copyright Confidential Containers Contributors
// SPDX-License-Identifier: Apache-2.0

package e2e

import (
"testing"
// pv "github.com/confidential-containers/cloud-api-adaptor/src/cloud-api-adaptor/test/provisioner/gcp"
)

func TestGCPCreateSimplePod(t *testing.T) {
assert := GCPAssert{}
DoTestCreateSimplePod(t, testEnv, assert)
}

func TestGCPCreatePodWithConfigMap(t *testing.T) {
t.Skip("Test not passing")
assert := NewGCPAssert()

DoTestCreatePodWithConfigMap(t, testEnv, assert)
}

func TestGCPCreatePodWithSecret(t *testing.T) {
t.Skip("Test not passing")
assert := NewGCPAssert()

DoTestCreatePodWithSecret(t, testEnv, assert)
}

// func TestAwsCreatePeerPodContainerWithExternalIPAccess(t *testing.T) {
// t.Skip("Test not passing")
// assert := NewAWSAssert()
//
// DoTestCreatePeerPodContainerWithExternalIPAccess(t, testEnv, assert)
// }
//
// func TestAwsCreatePeerPodWithJob(t *testing.T) {
// assert := NewAWSAssert()
//
// DoTestCreatePeerPodWithJob(t, testEnv, assert)
// }
//
// func TestAwsCreatePeerPodAndCheckUserLogs(t *testing.T) {
// assert := NewAWSAssert()
//
// DoTestCreatePeerPodAndCheckUserLogs(t, testEnv, assert)
// }
//
// func TestAwsCreatePeerPodAndCheckWorkDirLogs(t *testing.T) {
// assert := NewAWSAssert()
//
// DoTestCreatePeerPodAndCheckWorkDirLogs(t, testEnv, assert)
// }
//
// func TestAwsCreatePeerPodAndCheckEnvVariableLogsWithImageOnly(t *testing.T) {
// assert := NewAWSAssert()
//
// DoTestCreatePeerPodAndCheckEnvVariableLogsWithImageOnly(t, testEnv, assert)
// }
//
// func TestAwsCreatePeerPodAndCheckEnvVariableLogsWithDeploymentOnly(t *testing.T) {
// assert := NewAWSAssert()
//
// DoTestCreatePeerPodAndCheckEnvVariableLogsWithDeploymentOnly(t, testEnv, assert)
// }
//
// func TestAwsCreatePeerPodAndCheckEnvVariableLogsWithImageAndDeployment(t *testing.T) {
// assert := NewAWSAssert()
//
// DoTestCreatePeerPodAndCheckEnvVariableLogsWithImageAndDeployment(t, testEnv, assert)
// }
//
// func TestAwsCreatePeerPodWithLargeImage(t *testing.T) {
// assert := NewAWSAssert()
//
// DoTestCreatePeerPodWithLargeImage(t, testEnv, assert)
// }
//
// func TestAwsCreatePeerPodWithPVC(t *testing.T) {
// t.Skip("To be implemented")
// }
//
// func TestAwsCreatePeerPodWithAuthenticatedImagewithValidCredentials(t *testing.T) {
// t.Skip("To be implemented")
// }
//
// func TestAwsCreatePeerPodWithAuthenticatedImageWithInvalidCredentials(t *testing.T) {
// t.Skip("To be implemented")
// }
//
// func TestAwsCreatePeerPodWithAuthenticatedImageWithoutCredentials(t *testing.T) {
// t.Skip("To be implemented")
// }
//
// func TestAwsDeletePod(t *testing.T) {
// assert := NewAWSAssert()
// DoTestDeleteSimplePod(t, testEnv, assert)
// }
//
// func TestAwsCreateNginxDeployment(t *testing.T) {
// assert := NewAWSAssert()
// DoTestNginxDeployment(t, testEnv, assert)
// }
1 change: 1 addition & 0 deletions src/cloud-api-adaptor/test/provisioner/gcp/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (g *GKECluster) CreateCluster(ctx context.Context) error {
InitialNodeCount: g.nodeCount,
NodeConfig: &container.NodeConfig{
MachineType: g.machineType,
ImageType: "UBUNTU_CONTAINERD", // Default CO OS has a ro fs.
},
}

Expand Down
19 changes: 9 additions & 10 deletions src/cloud-api-adaptor/test/provisioner/gcp/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

type GCPInstallOverlay struct {
Overlay *pv.KustomizeOverlay
CaaImage string
}

func NewGCPInstallOverlay(installDir, provider string) (pv.InstallOverlay, error) {
Expand All @@ -38,6 +37,15 @@ func (a *GCPInstallOverlay) Delete(ctx context.Context, cfg *envconf.Config) err
func (a *GCPInstallOverlay) Edit(ctx context.Context, cfg *envconf.Config, properties map[string]string) error {
var err error

image := properties["caa_image_name"]
log.Infof("Updating caa image with %s", image)
if image != "" {
err = a.Overlay.SetKustomizeImage("cloud-api-adaptor", "newName", image)
if err != nil {
return err
}
}

// Mapping the internal properties to ConfigMapGenerator properties.
mapProps := map[string]string{
"pause_image": "PAUSE_IMAGE",
Expand All @@ -49,15 +57,6 @@ func (a *GCPInstallOverlay) Edit(ctx context.Context, cfg *envconf.Config, prope
"vxlan_port": "VXLAN_PORT",
}

if value, ok := properties["caa_image_name"]; ok {
if value != "" {
log.Infof("Updating caa image with %s", value)
if err = a.Overlay.SetKustomizeImage("cloud-api-adaptor", "newImage", value); err != nil {
return err
}
}
}

for k, v := range mapProps {
if properties[k] != "" {
if err = a.Overlay.SetKustomizeConfigMapGeneratorLiteral("peer-pods-cm",
Expand Down
19 changes: 10 additions & 9 deletions src/cloud-api-adaptor/test/provisioner/gcp/provision_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"google.golang.org/api/option"

pv "github.com/confidential-containers/cloud-api-adaptor/src/cloud-api-adaptor/test/provisioner"
log "github.com/sirupsen/logrus"
// log "github.com/sirupsen/logrus"
"google.golang.org/api/compute/v1"
"sigs.k8s.io/e2e-framework/pkg/envconf"
)
Expand All @@ -18,9 +18,10 @@ var GCPProps = &GCPProvisioner{}

// GCPProvisioner implements the CloudProvisioner interface.
type GCPProvisioner struct {
GkeCluster *GKECluster
GcpVPC *GCPVPC
PodvmImage *GCPImage
GkeCluster *GKECluster
GcpVPC *GCPVPC
PodvmImage *GCPImage
CaaImageName string
}

// NewGCPProvisioner creates a new GCPProvisioner with the given properties.
Expand Down Expand Up @@ -49,12 +50,11 @@ func NewGCPProvisioner(properties map[string]string) (pv.CloudProvisioner, error
return nil, err
}

log.Info(image.Name)

GCPProps = &GCPProvisioner{
GkeCluster: gkeCluster,
GcpVPC: gcpVPC,
PodvmImage: image,
GkeCluster: gkeCluster,
GcpVPC: gcpVPC,
PodvmImage: image,
CaaImageName: properties["caa_image_name"],
}
return GCPProps, nil
}
Expand Down Expand Up @@ -97,6 +97,7 @@ func (p *GCPProvisioner) GetProperties(ctx context.Context, cfg *envconf.Config)
"project_id": p.GkeCluster.projectID,
"zone": p.GkeCluster.zone,
"network": p.GcpVPC.vpcName,
"caa_image_name": p.CaaImageName,
}
}

Expand Down
19 changes: 18 additions & 1 deletion src/cloud-api-adaptor/test/provisioner/gcp/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type GCPVPC struct {
// NewGCPVPC creates a new GCPVPC object.
func NewGCPVPC(properties map[string]string) (*GCPVPC, error) {
defaults := map[string]string{
"vpc_name": "peer-pods-vpc",
"vpc_name": "default",
}

for key, value := range properties {
Expand Down Expand Up @@ -58,6 +58,12 @@ func (g *GCPVPC) CreateVPC(
return fmt.Errorf("GKE: compute.NewService: %v", err)
}

_, err = srv.Networks.Get(g.projectID, g.vpcName).Context(ctx).Do()
if err == nil {
log.Infof("GKE: Using existing VPC %s.\n", g.vpcName)
return nil
}

network := &compute.Network{
Name: g.vpcName,
AutoCreateSubnetworks: true,
Expand All @@ -74,6 +80,17 @@ func (g *GCPVPC) CreateVPC(
if err != nil {
return fmt.Errorf("GKE: Error waiting for VPC to be created: %v", err)
}

// subnetwork := &compute.Subnetwork{
// Name: "peer-pods-subnet",
// Network: op.SelfLink,
// Region: "us-west1",
// }
//
// _, err = srv.Subnetworks.Insert(g.projectID, "us-west1", subnetwork).Context(ctx).Do()
// if err != nil {
// return fmt.Errorf("GKE: Subnetworks.Insert: %v", err)
// }
return nil
}

Expand Down
Loading

0 comments on commit 82e9de3

Please sign in to comment.