-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert legacy docker tests from bash to golang (#11357)
* Convert the following Docker test from Bash to Go - basics - bootstraptoken - cacerts - compat -> skew - etcd - lazypull - upgrade Signed-off-by: Derek Nola <[email protected]> * Add Docker go tests to GHA * Prebuild K3s Go Tests * Strip go test binaries to reduce size * Handle complex branch options Signed-off-by: Derek Nola <[email protected]> * Implement basic golang tests on arm and arm64 pipelines Signed-off-by: Derek Nola <[email protected]>
- Loading branch information
Showing
13 changed files
with
1,430 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
tester "github.com/k3s-io/k3s/tests/docker" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var k3sImage = flag.String("k3sImage", "", "The k3s image used to provision containers") | ||
var config *tester.TestConfig | ||
|
||
func Test_DockerBasic(t *testing.T) { | ||
flag.Parse() | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Basic Docker Test Suite") | ||
} | ||
|
||
var _ = Describe("Basic Tests", Ordered, func() { | ||
|
||
Context("Setup Cluster", func() { | ||
It("should provision servers and agents", func() { | ||
var err error | ||
config, err = tester.NewTestConfig(*k3sImage) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(config.ProvisionServers(1)).To(Succeed()) | ||
Expect(config.ProvisionAgents(1)).To(Succeed()) | ||
Eventually(func() error { | ||
return tester.DeploymentsReady([]string{"coredns", "local-path-provisioner", "metrics-server", "traefik"}, config.KubeconfigFile) | ||
}, "60s", "5s").Should(Succeed()) | ||
Eventually(func() error { | ||
return tester.NodesReady(config.KubeconfigFile) | ||
}, "40s", "5s").Should(Succeed()) | ||
}) | ||
}) | ||
|
||
Context("Use Local Storage Volume", func() { | ||
It("should apply local storage volume", func() { | ||
const volumeTestManifest = "../resources/volume-test.yaml" | ||
|
||
// Apply the manifest | ||
cmd := fmt.Sprintf("kubectl apply -f %s --kubeconfig=%s", volumeTestManifest, config.KubeconfigFile) | ||
_, err := tester.RunCommand(cmd) | ||
Expect(err).NotTo(HaveOccurred(), "failed to apply volume test manifest") | ||
}) | ||
It("should validate local storage volume", func() { | ||
Eventually(func() (bool, error) { | ||
return tester.PodReady("volume-test", "kube-system", config.KubeconfigFile) | ||
}, "20s", "5s").Should(BeTrue()) | ||
}) | ||
}) | ||
|
||
Context("Verify Binaries and Images", func() { | ||
It("has valid bundled binaries", func() { | ||
for _, server := range config.Servers { | ||
Expect(tester.VerifyValidVersion(server.Name, "kubectl")).To(Succeed()) | ||
Expect(tester.VerifyValidVersion(server.Name, "ctr")).To(Succeed()) | ||
Expect(tester.VerifyValidVersion(server.Name, "crictl")).To(Succeed()) | ||
} | ||
}) | ||
It("has valid airgap images", func() { | ||
Expect(config).To(Not(BeNil())) | ||
err := VerifyAirgapImages(config) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
}) | ||
}) | ||
|
||
var failed bool | ||
var _ = AfterEach(func() { | ||
failed = failed || CurrentSpecReport().Failed() | ||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
if config != nil && !failed { | ||
config.Cleanup() | ||
} | ||
}) | ||
|
||
// VerifyAirgapImages checks for changes in the airgap image list | ||
func VerifyAirgapImages(config *tester.TestConfig) error { | ||
// This file is generated during the build packaging step | ||
const airgapImageList = "../../../scripts/airgap/image-list.txt" | ||
|
||
// Use a map to automatically handle duplicates | ||
imageSet := make(map[string]struct{}) | ||
|
||
// Collect all images from nodes | ||
for _, node := range config.GetNodeNames() { | ||
cmd := fmt.Sprintf("docker exec %s crictl images -o json | jq -r '.images[].repoTags[0] | select(. != null)'", node) | ||
output, err := tester.RunCommand(cmd) | ||
Expect(err).NotTo(HaveOccurred(), "failed to execute crictl and jq: %v", err) | ||
|
||
for _, line := range strings.Split(strings.TrimSpace(string(output)), "\n") { | ||
if line != "" { | ||
imageSet[line] = struct{}{} | ||
} | ||
} | ||
} | ||
|
||
// Convert map keys to slice | ||
uniqueImages := make([]string, 0, len(imageSet)) | ||
for image := range imageSet { | ||
uniqueImages = append(uniqueImages, image) | ||
} | ||
|
||
existing, err := os.ReadFile(airgapImageList) | ||
if err != nil && !os.IsNotExist(err) { | ||
return fmt.Errorf("failed to read airgap list file: %v", err) | ||
} | ||
|
||
// Sorting doesn't matter with ConsistOf | ||
existingImages := strings.Split(strings.TrimSpace(string(existing)), "\n") | ||
Expect(existingImages).To(ConsistOf(uniqueImages)) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"strings" | ||
"testing" | ||
|
||
tester "github.com/k3s-io/k3s/tests/docker" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var k3sImage = flag.String("k3sImage", "", "The k3s image used to provision containers") | ||
var config *tester.TestConfig | ||
|
||
func Test_DockerBootstrapToken(t *testing.T) { | ||
flag.Parse() | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "BoostrapToken Docker Test Suite") | ||
} | ||
|
||
var _ = Describe("Boostrap Token Tests", Ordered, func() { | ||
|
||
Context("Setup Cluster", func() { | ||
It("should provision servers", func() { | ||
var err error | ||
config, err = tester.NewTestConfig(*k3sImage) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(config.ProvisionServers(1)).To(Succeed()) | ||
Eventually(func() error { | ||
return tester.DeploymentsReady([]string{"coredns", "local-path-provisioner", "metrics-server", "traefik"}, config.KubeconfigFile) | ||
}, "60s", "5s").Should(Succeed()) | ||
}) | ||
}) | ||
|
||
Context("Add Agent with Bootstrap token", func() { | ||
var newSecret string | ||
It("creates a bootstrap token", func() { | ||
var err error | ||
newSecret, err = tester.RunCmdOnDocker(config.Servers[0].Name, "k3s token create --ttl=5m --description=Test") | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(newSecret).NotTo(BeEmpty()) | ||
}) | ||
It("joins the agent with the new tokens", func() { | ||
newSecret = strings.ReplaceAll(newSecret, "\n", "") | ||
config.Secret = newSecret | ||
Expect(config.ProvisionAgents(1)).To(Succeed()) | ||
Eventually(func(g Gomega) { | ||
nodes, err := tester.ParseNodes(config.KubeconfigFile) | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
g.Expect(nodes).To(HaveLen(2)) | ||
g.Expect(tester.NodesReady(config.KubeconfigFile)).To(Succeed()) | ||
}, "40s", "5s").Should(Succeed()) | ||
}) | ||
}) | ||
}) | ||
|
||
var failed bool | ||
var _ = AfterEach(func() { | ||
failed = failed || CurrentSpecReport().Failed() | ||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
if config != nil && !failed { | ||
config.Cleanup() | ||
} | ||
}) |
Oops, something went wrong.