Skip to content

Commit

Permalink
fix: refactor integration tests to work
Browse files Browse the repository at this point in the history
  • Loading branch information
nandor-magyar committed Oct 5, 2023
1 parent 43655e1 commit f06c50e
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 65 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/product_builder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,11 @@ jobs:
- name: Load go mod
run: go mod tidy
- name: Init k3d
run: make k3d-init-ext
run: make k3d-init
- name: Run integration tests
run: |
export KUBECONFIG='$(PWD)/k3d-auth.yaml'
make k3d-config && \
export KUBECONFIG="$(pwd)/k3d-auth.yaml" && \
make test-integration
- name: Upload integration test results
uses: actions/upload-artifact@v3
Expand Down
4 changes: 4 additions & 0 deletions golang/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ test-internal:
test-dagent:
go test -tags=integration -race -coverpkg=./... -coverprofile=./dagent.cov -covermode=atomic ./pkg/dagent/...

.PHONY: test-cli
test-cli:
go test -tags=integration -race -coverpkg=./... -coverprofile=./cli.cov -covermode=atomic ./pkg/cli/...

.PHONY: coverage
coverage:
go tool cover -func ./merged.cov
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func assertPortBinding(t *testing.T, portMap nat.PortMap, internal, external str
}

list := portMap[lookup]
assert.Contains(t, list, nat.PortBinding{HostIP: "0.0.0.0", HostPort: external})
assert.Contains(t, list, nat.PortBinding{HostIP: "", HostPort: external})
}

func hookCallback(callback func()) containerbuilder.LifecycleFunc {
Expand Down
10 changes: 6 additions & 4 deletions golang/pkg/crane/k8s/deploy_facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (d *DeployFacade) Deploy() error {
}
}

if err := d.deployment.DeployDeployment(&deploymentParams{
if err := d.deployment.DeployDeployment(&DeploymentParams{
image: d.params.Image,
namespace: d.params.InstanceConfig.ContainerPreName,
containerConfig: &d.params.ContainerConfig,
Expand Down Expand Up @@ -270,9 +270,11 @@ func Deploy(c context.Context, dog *dogger.DeploymentLogger, deployImageRequest
dog.Write(deployImageRequest.InstanceConfig.Strings()...)
dog.Write(deployImageRequest.ContainerConfig.Strings(&cfg.CommonConfiguration)...)

imageName := util.JoinV("/",
*deployImageRequest.Registry,
util.JoinV(":", deployImageRequest.ImageName, deployImageRequest.Tag))
imageName := util.JoinV(":", deployImageRequest.ImageName, deployImageRequest.Tag)
if deployImageRequest.Registry != nil {
imageName = util.JoinV("/",
*deployImageRequest.Registry, imageName)
}

expandedImageName, err := imageHelper.ExpandImageName(imageName)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions golang/pkg/crane/k8s/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewDeployment(ctx context.Context, cfg *config.Configuration) *Deployment {
return &Deployment{status: "", ctx: ctx, appConfig: cfg}
}

type deploymentParams struct {
type DeploymentParams struct {
namespace string
image string
containerConfig *v1.ContainerConfig
Expand All @@ -63,7 +63,7 @@ type deploymentParams struct {
issuer string
}

func (d *Deployment) DeployDeployment(p *deploymentParams) error {
func (d *Deployment) DeployDeployment(p *DeploymentParams) error {
client := getDeploymentsClient(p.namespace, d.appConfig)

containerConfig, err := buildContainer(p, d.appConfig)
Expand Down Expand Up @@ -277,7 +277,7 @@ func (d *Deployment) GetPodDeployment(namespace, name string) (*kappsv1.Deployme
}

// builds the container using the builder interface, with healthchecks, volumes, configs, ports...
func buildContainer(p *deploymentParams,
func buildContainer(p *DeploymentParams,
cfg *config.Configuration,
) (*corev1.ContainerApplyConfiguration, error) {
healthCheckConfig := p.containerConfig.HealthCheckConfig
Expand Down Expand Up @@ -419,7 +419,7 @@ func getResourceManagement(resourceConfig v1.ResourceConfig,
}

// getInitContainers returns every init container specific(import/config) or custom ones
func getInitContainers(params *deploymentParams, cfg *config.Configuration) []*corev1.ContainerApplyConfiguration {
func getInitContainers(params *DeploymentParams, cfg *config.Configuration) []*corev1.ContainerApplyConfiguration {
// this is only the config container / could be general / wait for it / other init purposes
initContainers := []*corev1.ContainerApplyConfiguration{}

Expand Down Expand Up @@ -485,7 +485,7 @@ func addImportContainer(initContainers []*corev1.ContainerApplyConfiguration,
}

func addInitContainers(initContainers []*corev1.ContainerApplyConfiguration,
params *deploymentParams,
params *DeploymentParams,
) []*corev1.ContainerApplyConfiguration {
for _, iCont := range params.containerConfig.InitContainers {
container := corev1.Container().
Expand Down
129 changes: 76 additions & 53 deletions golang/pkg/crane/k8s/deployment_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,76 @@
//go:build integration

package k8s_test

import (
"context"
"testing"

"github.com/ilyakaznacheev/cleanenv"

"github.com/stretchr/testify/assert"

"github.com/dyrector-io/dyrectorio/golang/pkg/crane/config"
"github.com/dyrector-io/dyrectorio/golang/pkg/crane/k8s"
)

func TestGetPods(t *testing.T) {
ctx := context.Background()

cfg := config.Configuration{}
_ = cleanenv.ReadEnv(&cfg)

deploymentHandler := k8s.NewDeployment(ctx, &cfg)

deployments, err := deploymentHandler.GetDeployments(ctx, "default", &cfg)
assert.NoError(t, err)
assert.Equal(t, 2, len(deployments.Items))

pods, err := deploymentHandler.GetPods("default", "deployment-1")
assert.NoError(t, err)
assert.Equal(t, 1, len(pods))
}

func TestGetPod(t *testing.T) {
ctx := context.Background()

cfg := config.Configuration{}
_ = cleanenv.ReadEnv(&cfg)

deploymentHandler := k8s.NewDeployment(ctx, &cfg)

deployments, err := deploymentHandler.GetDeployments(ctx, "default", &cfg)
assert.NoError(t, err)
assert.Equal(t, 2, len(deployments.Items))

pods, err := deploymentHandler.GetPods("default", "deployment-1")
assert.NoError(t, err)
assert.Equal(t, 1, len(pods))

pod, err := deploymentHandler.GetPod("default", pods[0].Name)
assert.NoError(t, err)
assert.NotNil(t, pod)
}
//go:build integration

package k8s

import (
"context"
"testing"
"time"

"github.com/AlekSi/pointer"
"github.com/ilyakaznacheev/cleanenv"

"github.com/stretchr/testify/assert"

v1 "github.com/dyrector-io/dyrectorio/golang/api/v1"
"github.com/dyrector-io/dyrectorio/golang/internal/dogger"
"github.com/dyrector-io/dyrectorio/golang/internal/grpc"
"github.com/dyrector-io/dyrectorio/golang/pkg/crane/config"
)

func TestFetchPods(t *testing.T) {
ctx := context.Background()

cfg := config.Configuration{}
err := cleanenv.ReadEnv(&cfg)

ctx = grpc.WithGRPCConfig(ctx, &cfg)
assert.Nil(t, err, "error for test deployment is unexpected")
deploymentHandler := NewDeployment(ctx, &cfg)

err = Deploy(ctx,
dogger.NewDeploymentLogger(
ctx,
pointer.ToString("test"), nil,
&cfg.CommonConfiguration),
&v1.DeployImageRequest{
RequestID: "test",
ImageName: "nginx:latest",
InstanceConfig: v1.InstanceConfig{
ContainerPreName: "pods",
}, ContainerConfig: v1.ContainerConfig{
Container: "deployment-1",
},
},
&v1.VersionData{})

WaitForRunningDeployment(ctx, "pods", "deployment-1", 1, 30*time.Second, &cfg)

assert.Nil(t, err, "error for test deployment is unexpected")
t.Run("Test get pods", func(t *testing.T) {
deployments, err := deploymentHandler.GetDeployments(ctx, "pods", &cfg)
assert.NoError(t, err)
assert.Len(t, deployments.Items, 1)

pods, err := deploymentHandler.GetPods("pods", "deployment-1")
assert.NoError(t, err)
assert.Len(t, pods, 1)
})

t.Run("Test get single pod", func(t *testing.T) {
deployments, err := deploymentHandler.GetDeployments(ctx, "pods", &cfg)
assert.NoError(t, err)
assert.Equal(t, 1, len(deployments.Items))

pods, err := deploymentHandler.GetPods("pods", "deployment-1")
assert.NoError(t, err)
assert.Equal(t, 1, len(pods))

pod, err := deploymentHandler.GetPod("pods", pods[0].Name)
assert.NoError(t, err)
assert.NotNil(t, pod)
})

err = deploymentHandler.deleteDeployment("pods", "deployment-1")
assert.Nil(t, err, "error for deleting test deployment is unexpected")
}

0 comments on commit f06c50e

Please sign in to comment.