Skip to content

Commit

Permalink
add test base file (sealerio#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
kakaZhou719 authored Jun 3, 2021
1 parent 414b256 commit 97758e1
Show file tree
Hide file tree
Showing 25 changed files with 344 additions and 53 deletions.
1 change: 1 addition & 0 deletions test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var _ = SynchronizedBeforeSuite(func() []byte {
output, err := exec.LookPath("sealer")
Expect(err).NotTo(HaveOccurred(), output)
SetDefaultEventuallyTimeout(settings.DefaultWaiteTime)
settings.DefaultSealerBin = output
return nil
}, func(data []byte) {
SetDefaultEventuallyTimeout(settings.DefaultWaiteTime)
Expand Down
4 changes: 2 additions & 2 deletions test/sealer_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package test

import (
/*import (
"fmt"
"github.com/alibaba/sealer/test/suites/apply"
Expand Down Expand Up @@ -48,4 +48,4 @@ var _ = Describe("sealer apply", func() {
})
})
})*/
4 changes: 2 additions & 2 deletions test/sealer_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package test

import (
/*import (
"fmt"
"os"
Expand Down Expand Up @@ -78,4 +78,4 @@ var _ = Describe("sealer build", func() {
})
})
})
})*/
4 changes: 2 additions & 2 deletions test/sealer_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package test

import (
/*import (
"fmt"
"github.com/alibaba/sealer/test/suites/registry"
Expand All @@ -40,4 +40,4 @@ var _ = Describe("sealer login", func() {
Eventually(sess).Should(Exit(0))
})
})
})
})*/
55 changes: 41 additions & 14 deletions test/suites/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
"fmt"
"path/filepath"

"github.com/alibaba/sealer/logger"
"github.com/alibaba/sealer/test/testhelper"
"github.com/alibaba/sealer/test/testhelper/settings"

"github.com/onsi/gomega"

"github.com/alibaba/sealer/test/testhelper"
v1 "github.com/alibaba/sealer/types/api/v1"
"github.com/alibaba/sealer/utils"
)
Expand All @@ -30,21 +32,46 @@ func getFixtures() string {
return filepath.Join(pwd, "suites", "apply", "fixtures")
}

func GetClusterFilePathOfRootfs() string {
func GetRawClusterFilePath() string {
fixtures := getFixtures()
return filepath.Join(fixtures, "cluster_file_rootfs.yaml")
return filepath.Join(fixtures, "cluster_file_for_test.yaml")
}

func GetClusterFileData(clusterFile string) *v1.Cluster {
cluster := &v1.Cluster{}
if err := utils.UnmarshalYamlFile(clusterFile, cluster); err != nil {
logger.Error("failed to unmarshal yamlFile to get clusterFile data")
return nil
}
return cluster
func DeleteCluster(clusterFile string) {
cmd := fmt.Sprintf("%s delete -f %s", settings.DefaultSealerBin, clusterFile)
testhelper.RunCmdAndCheckResult(cmd, 0)
}

func DeleteCluster(clusterName string) {
cmd := "sealer delete -f " + fmt.Sprintf(settings.DefaultClusterFileNeedToBeCleaned, clusterName)
testhelper.RunCmdAndCheckResult(cmd, 0)
func WriteClusterFileToDisk(cluster *v1.Cluster, clusterFilePath string) {
gomega.Expect(cluster).NotTo(gomega.BeNil())
err := utils.MarshalYamlToFile(clusterFilePath, cluster)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}

func LoadClusterFileFromDisk(clusterFilePath string) *v1.Cluster {
var cluster v1.Cluster
err := utils.UnmarshalYamlFile(clusterFilePath, &cluster)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(cluster).NotTo(gomega.BeNil())
return &cluster
}

func GetClusterNodes() int {
client, err := testhelper.NewClientSet()
gomega.Expect(err).NotTo(gomega.HaveOccurred())
nodes, err := testhelper.ListNodes(client)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
return len(nodes.Items)
}

func CheckClusterPods() int {
client, err := testhelper.NewClientSet()
gomega.Expect(err).NotTo(gomega.HaveOccurred())
pods, err := testhelper.ListNodes(client)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
return len(pods.Items)
}

func SealerApplyCmd(clusterFile string) string {
return fmt.Sprintf("%s apply -f %s", settings.DefaultSealerBin, clusterFile)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ kind: Cluster
metadata:
name: my-test-cluster
spec:
image: seadent/rootfs:latest
image: sealer-io/kubernetes:v1.19.9
provider: ALI_CLOUD
ssh:
passwd: Seadent123
passwd: Sealer123
pk: xxx
pkPasswd: xxx
user: root
Expand All @@ -37,14 +37,14 @@ spec:
masters:
cpu: 4
memory: 4
count: 3
count: 1
systemDisk: 100
dataDisks:
- 100
nodes:
cpu: 4
memory: 4
count: 3
count: 1
systemDisk: 100
dataDisks:
- 100
90 changes: 81 additions & 9 deletions test/suites/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,94 @@
package build

import (
"fmt"
"path/filepath"
"strconv"
"strings"

"github.com/alibaba/sealer/test/testhelper"
"github.com/alibaba/sealer/test/testhelper/settings"
"github.com/alibaba/sealer/utils"
"github.com/onsi/gomega"
)

func getFixtures() string {
pwd := testhelper.GetPwd()
return filepath.Join(pwd, "suites", "build", "fixtures")
func GetFixtures() string {
return filepath.Join(testhelper.GetPwd(), "suites", "build", "fixtures")
}

func GetOnlyCopyDir() string {
fixtures := getFixtures()
return filepath.Join(fixtures, "only_copy")
func GetLocalBuildDir() string {
return "local_build"
}

func GetBuildTestDir() string {
fixtures := getFixtures()
return filepath.Join(fixtures, "build_test")
func GetCloudBuildDir() string {
return "cloud_build"
}

func GetTestImageName() string {
return fmt.Sprintf("sealer-io/%s%d:%s", settings.ImageName, 719, "v1")
}

type ArgsOfBuild struct {
KubeFile, ImageName, Context, BuildType string
}

func (a *ArgsOfBuild) SetKubeFile(kubeFile string) *ArgsOfBuild {
a.KubeFile = kubeFile
return a
}

func (a *ArgsOfBuild) SetImageName(imageName string) *ArgsOfBuild {
a.ImageName = imageName
return a
}

func (a *ArgsOfBuild) SetContext(context string) *ArgsOfBuild {
a.Context = context
return a
}

func (a *ArgsOfBuild) SetBuildType(buildType string) *ArgsOfBuild {
a.BuildType = buildType
return a
}

func (a *ArgsOfBuild) Build() string {
if settings.DefaultSealerBin == "" || a.KubeFile == "" || a.ImageName == "" {
return ""
}

if a.Context == "" {
a.Context = "."
}

if a.BuildType == "" {
a.BuildType = settings.LocalBuild
}
return fmt.Sprintf("%s build -f %s -t %s -c %s -b %s", settings.DefaultSealerBin, a.KubeFile, a.ImageName, a.Context, a.BuildType)
}

func NewArgsOfBuild() *ArgsOfBuild {
return &ArgsOfBuild{}
}

func CheckIsImageExist(imageName string) bool {
cmd := fmt.Sprintf("%s images | grep %s | wc -l", settings.DefaultSealerBin, imageName)
result, err := utils.RunSimpleCmd(cmd)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
num, err := strconv.Atoi(strings.Replace(result, "\n", "", -1))
gomega.Expect(err).NotTo(gomega.HaveOccurred())
return num == 1
}

func CheckClusterFile(imageName string) bool {
cmd := fmt.Sprintf("%s images | grep %s | awk '{print $4}'", settings.DefaultSealerBin, imageName)
result, err := utils.RunSimpleCmd(cmd)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
cmd = fmt.Sprintf("%s inspect -c %s | grep Cluster | wc -l", settings.DefaultSealerBin,
strings.Replace(result, "\n", "", -1))
result, err = utils.RunSimpleCmd(cmd)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
num, err := strconv.Atoi(strings.Replace(result, "\n", "", -1))
gomega.Expect(err).NotTo(gomega.HaveOccurred())
return num == 1
}
36 changes: 36 additions & 0 deletions test/suites/build/fixtures/cloud_build/Clusterfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
apiVersion: zlink.aliyun.com/v1alpha1
kind: Cluster
metadata:
name: my-test-cluster
spec:
image: sealer-io/kubernetes:v1.19.9
provider: ALI_CLOUD
ssh:
passwd: Sealer123
pk: xxx
pkPasswd: xxx
user: root
network:
interface: eth0
cniName: calico
podCIDR: 100.64.0.0/10
svcCIDR: 10.96.0.0/22
withoutCNI: false
certSANS:
- aliyun-inc.com
- 10.0.0.2

masters:
cpu: 4
memory: 4
count: 1
systemDisk: 100
dataDisks:
- 100
nodes:
cpu: 4
memory: 4
count: 1
systemDisk: 100
dataDisks:
- 100
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM seadent/rootfs:latest
FROM sealer-io/kubernetes:v1.19.9
COPY Clusterfile etc
COPY test1 .
COPY recommended.yaml .
CMD kubectl apply -f recommended.yaml
Expand Down
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions test/suites/build/fixtures/local_build/Kubefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM sealer-io/kubernetes:v1.19.9
COPY test1 .
COPY recommended.yaml .
COPY test2 .
COPY test3 .
RUN wget helm.sh/helm
RUN wget -O redis.tar.gz http://download.redis.io/releases/redis-5.0.3.tar.gz
RUN tar zxvf redis.tar.gz
CMD yum install tree -y
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM seadent/rootfs:latest
FROM sealer-io/kubernetes:v1.19.9
COPY test1 .
COPY recommended.yaml .
COPY test2 .
COPY test3 .
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 7 additions & 5 deletions test/suites/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,27 @@ package image
import (
"fmt"

"github.com/alibaba/sealer/test/testhelper/settings"

"github.com/alibaba/sealer/test/testhelper"
)

func DoImageOps(action, imageName string) {
cmd := ""
switch action {
case "pull":
cmd = fmt.Sprintf("sealer pull %s", imageName)
cmd = fmt.Sprintf("%s pull %s", settings.DefaultSealerBin, imageName)
case "push":
cmd = fmt.Sprintf("sealer push %s", imageName)
cmd = fmt.Sprintf("%s push %s", settings.DefaultSealerBin, imageName)
case "rmi":
cmd = fmt.Sprintf("sealer rmi %s", imageName)
cmd = fmt.Sprintf("%s rmi %s", settings.DefaultSealerBin, imageName)
case "run":
cmd = fmt.Sprintf("sealer run %s", imageName)
cmd = fmt.Sprintf("%s run %s", settings.DefaultSealerBin, imageName)
}

testhelper.RunCmdAndCheckResult(cmd, 0)
}
func TagImages(oldName, newName string) {
cmd := fmt.Sprintf("sealer tag %s %s", oldName, newName)
cmd := fmt.Sprintf("%s tag %s %s", settings.DefaultSealerBin, oldName, newName)
testhelper.RunCmdAndCheckResult(cmd, 0)
}
22 changes: 20 additions & 2 deletions test/suites/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ package registry
import (
"fmt"
"os"
"path/filepath"

"github.com/mitchellh/go-homedir"

"github.com/alibaba/sealer/utils"

"github.com/alibaba/sealer/test/testhelper"
"github.com/alibaba/sealer/test/testhelper/settings"
Expand All @@ -26,7 +31,12 @@ import (
)

func Login() {
sess, err := testhelper.Start(fmt.Sprintf("sealer login %s -u %s -p %s", settings.RegistryURL,
// check if docker json already exist
config := DefaultRegistryAuthConfigDir()
if utils.IsFileExist(config) {
return
}
sess, err := testhelper.Start(fmt.Sprintf("%s login %s -u %s -p %s", settings.DefaultSealerBin, settings.RegistryURL,
settings.RegistryUsername,
settings.RegistryPasswd))

Expand All @@ -42,5 +52,13 @@ func Logout() {
}

func CleanLoginFile() error {
return os.RemoveAll(settings.DefaultRegistryAuthDir)
return os.RemoveAll(DefaultRegistryAuthConfigDir())
}
func DefaultRegistryAuthConfigDir() string {
dir, err := homedir.Dir()
if err != nil {
return settings.DefaultRegistryAuthFile
}

return filepath.Join(dir, ".docker/config.json")
}
Loading

0 comments on commit 97758e1

Please sign in to comment.