Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ci): fix acceptance tests #2158

Merged
merged 8 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/config/acceptance_tests_kind_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
extraMounts:
- hostPath: "./.github/config/seccomp-profiles"
containerPath: "/var/lib/kubelet/seccomp/profiles"
3 changes: 3 additions & 0 deletions .github/config/seccomp-profiles/audit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"defaultAction": "SCMP_ACT_LOG"
}
1 change: 1 addition & 0 deletions .github/workflows/acceptance_tests_kind.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
with:
wait: 2m
version: v${{ github.event.inputs.kindVersion }}
config: .github/config/acceptance_tests_kind_config.yaml
- name: Run Acceptance Test Suite
env:
KUBE_CONFIG_PATH: ${{ env.KUBECONFIG }}
Expand Down
26 changes: 26 additions & 0 deletions kubernetes/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -317,6 +318,16 @@ func skipIfNotRunningInMinikube(t *testing.T) {
}
}

func skipIfNotRunningInKind(t *testing.T) {
isRunningInKind, err := isRunningInKind()
if err != nil {
t.Fatal(err)
}
if !isRunningInKind {
t.Skip("The Kubernetes endpoint must come from Kind for this test to run - skipping")
}
}

func skipIfRunningInMinikube(t *testing.T) {
isInMinikube, err := isRunningInMinikube()
if err != nil {
Expand Down Expand Up @@ -344,6 +355,21 @@ func isRunningInMinikube() (bool, error) {
return false, nil
}

func isRunningInKind() (bool, error) {
node, err := getFirstNode()
if err != nil {
return false, err
}
u, err := url.Parse(node.Spec.ProviderID)
if err != nil {
return false, err
}
if u.Scheme == "kind" {
return true, nil
}
return false, nil
}

func isRunningInGke() (bool, error) {
node, err := getFirstNode()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion kubernetes/resource_kubernetes_cron_job_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestAccKubernetesCronJobV1_basic(t *testing.T) {
resource.TestCheckResourceAttr("kubernetes_cron_job_v1.test", "spec.0.successful_jobs_history_limit", "3"),
resource.TestCheckResourceAttr("kubernetes_cron_job_v1.test", "spec.0.suspend", "false"),
resource.TestCheckResourceAttr("kubernetes_cron_job_v1.test", "spec.0.job_template.0.spec.0.parallelism", "2"),
resource.TestCheckResourceAttr("kubernetes_cron_job_v1.test", "spec.0.job_template.0.spec.0.backoff_limit", "0"),
resource.TestCheckResourceAttr("kubernetes_cron_job_v1.test", "spec.0.job_template.0.spec.0.backoff_limit", "6"),
resource.TestCheckResourceAttr("kubernetes_cron_job_v1.test", "spec.0.job_template.0.spec.0.template.0.spec.0.container.0.name", "hello"),
resource.TestCheckResourceAttr("kubernetes_cron_job_v1.test", "spec.0.job_template.0.spec.0.template.0.metadata.#", "1"),
resource.TestCheckResourceAttr("kubernetes_cron_job_v1.test", "spec.0.job_template.0.spec.0.template.0.metadata.0.labels.%", "1"),
Expand Down
25 changes: 21 additions & 4 deletions kubernetes/resource_kubernetes_daemonset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,31 @@ func TestAccKubernetesDaemonSet_with_container_security_context_seccomp_profile(
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.security_context.0.seccomp_profile.0.type", "RuntimeDefault"),
),
},
},
})
}

func TestAccKubernetesDaemonSet_with_container_security_context_seccomp_localhost_profile(t *testing.T) {
var conf appsv1.DaemonSet
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
imageName := nginxImageVersion
resourceName := "kubernetes_daemonset.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); skipIfNotRunningInKind(t); skipIfClusterVersionLessThan(t, "1.19.0") },
IDRefreshName: "kubernetes_daemonset.test",
IDRefreshIgnore: []string{"metadata.0.resource_version"},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckKubernetesDaemonSetDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesDaemonSetConfigWithContainerSecurityContextSeccompProfileLocalhost(name, imageName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDaemonSetExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.security_context.0.seccomp_profile.0.type", "Localhost"),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.security_context.0.seccomp_profile.0.localhost_profile", ""),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.security_context.0.seccomp_profile.0.localhost_profile", "profiles/audit.json"),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.security_context.0.seccomp_profile.0.type", "Localhost"),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.security_context.0.seccomp_profile.0.localhost_profile", ""),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.security_context.0.seccomp_profile.0.localhost_profile", "profiles/audit.json"),
),
},
},
Expand Down Expand Up @@ -870,7 +887,7 @@ func testAccKubernetesDaemonSetConfigWithContainerSecurityContextSeccompProfileL
security_context {
seccomp_profile {
type = "Localhost"
localhost_profile = ""
localhost_profile = "profiles/audit.json"
}
}
container {
Expand All @@ -880,7 +897,7 @@ func testAccKubernetesDaemonSetConfigWithContainerSecurityContextSeccompProfileL
security_context {
seccomp_profile {
type = "Localhost"
localhost_profile = ""
localhost_profile = "profiles/audit.json"
}
}
}
Expand Down
26 changes: 21 additions & 5 deletions kubernetes/resource_kubernetes_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ func TestAccKubernetesDeployment_with_container_security_context_seccomp_profile
resourceName := "kubernetes_deployment.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() { testAccPreCheck(t); skipIfClusterVersionLessThan(t, "1.19.0") },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckKubernetesDeploymentDestroy,
Steps: []resource.TestStep{
Expand All @@ -574,14 +574,30 @@ func TestAccKubernetesDeployment_with_container_security_context_seccomp_profile
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.security_context.0.seccomp_profile.0.type", "RuntimeDefault"),
),
},
},
})
}

func TestAccKubernetesDeployment_with_container_security_context_seccomp_localhost_profile(t *testing.T) {
var conf appsv1.Deployment

deploymentName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
imageName := nginxImageVersion
resourceName := "kubernetes_deployment.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); skipIfNotRunningInKind(t); skipIfClusterVersionLessThan(t, "1.19.0") },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckKubernetesDeploymentDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesDeploymentConfigWithContainerSecurityContextSeccompProfileLocalhost(deploymentName, imageName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDeploymentExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.security_context.0.seccomp_profile.0.type", "Localhost"),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.security_context.0.seccomp_profile.0.localhost_profile", ""),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.security_context.0.seccomp_profile.0.localhost_profile", "profiles/audit.json"),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.security_context.0.seccomp_profile.0.type", "Localhost"),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.security_context.0.seccomp_profile.0.localhost_profile", ""),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.security_context.0.seccomp_profile.0.localhost_profile", "profiles/audit.json"),
),
},
},
Expand Down Expand Up @@ -2149,7 +2165,7 @@ func testAccKubernetesDeploymentConfigWithContainerSecurityContextSeccompProfile
security_context {
seccomp_profile {
type = "Localhost"
localhost_profile = ""
localhost_profile = "profiles/audit.json"
}
}
container {
Expand All @@ -2159,7 +2175,7 @@ func testAccKubernetesDeploymentConfigWithContainerSecurityContextSeccompProfile
security_context {
seccomp_profile {
type = "Localhost"
localhost_profile = ""
localhost_profile = "profiles/audit.json"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion kubernetes/resource_kubernetes_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestAccKubernetesJob_basic(t *testing.T) {
resource.TestCheckResourceAttr("kubernetes_job.test", "metadata.0.labels.foo", "bar"),
resource.TestCheckResourceAttr("kubernetes_job.test", "spec.#", "1"),
resource.TestCheckResourceAttr("kubernetes_job.test", "spec.0.active_deadline_seconds", "0"),
resource.TestCheckResourceAttr("kubernetes_job.test", "spec.0.backoff_limit", "0"),
resource.TestCheckResourceAttr("kubernetes_job.test", "spec.0.backoff_limit", "6"),
resource.TestCheckResourceAttr("kubernetes_job.test", "spec.0.completions", "1"),
resource.TestCheckResourceAttr("kubernetes_job.test", "spec.0.parallelism", "1"),
resource.TestCheckResourceAttr("kubernetes_job.test", "spec.0.manual_selector", "true"),
Expand Down
30 changes: 26 additions & 4 deletions kubernetes/resource_kubernetes_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,14 +454,36 @@ func TestAccKubernetesPod_with_pod_security_context_seccomp_profile(t *testing.T
resource.TestCheckResourceAttr(resourceName, "spec.0.container.0.security_context.0.seccomp_profile.0.type", "RuntimeDefault"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"metadata.0.resource_version"},
},
},
})
}

func TestAccKubernetesPod_with_pod_security_context_seccomp_localhost_profile(t *testing.T) {
var conf api.Pod

podName := acctest.RandomWithPrefix("tf-acc-test")
imageName := nginxImageVersion
resourceName := "kubernetes_pod.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); skipIfNotRunningInKind(t); skipIfClusterVersionLessThan(t, "1.19.0") },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckKubernetesPodDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesPodConfigWithSecurityContextSeccompProfileLocalhost(podName, imageName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesPodExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "spec.0.security_context.0.seccomp_profile.0.type", "Localhost"),
resource.TestCheckResourceAttr(resourceName, "spec.0.security_context.0.seccomp_profile.0.localhost_profile", ""),
resource.TestCheckResourceAttr(resourceName, "spec.0.security_context.0.seccomp_profile.0.localhost_profile", "profiles/audit.json"),
resource.TestCheckResourceAttr(resourceName, "spec.0.container.0.security_context.0.seccomp_profile.0.type", "Localhost"),
resource.TestCheckResourceAttr(resourceName, "spec.0.container.0.security_context.0.seccomp_profile.0.localhost_profile", ""),
resource.TestCheckResourceAttr(resourceName, "spec.0.container.0.security_context.0.seccomp_profile.0.localhost_profile", "profiles/audit.json"),
),
},
{
Expand Down Expand Up @@ -1875,7 +1897,7 @@ resource "kubernetes_pod" "test" {
security_context {
seccomp_profile {
type = "Localhost"
localhost_profile = ""
localhost_profile = "profiles/audit.json"
}
}

Expand All @@ -1885,7 +1907,7 @@ resource "kubernetes_pod" "test" {
security_context {
seccomp_profile {
type = "Localhost"
localhost_profile = ""
localhost_profile = "profiles/audit.json"
}
}
}
Expand Down