Skip to content

Commit

Permalink
chore: improve tests runner
Browse files Browse the repository at this point in the history
* run subtests in parallel;
* enabled race detector;
* enable codecov.io integration.

Signed-off-by: Alexey Palazhchenko <[email protected]>
  • Loading branch information
AlekSi committed Sep 2, 2021
1 parent c6ce363 commit d6258cf
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 151 deletions.
15 changes: 15 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
codecov:
require_ci_to_pass: false

coverage:
status:
project:
default:
target: 15%
threshold: 0.5%
base: auto
if_ci_failed: success
patch: off

comment: false
19 changes: 18 additions & 1 deletion .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ steps:
pull: always
environment:
PLATFORM: linux/amd64,linux/arm64
INTEGRATION_SKIP_CLEANUP: 1 # make things a bit faster
commands:
- make env-up
- make test
Expand All @@ -65,6 +66,22 @@ steps:
- name: docker
path: /root/.docker/buildx

- name: coverage
image: autonomy/build-container:latest
pull: always
commands:
- make coverage
environment:
CODECOV_TOKEN:
from_secret: CODECOV_TOKEN
volumes:
- name: docker-socket
path: /var/run
- name: outerdockersock
path: /var/outer-run
- name: docker
path: /root/.docker/buildx

- name: build-and-publish
image: autonomy/build-container:latest
pull: always
Expand Down Expand Up @@ -158,6 +175,6 @@ depends_on:

---
kind: signature
hmac: 0e8710aa7032501fcad228e01d94233a8bbe70b432c232887c4ab9d4f55a707a
hmac: 85ad07759e326ade61ea3aceae13b64264408214343dd5c99b4c0b28a8d21641

...
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ bin
_out/

talosctl
talosconfig
kubeconfig
coverage.txt
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ FROM scratch AS generate
COPY --from=generate-build /src/api /api

FROM build AS integration-test-build
RUN --mount=type=cache,target=/.cache go test -v -c ./internal/integration
ENV CGO_ENABLED 1
ARG GO_LDFLAGS="-linkmode=external -extldflags '-static'"
RUN --mount=type=cache,target=/.cache go test -race -ldflags "${GO_LDFLAGS}" -coverpkg=./... -v -c ./internal/integration

FROM scratch AS integration-test
COPY --from=integration-test-build /src/integration.test /integration.test
Expand Down
21 changes: 14 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,31 @@ clean:
@rm -rf $(ARTIFACTS)

# Make `make test` behave just like `go test` regarding relative paths.
test:
test: ## Run tests.
@$(MAKE) local-integration-test DEST=./internal/integration PLATFORM=linux/amd64
cd internal/integration && KUBECONFIG=../../kubeconfig ./integration.test -test.v
cd internal/integration && KUBECONFIG=../../kubeconfig ./integration.test -test.v -test.coverprofile=../../coverage.txt

coverage: ## Upload coverage data to codecov.io.
bash -c "bash <(curl -s https://codecov.io/bash) -f coverage.txt -X fix"

talosctl:
curl -Lo talosctl https://github.com/talos-systems/talos/releases/download/$(TALOS_VERSION)/talosctl-$(shell uname -s | tr "[:upper:]" "[:lower:]")-amd64
chmod +x ./talosctl

env-up: talosctl
env-up: talosctl ## Start development environment.
./talosctl cluster create \
--talosconfig=talosconfig \
--name=cabpt-env \
--kubernetes-version=$(K8S_VERSION) \
--mtu=1450 \
--memory=2048 \
--cpus=2.0 \
--crashdump
./talosctl -n 10.5.0.2 kubeconfig -f kubeconfig
./talosctl kubeconfig kubeconfig \
--talosconfig=talosconfig \
--nodes=10.5.0.2 \
--force

env-down: talosctl
env-down: talosctl ## Stop development environment.
./talosctl cluster destroy \
--talosconfig=talosconfig \
--name=cabpt-env
rm -f talosconfig kubeconfig
142 changes: 74 additions & 68 deletions internal/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,82 +30,88 @@ import (
// +kubebuilder:scaffold:imports
)

const (
nsName = "test"
)

func TestIntegration(t *testing.T) {
ctx, c := setup(t, true, nsName)

cluster := &capiv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Namespace: nsName,
Name: "my-cluster",
},
Spec: capiv1.ClusterSpec{
ClusterNetwork: &capiv1.ClusterNetwork{
Pods: &capiv1.NetworkRanges{
CIDRBlocks: []string{"192.168.0.0/16"},
ctx, c := setupSuite(t)

// namespaced objects
var (
clusterName = "test-cluster"
machineName = "test-machine"
dataSecretName = "test-secret"
talosConfigName = "test-config"
)

t.Run("Basic", func(t *testing.T) {
t.Parallel()
namespaceName := setupTest(ctx, t, c)

cluster := &capiv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespaceName,
Name: clusterName,
},
Spec: capiv1.ClusterSpec{
ClusterNetwork: &capiv1.ClusterNetwork{
Pods: &capiv1.NetworkRanges{
CIDRBlocks: []string{"192.168.0.0/16"},
},
ServiceDomain: "cluster.local",
Services: &capiv1.NetworkRanges{
CIDRBlocks: []string{"10.128.0.0/12"},
},
},
ServiceDomain: "cluster.local",
Services: &capiv1.NetworkRanges{
CIDRBlocks: []string{"10.128.0.0/12"},
},
}
require.NoError(t, c.Create(ctx, cluster), "can't create a cluster")

cluster.Status.InfrastructureReady = true
require.NoError(t, c.Status().Update(ctx, cluster))

machine := &capiv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespaceName,
Name: machineName,
},
Spec: capiv1.MachineSpec{
ClusterName: cluster.Name,
Bootstrap: capiv1.Bootstrap{
DataSecretName: &dataSecretName,
},
},
},
}
require.NoError(t, c.Create(ctx, cluster))

cluster.Status.InfrastructureReady = true
require.NoError(t, c.Status().Update(ctx, cluster))

machineName := "my-cluster-machine"
dataSecretName := machineName + "-secret"

machine := &capiv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Namespace: nsName,
Name: machineName,
},
Spec: capiv1.MachineSpec{
ClusterName: cluster.Name,
Bootstrap: capiv1.Bootstrap{
DataSecretName: &dataSecretName,
}

require.NoError(t, controllerutil.SetOwnerReference(cluster, machine, scheme.Scheme))
require.NoError(t, c.Create(ctx, machine))

config := &bootstrapv1alpha3.TalosConfig{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespaceName,
Name: talosConfigName,
},
Spec: bootstrapv1alpha3.TalosConfigSpec{
GenerateType: "init",
},
},
}

require.NoError(t, controllerutil.SetOwnerReference(cluster, machine, scheme.Scheme))
require.NoError(t, c.Create(ctx, machine))

config := &bootstrapv1alpha3.TalosConfig{
ObjectMeta: metav1.ObjectMeta{
Namespace: nsName,
Name: "test",
},
Spec: bootstrapv1alpha3.TalosConfigSpec{
GenerateType: "init",
},
}
require.NoError(t, controllerutil.SetOwnerReference(machine, config, scheme.Scheme))

err := c.Create(ctx, config)
require.NoError(t, err)

for ctx.Err() == nil {
key := types.NamespacedName{
Namespace: nsName,
Name: "test",
}
require.NoError(t, controllerutil.SetOwnerReference(machine, config, scheme.Scheme))

err = c.Get(ctx, key, config)
err := c.Create(ctx, config)
require.NoError(t, err)

if config.Status.Ready {
break
}
for ctx.Err() == nil {
key := types.NamespacedName{
Namespace: namespaceName,
Name: talosConfigName,
}

err = c.Get(ctx, key, config)
require.NoError(t, err)

t.Logf("Config: %+v", config)
time.Sleep(5 * time.Second)
}
if config.Status.Ready {
break
}

t.Logf("Config: %+v", config)
time.Sleep(5 * time.Second)
}
})
}
Loading

0 comments on commit d6258cf

Please sign in to comment.